blob: ae0433885b69104130421a2a4fca9ff6bf709e01 [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 Singhf64c3f52009-09-23 15:56:37 -070042#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070043#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080045#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080046#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080047#include <linux/eventfd.h>
48#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080049#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080050#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070051#include <linux/vmalloc.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 */
94 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070095 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080096 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -070097 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080098 MEM_CGROUP_STAT_NSTATS,
99};
100
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700101static const char * const mem_cgroup_stat_names[] = {
102 "cache",
103 "rss",
104 "mapped_file",
105 "swap",
106};
107
Johannes Weinere9f89742011-03-23 16:42:37 -0700108enum mem_cgroup_events_index {
109 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
110 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -0700111 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
112 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700113 MEM_CGROUP_EVENTS_NSTATS,
114};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700115
116static const char * const mem_cgroup_events_names[] = {
117 "pgpgin",
118 "pgpgout",
119 "pgfault",
120 "pgmajfault",
121};
122
Sha Zhengju58cf1882013-02-22 16:32:05 -0800123static const char * const mem_cgroup_lru_names[] = {
124 "inactive_anon",
125 "active_anon",
126 "inactive_file",
127 "active_file",
128 "unevictable",
129};
130
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700131/*
132 * Per memcg event counter is incremented at every pagein/pageout. With THP,
133 * it will be incremated by the number of pages. This counter is used for
134 * for trigger some periodic events. This is straightforward and better
135 * than using jiffies etc. to handle periodic memcg event.
136 */
137enum mem_cgroup_events_target {
138 MEM_CGROUP_TARGET_THRESH,
139 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -0700140 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700141 MEM_CGROUP_NTARGETS,
142};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700143#define THRESHOLDS_EVENTS_TARGET 128
144#define SOFTLIMIT_EVENTS_TARGET 1024
145#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700146
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800147struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700148 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700149 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700150 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700151 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800152};
153
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800154struct mem_cgroup_reclaim_iter {
155 /* css_id of the last scanned hierarchy member */
156 int position;
157 /* scan generation, increased every round-trip */
158 unsigned int generation;
159};
160
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800161/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800162 * per-zone information in memory controller.
163 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800164struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800165 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700166 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800167
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800168 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
169
Balbir Singhf64c3f52009-09-23 15:56:37 -0700170 struct rb_node tree_node; /* RB tree node */
171 unsigned long long usage_in_excess;/* Set to the value by which */
172 /* the soft limit is exceeded*/
173 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700174 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700175 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800176};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800177
178struct mem_cgroup_per_node {
179 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
180};
181
182struct mem_cgroup_lru_info {
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800183 struct mem_cgroup_per_node *nodeinfo[0];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800184};
185
186/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700187 * Cgroups above their limits are maintained in a RB-Tree, independent of
188 * their hierarchy representation
189 */
190
191struct mem_cgroup_tree_per_zone {
192 struct rb_root rb_root;
193 spinlock_t lock;
194};
195
196struct mem_cgroup_tree_per_node {
197 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
198};
199
200struct mem_cgroup_tree {
201 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
202};
203
204static struct mem_cgroup_tree soft_limit_tree __read_mostly;
205
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800206struct mem_cgroup_threshold {
207 struct eventfd_ctx *eventfd;
208 u64 threshold;
209};
210
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700211/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800212struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700213 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700214 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800215 /* Size of entries[] */
216 unsigned int size;
217 /* Array of thresholds */
218 struct mem_cgroup_threshold entries[0];
219};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700220
221struct mem_cgroup_thresholds {
222 /* Primary thresholds array */
223 struct mem_cgroup_threshold_ary *primary;
224 /*
225 * Spare threshold array.
226 * This is needed to make mem_cgroup_unregister_event() "never fail".
227 * It must be able to store at least primary->size - 1 entries.
228 */
229 struct mem_cgroup_threshold_ary *spare;
230};
231
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700232/* for OOM */
233struct mem_cgroup_eventfd_list {
234 struct list_head list;
235 struct eventfd_ctx *eventfd;
236};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800237
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700238static void mem_cgroup_threshold(struct mem_cgroup *memcg);
239static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800240
Balbir Singhf64c3f52009-09-23 15:56:37 -0700241/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800242 * The memory controller data structure. The memory controller controls both
243 * page cache and RSS per cgroup. We would eventually like to provide
244 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
245 * to help the administrator determine what knobs to tune.
246 *
247 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800248 * we hit the water mark. May be even add a low water mark, such that
249 * no reclaim occurs from a cgroup at it's low water mark, this is
250 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800251 */
252struct mem_cgroup {
253 struct cgroup_subsys_state css;
254 /*
255 * the counter to account for memory usage
256 */
257 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700258
259 union {
260 /*
261 * the counter to account for mem+swap usage.
262 */
263 struct res_counter memsw;
264
265 /*
266 * rcu_freeing is used only when freeing struct mem_cgroup,
267 * so put it into a union to avoid wasting more memory.
268 * It must be disjoint from the css field. It could be
269 * in a union with the res field, but res plays a much
270 * larger part in mem_cgroup life than memsw, and might
271 * be of interest, even at time of free, when debugging.
272 * So share rcu_head with the less interesting memsw.
273 */
274 struct rcu_head rcu_freeing;
275 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700276 * We also need some space for a worker in deferred freeing.
277 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700278 */
279 struct work_struct work_freeing;
280 };
281
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800282 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800283 * the counter to account for kernel memory usage.
284 */
285 struct res_counter kmem;
286 /*
Balbir Singh18f59ea2009-01-07 18:08:07 -0800287 * Should the accounting and control be hierarchical, per subtree?
288 */
289 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800290 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700291
292 bool oom_lock;
293 atomic_t under_oom;
294
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800295 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800296
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700297 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700298 /* OOM-Killer disable */
299 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800300
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700301 /* set when res.limit == memsw.limit */
302 bool memsw_is_minimum;
303
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800304 /* protect arrays of thresholds */
305 struct mutex thresholds_lock;
306
307 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700308 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700309
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800310 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700311 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700312
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700313 /* For oom notifier event fd */
314 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700315
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800316 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800317 * Should we move charges of a task when a task is moved into this
318 * mem_cgroup ? And what type of charges should we move ?
319 */
320 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800321 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700322 * set > 0 if pages under this cgroup are moving to other cgroup.
323 */
324 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700325 /* taken only while moving_account > 0 */
326 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700327 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800328 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800329 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700330 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700331 /*
332 * used when a cpu is offlined or other synchronizations
333 * See mem_cgroup_read_stat().
334 */
335 struct mem_cgroup_stat_cpu nocpu_base;
336 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000337
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700338#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000339 struct tcp_memcontrol tcp_mem;
340#endif
Glauber Costa2633d7a2012-12-18 14:22:34 -0800341#if defined(CONFIG_MEMCG_KMEM)
342 /* analogous to slab_common's slab_caches list. per-memcg */
343 struct list_head memcg_slab_caches;
344 /* Not a spinlock, we can take a lot of time walking the list */
345 struct mutex slab_caches_mutex;
346 /* Index in the kmem_cache->memcg_params->memcg_caches array */
347 int kmemcg_id;
348#endif
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800349
350 int last_scanned_node;
351#if MAX_NUMNODES > 1
352 nodemask_t scan_nodes;
353 atomic_t numainfo_events;
354 atomic_t numainfo_updating;
355#endif
356 /*
357 * Per cgroup active and inactive list, similar to the
358 * per zone LRU lists.
359 *
360 * WARNING: This has to be the last element of the struct. Don't
361 * add new fields after this point.
362 */
363 struct mem_cgroup_lru_info info;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800364};
365
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800366static size_t memcg_size(void)
367{
368 return sizeof(struct mem_cgroup) +
369 nr_node_ids * sizeof(struct mem_cgroup_per_node);
370}
371
Glauber Costa510fc4e2012-12-18 14:21:47 -0800372/* internal only representation about the status of kmem accounting. */
373enum {
374 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costaa8964b92012-12-18 14:22:09 -0800375 KMEM_ACCOUNTED_ACTIVATED, /* static key enabled. */
Glauber Costa7de37682012-12-18 14:22:07 -0800376 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800377};
378
Glauber Costaa8964b92012-12-18 14:22:09 -0800379/* We account when limit is on, but only after call sites are patched */
380#define KMEM_ACCOUNTED_MASK \
381 ((1 << KMEM_ACCOUNTED_ACTIVE) | (1 << KMEM_ACCOUNTED_ACTIVATED))
Glauber Costa510fc4e2012-12-18 14:21:47 -0800382
383#ifdef CONFIG_MEMCG_KMEM
384static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
385{
386 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
387}
Glauber Costa7de37682012-12-18 14:22:07 -0800388
389static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
390{
391 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
392}
393
Glauber Costaa8964b92012-12-18 14:22:09 -0800394static void memcg_kmem_set_activated(struct mem_cgroup *memcg)
395{
396 set_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
397}
398
Glauber Costa55007d82012-12-18 14:22:38 -0800399static void memcg_kmem_clear_activated(struct mem_cgroup *memcg)
400{
401 clear_bit(KMEM_ACCOUNTED_ACTIVATED, &memcg->kmem_account_flags);
402}
403
Glauber Costa7de37682012-12-18 14:22:07 -0800404static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
405{
406 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
407 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
408}
409
410static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
411{
412 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
413 &memcg->kmem_account_flags);
414}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800415#endif
416
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800417/* Stuffs for move charges at task migration. */
418/*
419 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
420 * left-shifted bitmap of these types.
421 */
422enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800423 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700424 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800425 NR_MOVE_TYPE,
426};
427
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800428/* "mc" and its members are protected by cgroup_mutex */
429static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800430 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800431 struct mem_cgroup *from;
432 struct mem_cgroup *to;
433 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800434 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800435 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800436 struct task_struct *moving_task; /* a task moving charges */
437 wait_queue_head_t waitq; /* a waitq for other context */
438} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700439 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800440 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
441};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800442
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700443static bool move_anon(void)
444{
445 return test_bit(MOVE_CHARGE_TYPE_ANON,
446 &mc.to->move_charge_at_immigrate);
447}
448
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700449static bool move_file(void)
450{
451 return test_bit(MOVE_CHARGE_TYPE_FILE,
452 &mc.to->move_charge_at_immigrate);
453}
454
Balbir Singh4e416952009-09-23 15:56:39 -0700455/*
456 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
457 * limit reclaim to prevent infinite loops, if they ever occur.
458 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700459#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
460#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700461
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800462enum charge_type {
463 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700464 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800465 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700466 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700467 NR_CHARGE_TYPE,
468};
469
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800470/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800471enum res_type {
472 _MEM,
473 _MEMSWAP,
474 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800475 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800476};
477
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700478#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
479#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800480#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700481/* Used for OOM nofiier */
482#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800483
Balbir Singh75822b42009-09-23 15:56:38 -0700484/*
485 * Reclaim flags for mem_cgroup_hierarchical_reclaim
486 */
487#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
488#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
489#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
490#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
491
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700492static void mem_cgroup_get(struct mem_cgroup *memcg);
493static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000494
Wanpeng Lib2145142012-07-31 16:46:01 -0700495static inline
496struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
497{
498 return container_of(s, struct mem_cgroup, css);
499}
500
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700501static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
502{
503 return (memcg == root_mem_cgroup);
504}
505
Glauber Costae1aab162011-12-11 21:47:03 +0000506/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700507#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000508
Glauber Costae1aab162011-12-11 21:47:03 +0000509void sock_update_memcg(struct sock *sk)
510{
Glauber Costa376be5f2012-01-20 04:57:14 +0000511 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000512 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700513 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000514
515 BUG_ON(!sk->sk_prot->proto_cgroup);
516
Glauber Costaf3f511e2012-01-05 20:16:39 +0000517 /* Socket cloning can throw us here with sk_cgrp already
518 * filled. It won't however, necessarily happen from
519 * process context. So the test for root memcg given
520 * the current task's memcg won't help us in this case.
521 *
522 * Respecting the original socket's memcg is a better
523 * decision in this case.
524 */
525 if (sk->sk_cgrp) {
526 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
527 mem_cgroup_get(sk->sk_cgrp->memcg);
528 return;
529 }
530
Glauber Costae1aab162011-12-11 21:47:03 +0000531 rcu_read_lock();
532 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700533 cg_proto = sk->sk_prot->proto_cgroup(memcg);
534 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000535 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700536 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000537 }
538 rcu_read_unlock();
539 }
540}
541EXPORT_SYMBOL(sock_update_memcg);
542
543void sock_release_memcg(struct sock *sk)
544{
Glauber Costa376be5f2012-01-20 04:57:14 +0000545 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000546 struct mem_cgroup *memcg;
547 WARN_ON(!sk->sk_cgrp->memcg);
548 memcg = sk->sk_cgrp->memcg;
549 mem_cgroup_put(memcg);
550 }
551}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000552
553struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
554{
555 if (!memcg || mem_cgroup_is_root(memcg))
556 return NULL;
557
558 return &memcg->tcp_mem.cg_proto;
559}
560EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000561
Glauber Costa3f134612012-05-29 15:07:11 -0700562static void disarm_sock_keys(struct mem_cgroup *memcg)
563{
564 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
565 return;
566 static_key_slow_dec(&memcg_socket_limit_enabled);
567}
568#else
569static void disarm_sock_keys(struct mem_cgroup *memcg)
570{
571}
572#endif
573
Glauber Costaa8964b92012-12-18 14:22:09 -0800574#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800575/*
576 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
577 * There are two main reasons for not using the css_id for this:
578 * 1) this works better in sparse environments, where we have a lot of memcgs,
579 * but only a few kmem-limited. Or also, if we have, for instance, 200
580 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
581 * 200 entry array for that.
582 *
583 * 2) In order not to violate the cgroup API, we would like to do all memory
584 * allocation in ->create(). At that point, we haven't yet allocated the
585 * css_id. Having a separate index prevents us from messing with the cgroup
586 * core for this
587 *
588 * The current size of the caches array is stored in
589 * memcg_limited_groups_array_size. It will double each time we have to
590 * increase it.
591 */
592static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800593int memcg_limited_groups_array_size;
594
Glauber Costa55007d82012-12-18 14:22:38 -0800595/*
596 * MIN_SIZE is different than 1, because we would like to avoid going through
597 * the alloc/free process all the time. In a small machine, 4 kmem-limited
598 * cgroups is a reasonable guess. In the future, it could be a parameter or
599 * tunable, but that is strictly not necessary.
600 *
601 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
602 * this constant directly from cgroup, but it is understandable that this is
603 * better kept as an internal representation in cgroup.c. In any case, the
604 * css_id space is not getting any smaller, and we don't have to necessarily
605 * increase ours as well if it increases.
606 */
607#define MEMCG_CACHES_MIN_SIZE 4
608#define MEMCG_CACHES_MAX_SIZE 65535
609
Glauber Costad7f25f82012-12-18 14:22:40 -0800610/*
611 * A lot of the calls to the cache allocation functions are expected to be
612 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
613 * conditional to this static branch, we'll have to allow modules that does
614 * kmem_cache_alloc and the such to see this symbol as well
615 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800616struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800617EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800618
619static void disarm_kmem_keys(struct mem_cgroup *memcg)
620{
Glauber Costa55007d82012-12-18 14:22:38 -0800621 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800622 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800623 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
624 }
Glauber Costabea207c2012-12-18 14:22:11 -0800625 /*
626 * This check can't live in kmem destruction function,
627 * since the charges will outlive the cgroup
628 */
629 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800630}
631#else
632static void disarm_kmem_keys(struct mem_cgroup *memcg)
633{
634}
635#endif /* CONFIG_MEMCG_KMEM */
636
637static void disarm_static_keys(struct mem_cgroup *memcg)
638{
639 disarm_sock_keys(memcg);
640 disarm_kmem_keys(memcg);
641}
642
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700643static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800644
Balbir Singhf64c3f52009-09-23 15:56:37 -0700645static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700646mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700647{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800648 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700649 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700650}
651
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700652struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100653{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700654 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100655}
656
Balbir Singhf64c3f52009-09-23 15:56:37 -0700657static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700658page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700659{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700660 int nid = page_to_nid(page);
661 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700662
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700663 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700664}
665
666static struct mem_cgroup_tree_per_zone *
667soft_limit_tree_node_zone(int nid, int zid)
668{
669 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
670}
671
672static struct mem_cgroup_tree_per_zone *
673soft_limit_tree_from_page(struct page *page)
674{
675 int nid = page_to_nid(page);
676 int zid = page_zonenum(page);
677
678 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
679}
680
681static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700682__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700683 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700684 struct mem_cgroup_tree_per_zone *mctz,
685 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700686{
687 struct rb_node **p = &mctz->rb_root.rb_node;
688 struct rb_node *parent = NULL;
689 struct mem_cgroup_per_zone *mz_node;
690
691 if (mz->on_tree)
692 return;
693
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700694 mz->usage_in_excess = new_usage_in_excess;
695 if (!mz->usage_in_excess)
696 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700697 while (*p) {
698 parent = *p;
699 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
700 tree_node);
701 if (mz->usage_in_excess < mz_node->usage_in_excess)
702 p = &(*p)->rb_left;
703 /*
704 * We can't avoid mem cgroups that are over their soft
705 * limit by the same amount
706 */
707 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
708 p = &(*p)->rb_right;
709 }
710 rb_link_node(&mz->tree_node, parent, p);
711 rb_insert_color(&mz->tree_node, &mctz->rb_root);
712 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700713}
714
715static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700716__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700717 struct mem_cgroup_per_zone *mz,
718 struct mem_cgroup_tree_per_zone *mctz)
719{
720 if (!mz->on_tree)
721 return;
722 rb_erase(&mz->tree_node, &mctz->rb_root);
723 mz->on_tree = false;
724}
725
726static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700727mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700728 struct mem_cgroup_per_zone *mz,
729 struct mem_cgroup_tree_per_zone *mctz)
730{
731 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700732 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700733 spin_unlock(&mctz->lock);
734}
735
Balbir Singhf64c3f52009-09-23 15:56:37 -0700736
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700737static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700738{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700739 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700740 struct mem_cgroup_per_zone *mz;
741 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700742 int nid = page_to_nid(page);
743 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700744 mctz = soft_limit_tree_from_page(page);
745
746 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700747 * Necessary to update all ancestors when hierarchy is used.
748 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700749 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700750 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
751 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
752 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700753 /*
754 * We have to update the tree if mz is on RB-tree or
755 * mem is over its softlimit.
756 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700757 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700758 spin_lock(&mctz->lock);
759 /* if on-tree, remove it */
760 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700761 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700762 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700763 * Insert again. mz->usage_in_excess will be updated.
764 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700765 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700766 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700767 spin_unlock(&mctz->lock);
768 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700769 }
770}
771
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700772static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700773{
774 int node, zone;
775 struct mem_cgroup_per_zone *mz;
776 struct mem_cgroup_tree_per_zone *mctz;
777
Bob Liu3ed28fa2012-01-12 17:19:04 -0800778 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700779 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700780 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700781 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700782 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700783 }
784 }
785}
786
Balbir Singh4e416952009-09-23 15:56:39 -0700787static struct mem_cgroup_per_zone *
788__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
789{
790 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700791 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700792
793retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700794 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700795 rightmost = rb_last(&mctz->rb_root);
796 if (!rightmost)
797 goto done; /* Nothing to reclaim from */
798
799 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
800 /*
801 * Remove the node now but someone else can add it back,
802 * we will to add it back at the end of reclaim to its correct
803 * position in the tree.
804 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700805 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
806 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
807 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700808 goto retry;
809done:
810 return mz;
811}
812
813static struct mem_cgroup_per_zone *
814mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
815{
816 struct mem_cgroup_per_zone *mz;
817
818 spin_lock(&mctz->lock);
819 mz = __mem_cgroup_largest_soft_limit_node(mctz);
820 spin_unlock(&mctz->lock);
821 return mz;
822}
823
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700824/*
825 * Implementation Note: reading percpu statistics for memcg.
826 *
827 * Both of vmstat[] and percpu_counter has threshold and do periodic
828 * synchronization to implement "quick" read. There are trade-off between
829 * reading cost and precision of value. Then, we may have a chance to implement
830 * a periodic synchronizion of counter in memcg's counter.
831 *
832 * But this _read() function is used for user interface now. The user accounts
833 * memory usage by memory cgroup and he _always_ requires exact value because
834 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
835 * have to visit all online cpus and make sum. So, for now, unnecessary
836 * synchronization is not implemented. (just implemented for cpu hotplug)
837 *
838 * If there are kernel internal actions which can make use of some not-exact
839 * value, and reading all cpu value can be performance bottleneck in some
840 * common workload, threashold and synchonization as vmstat[] should be
841 * implemented.
842 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700843static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700844 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800845{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700846 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800847 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800848
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700849 get_online_cpus();
850 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700851 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700852#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700853 spin_lock(&memcg->pcp_counter_lock);
854 val += memcg->nocpu_base.count[idx];
855 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700856#endif
857 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800858 return val;
859}
860
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700861static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700862 bool charge)
863{
864 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700865 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700866}
867
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700868static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700869 enum mem_cgroup_events_index idx)
870{
871 unsigned long val = 0;
872 int cpu;
873
874 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700875 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700876#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700877 spin_lock(&memcg->pcp_counter_lock);
878 val += memcg->nocpu_base.events[idx];
879 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700880#endif
881 return val;
882}
883
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700884static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700885 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800886{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800887 preempt_disable();
888
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700889 /*
890 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
891 * counted as CACHE even if it's on ANON LRU.
892 */
893 if (anon)
894 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700895 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800896 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700897 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700898 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700899
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800900 /* pagein of a big page is an event. So, ignore page size */
901 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700902 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800903 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700904 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800905 nr_pages = -nr_pages; /* for event */
906 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800907
Johannes Weiner13114712012-05-29 15:07:07 -0700908 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800909
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800910 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800911}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800912
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700913unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700914mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700915{
916 struct mem_cgroup_per_zone *mz;
917
918 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
919 return mz->lru_size[lru];
920}
921
922static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700923mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700924 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700925{
926 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700927 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700928 unsigned long ret = 0;
929
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700930 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700931
Hugh Dickinsf156ab932012-03-21 16:34:19 -0700932 for_each_lru(lru) {
933 if (BIT(lru) & lru_mask)
934 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700935 }
936 return ret;
937}
938
939static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700940mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700941 int nid, unsigned int lru_mask)
942{
Ying Han889976d2011-05-26 16:25:33 -0700943 u64 total = 0;
944 int zid;
945
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700946 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700947 total += mem_cgroup_zone_nr_lru_pages(memcg,
948 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700949
Ying Han889976d2011-05-26 16:25:33 -0700950 return total;
951}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700952
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700953static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700954 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800955{
Ying Han889976d2011-05-26 16:25:33 -0700956 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800957 u64 total = 0;
958
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800959 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700960 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800961 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800962}
963
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800964static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
965 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800966{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700967 unsigned long val, next;
968
Johannes Weiner13114712012-05-29 15:07:07 -0700969 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700970 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700971 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800972 if ((long)next - (long)val < 0) {
973 switch (target) {
974 case MEM_CGROUP_TARGET_THRESH:
975 next = val + THRESHOLDS_EVENTS_TARGET;
976 break;
977 case MEM_CGROUP_TARGET_SOFTLIMIT:
978 next = val + SOFTLIMIT_EVENTS_TARGET;
979 break;
980 case MEM_CGROUP_TARGET_NUMAINFO:
981 next = val + NUMAINFO_EVENTS_TARGET;
982 break;
983 default:
984 break;
985 }
986 __this_cpu_write(memcg->stat->targets[target], next);
987 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700988 }
Johannes Weinerf53d7ce32012-01-12 17:18:23 -0800989 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800990}
991
992/*
993 * Check events in order.
994 *
995 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700996static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800997{
Steven Rostedt47994012011-11-02 13:38:33 -0700998 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800999 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce32012-01-12 17:18:23 -08001000 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1001 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001002 bool do_softlimit;
1003 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce32012-01-12 17:18:23 -08001004
1005 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1006 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001007#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce32012-01-12 17:18:23 -08001008 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1009 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001010#endif
Johannes Weinerf53d7ce32012-01-12 17:18:23 -08001011 preempt_enable();
1012
1013 mem_cgroup_threshold(memcg);
1014 if (unlikely(do_softlimit))
1015 mem_cgroup_update_tree(memcg, page);
1016#if MAX_NUMNODES > 1
1017 if (unlikely(do_numainfo))
1018 atomic_inc(&memcg->numainfo_events);
1019#endif
1020 } else
1021 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001022}
1023
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001024struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001025{
Wanpeng Lib2145142012-07-31 16:46:01 -07001026 return mem_cgroup_from_css(
1027 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001028}
1029
Balbir Singhcf475ad2008-04-29 01:00:16 -07001030struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001031{
Balbir Singh31a78f22008-09-28 23:09:31 +01001032 /*
1033 * mm_update_next_owner() may clear mm->owner to NULL
1034 * if it races with swapoff, page migration, etc.
1035 * So this can be called with p == NULL.
1036 */
1037 if (unlikely(!p))
1038 return NULL;
1039
Wanpeng Lib2145142012-07-31 16:46:01 -07001040 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001041}
1042
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001043struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001044{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001045 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001046
1047 if (!mm)
1048 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001049 /*
1050 * Because we have no locks, mm->owner's may be being moved to other
1051 * cgroup. We use css_tryget() here even if this looks
1052 * pessimistic (rather than adding locks here).
1053 */
1054 rcu_read_lock();
1055 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001056 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1057 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001058 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001059 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001060 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001061 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001062}
1063
Johannes Weiner56600482012-01-12 17:17:59 -08001064/**
1065 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1066 * @root: hierarchy root
1067 * @prev: previously returned memcg, NULL on first invocation
1068 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1069 *
1070 * Returns references to children of the hierarchy below @root, or
1071 * @root itself, or %NULL after a full round-trip.
1072 *
1073 * Caller must pass the return value in @prev on subsequent
1074 * invocations for reference counting, or use mem_cgroup_iter_break()
1075 * to cancel a hierarchy walk before the round-trip is complete.
1076 *
1077 * Reclaimers can specify a zone and a priority level in @reclaim to
1078 * divide up the memcgs in the hierarchy among all concurrent
1079 * reclaimers operating on the same zone and priority.
1080 */
1081struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1082 struct mem_cgroup *prev,
1083 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001084{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001085 struct mem_cgroup *memcg = NULL;
1086 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001087
Johannes Weiner56600482012-01-12 17:17:59 -08001088 if (mem_cgroup_disabled())
1089 return NULL;
1090
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001091 if (!root)
1092 root = root_mem_cgroup;
1093
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001094 if (prev && !reclaim)
1095 id = css_id(&prev->css);
1096
1097 if (prev && prev != root)
1098 css_put(&prev->css);
1099
1100 if (!root->use_hierarchy && root != root_mem_cgroup) {
1101 if (prev)
1102 return NULL;
1103 return root;
1104 }
1105
1106 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001107 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001108 struct cgroup_subsys_state *css;
1109
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001110 if (reclaim) {
1111 int nid = zone_to_nid(reclaim->zone);
1112 int zid = zone_idx(reclaim->zone);
1113 struct mem_cgroup_per_zone *mz;
1114
1115 mz = mem_cgroup_zoneinfo(root, nid, zid);
1116 iter = &mz->reclaim_iter[reclaim->priority];
1117 if (prev && reclaim->generation != iter->generation)
1118 return NULL;
1119 id = iter->position;
1120 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001121
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001122 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001123 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1124 if (css) {
1125 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001126 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001127 } else
1128 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001129 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001130
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001131 if (reclaim) {
1132 iter->position = id;
1133 if (!css)
1134 iter->generation++;
1135 else if (!prev && memcg)
1136 reclaim->generation = iter->generation;
1137 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001138
1139 if (prev && !css)
1140 return NULL;
1141 }
1142 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001143}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001144
Johannes Weiner56600482012-01-12 17:17:59 -08001145/**
1146 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1147 * @root: hierarchy root
1148 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1149 */
1150void mem_cgroup_iter_break(struct mem_cgroup *root,
1151 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001152{
1153 if (!root)
1154 root = root_mem_cgroup;
1155 if (prev && prev != root)
1156 css_put(&prev->css);
1157}
1158
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001159/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001160 * Iteration constructs for visiting all cgroups (under a tree). If
1161 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1162 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001163 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001164#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001165 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001166 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001167 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001168
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001169#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001170 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001171 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001172 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001173
David Rientjes68ae5642012-12-12 13:51:57 -08001174void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001175{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001176 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001177
Ying Han456f9982011-05-26 16:25:38 -07001178 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001179 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1180 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001181 goto out;
1182
1183 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001184 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001185 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1186 break;
1187 case PGMAJFAULT:
1188 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001189 break;
1190 default:
1191 BUG();
1192 }
1193out:
1194 rcu_read_unlock();
1195}
David Rientjes68ae5642012-12-12 13:51:57 -08001196EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001197
Johannes Weiner925b7672012-01-12 17:18:15 -08001198/**
1199 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1200 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001201 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001202 *
1203 * Returns the lru list vector holding pages for the given @zone and
1204 * @mem. This can be the global zone lruvec, if the memory controller
1205 * is disabled.
1206 */
1207struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1208 struct mem_cgroup *memcg)
1209{
1210 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001211 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001212
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001213 if (mem_cgroup_disabled()) {
1214 lruvec = &zone->lruvec;
1215 goto out;
1216 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001217
1218 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001219 lruvec = &mz->lruvec;
1220out:
1221 /*
1222 * Since a node can be onlined after the mem_cgroup was created,
1223 * we have to be prepared to initialize lruvec->zone here;
1224 * and if offlined then reonlined, we need to reinitialize it.
1225 */
1226 if (unlikely(lruvec->zone != zone))
1227 lruvec->zone = zone;
1228 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001229}
1230
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001231/*
1232 * Following LRU functions are allowed to be used without PCG_LOCK.
1233 * Operations are called by routine of global LRU independently from memcg.
1234 * What we have to take care of here is validness of pc->mem_cgroup.
1235 *
1236 * Changes to pc->mem_cgroup happens when
1237 * 1. charge
1238 * 2. moving account
1239 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1240 * It is added to LRU before charge.
1241 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1242 * When moving account, the page is not on LRU. It's isolated.
1243 */
1244
Johannes Weiner925b7672012-01-12 17:18:15 -08001245/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001246 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001247 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001248 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001249 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001250struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001251{
1252 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001253 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001254 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001255 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001256
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001257 if (mem_cgroup_disabled()) {
1258 lruvec = &zone->lruvec;
1259 goto out;
1260 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001261
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001262 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001263 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001264
1265 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001266 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001267 * an uncharged page off lru does nothing to secure
1268 * its former mem_cgroup from sudden removal.
1269 *
1270 * Our caller holds lru_lock, and PageCgroupUsed is updated
1271 * under page_cgroup lock: between them, they make all uses
1272 * of pc->mem_cgroup safe.
1273 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001274 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001275 pc->mem_cgroup = memcg = root_mem_cgroup;
1276
Johannes Weiner925b7672012-01-12 17:18:15 -08001277 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001278 lruvec = &mz->lruvec;
1279out:
1280 /*
1281 * Since a node can be onlined after the mem_cgroup was created,
1282 * we have to be prepared to initialize lruvec->zone here;
1283 * and if offlined then reonlined, we need to reinitialize it.
1284 */
1285 if (unlikely(lruvec->zone != zone))
1286 lruvec->zone = zone;
1287 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001288}
1289
1290/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001291 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1292 * @lruvec: mem_cgroup per zone lru vector
1293 * @lru: index of lru list the page is sitting on
1294 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001295 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001296 * This function must be called when a page is added to or removed from an
1297 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001298 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001299void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1300 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001301{
1302 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001303 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001304
1305 if (mem_cgroup_disabled())
1306 return;
1307
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001308 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1309 lru_size = mz->lru_size + lru;
1310 *lru_size += nr_pages;
1311 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001312}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001313
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001314/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001315 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001316 * hierarchy subtree
1317 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001318bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1319 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001320{
Johannes Weiner91c637342012-05-29 15:06:24 -07001321 if (root_memcg == memcg)
1322 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001323 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001324 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001325 return css_is_ancestor(&memcg->css, &root_memcg->css);
1326}
1327
1328static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1329 struct mem_cgroup *memcg)
1330{
1331 bool ret;
1332
Johannes Weiner91c637342012-05-29 15:06:24 -07001333 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001334 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001335 rcu_read_unlock();
1336 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001337}
1338
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001339int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001340{
1341 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001342 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001343 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001344
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001345 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001346 if (p) {
1347 curr = try_get_mem_cgroup_from_mm(p->mm);
1348 task_unlock(p);
1349 } else {
1350 /*
1351 * All threads may have already detached their mm's, but the oom
1352 * killer still needs to detect if they have already been oom
1353 * killed to prevent needlessly killing additional tasks.
1354 */
1355 task_lock(task);
1356 curr = mem_cgroup_from_task(task);
1357 if (curr)
1358 css_get(&curr->css);
1359 task_unlock(task);
1360 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001361 if (!curr)
1362 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001363 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001364 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001365 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001366 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1367 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001368 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001369 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001370 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001371 return ret;
1372}
1373
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001374int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001375{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001376 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001377 unsigned long inactive;
1378 unsigned long active;
1379 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001380
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001381 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1382 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001383
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001384 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1385 if (gb)
1386 inactive_ratio = int_sqrt(10 * gb);
1387 else
1388 inactive_ratio = 1;
1389
Johannes Weiner9b272972011-11-02 13:38:23 -07001390 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001391}
1392
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001393int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001394{
1395 unsigned long active;
1396 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001397
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001398 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1399 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001400
1401 return (active > inactive);
1402}
1403
Balbir Singh6d61ef42009-01-07 18:08:06 -08001404#define mem_cgroup_from_res_counter(counter, member) \
1405 container_of(counter, struct mem_cgroup, member)
1406
Johannes Weiner19942822011-02-01 15:52:43 -08001407/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001408 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001409 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001410 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001411 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001412 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001413 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001414static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001415{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001416 unsigned long long margin;
1417
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001418 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001419 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001420 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001421 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001422}
1423
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001424int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001425{
1426 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001427
1428 /* root ? */
1429 if (cgrp->parent == NULL)
1430 return vm_swappiness;
1431
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001432 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001433}
1434
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001435/*
1436 * memcg->moving_account is used for checking possibility that some thread is
1437 * calling move_account(). When a thread on CPU-A starts moving pages under
1438 * a memcg, other threads should check memcg->moving_account under
1439 * rcu_read_lock(), like this:
1440 *
1441 * CPU-A CPU-B
1442 * rcu_read_lock()
1443 * memcg->moving_account+1 if (memcg->mocing_account)
1444 * take heavy locks.
1445 * synchronize_rcu() update something.
1446 * rcu_read_unlock()
1447 * start move here.
1448 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001449
1450/* for quick checking without looking up memcg */
1451atomic_t memcg_moving __read_mostly;
1452
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001453static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001454{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001455 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001456 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001457 synchronize_rcu();
1458}
1459
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001460static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001461{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001462 /*
1463 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1464 * We check NULL in callee rather than caller.
1465 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001466 if (memcg) {
1467 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001468 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001469 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001470}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001471
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001472/*
1473 * 2 routines for checking "mem" is under move_account() or not.
1474 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001475 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1476 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001477 * pc->mem_cgroup may be overwritten.
1478 *
1479 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1480 * under hierarchy of moving cgroups. This is for
1481 * waiting at hith-memory prressure caused by "move".
1482 */
1483
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001484static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001485{
1486 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001487 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001488}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001489
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001490static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001491{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001492 struct mem_cgroup *from;
1493 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001494 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001495 /*
1496 * Unlike task_move routines, we access mc.to, mc.from not under
1497 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1498 */
1499 spin_lock(&mc.lock);
1500 from = mc.from;
1501 to = mc.to;
1502 if (!from)
1503 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001504
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001505 ret = mem_cgroup_same_or_subtree(memcg, from)
1506 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001507unlock:
1508 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001509 return ret;
1510}
1511
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001512static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001513{
1514 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001515 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001516 DEFINE_WAIT(wait);
1517 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1518 /* moving charge context might have finished. */
1519 if (mc.moving_task)
1520 schedule();
1521 finish_wait(&mc.waitq, &wait);
1522 return true;
1523 }
1524 }
1525 return false;
1526}
1527
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001528/*
1529 * Take this lock when
1530 * - a code tries to modify page's memcg while it's USED.
1531 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001532 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001533 */
1534static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1535 unsigned long *flags)
1536{
1537 spin_lock_irqsave(&memcg->move_lock, *flags);
1538}
1539
1540static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1541 unsigned long *flags)
1542{
1543 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1544}
1545
Sha Zhengju58cf1882013-02-22 16:32:05 -08001546#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001547/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001548 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001549 * @memcg: The memory cgroup that went over limit
1550 * @p: Task that is going to be killed
1551 *
1552 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1553 * enabled
1554 */
1555void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1556{
1557 struct cgroup *task_cgrp;
1558 struct cgroup *mem_cgrp;
1559 /*
1560 * Need a buffer in BSS, can't rely on allocations. The code relies
1561 * on the assumption that OOM is serialized for memory controller.
1562 * If this assumption is broken, revisit this code.
1563 */
1564 static char memcg_name[PATH_MAX];
1565 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001566 struct mem_cgroup *iter;
1567 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001568
Sha Zhengju58cf1882013-02-22 16:32:05 -08001569 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001570 return;
1571
Balbir Singhe2224322009-04-02 16:57:39 -07001572 rcu_read_lock();
1573
1574 mem_cgrp = memcg->css.cgroup;
1575 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1576
1577 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1578 if (ret < 0) {
1579 /*
1580 * Unfortunately, we are unable to convert to a useful name
1581 * But we'll still print out the usage information
1582 */
1583 rcu_read_unlock();
1584 goto done;
1585 }
1586 rcu_read_unlock();
1587
Andrew Mortond0451972013-02-22 16:32:06 -08001588 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001589
1590 rcu_read_lock();
1591 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1592 if (ret < 0) {
1593 rcu_read_unlock();
1594 goto done;
1595 }
1596 rcu_read_unlock();
1597
1598 /*
1599 * Continues from above, so we don't need an KERN_ level
1600 */
Andrew Mortond0451972013-02-22 16:32:06 -08001601 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001602done:
1603
Andrew Mortond0451972013-02-22 16:32:06 -08001604 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001605 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1606 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1607 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001608 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001609 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1610 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1611 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001612 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001613 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1614 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1615 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001616
1617 for_each_mem_cgroup_tree(iter, memcg) {
1618 pr_info("Memory cgroup stats");
1619
1620 rcu_read_lock();
1621 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1622 if (!ret)
1623 pr_cont(" for %s", memcg_name);
1624 rcu_read_unlock();
1625 pr_cont(":");
1626
1627 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1628 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1629 continue;
1630 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1631 K(mem_cgroup_read_stat(iter, i)));
1632 }
1633
1634 for (i = 0; i < NR_LRU_LISTS; i++)
1635 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1636 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1637
1638 pr_cont("\n");
1639 }
Balbir Singhe2224322009-04-02 16:57:39 -07001640}
1641
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001642/*
1643 * This function returns the number of memcg under hierarchy tree. Returns
1644 * 1(self count) if no children.
1645 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001646static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001647{
1648 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001649 struct mem_cgroup *iter;
1650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001651 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001652 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001653 return num;
1654}
1655
Balbir Singh6d61ef42009-01-07 18:08:06 -08001656/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001657 * Return the memory (and swap, if configured) limit for a memcg.
1658 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001659static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001660{
1661 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001662
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001663 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001664
David Rientjesa63d83f2010-08-09 17:19:46 -07001665 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001666 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001667 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001668 if (mem_cgroup_swappiness(memcg)) {
1669 u64 memsw;
1670
1671 limit += total_swap_pages << PAGE_SHIFT;
1672 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1673
1674 /*
1675 * If memsw is finite and limits the amount of swap space
1676 * available to this memcg, return that limit.
1677 */
1678 limit = min(limit, memsw);
1679 }
1680
1681 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001682}
1683
David Rientjes19965462012-12-11 16:00:26 -08001684static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1685 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001686{
1687 struct mem_cgroup *iter;
1688 unsigned long chosen_points = 0;
1689 unsigned long totalpages;
1690 unsigned int points = 0;
1691 struct task_struct *chosen = NULL;
1692
David Rientjes876aafb2012-07-31 16:43:48 -07001693 /*
1694 * If current has a pending SIGKILL, then automatically select it. The
1695 * goal is to allow it to allocate so that it may quickly exit and free
1696 * its memory.
1697 */
1698 if (fatal_signal_pending(current)) {
1699 set_thread_flag(TIF_MEMDIE);
1700 return;
1701 }
1702
1703 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001704 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1705 for_each_mem_cgroup_tree(iter, memcg) {
1706 struct cgroup *cgroup = iter->css.cgroup;
1707 struct cgroup_iter it;
1708 struct task_struct *task;
1709
1710 cgroup_iter_start(cgroup, &it);
1711 while ((task = cgroup_iter_next(cgroup, &it))) {
1712 switch (oom_scan_process_thread(task, totalpages, NULL,
1713 false)) {
1714 case OOM_SCAN_SELECT:
1715 if (chosen)
1716 put_task_struct(chosen);
1717 chosen = task;
1718 chosen_points = ULONG_MAX;
1719 get_task_struct(chosen);
1720 /* fall through */
1721 case OOM_SCAN_CONTINUE:
1722 continue;
1723 case OOM_SCAN_ABORT:
1724 cgroup_iter_end(cgroup, &it);
1725 mem_cgroup_iter_break(memcg, iter);
1726 if (chosen)
1727 put_task_struct(chosen);
1728 return;
1729 case OOM_SCAN_OK:
1730 break;
1731 };
1732 points = oom_badness(task, memcg, NULL, totalpages);
1733 if (points > chosen_points) {
1734 if (chosen)
1735 put_task_struct(chosen);
1736 chosen = task;
1737 chosen_points = points;
1738 get_task_struct(chosen);
1739 }
1740 }
1741 cgroup_iter_end(cgroup, &it);
1742 }
1743
1744 if (!chosen)
1745 return;
1746 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001747 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1748 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001749}
1750
Johannes Weiner56600482012-01-12 17:17:59 -08001751static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1752 gfp_t gfp_mask,
1753 unsigned long flags)
1754{
1755 unsigned long total = 0;
1756 bool noswap = false;
1757 int loop;
1758
1759 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1760 noswap = true;
1761 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1762 noswap = true;
1763
1764 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1765 if (loop)
1766 drain_all_stock_async(memcg);
1767 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1768 /*
1769 * Allow limit shrinkers, which are triggered directly
1770 * by userspace, to catch signals and stop reclaim
1771 * after minimal progress, regardless of the margin.
1772 */
1773 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1774 break;
1775 if (mem_cgroup_margin(memcg))
1776 break;
1777 /*
1778 * If nothing was reclaimed after two attempts, there
1779 * may be no reclaimable pages in this hierarchy.
1780 */
1781 if (loop && !total)
1782 break;
1783 }
1784 return total;
1785}
1786
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001787/**
1788 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001789 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001790 * @nid: the node ID to be checked.
1791 * @noswap : specify true here if the user wants flle only information.
1792 *
1793 * This function returns whether the specified memcg contains any
1794 * reclaimable pages on a node. Returns true if there are any reclaimable
1795 * pages in the node.
1796 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001797static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001798 int nid, bool noswap)
1799{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001800 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001801 return true;
1802 if (noswap || !total_swap_pages)
1803 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001804 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001805 return true;
1806 return false;
1807
1808}
Ying Han889976d2011-05-26 16:25:33 -07001809#if MAX_NUMNODES > 1
1810
1811/*
1812 * Always updating the nodemask is not very good - even if we have an empty
1813 * list or the wrong list here, we can start from some node and traverse all
1814 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1815 *
1816 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001817static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001818{
1819 int nid;
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001820 /*
1821 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1822 * pagein/pageout changes since the last update.
1823 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001824 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001825 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001826 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001827 return;
1828
Ying Han889976d2011-05-26 16:25:33 -07001829 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001830 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001831
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001832 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001833
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001834 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1835 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001836 }
KAMEZAWA Hiroyuki453a9bf32011-07-08 15:39:43 -07001837
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001838 atomic_set(&memcg->numainfo_events, 0);
1839 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001840}
1841
1842/*
1843 * Selecting a node where we start reclaim from. Because what we need is just
1844 * reducing usage counter, start from anywhere is O,K. Considering
1845 * memory reclaim from current node, there are pros. and cons.
1846 *
1847 * Freeing memory from current node means freeing memory from a node which
1848 * we'll use or we've used. So, it may make LRU bad. And if several threads
1849 * hit limits, it will see a contention on a node. But freeing from remote
1850 * node means more costs for memory reclaim because of memory latency.
1851 *
1852 * Now, we use round-robin. Better algorithm is welcomed.
1853 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001854int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001855{
1856 int node;
1857
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001858 mem_cgroup_may_update_nodemask(memcg);
1859 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001860
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001861 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001862 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001863 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001864 /*
1865 * We call this when we hit limit, not when pages are added to LRU.
1866 * No LRU may hold pages because all pages are UNEVICTABLE or
1867 * memcg is too small and all pages are not on LRU. In that case,
1868 * we use curret node.
1869 */
1870 if (unlikely(node == MAX_NUMNODES))
1871 node = numa_node_id();
1872
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001873 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001874 return node;
1875}
1876
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001877/*
1878 * Check all nodes whether it contains reclaimable pages or not.
1879 * For quick scan, we make use of scan_nodes. This will allow us to skip
1880 * unused nodes. But scan_nodes is lazily updated and may not cotain
1881 * enough new information. We need to do double check.
1882 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001883static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001884{
1885 int nid;
1886
1887 /*
1888 * quick check...making use of scan_node.
1889 * We can skip unused nodes.
1890 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001891 if (!nodes_empty(memcg->scan_nodes)) {
1892 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001893 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001894 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001895
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001897 return true;
1898 }
1899 }
1900 /*
1901 * Check rest of nodes.
1902 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001903 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001904 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001905 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001906 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001907 return true;
1908 }
1909 return false;
1910}
1911
Ying Han889976d2011-05-26 16:25:33 -07001912#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001913int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001914{
1915 return 0;
1916}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001917
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001918static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001919{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001920 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001921}
Ying Han889976d2011-05-26 16:25:33 -07001922#endif
1923
Johannes Weiner56600482012-01-12 17:17:59 -08001924static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1925 struct zone *zone,
1926 gfp_t gfp_mask,
1927 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001928{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001929 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001930 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001931 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001932 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001933 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001934 struct mem_cgroup_reclaim_cookie reclaim = {
1935 .zone = zone,
1936 .priority = 0,
1937 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001938
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001939 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001940
Balbir Singh4e416952009-09-23 15:56:39 -07001941 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001942 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001943 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001944 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001945 if (loop >= 2) {
1946 /*
1947 * If we have not been able to reclaim
1948 * anything, it might because there are
1949 * no reclaimable pages under this hierarchy
1950 */
Johannes Weiner56600482012-01-12 17:17:59 -08001951 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001952 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001953 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001954 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001955 * excess >> 2 is not to excessive so as to
1956 * reclaim too much, nor too less that we keep
1957 * coming back to reclaim from this cgroup
1958 */
1959 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001960 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001961 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001962 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001963 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001964 }
Johannes Weiner56600482012-01-12 17:17:59 -08001965 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001966 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001967 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1968 zone, &nr_scanned);
1969 *total_scanned += nr_scanned;
1970 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001971 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001972 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001973 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001974 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001975}
1976
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001977/*
1978 * Check OOM-Killer is already running under our hierarchy.
1979 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001980 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001981 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001982static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001983{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001984 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001985
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001986 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001987 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001988 /*
1989 * this subtree of our hierarchy is already locked
1990 * so we cannot give a lock.
1991 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001992 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001993 mem_cgroup_iter_break(memcg, iter);
1994 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001995 } else
1996 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001997 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001998
Michal Hocko79dfdac2011-07-26 16:08:23 -07001999 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07002000 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002001
2002 /*
2003 * OK, we failed to lock the whole subtree so we have to clean up
2004 * what we set up to the failing subtree
2005 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002006 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002007 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002008 mem_cgroup_iter_break(memcg, iter);
2009 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002010 }
2011 iter->oom_lock = false;
2012 }
Johannes Weiner23751be2011-08-25 15:59:16 -07002013 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002014}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002015
Michal Hocko79dfdac2011-07-26 16:08:23 -07002016/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07002017 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07002018 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002019static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002020{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002021 struct mem_cgroup *iter;
2022
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002023 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002024 iter->oom_lock = false;
2025 return 0;
2026}
2027
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002028static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002029{
2030 struct mem_cgroup *iter;
2031
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002032 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002033 atomic_inc(&iter->under_oom);
2034}
2035
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002036static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002037{
2038 struct mem_cgroup *iter;
2039
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002040 /*
2041 * When a new child is created while the hierarchy is under oom,
2042 * mem_cgroup_oom_lock() may not be called. We have to use
2043 * atomic_add_unless() here.
2044 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002045 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002046 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002047}
2048
Michal Hocko1af8efe2011-07-26 16:08:24 -07002049static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002050static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2051
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002052struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002053 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002054 wait_queue_t wait;
2055};
2056
2057static int memcg_oom_wake_function(wait_queue_t *wait,
2058 unsigned mode, int sync, void *arg)
2059{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002060 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2061 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002062 struct oom_wait_info *oom_wait_info;
2063
2064 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002065 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002066
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002067 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002068 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002069 * Then we can use css_is_ancestor without taking care of RCU.
2070 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002071 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2072 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002073 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002074 return autoremove_wake_function(wait, mode, sync, arg);
2075}
2076
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002077static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002078{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002079 /* for filtering, pass "memcg" as argument. */
2080 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002081}
2082
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002083static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002084{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002085 if (memcg && atomic_read(&memcg->under_oom))
2086 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002087}
2088
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002089/*
2090 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2091 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002092static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2093 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002094{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002095 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002096 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002097
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002098 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002099 owait.wait.flags = 0;
2100 owait.wait.func = memcg_oom_wake_function;
2101 owait.wait.private = current;
2102 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002103 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002104 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002105
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002106 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002107 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002108 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002109 /*
2110 * Even if signal_pending(), we can't quit charge() loop without
2111 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2112 * under OOM is always welcomed, use TASK_KILLABLE here.
2113 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002114 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002115 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002116 need_to_kill = false;
2117 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002118 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002119 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002120
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002121 if (need_to_kill) {
2122 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002123 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002124 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002125 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002126 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002127 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002128 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002129 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002130 mem_cgroup_oom_unlock(memcg);
2131 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002132 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002133
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002134 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002135
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002136 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2137 return false;
2138 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002139 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002140 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002141}
2142
Balbir Singhd69b0422009-06-17 16:26:34 -07002143/*
2144 * Currently used to update mapped file statistics, but the routine can be
2145 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002146 *
2147 * Notes: Race condition
2148 *
2149 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2150 * it tends to be costly. But considering some conditions, we doesn't need
2151 * to do so _always_.
2152 *
2153 * Considering "charge", lock_page_cgroup() is not required because all
2154 * file-stat operations happen after a page is attached to radix-tree. There
2155 * are no race with "charge".
2156 *
2157 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2158 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2159 * if there are race with "uncharge". Statistics itself is properly handled
2160 * by flags.
2161 *
2162 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002163 * small, we check mm->moving_account and detect there are possibility of race
2164 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002165 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002166
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002167void __mem_cgroup_begin_update_page_stat(struct page *page,
2168 bool *locked, unsigned long *flags)
2169{
2170 struct mem_cgroup *memcg;
2171 struct page_cgroup *pc;
2172
2173 pc = lookup_page_cgroup(page);
2174again:
2175 memcg = pc->mem_cgroup;
2176 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2177 return;
2178 /*
2179 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002180 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002181 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002182 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002183 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002184 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002185 return;
2186
2187 move_lock_mem_cgroup(memcg, flags);
2188 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2189 move_unlock_mem_cgroup(memcg, flags);
2190 goto again;
2191 }
2192 *locked = true;
2193}
2194
2195void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2196{
2197 struct page_cgroup *pc = lookup_page_cgroup(page);
2198
2199 /*
2200 * It's guaranteed that pc->mem_cgroup never changes while
2201 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002202 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002203 */
2204 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2205}
2206
Greg Thelen2a7106f2011-01-13 15:47:37 -08002207void mem_cgroup_update_page_stat(struct page *page,
2208 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002209{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002210 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002211 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002212 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002213
Johannes Weinercfa44942012-01-12 17:18:38 -08002214 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002215 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002216
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002217 memcg = pc->mem_cgroup;
2218 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002219 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002220
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002221 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002222 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002223 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002224 break;
2225 default:
2226 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002227 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002228
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002229 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002230}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002231
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002232/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002233 * size of first charge trial. "32" comes from vmscan.c's magic value.
2234 * TODO: maybe necessary to use big numbers in big irons.
2235 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002236#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002237struct memcg_stock_pcp {
2238 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002239 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002240 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002241 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002242#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002243};
2244static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002245static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002246
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002247/**
2248 * consume_stock: Try to consume stocked charge on this cpu.
2249 * @memcg: memcg to consume from.
2250 * @nr_pages: how many pages to charge.
2251 *
2252 * The charges will only happen if @memcg matches the current cpu's memcg
2253 * stock, and at least @nr_pages are available in that stock. Failure to
2254 * service an allocation will refill the stock.
2255 *
2256 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002257 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002258static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002259{
2260 struct memcg_stock_pcp *stock;
2261 bool ret = true;
2262
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002263 if (nr_pages > CHARGE_BATCH)
2264 return false;
2265
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002266 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002267 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2268 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002269 else /* need to call res_counter_charge */
2270 ret = false;
2271 put_cpu_var(memcg_stock);
2272 return ret;
2273}
2274
2275/*
2276 * Returns stocks cached in percpu to res_counter and reset cached information.
2277 */
2278static void drain_stock(struct memcg_stock_pcp *stock)
2279{
2280 struct mem_cgroup *old = stock->cached;
2281
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002282 if (stock->nr_pages) {
2283 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2284
2285 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002286 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002287 res_counter_uncharge(&old->memsw, bytes);
2288 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002289 }
2290 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002291}
2292
2293/*
2294 * This must be called under preempt disabled or must be called by
2295 * a thread which is pinned to local cpu.
2296 */
2297static void drain_local_stock(struct work_struct *dummy)
2298{
2299 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2300 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002301 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002302}
2303
2304/*
2305 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002306 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002307 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002308static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002309{
2310 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2311
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002312 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002313 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002314 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002315 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002316 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002317 put_cpu_var(memcg_stock);
2318}
2319
2320/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002321 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002322 * of the hierarchy under it. sync flag says whether we should block
2323 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002324 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002325static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002326{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002327 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002328
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002329 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002330 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002331 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002332 for_each_online_cpu(cpu) {
2333 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002334 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002335
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002336 memcg = stock->cached;
2337 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002338 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002339 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002340 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002341 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2342 if (cpu == curcpu)
2343 drain_local_stock(&stock->work);
2344 else
2345 schedule_work_on(cpu, &stock->work);
2346 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002347 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002348 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002349
2350 if (!sync)
2351 goto out;
2352
2353 for_each_online_cpu(cpu) {
2354 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002355 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002356 flush_work(&stock->work);
2357 }
2358out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002359 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002360}
2361
2362/*
2363 * Tries to drain stocked charges in other cpus. This function is asynchronous
2364 * and just put a work per cpu for draining localy on each cpu. Caller can
2365 * expects some charges will be back to res_counter later but cannot wait for
2366 * it.
2367 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002368static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002369{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002370 /*
2371 * If someone calls draining, avoid adding more kworker runs.
2372 */
2373 if (!mutex_trylock(&percpu_charge_mutex))
2374 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002375 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002376 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002377}
2378
2379/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002380static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002381{
2382 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002383 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002384 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002385 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002386}
2387
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002388/*
2389 * This function drains percpu counter value from DEAD cpu and
2390 * move it to local cpu. Note that this function can be preempted.
2391 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002392static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002393{
2394 int i;
2395
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002396 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002397 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002398 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002399
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002400 per_cpu(memcg->stat->count[i], cpu) = 0;
2401 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002402 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002403 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002404 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002405
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002406 per_cpu(memcg->stat->events[i], cpu) = 0;
2407 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002408 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002409 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002410}
2411
2412static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002413 unsigned long action,
2414 void *hcpu)
2415{
2416 int cpu = (unsigned long)hcpu;
2417 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002418 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002419
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002420 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002421 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002422
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002423 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002424 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002425
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002426 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002427 mem_cgroup_drain_pcp_counter(iter, cpu);
2428
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002429 stock = &per_cpu(memcg_stock, cpu);
2430 drain_stock(stock);
2431 return NOTIFY_OK;
2432}
2433
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002434
2435/* See __mem_cgroup_try_charge() for details */
2436enum {
2437 CHARGE_OK, /* success */
2438 CHARGE_RETRY, /* need to retry but retry is not bad */
2439 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2440 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2441 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2442};
2443
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002444static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002445 unsigned int nr_pages, unsigned int min_pages,
2446 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002447{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002448 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002449 struct mem_cgroup *mem_over_limit;
2450 struct res_counter *fail_res;
2451 unsigned long flags = 0;
2452 int ret;
2453
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002454 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002455
2456 if (likely(!ret)) {
2457 if (!do_swap_account)
2458 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002459 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002460 if (likely(!ret))
2461 return CHARGE_OK;
2462
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002463 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002464 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2465 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2466 } else
2467 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002468 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002469 * Never reclaim on behalf of optional batching, retry with a
2470 * single page instead.
2471 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002472 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002473 return CHARGE_RETRY;
2474
2475 if (!(gfp_mask & __GFP_WAIT))
2476 return CHARGE_WOULDBLOCK;
2477
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002478 if (gfp_mask & __GFP_NORETRY)
2479 return CHARGE_NOMEM;
2480
Johannes Weiner56600482012-01-12 17:17:59 -08002481 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002482 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002483 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002484 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002485 * Even though the limit is exceeded at this point, reclaim
2486 * may have been able to free some pages. Retry the charge
2487 * before killing the task.
2488 *
2489 * Only for regular pages, though: huge pages are rather
2490 * unlikely to succeed so close to the limit, and we fall back
2491 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002492 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002493 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002494 return CHARGE_RETRY;
2495
2496 /*
2497 * At task move, charge accounts can be doubly counted. So, it's
2498 * better to wait until the end of task_move if something is going on.
2499 */
2500 if (mem_cgroup_wait_acct_move(mem_over_limit))
2501 return CHARGE_RETRY;
2502
2503 /* If we don't need to call oom-killer at el, return immediately */
2504 if (!oom_check)
2505 return CHARGE_NOMEM;
2506 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002507 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002508 return CHARGE_OOM_DIE;
2509
2510 return CHARGE_RETRY;
2511}
2512
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002513/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002514 * __mem_cgroup_try_charge() does
2515 * 1. detect memcg to be charged against from passed *mm and *ptr,
2516 * 2. update res_counter
2517 * 3. call memory reclaim if necessary.
2518 *
2519 * In some special case, if the task is fatal, fatal_signal_pending() or
2520 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2521 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2522 * as possible without any hazards. 2: all pages should have a valid
2523 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2524 * pointer, that is treated as a charge to root_mem_cgroup.
2525 *
2526 * So __mem_cgroup_try_charge() will return
2527 * 0 ... on success, filling *ptr with a valid memcg pointer.
2528 * -ENOMEM ... charge failure because of resource limits.
2529 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2530 *
2531 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2532 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002533 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002534static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002535 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002536 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002537 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002538 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002539{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002540 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002541 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002542 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002543 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002544
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002545 /*
2546 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2547 * in system level. So, allow to go ahead dying process in addition to
2548 * MEMDIE process.
2549 */
2550 if (unlikely(test_thread_flag(TIF_MEMDIE)
2551 || fatal_signal_pending(current)))
2552 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002553
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002554 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08002555 * We always charge the cgroup the mm_struct belongs to.
2556 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002557 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002558 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002559 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002560 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002561 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002562again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002563 if (*ptr) { /* css should be a valid one */
2564 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002565 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002566 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002567 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002568 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002569 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002570 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002571 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002572
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002573 rcu_read_lock();
2574 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002575 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002576 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002577 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002578 * race with swapoff. Then, we have small risk of mis-accouning.
2579 * But such kind of mis-account by race always happens because
2580 * we don't have cgroup_mutex(). It's overkill and we allo that
2581 * small race, here.
2582 * (*) swapoff at el will charge against mm-struct not against
2583 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002584 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002585 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002586 if (!memcg)
2587 memcg = root_mem_cgroup;
2588 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002589 rcu_read_unlock();
2590 goto done;
2591 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002592 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002593 /*
2594 * It seems dagerous to access memcg without css_get().
2595 * But considering how consume_stok works, it's not
2596 * necessary. If consume_stock success, some charges
2597 * from this memcg are cached on this cpu. So, we
2598 * don't need to call css_get()/css_tryget() before
2599 * calling consume_stock().
2600 */
2601 rcu_read_unlock();
2602 goto done;
2603 }
2604 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002605 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002606 rcu_read_unlock();
2607 goto again;
2608 }
2609 rcu_read_unlock();
2610 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002611
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002612 do {
2613 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002614
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002615 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002616 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002617 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002618 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002619 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002620
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002621 oom_check = false;
2622 if (oom && !nr_oom_retries) {
2623 oom_check = true;
2624 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2625 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002626
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002627 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2628 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002629 switch (ret) {
2630 case CHARGE_OK:
2631 break;
2632 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002633 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002634 css_put(&memcg->css);
2635 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002636 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002637 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002638 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002639 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002640 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002641 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002642 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002643 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002644 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002645 /* If oom, we never return -ENOMEM */
2646 nr_oom_retries--;
2647 break;
2648 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002649 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002650 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002651 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002652 } while (ret != CHARGE_OK);
2653
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002654 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002655 refill_stock(memcg, batch - nr_pages);
2656 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002657done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002658 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002659 return 0;
2660nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002661 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002662 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002663bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002664 *ptr = root_mem_cgroup;
2665 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002666}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002667
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002668/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002669 * Somemtimes we have to undo a charge we got by try_charge().
2670 * This function is for that and do uncharge, put css's refcnt.
2671 * gotten by try_charge().
2672 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002673static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002674 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002675{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002676 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002677 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002678
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002679 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002680 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002681 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002682 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002683}
2684
2685/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002686 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2687 * This is useful when moving usage to parent cgroup.
2688 */
2689static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2690 unsigned int nr_pages)
2691{
2692 unsigned long bytes = nr_pages * PAGE_SIZE;
2693
2694 if (mem_cgroup_is_root(memcg))
2695 return;
2696
2697 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2698 if (do_swap_account)
2699 res_counter_uncharge_until(&memcg->memsw,
2700 memcg->memsw.parent, bytes);
2701}
2702
2703/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002704 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002705 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2706 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2707 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002708 */
2709static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2710{
2711 struct cgroup_subsys_state *css;
2712
2713 /* ID 0 is unused ID */
2714 if (!id)
2715 return NULL;
2716 css = css_lookup(&mem_cgroup_subsys, id);
2717 if (!css)
2718 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002719 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002720}
2721
Wu Fengguange42d9d52009-12-16 12:19:59 +01002722struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002723{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002724 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002725 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002726 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002727 swp_entry_t ent;
2728
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002729 VM_BUG_ON(!PageLocked(page));
2730
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002731 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002732 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002733 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002734 memcg = pc->mem_cgroup;
2735 if (memcg && !css_tryget(&memcg->css))
2736 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002737 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002738 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002739 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002740 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002741 memcg = mem_cgroup_lookup(id);
2742 if (memcg && !css_tryget(&memcg->css))
2743 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002744 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002745 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002746 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002747 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002748}
2749
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002750static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002751 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002752 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002753 enum charge_type ctype,
2754 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002755{
Johannes Weinerce587e62012-04-24 20:22:33 +02002756 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002757 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002758 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002759 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002760 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002761
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002762 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002763 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002764 /*
2765 * we don't need page_cgroup_lock about tail pages, becase they are not
2766 * accessed by any other context at this point.
2767 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002768
2769 /*
2770 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2771 * may already be on some other mem_cgroup's LRU. Take care of it.
2772 */
2773 if (lrucare) {
2774 zone = page_zone(page);
2775 spin_lock_irq(&zone->lru_lock);
2776 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002777 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002778 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002779 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002780 was_on_lru = true;
2781 }
2782 }
2783
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002784 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002785 /*
2786 * We access a page_cgroup asynchronously without lock_page_cgroup().
2787 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2788 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2789 * before USED bit, we need memory barrier here.
2790 * See mem_cgroup_add_lru_list(), etc.
2791 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002792 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002793 SetPageCgroupUsed(pc);
Hugh Dickins3be912772008-02-07 00:14:19 -08002794
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002795 if (lrucare) {
2796 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002797 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002798 VM_BUG_ON(PageLRU(page));
2799 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002800 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002801 }
2802 spin_unlock_irq(&zone->lru_lock);
2803 }
2804
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002805 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002806 anon = true;
2807 else
2808 anon = false;
2809
2810 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002811 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002812
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002813 /*
2814 * "charge_statistics" updated event counter. Then, check it.
2815 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2816 * if they exceeds softlimit.
2817 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002818 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002819}
2820
Glauber Costa7cf27982012-12-18 14:22:55 -08002821static DEFINE_MUTEX(set_limit_mutex);
2822
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002823#ifdef CONFIG_MEMCG_KMEM
2824static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2825{
2826 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2827 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2828}
2829
Glauber Costa1f458cb2012-12-18 14:22:50 -08002830/*
2831 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2832 * in the memcg_cache_params struct.
2833 */
2834static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2835{
2836 struct kmem_cache *cachep;
2837
2838 VM_BUG_ON(p->is_root_cache);
2839 cachep = p->root_cache;
2840 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2841}
2842
Glauber Costa749c5412012-12-18 14:23:01 -08002843#ifdef CONFIG_SLABINFO
2844static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2845 struct seq_file *m)
2846{
2847 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2848 struct memcg_cache_params *params;
2849
2850 if (!memcg_can_account_kmem(memcg))
2851 return -EIO;
2852
2853 print_slabinfo_header(m);
2854
2855 mutex_lock(&memcg->slab_caches_mutex);
2856 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2857 cache_show(memcg_params_to_cache(params), m);
2858 mutex_unlock(&memcg->slab_caches_mutex);
2859
2860 return 0;
2861}
2862#endif
2863
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002864static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2865{
2866 struct res_counter *fail_res;
2867 struct mem_cgroup *_memcg;
2868 int ret = 0;
2869 bool may_oom;
2870
2871 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2872 if (ret)
2873 return ret;
2874
2875 /*
2876 * Conditions under which we can wait for the oom_killer. Those are
2877 * the same conditions tested by the core page allocator
2878 */
2879 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2880
2881 _memcg = memcg;
2882 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2883 &_memcg, may_oom);
2884
2885 if (ret == -EINTR) {
2886 /*
2887 * __mem_cgroup_try_charge() chosed to bypass to root due to
2888 * OOM kill or fatal signal. Since our only options are to
2889 * either fail the allocation or charge it to this cgroup, do
2890 * it as a temporary condition. But we can't fail. From a
2891 * kmem/slab perspective, the cache has already been selected,
2892 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2893 * our minds.
2894 *
2895 * This condition will only trigger if the task entered
2896 * memcg_charge_kmem in a sane state, but was OOM-killed during
2897 * __mem_cgroup_try_charge() above. Tasks that were already
2898 * dying when the allocation triggers should have been already
2899 * directed to the root cgroup in memcontrol.h
2900 */
2901 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2902 if (do_swap_account)
2903 res_counter_charge_nofail(&memcg->memsw, size,
2904 &fail_res);
2905 ret = 0;
2906 } else if (ret)
2907 res_counter_uncharge(&memcg->kmem, size);
2908
2909 return ret;
2910}
2911
2912static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2913{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002914 res_counter_uncharge(&memcg->res, size);
2915 if (do_swap_account)
2916 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002917
2918 /* Not down to 0 */
2919 if (res_counter_uncharge(&memcg->kmem, size))
2920 return;
2921
2922 if (memcg_kmem_test_and_clear_dead(memcg))
2923 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002924}
2925
Glauber Costa2633d7a2012-12-18 14:22:34 -08002926void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2927{
2928 if (!memcg)
2929 return;
2930
2931 mutex_lock(&memcg->slab_caches_mutex);
2932 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2933 mutex_unlock(&memcg->slab_caches_mutex);
2934}
2935
2936/*
2937 * helper for acessing a memcg's index. It will be used as an index in the
2938 * child cache array in kmem_cache, and also to derive its name. This function
2939 * will return -1 when this is not a kmem-limited memcg.
2940 */
2941int memcg_cache_id(struct mem_cgroup *memcg)
2942{
2943 return memcg ? memcg->kmemcg_id : -1;
2944}
2945
Glauber Costa55007d82012-12-18 14:22:38 -08002946/*
2947 * This ends up being protected by the set_limit mutex, during normal
2948 * operation, because that is its main call site.
2949 *
2950 * But when we create a new cache, we can call this as well if its parent
2951 * is kmem-limited. That will have to hold set_limit_mutex as well.
2952 */
2953int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2954{
2955 int num, ret;
2956
2957 num = ida_simple_get(&kmem_limited_groups,
2958 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2959 if (num < 0)
2960 return num;
2961 /*
2962 * After this point, kmem_accounted (that we test atomically in
2963 * the beginning of this conditional), is no longer 0. This
2964 * guarantees only one process will set the following boolean
2965 * to true. We don't need test_and_set because we're protected
2966 * by the set_limit_mutex anyway.
2967 */
2968 memcg_kmem_set_activated(memcg);
2969
2970 ret = memcg_update_all_caches(num+1);
2971 if (ret) {
2972 ida_simple_remove(&kmem_limited_groups, num);
2973 memcg_kmem_clear_activated(memcg);
2974 return ret;
2975 }
2976
2977 memcg->kmemcg_id = num;
2978 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2979 mutex_init(&memcg->slab_caches_mutex);
2980 return 0;
2981}
2982
2983static size_t memcg_caches_array_size(int num_groups)
2984{
2985 ssize_t size;
2986 if (num_groups <= 0)
2987 return 0;
2988
2989 size = 2 * num_groups;
2990 if (size < MEMCG_CACHES_MIN_SIZE)
2991 size = MEMCG_CACHES_MIN_SIZE;
2992 else if (size > MEMCG_CACHES_MAX_SIZE)
2993 size = MEMCG_CACHES_MAX_SIZE;
2994
2995 return size;
2996}
2997
2998/*
2999 * We should update the current array size iff all caches updates succeed. This
3000 * can only be done from the slab side. The slab mutex needs to be held when
3001 * calling this.
3002 */
3003void memcg_update_array_size(int num)
3004{
3005 if (num > memcg_limited_groups_array_size)
3006 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3007}
3008
3009int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3010{
3011 struct memcg_cache_params *cur_params = s->memcg_params;
3012
3013 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3014
3015 if (num_groups > memcg_limited_groups_array_size) {
3016 int i;
3017 ssize_t size = memcg_caches_array_size(num_groups);
3018
3019 size *= sizeof(void *);
3020 size += sizeof(struct memcg_cache_params);
3021
3022 s->memcg_params = kzalloc(size, GFP_KERNEL);
3023 if (!s->memcg_params) {
3024 s->memcg_params = cur_params;
3025 return -ENOMEM;
3026 }
3027
3028 s->memcg_params->is_root_cache = true;
3029
3030 /*
3031 * There is the chance it will be bigger than
3032 * memcg_limited_groups_array_size, if we failed an allocation
3033 * in a cache, in which case all caches updated before it, will
3034 * have a bigger array.
3035 *
3036 * But if that is the case, the data after
3037 * memcg_limited_groups_array_size is certainly unused
3038 */
3039 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3040 if (!cur_params->memcg_caches[i])
3041 continue;
3042 s->memcg_params->memcg_caches[i] =
3043 cur_params->memcg_caches[i];
3044 }
3045
3046 /*
3047 * Ideally, we would wait until all caches succeed, and only
3048 * then free the old one. But this is not worth the extra
3049 * pointer per-cache we'd have to have for this.
3050 *
3051 * It is not a big deal if some caches are left with a size
3052 * bigger than the others. And all updates will reset this
3053 * anyway.
3054 */
3055 kfree(cur_params);
3056 }
3057 return 0;
3058}
3059
Glauber Costa943a4512012-12-18 14:23:03 -08003060int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3061 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003062{
3063 size_t size = sizeof(struct memcg_cache_params);
3064
3065 if (!memcg_kmem_enabled())
3066 return 0;
3067
Glauber Costa55007d82012-12-18 14:22:38 -08003068 if (!memcg)
3069 size += memcg_limited_groups_array_size * sizeof(void *);
3070
Glauber Costa2633d7a2012-12-18 14:22:34 -08003071 s->memcg_params = kzalloc(size, GFP_KERNEL);
3072 if (!s->memcg_params)
3073 return -ENOMEM;
3074
Glauber Costa943a4512012-12-18 14:23:03 -08003075 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003076 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003077 s->memcg_params->root_cache = root_cache;
Glauber Costa4ba902b2013-02-12 13:46:22 -08003078 } else
3079 s->memcg_params->is_root_cache = true;
3080
Glauber Costa2633d7a2012-12-18 14:22:34 -08003081 return 0;
3082}
3083
3084void memcg_release_cache(struct kmem_cache *s)
3085{
Glauber Costad7f25f82012-12-18 14:22:40 -08003086 struct kmem_cache *root;
3087 struct mem_cgroup *memcg;
3088 int id;
3089
3090 /*
3091 * This happens, for instance, when a root cache goes away before we
3092 * add any memcg.
3093 */
3094 if (!s->memcg_params)
3095 return;
3096
3097 if (s->memcg_params->is_root_cache)
3098 goto out;
3099
3100 memcg = s->memcg_params->memcg;
3101 id = memcg_cache_id(memcg);
3102
3103 root = s->memcg_params->root_cache;
3104 root->memcg_params->memcg_caches[id] = NULL;
3105 mem_cgroup_put(memcg);
3106
3107 mutex_lock(&memcg->slab_caches_mutex);
3108 list_del(&s->memcg_params->list);
3109 mutex_unlock(&memcg->slab_caches_mutex);
3110
3111out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003112 kfree(s->memcg_params);
3113}
3114
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003115/*
3116 * During the creation a new cache, we need to disable our accounting mechanism
3117 * altogether. This is true even if we are not creating, but rather just
3118 * enqueing new caches to be created.
3119 *
3120 * This is because that process will trigger allocations; some visible, like
3121 * explicit kmallocs to auxiliary data structures, name strings and internal
3122 * cache structures; some well concealed, like INIT_WORK() that can allocate
3123 * objects during debug.
3124 *
3125 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3126 * to it. This may not be a bounded recursion: since the first cache creation
3127 * failed to complete (waiting on the allocation), we'll just try to create the
3128 * cache again, failing at the same point.
3129 *
3130 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3131 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3132 * inside the following two functions.
3133 */
3134static inline void memcg_stop_kmem_account(void)
3135{
3136 VM_BUG_ON(!current->mm);
3137 current->memcg_kmem_skip_account++;
3138}
3139
3140static inline void memcg_resume_kmem_account(void)
3141{
3142 VM_BUG_ON(!current->mm);
3143 current->memcg_kmem_skip_account--;
3144}
3145
Glauber Costa1f458cb2012-12-18 14:22:50 -08003146static void kmem_cache_destroy_work_func(struct work_struct *w)
3147{
3148 struct kmem_cache *cachep;
3149 struct memcg_cache_params *p;
3150
3151 p = container_of(w, struct memcg_cache_params, destroy);
3152
3153 cachep = memcg_params_to_cache(p);
3154
Glauber Costa22933152012-12-18 14:22:59 -08003155 /*
3156 * If we get down to 0 after shrink, we could delete right away.
3157 * However, memcg_release_pages() already puts us back in the workqueue
3158 * in that case. If we proceed deleting, we'll get a dangling
3159 * reference, and removing the object from the workqueue in that case
3160 * is unnecessary complication. We are not a fast path.
3161 *
3162 * Note that this case is fundamentally different from racing with
3163 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3164 * kmem_cache_shrink, not only we would be reinserting a dead cache
3165 * into the queue, but doing so from inside the worker racing to
3166 * destroy it.
3167 *
3168 * So if we aren't down to zero, we'll just schedule a worker and try
3169 * again
3170 */
3171 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3172 kmem_cache_shrink(cachep);
3173 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3174 return;
3175 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003176 kmem_cache_destroy(cachep);
3177}
3178
3179void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3180{
3181 if (!cachep->memcg_params->dead)
3182 return;
3183
3184 /*
Glauber Costa22933152012-12-18 14:22:59 -08003185 * There are many ways in which we can get here.
3186 *
3187 * We can get to a memory-pressure situation while the delayed work is
3188 * still pending to run. The vmscan shrinkers can then release all
3189 * cache memory and get us to destruction. If this is the case, we'll
3190 * be executed twice, which is a bug (the second time will execute over
3191 * bogus data). In this case, cancelling the work should be fine.
3192 *
3193 * But we can also get here from the worker itself, if
3194 * kmem_cache_shrink is enough to shake all the remaining objects and
3195 * get the page count to 0. In this case, we'll deadlock if we try to
3196 * cancel the work (the worker runs with an internal lock held, which
3197 * is the same lock we would hold for cancel_work_sync().)
3198 *
3199 * Since we can't possibly know who got us here, just refrain from
3200 * running if there is already work pending
3201 */
3202 if (work_pending(&cachep->memcg_params->destroy))
3203 return;
3204 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003205 * We have to defer the actual destroying to a workqueue, because
3206 * we might currently be in a context that cannot sleep.
3207 */
3208 schedule_work(&cachep->memcg_params->destroy);
3209}
3210
Glauber Costad7f25f82012-12-18 14:22:40 -08003211static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s)
3212{
3213 char *name;
3214 struct dentry *dentry;
3215
3216 rcu_read_lock();
3217 dentry = rcu_dereference(memcg->css.cgroup->dentry);
3218 rcu_read_unlock();
3219
3220 BUG_ON(dentry == NULL);
3221
3222 name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name,
3223 memcg_cache_id(memcg), dentry->d_name.name);
3224
3225 return name;
3226}
3227
3228static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3229 struct kmem_cache *s)
3230{
3231 char *name;
3232 struct kmem_cache *new;
3233
3234 name = memcg_cache_name(memcg, s);
3235 if (!name)
3236 return NULL;
3237
3238 new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align,
Glauber Costa943a4512012-12-18 14:23:03 -08003239 (s->flags & ~SLAB_PANIC), s->ctor, s);
Glauber Costad7f25f82012-12-18 14:22:40 -08003240
Glauber Costad79923f2012-12-18 14:22:48 -08003241 if (new)
3242 new->allocflags |= __GFP_KMEMCG;
3243
Glauber Costad7f25f82012-12-18 14:22:40 -08003244 kfree(name);
3245 return new;
3246}
3247
3248/*
3249 * This lock protects updaters, not readers. We want readers to be as fast as
3250 * they can, and they will either see NULL or a valid cache value. Our model
3251 * allow them to see NULL, in which case the root memcg will be selected.
3252 *
3253 * We need this lock because multiple allocations to the same cache from a non
3254 * will span more than one worker. Only one of them can create the cache.
3255 */
3256static DEFINE_MUTEX(memcg_cache_mutex);
3257static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3258 struct kmem_cache *cachep)
3259{
3260 struct kmem_cache *new_cachep;
3261 int idx;
3262
3263 BUG_ON(!memcg_can_account_kmem(memcg));
3264
3265 idx = memcg_cache_id(memcg);
3266
3267 mutex_lock(&memcg_cache_mutex);
3268 new_cachep = cachep->memcg_params->memcg_caches[idx];
3269 if (new_cachep)
3270 goto out;
3271
3272 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003273 if (new_cachep == NULL) {
3274 new_cachep = cachep;
3275 goto out;
3276 }
3277
3278 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003279 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003280
3281 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3282 /*
3283 * the readers won't lock, make sure everybody sees the updated value,
3284 * so they won't put stuff in the queue again for no reason
3285 */
3286 wmb();
3287out:
3288 mutex_unlock(&memcg_cache_mutex);
3289 return new_cachep;
3290}
3291
Glauber Costa7cf27982012-12-18 14:22:55 -08003292void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3293{
3294 struct kmem_cache *c;
3295 int i;
3296
3297 if (!s->memcg_params)
3298 return;
3299 if (!s->memcg_params->is_root_cache)
3300 return;
3301
3302 /*
3303 * If the cache is being destroyed, we trust that there is no one else
3304 * requesting objects from it. Even if there are, the sanity checks in
3305 * kmem_cache_destroy should caught this ill-case.
3306 *
3307 * Still, we don't want anyone else freeing memcg_caches under our
3308 * noses, which can happen if a new memcg comes to life. As usual,
3309 * we'll take the set_limit_mutex to protect ourselves against this.
3310 */
3311 mutex_lock(&set_limit_mutex);
3312 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3313 c = s->memcg_params->memcg_caches[i];
3314 if (!c)
3315 continue;
3316
3317 /*
3318 * We will now manually delete the caches, so to avoid races
3319 * we need to cancel all pending destruction workers and
3320 * proceed with destruction ourselves.
3321 *
3322 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3323 * and that could spawn the workers again: it is likely that
3324 * the cache still have active pages until this very moment.
3325 * This would lead us back to mem_cgroup_destroy_cache.
3326 *
3327 * But that will not execute at all if the "dead" flag is not
3328 * set, so flip it down to guarantee we are in control.
3329 */
3330 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003331 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003332 kmem_cache_destroy(c);
3333 }
3334 mutex_unlock(&set_limit_mutex);
3335}
3336
Glauber Costad7f25f82012-12-18 14:22:40 -08003337struct create_work {
3338 struct mem_cgroup *memcg;
3339 struct kmem_cache *cachep;
3340 struct work_struct work;
3341};
3342
Glauber Costa1f458cb2012-12-18 14:22:50 -08003343static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3344{
3345 struct kmem_cache *cachep;
3346 struct memcg_cache_params *params;
3347
3348 if (!memcg_kmem_is_active(memcg))
3349 return;
3350
3351 mutex_lock(&memcg->slab_caches_mutex);
3352 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3353 cachep = memcg_params_to_cache(params);
3354 cachep->memcg_params->dead = true;
3355 INIT_WORK(&cachep->memcg_params->destroy,
Glauber Costa22933152012-12-18 14:22:59 -08003356 kmem_cache_destroy_work_func);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003357 schedule_work(&cachep->memcg_params->destroy);
3358 }
3359 mutex_unlock(&memcg->slab_caches_mutex);
3360}
3361
Glauber Costad7f25f82012-12-18 14:22:40 -08003362static void memcg_create_cache_work_func(struct work_struct *w)
3363{
3364 struct create_work *cw;
3365
3366 cw = container_of(w, struct create_work, work);
3367 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3368 /* Drop the reference gotten when we enqueued. */
3369 css_put(&cw->memcg->css);
3370 kfree(cw);
3371}
3372
3373/*
3374 * Enqueue the creation of a per-memcg kmem_cache.
3375 * Called with rcu_read_lock.
3376 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003377static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3378 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003379{
3380 struct create_work *cw;
3381
3382 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3383 if (cw == NULL)
3384 return;
3385
3386 /* The corresponding put will be done in the workqueue. */
3387 if (!css_tryget(&memcg->css)) {
3388 kfree(cw);
3389 return;
3390 }
3391
3392 cw->memcg = memcg;
3393 cw->cachep = cachep;
3394
3395 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3396 schedule_work(&cw->work);
3397}
3398
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003399static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3400 struct kmem_cache *cachep)
3401{
3402 /*
3403 * We need to stop accounting when we kmalloc, because if the
3404 * corresponding kmalloc cache is not yet created, the first allocation
3405 * in __memcg_create_cache_enqueue will recurse.
3406 *
3407 * However, it is better to enclose the whole function. Depending on
3408 * the debugging options enabled, INIT_WORK(), for instance, can
3409 * trigger an allocation. This too, will make us recurse. Because at
3410 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3411 * the safest choice is to do it like this, wrapping the whole function.
3412 */
3413 memcg_stop_kmem_account();
3414 __memcg_create_cache_enqueue(memcg, cachep);
3415 memcg_resume_kmem_account();
3416}
Glauber Costad7f25f82012-12-18 14:22:40 -08003417/*
3418 * Return the kmem_cache we're supposed to use for a slab allocation.
3419 * We try to use the current memcg's version of the cache.
3420 *
3421 * If the cache does not exist yet, if we are the first user of it,
3422 * we either create it immediately, if possible, or create it asynchronously
3423 * in a workqueue.
3424 * In the latter case, we will let the current allocation go through with
3425 * the original cache.
3426 *
3427 * Can't be called in interrupt context or from kernel threads.
3428 * This function needs to be called with rcu_read_lock() held.
3429 */
3430struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3431 gfp_t gfp)
3432{
3433 struct mem_cgroup *memcg;
3434 int idx;
3435
3436 VM_BUG_ON(!cachep->memcg_params);
3437 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3438
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003439 if (!current->mm || current->memcg_kmem_skip_account)
3440 return cachep;
3441
Glauber Costad7f25f82012-12-18 14:22:40 -08003442 rcu_read_lock();
3443 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3444 rcu_read_unlock();
3445
3446 if (!memcg_can_account_kmem(memcg))
3447 return cachep;
3448
3449 idx = memcg_cache_id(memcg);
3450
3451 /*
3452 * barrier to mare sure we're always seeing the up to date value. The
3453 * code updating memcg_caches will issue a write barrier to match this.
3454 */
3455 read_barrier_depends();
3456 if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) {
3457 /*
3458 * If we are in a safe context (can wait, and not in interrupt
3459 * context), we could be be predictable and return right away.
3460 * This would guarantee that the allocation being performed
3461 * already belongs in the new cache.
3462 *
3463 * However, there are some clashes that can arrive from locking.
3464 * For instance, because we acquire the slab_mutex while doing
3465 * kmem_cache_dup, this means no further allocation could happen
3466 * with the slab_mutex held.
3467 *
3468 * Also, because cache creation issue get_online_cpus(), this
3469 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3470 * that ends up reversed during cpu hotplug. (cpuset allocates
3471 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3472 * better to defer everything.
3473 */
3474 memcg_create_cache_enqueue(memcg, cachep);
3475 return cachep;
3476 }
3477
3478 return cachep->memcg_params->memcg_caches[idx];
3479}
3480EXPORT_SYMBOL(__memcg_kmem_get_cache);
3481
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003482/*
3483 * We need to verify if the allocation against current->mm->owner's memcg is
3484 * possible for the given order. But the page is not allocated yet, so we'll
3485 * need a further commit step to do the final arrangements.
3486 *
3487 * It is possible for the task to switch cgroups in this mean time, so at
3488 * commit time, we can't rely on task conversion any longer. We'll then use
3489 * the handle argument to return to the caller which cgroup we should commit
3490 * against. We could also return the memcg directly and avoid the pointer
3491 * passing, but a boolean return value gives better semantics considering
3492 * the compiled-out case as well.
3493 *
3494 * Returning true means the allocation is possible.
3495 */
3496bool
3497__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3498{
3499 struct mem_cgroup *memcg;
3500 int ret;
3501
3502 *_memcg = NULL;
3503 memcg = try_get_mem_cgroup_from_mm(current->mm);
3504
3505 /*
3506 * very rare case described in mem_cgroup_from_task. Unfortunately there
3507 * isn't much we can do without complicating this too much, and it would
3508 * be gfp-dependent anyway. Just let it go
3509 */
3510 if (unlikely(!memcg))
3511 return true;
3512
3513 if (!memcg_can_account_kmem(memcg)) {
3514 css_put(&memcg->css);
3515 return true;
3516 }
3517
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003518 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3519 if (!ret)
3520 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003521
3522 css_put(&memcg->css);
3523 return (ret == 0);
3524}
3525
3526void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3527 int order)
3528{
3529 struct page_cgroup *pc;
3530
3531 VM_BUG_ON(mem_cgroup_is_root(memcg));
3532
3533 /* The page allocation failed. Revert */
3534 if (!page) {
3535 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003536 return;
3537 }
3538
3539 pc = lookup_page_cgroup(page);
3540 lock_page_cgroup(pc);
3541 pc->mem_cgroup = memcg;
3542 SetPageCgroupUsed(pc);
3543 unlock_page_cgroup(pc);
3544}
3545
3546void __memcg_kmem_uncharge_pages(struct page *page, int order)
3547{
3548 struct mem_cgroup *memcg = NULL;
3549 struct page_cgroup *pc;
3550
3551
3552 pc = lookup_page_cgroup(page);
3553 /*
3554 * Fast unlocked return. Theoretically might have changed, have to
3555 * check again after locking.
3556 */
3557 if (!PageCgroupUsed(pc))
3558 return;
3559
3560 lock_page_cgroup(pc);
3561 if (PageCgroupUsed(pc)) {
3562 memcg = pc->mem_cgroup;
3563 ClearPageCgroupUsed(pc);
3564 }
3565 unlock_page_cgroup(pc);
3566
3567 /*
3568 * We trust that only if there is a memcg associated with the page, it
3569 * is a valid allocation
3570 */
3571 if (!memcg)
3572 return;
3573
3574 VM_BUG_ON(mem_cgroup_is_root(memcg));
3575 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003576}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003577#else
3578static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3579{
3580}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003581#endif /* CONFIG_MEMCG_KMEM */
3582
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003583#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3584
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003585#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003586/*
3587 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003588 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3589 * charge/uncharge will be never happen and move_account() is done under
3590 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003591 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003592void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003593{
3594 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003595 struct page_cgroup *pc;
3596 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003597
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003598 if (mem_cgroup_disabled())
3599 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003600 for (i = 1; i < HPAGE_PMD_NR; i++) {
3601 pc = head_pc + i;
3602 pc->mem_cgroup = head_pc->mem_cgroup;
3603 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003604 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3605 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003606}
Hugh Dickins12d27102012-01-12 17:19:52 -08003607#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003608
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003609/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003610 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003611 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003612 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003613 * @pc: page_cgroup of the page.
3614 * @from: mem_cgroup which the page is moved from.
3615 * @to: mem_cgroup which the page is moved to. @from != @to.
3616 *
3617 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003618 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003619 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003620 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003621 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3622 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003623 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003624static int mem_cgroup_move_account(struct page *page,
3625 unsigned int nr_pages,
3626 struct page_cgroup *pc,
3627 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003628 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003629{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003630 unsigned long flags;
3631 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003632 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003633
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003634 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003635 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003636 /*
3637 * The page is isolated from LRU. So, collapse function
3638 * will not handle this page. But page splitting can happen.
3639 * Do this check under compound_page_lock(). The caller should
3640 * hold it.
3641 */
3642 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003643 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003644 goto out;
3645
3646 lock_page_cgroup(pc);
3647
3648 ret = -EINVAL;
3649 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3650 goto unlock;
3651
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003652 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003653
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003654 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003655 /* Update mapped_file data for mem_cgroup */
3656 preempt_disable();
3657 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3658 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3659 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003660 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003661 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003662
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003663 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003664 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003665 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003666 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003667 ret = 0;
3668unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003669 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003670 /*
3671 * check events
3672 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003673 memcg_check_events(to, page);
3674 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003675out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003676 return ret;
3677}
3678
Michal Hocko2ef37d32012-10-26 13:37:30 +02003679/**
3680 * mem_cgroup_move_parent - moves page to the parent group
3681 * @page: the page to move
3682 * @pc: page_cgroup of the page
3683 * @child: page's cgroup
3684 *
3685 * move charges to its parent or the root cgroup if the group has no
3686 * parent (aka use_hierarchy==0).
3687 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3688 * mem_cgroup_move_account fails) the failure is always temporary and
3689 * it signals a race with a page removal/uncharge or migration. In the
3690 * first case the page is on the way out and it will vanish from the LRU
3691 * on the next attempt and the call should be retried later.
3692 * Isolation from the LRU fails only if page has been isolated from
3693 * the LRU since we looked at it and that usually means either global
3694 * reclaim or migration going on. The page will either get back to the
3695 * LRU or vanish.
3696 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3697 * (!PageCgroupUsed) or moved to a different group. The page will
3698 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003699 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003700static int mem_cgroup_move_parent(struct page *page,
3701 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003702 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003703{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003704 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003705 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003706 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003707 int ret;
3708
Michal Hockod8423012012-10-26 13:37:29 +02003709 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003710
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003711 ret = -EBUSY;
3712 if (!get_page_unless_zero(page))
3713 goto out;
3714 if (isolate_lru_page(page))
3715 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003716
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003717 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003718
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003719 parent = parent_mem_cgroup(child);
3720 /*
3721 * If no parent, move charges to root cgroup.
3722 */
3723 if (!parent)
3724 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003725
Michal Hocko2ef37d32012-10-26 13:37:30 +02003726 if (nr_pages > 1) {
3727 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003728 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003729 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003730
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003731 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003732 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003733 if (!ret)
3734 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003735
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003736 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003737 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003738 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003739put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003740 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003741out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003742 return ret;
3743}
3744
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003745/*
3746 * Charge the memory controller for page usage.
3747 * Return
3748 * 0 if the charge was successful
3749 * < 0 if the cgroup is over its limit
3750 */
3751static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003752 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003753{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003754 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003755 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003756 bool oom = true;
3757 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003758
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003759 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003760 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003761 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003762 /*
3763 * Never OOM-kill a process for a huge page. The
3764 * fault handler will fall back to regular pages.
3765 */
3766 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003767 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003768
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003769 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003770 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003771 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003772 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003773 return 0;
3774}
3775
3776int mem_cgroup_newpage_charge(struct page *page,
3777 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003778{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003779 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003780 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003781 VM_BUG_ON(page_mapped(page));
3782 VM_BUG_ON(page->mapping && !PageAnon(page));
3783 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003784 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003785 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003786}
3787
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003788/*
3789 * While swap-in, try_charge -> commit or cancel, the page is locked.
3790 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003791 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003792 * "commit()" or removed by "cancel()"
3793 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003794static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3795 struct page *page,
3796 gfp_t mask,
3797 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003798{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003799 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003800 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003801 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003802
Johannes Weiner90deb782012-07-31 16:45:47 -07003803 pc = lookup_page_cgroup(page);
3804 /*
3805 * Every swap fault against a single page tries to charge the
3806 * page, bail as early as possible. shmem_unuse() encounters
3807 * already charged pages, too. The USED bit is protected by
3808 * the page lock, which serializes swap cache removal, which
3809 * in turn serializes uncharging.
3810 */
3811 if (PageCgroupUsed(pc))
3812 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003813 if (!do_swap_account)
3814 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003815 memcg = try_get_mem_cgroup_from_page(page);
3816 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003817 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003818 *memcgp = memcg;
3819 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003820 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003821 if (ret == -EINTR)
3822 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003823 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003824charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003825 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3826 if (ret == -EINTR)
3827 ret = 0;
3828 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003829}
3830
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003831int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3832 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3833{
3834 *memcgp = NULL;
3835 if (mem_cgroup_disabled())
3836 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003837 /*
3838 * A racing thread's fault, or swapoff, may have already
3839 * updated the pte, and even removed page from swap cache: in
3840 * those cases unuse_pte()'s pte_same() test will fail; but
3841 * there's also a KSM case which does need to charge the page.
3842 */
3843 if (!PageSwapCache(page)) {
3844 int ret;
3845
3846 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3847 if (ret == -EINTR)
3848 ret = 0;
3849 return ret;
3850 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003851 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3852}
3853
Johannes Weiner827a03d2012-07-31 16:45:36 -07003854void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3855{
3856 if (mem_cgroup_disabled())
3857 return;
3858 if (!memcg)
3859 return;
3860 __mem_cgroup_cancel_charge(memcg, 1);
3861}
3862
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003863static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003864__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003865 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003866{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003867 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003868 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003869 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003870 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003871
Johannes Weinerce587e62012-04-24 20:22:33 +02003872 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003873 /*
3874 * Now swap is on-memory. This means this page may be
3875 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003876 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3877 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3878 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003879 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003880 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003881 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003882 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003883 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003884}
3885
Johannes Weiner72835c82012-01-12 17:18:32 -08003886void mem_cgroup_commit_charge_swapin(struct page *page,
3887 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003888{
Johannes Weiner72835c82012-01-12 17:18:32 -08003889 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003890 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003891}
3892
Johannes Weiner827a03d2012-07-31 16:45:36 -07003893int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3894 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003895{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003896 struct mem_cgroup *memcg = NULL;
3897 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3898 int ret;
3899
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003900 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003901 return 0;
3902 if (PageCompound(page))
3903 return 0;
3904
Johannes Weiner827a03d2012-07-31 16:45:36 -07003905 if (!PageSwapCache(page))
3906 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3907 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003908 ret = __mem_cgroup_try_charge_swapin(mm, page,
3909 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003910 if (!ret)
3911 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3912 }
3913 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003914}
3915
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003916static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003917 unsigned int nr_pages,
3918 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003919{
3920 struct memcg_batch_info *batch = NULL;
3921 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003922
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003923 /* If swapout, usage of swap doesn't decrease */
3924 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3925 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003926
3927 batch = &current->memcg_batch;
3928 /*
3929 * In usual, we do css_get() when we remember memcg pointer.
3930 * But in this case, we keep res->usage until end of a series of
3931 * uncharges. Then, it's ok to ignore memcg's refcnt.
3932 */
3933 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003934 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003935 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003936 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003937 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003938 * the same cgroup and we have chance to coalesce uncharges.
3939 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3940 * because we want to do uncharge as soon as possible.
3941 */
3942
3943 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3944 goto direct_uncharge;
3945
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003946 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003947 goto direct_uncharge;
3948
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003949 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003950 * In typical case, batch->memcg == mem. This means we can
3951 * merge a series of uncharges to an uncharge of res_counter.
3952 * If not, we uncharge res_counter ony by one.
3953 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003954 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003955 goto direct_uncharge;
3956 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003957 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003958 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003959 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003960 return;
3961direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003962 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003963 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003964 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3965 if (unlikely(batch->memcg != memcg))
3966 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003967}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003968
Balbir Singh8697d332008-02-07 00:13:59 -08003969/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003970 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003971 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003972static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003973__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3974 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003975{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003976 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003977 unsigned int nr_pages = 1;
3978 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003979 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003980
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003981 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003982 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003983
Johannes Weiner0c59b892012-07-31 16:45:31 -07003984 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003985
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003986 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003987 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003988 VM_BUG_ON(!PageTransHuge(page));
3989 }
Balbir Singh8697d332008-02-07 00:13:59 -08003990 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003991 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003992 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003993 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003994 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003995 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003996
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003997 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003998
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003999 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004000
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004001 if (!PageCgroupUsed(pc))
4002 goto unlock_out;
4003
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004004 anon = PageAnon(page);
4005
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004006 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004007 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004008 /*
4009 * Generally PageAnon tells if it's the anon statistics to be
4010 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4011 * used before page reached the stage of being marked PageAnon.
4012 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004013 anon = true;
4014 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004015 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004016 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004017 if (page_mapped(page))
4018 goto unlock_out;
4019 /*
4020 * Pages under migration may not be uncharged. But
4021 * end_migration() /must/ be the one uncharging the
4022 * unused post-migration page and so it has to call
4023 * here with the migration bit still set. See the
4024 * res_counter handling below.
4025 */
4026 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004027 goto unlock_out;
4028 break;
4029 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4030 if (!PageAnon(page)) { /* Shared memory */
4031 if (page->mapping && !page_is_file_cache(page))
4032 goto unlock_out;
4033 } else if (page_mapped(page)) /* Anon */
4034 goto unlock_out;
4035 break;
4036 default:
4037 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004038 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004039
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004040 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004041
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004042 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004043 /*
4044 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4045 * freed from LRU. This is safe because uncharged page is expected not
4046 * to be reused (freed soon). Exception is SwapCache, it's handled by
4047 * special functions.
4048 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004049
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004050 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004051 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004052 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004053 * will never be freed.
4054 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004055 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004056 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004057 mem_cgroup_swap_statistics(memcg, true);
4058 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004059 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004060 /*
4061 * Migration does not charge the res_counter for the
4062 * replacement page, so leave it alone when phasing out the
4063 * page that is unused after the migration.
4064 */
4065 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004066 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004067
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004068 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004069
4070unlock_out:
4071 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004072 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004073}
4074
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004075void mem_cgroup_uncharge_page(struct page *page)
4076{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004077 /* early check. */
4078 if (page_mapped(page))
4079 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004080 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07004081 if (PageSwapCache(page))
4082 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004083 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004084}
4085
4086void mem_cgroup_uncharge_cache_page(struct page *page)
4087{
4088 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004089 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004090 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004091}
4092
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004093/*
4094 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4095 * In that cases, pages are freed continuously and we can expect pages
4096 * are in the same memcg. All these calls itself limits the number of
4097 * pages freed at once, then uncharge_start/end() is called properly.
4098 * This may be called prural(2) times in a context,
4099 */
4100
4101void mem_cgroup_uncharge_start(void)
4102{
4103 current->memcg_batch.do_batch++;
4104 /* We can do nest. */
4105 if (current->memcg_batch.do_batch == 1) {
4106 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004107 current->memcg_batch.nr_pages = 0;
4108 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004109 }
4110}
4111
4112void mem_cgroup_uncharge_end(void)
4113{
4114 struct memcg_batch_info *batch = &current->memcg_batch;
4115
4116 if (!batch->do_batch)
4117 return;
4118
4119 batch->do_batch--;
4120 if (batch->do_batch) /* If stacked, do nothing. */
4121 return;
4122
4123 if (!batch->memcg)
4124 return;
4125 /*
4126 * This "batch->memcg" is valid without any css_get/put etc...
4127 * bacause we hide charges behind us.
4128 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004129 if (batch->nr_pages)
4130 res_counter_uncharge(&batch->memcg->res,
4131 batch->nr_pages * PAGE_SIZE);
4132 if (batch->memsw_nr_pages)
4133 res_counter_uncharge(&batch->memcg->memsw,
4134 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004135 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004136 /* forget this pointer (for sanity check) */
4137 batch->memcg = NULL;
4138}
4139
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004140#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004141/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004142 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004143 * memcg information is recorded to swap_cgroup of "ent"
4144 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004145void
4146mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004147{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004148 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004149 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004150
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004151 if (!swapout) /* this was a swap cache but the swap is unused ! */
4152 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4153
Johannes Weiner0030f532012-07-31 16:45:25 -07004154 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004155
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004156 /*
4157 * record memcg information, if swapout && memcg != NULL,
4158 * mem_cgroup_get() was called in uncharge().
4159 */
4160 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004161 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004162}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004163#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004164
Andrew Mortonc255a452012-07-31 16:43:02 -07004165#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004166/*
4167 * called from swap_entry_free(). remove record in swap_cgroup and
4168 * uncharge "memsw" account.
4169 */
4170void mem_cgroup_uncharge_swap(swp_entry_t ent)
4171{
4172 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004173 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004174
4175 if (!do_swap_account)
4176 return;
4177
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004178 id = swap_cgroup_record(ent, 0);
4179 rcu_read_lock();
4180 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004181 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004182 /*
4183 * We uncharge this because swap is freed.
4184 * This memcg can be obsolete one. We avoid calling css_tryget
4185 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004186 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004187 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004188 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004189 mem_cgroup_put(memcg);
4190 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004191 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004192}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004193
4194/**
4195 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4196 * @entry: swap entry to be moved
4197 * @from: mem_cgroup which the entry is moved from
4198 * @to: mem_cgroup which the entry is moved to
4199 *
4200 * It succeeds only when the swap_cgroup's record for this entry is the same
4201 * as the mem_cgroup's id of @from.
4202 *
4203 * Returns 0 on success, -EINVAL on failure.
4204 *
4205 * The caller must have charged to @to, IOW, called res_counter_charge() about
4206 * both res and memsw, and called css_get().
4207 */
4208static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004209 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004210{
4211 unsigned short old_id, new_id;
4212
4213 old_id = css_id(&from->css);
4214 new_id = css_id(&to->css);
4215
4216 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004217 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004218 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004219 /*
4220 * This function is only called from task migration context now.
4221 * It postpones res_counter and refcount handling till the end
4222 * of task migration(mem_cgroup_clear_mc()) for performance
4223 * improvement. But we cannot postpone mem_cgroup_get(to)
4224 * because if the process that has been moved to @to does
4225 * swap-in, the refcount of @to might be decreased to 0.
4226 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004227 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004228 return 0;
4229 }
4230 return -EINVAL;
4231}
4232#else
4233static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004234 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004235{
4236 return -EINVAL;
4237}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004238#endif
4239
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004240/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004241 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4242 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004243 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004244void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4245 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004246{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004247 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004248 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004249 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004250 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004251
Johannes Weiner72835c82012-01-12 17:18:32 -08004252 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004253
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004254 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004255 return;
Balbir Singh40779602008-04-04 14:29:59 -07004256
Mel Gormanb32967f2012-11-19 12:35:47 +00004257 if (PageTransHuge(page))
4258 nr_pages <<= compound_order(page);
4259
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004260 pc = lookup_page_cgroup(page);
4261 lock_page_cgroup(pc);
4262 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004263 memcg = pc->mem_cgroup;
4264 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004265 /*
4266 * At migrating an anonymous page, its mapcount goes down
4267 * to 0 and uncharge() will be called. But, even if it's fully
4268 * unmapped, migration may fail and this page has to be
4269 * charged again. We set MIGRATION flag here and delay uncharge
4270 * until end_migration() is called
4271 *
4272 * Corner Case Thinking
4273 * A)
4274 * When the old page was mapped as Anon and it's unmap-and-freed
4275 * while migration was ongoing.
4276 * If unmap finds the old page, uncharge() of it will be delayed
4277 * until end_migration(). If unmap finds a new page, it's
4278 * uncharged when it make mapcount to be 1->0. If unmap code
4279 * finds swap_migration_entry, the new page will not be mapped
4280 * and end_migration() will find it(mapcount==0).
4281 *
4282 * B)
4283 * When the old page was mapped but migraion fails, the kernel
4284 * remaps it. A charge for it is kept by MIGRATION flag even
4285 * if mapcount goes down to 0. We can do remap successfully
4286 * without charging it again.
4287 *
4288 * C)
4289 * The "old" page is under lock_page() until the end of
4290 * migration, so, the old page itself will not be swapped-out.
4291 * If the new page is swapped out before end_migraton, our
4292 * hook to usual swap-out path will catch the event.
4293 */
4294 if (PageAnon(page))
4295 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004296 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004297 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004298 /*
4299 * If the page is not charged at this point,
4300 * we return here.
4301 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004302 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004303 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004304
Johannes Weiner72835c82012-01-12 17:18:32 -08004305 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004306 /*
4307 * We charge new page before it's used/mapped. So, even if unlock_page()
4308 * is called before end_migration, we can catch all events on this new
4309 * page. In the case new page is migrated but not remapped, new page's
4310 * mapcount will be finally 0 and we call uncharge in end_migration().
4311 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004312 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004313 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004314 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004315 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004316 /*
4317 * The page is committed to the memcg, but it's not actually
4318 * charged to the res_counter since we plan on replacing the
4319 * old one and only one page is going to be left afterwards.
4320 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004321 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004322}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004323
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004324/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004325void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004326 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004327{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004328 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004329 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004330 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004331
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004332 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004333 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004334
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004335 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004336 used = oldpage;
4337 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004338 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004339 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004340 unused = oldpage;
4341 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004342 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004343 __mem_cgroup_uncharge_common(unused,
4344 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4345 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4346 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004347 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004348 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004349 * We disallowed uncharge of pages under migration because mapcount
4350 * of the page goes down to zero, temporarly.
4351 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004352 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004353 pc = lookup_page_cgroup(oldpage);
4354 lock_page_cgroup(pc);
4355 ClearPageCgroupMigration(pc);
4356 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004357
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004358 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004359 * If a page is a file cache, radix-tree replacement is very atomic
4360 * and we can skip this check. When it was an Anon page, its mapcount
4361 * goes down to 0. But because we added MIGRATION flage, it's not
4362 * uncharged yet. There are several case but page->mapcount check
4363 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4364 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004365 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004366 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004367 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004368}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004369
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004370/*
4371 * At replace page cache, newpage is not under any memcg but it's on
4372 * LRU. So, this function doesn't touch res_counter but handles LRU
4373 * in correct way. Both pages are locked so we cannot race with uncharge.
4374 */
4375void mem_cgroup_replace_page_cache(struct page *oldpage,
4376 struct page *newpage)
4377{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004378 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004379 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004380 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004381
4382 if (mem_cgroup_disabled())
4383 return;
4384
4385 pc = lookup_page_cgroup(oldpage);
4386 /* fix accounting on old pages */
4387 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004388 if (PageCgroupUsed(pc)) {
4389 memcg = pc->mem_cgroup;
4390 mem_cgroup_charge_statistics(memcg, false, -1);
4391 ClearPageCgroupUsed(pc);
4392 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004393 unlock_page_cgroup(pc);
4394
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004395 /*
4396 * When called from shmem_replace_page(), in some cases the
4397 * oldpage has already been charged, and in some cases not.
4398 */
4399 if (!memcg)
4400 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004401 /*
4402 * Even if newpage->mapping was NULL before starting replacement,
4403 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4404 * LRU while we overwrite pc->mem_cgroup.
4405 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004406 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004407}
4408
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004409#ifdef CONFIG_DEBUG_VM
4410static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4411{
4412 struct page_cgroup *pc;
4413
4414 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004415 /*
4416 * Can be NULL while feeding pages into the page allocator for
4417 * the first time, i.e. during boot or memory hotplug;
4418 * or when mem_cgroup_disabled().
4419 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004420 if (likely(pc) && PageCgroupUsed(pc))
4421 return pc;
4422 return NULL;
4423}
4424
4425bool mem_cgroup_bad_page_check(struct page *page)
4426{
4427 if (mem_cgroup_disabled())
4428 return false;
4429
4430 return lookup_page_cgroup_used(page) != NULL;
4431}
4432
4433void mem_cgroup_print_bad_page(struct page *page)
4434{
4435 struct page_cgroup *pc;
4436
4437 pc = lookup_page_cgroup_used(page);
4438 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004439 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4440 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004441 }
4442}
4443#endif
4444
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004445static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004446 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004447{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004448 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004449 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004450 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004451 int children = mem_cgroup_count_children(memcg);
4452 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004453 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004454
4455 /*
4456 * For keeping hierarchical_reclaim simple, how long we should retry
4457 * is depends on callers. We set our retry-count to be function
4458 * of # of children which we should visit in this loop.
4459 */
4460 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4461
4462 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004463
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004464 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004465 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004466 if (signal_pending(current)) {
4467 ret = -EINTR;
4468 break;
4469 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004470 /*
4471 * Rather than hide all in some function, I do this in
4472 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004473 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004474 */
4475 mutex_lock(&set_limit_mutex);
4476 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4477 if (memswlimit < val) {
4478 ret = -EINVAL;
4479 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004480 break;
4481 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004482
4483 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4484 if (memlimit < val)
4485 enlarge = 1;
4486
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004487 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004488 if (!ret) {
4489 if (memswlimit == val)
4490 memcg->memsw_is_minimum = true;
4491 else
4492 memcg->memsw_is_minimum = false;
4493 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004494 mutex_unlock(&set_limit_mutex);
4495
4496 if (!ret)
4497 break;
4498
Johannes Weiner56600482012-01-12 17:17:59 -08004499 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4500 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004501 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4502 /* Usage is reduced ? */
4503 if (curusage >= oldusage)
4504 retry_count--;
4505 else
4506 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004507 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004508 if (!ret && enlarge)
4509 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004510
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004511 return ret;
4512}
4513
Li Zefan338c8432009-06-17 16:27:15 -07004514static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4515 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004516{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004517 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004518 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004519 int children = mem_cgroup_count_children(memcg);
4520 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004521 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004522
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004523 /* see mem_cgroup_resize_res_limit */
4524 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4525 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004526 while (retry_count) {
4527 if (signal_pending(current)) {
4528 ret = -EINTR;
4529 break;
4530 }
4531 /*
4532 * Rather than hide all in some function, I do this in
4533 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004534 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004535 */
4536 mutex_lock(&set_limit_mutex);
4537 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4538 if (memlimit > val) {
4539 ret = -EINVAL;
4540 mutex_unlock(&set_limit_mutex);
4541 break;
4542 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004543 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4544 if (memswlimit < val)
4545 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004546 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004547 if (!ret) {
4548 if (memlimit == val)
4549 memcg->memsw_is_minimum = true;
4550 else
4551 memcg->memsw_is_minimum = false;
4552 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004553 mutex_unlock(&set_limit_mutex);
4554
4555 if (!ret)
4556 break;
4557
Johannes Weiner56600482012-01-12 17:17:59 -08004558 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4559 MEM_CGROUP_RECLAIM_NOSWAP |
4560 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004561 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004562 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004563 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004564 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004565 else
4566 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004567 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004568 if (!ret && enlarge)
4569 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004570 return ret;
4571}
4572
Balbir Singh4e416952009-09-23 15:56:39 -07004573unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004574 gfp_t gfp_mask,
4575 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004576{
4577 unsigned long nr_reclaimed = 0;
4578 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4579 unsigned long reclaimed;
4580 int loop = 0;
4581 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004582 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004583 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004584
4585 if (order > 0)
4586 return 0;
4587
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004588 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004589 /*
4590 * This loop can run a while, specially if mem_cgroup's continuously
4591 * keep exceeding their soft limit and putting the system under
4592 * pressure
4593 */
4594 do {
4595 if (next_mz)
4596 mz = next_mz;
4597 else
4598 mz = mem_cgroup_largest_soft_limit_node(mctz);
4599 if (!mz)
4600 break;
4601
Ying Han0ae5e892011-05-26 16:25:25 -07004602 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004603 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004604 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004605 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004606 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004607 spin_lock(&mctz->lock);
4608
4609 /*
4610 * If we failed to reclaim anything from this memory cgroup
4611 * it is time to move on to the next cgroup
4612 */
4613 next_mz = NULL;
4614 if (!reclaimed) {
4615 do {
4616 /*
4617 * Loop until we find yet another one.
4618 *
4619 * By the time we get the soft_limit lock
4620 * again, someone might have aded the
4621 * group back on the RB tree. Iterate to
4622 * make sure we get a different mem.
4623 * mem_cgroup_largest_soft_limit_node returns
4624 * NULL if no other cgroup is present on
4625 * the tree
4626 */
4627 next_mz =
4628 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004629 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004630 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004631 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004632 break;
4633 } while (1);
4634 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004635 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4636 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004637 /*
4638 * One school of thought says that we should not add
4639 * back the node to the tree if reclaim returns 0.
4640 * But our reclaim could return 0, simply because due
4641 * to priority we are exposing a smaller subset of
4642 * memory to reclaim from. Consider this as a longer
4643 * term TODO.
4644 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004645 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004646 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004647 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004648 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004649 loop++;
4650 /*
4651 * Could not reclaim anything and there are no more
4652 * mem cgroups to try or we seem to be looping without
4653 * reclaiming anything.
4654 */
4655 if (!nr_reclaimed &&
4656 (next_mz == NULL ||
4657 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4658 break;
4659 } while (!nr_reclaimed);
4660 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004661 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004662 return nr_reclaimed;
4663}
4664
Michal Hocko2ef37d32012-10-26 13:37:30 +02004665/**
4666 * mem_cgroup_force_empty_list - clears LRU of a group
4667 * @memcg: group to clear
4668 * @node: NUMA node
4669 * @zid: zone id
4670 * @lru: lru to to clear
4671 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004672 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004673 * reclaim the pages page themselves - pages are moved to the parent (or root)
4674 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004675 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004676static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004677 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004678{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004679 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004680 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004681 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004682 struct page *busy;
4683 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004684
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004685 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004686 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4687 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004688
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004689 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004690 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004691 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004692 struct page *page;
4693
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004694 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004695 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004696 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004697 break;
4698 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004699 page = list_entry(list->prev, struct page, lru);
4700 if (busy == page) {
4701 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004702 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004703 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004704 continue;
4705 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004706 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004707
Johannes Weiner925b7672012-01-12 17:18:15 -08004708 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004709
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004710 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004711 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004712 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004713 cond_resched();
4714 } else
4715 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004716 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004717}
4718
4719/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004720 * make mem_cgroup's charge to be 0 if there is no task by moving
4721 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004722 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004723 *
4724 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004725 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004726static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004727{
Michal Hockoc26251f2012-10-26 13:37:28 +02004728 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004729 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004730
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004731 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004732 /* This is for making all *used* pages to be on LRU. */
4733 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004734 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004735 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004736 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004737 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab932012-03-21 16:34:19 -07004738 enum lru_list lru;
4739 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004740 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab932012-03-21 16:34:19 -07004741 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004742 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004743 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004744 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004745 mem_cgroup_end_move(memcg);
4746 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004747 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004748
Michal Hocko2ef37d32012-10-26 13:37:30 +02004749 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004750 * Kernel memory may not necessarily be trackable to a specific
4751 * process. So they are not migrated, and therefore we can't
4752 * expect their value to drop to 0 here.
4753 * Having res filled up with kmem only is enough.
4754 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004755 * This is a safety check because mem_cgroup_force_empty_list
4756 * could have raced with mem_cgroup_replace_page_cache callers
4757 * so the lru seemed empty but the page could have been added
4758 * right after the check. RES_USAGE should be safe as we always
4759 * charge before adding to the LRU.
4760 */
Glauber Costabea207c2012-12-18 14:22:11 -08004761 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4762 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4763 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004764}
4765
4766/*
4767 * Reclaims as many pages from the given memcg as possible and moves
4768 * the rest to the parent.
4769 *
4770 * Caller is responsible for holding css reference for memcg.
4771 */
4772static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4773{
4774 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4775 struct cgroup *cgrp = memcg->css.cgroup;
4776
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004777 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004778 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4779 return -EBUSY;
4780
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004781 /* we call try-to-free pages for make this cgroup empty */
4782 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004783 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004784 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004785 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004786
Michal Hockoc26251f2012-10-26 13:37:28 +02004787 if (signal_pending(current))
4788 return -EINTR;
4789
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004790 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004791 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004792 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004793 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004794 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004795 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004796 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004797
4798 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004799 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004800 mem_cgroup_reparent_charges(memcg);
4801
4802 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004803}
4804
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004805static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004806{
Michal Hockoc26251f2012-10-26 13:37:28 +02004807 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4808 int ret;
4809
Michal Hockod8423012012-10-26 13:37:29 +02004810 if (mem_cgroup_is_root(memcg))
4811 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004812 css_get(&memcg->css);
4813 ret = mem_cgroup_force_empty(memcg);
4814 css_put(&memcg->css);
4815
4816 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004817}
4818
4819
Balbir Singh18f59ea2009-01-07 18:08:07 -08004820static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4821{
4822 return mem_cgroup_from_cont(cont)->use_hierarchy;
4823}
4824
4825static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4826 u64 val)
4827{
4828 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004829 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004830 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004831 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004832
4833 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004834 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004835
4836 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004837
4838 if (memcg->use_hierarchy == val)
4839 goto out;
4840
Balbir Singh18f59ea2009-01-07 18:08:07 -08004841 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004842 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004843 * in the child subtrees. If it is unset, then the change can
4844 * occur, provided the current cgroup has no children.
4845 *
4846 * For the root cgroup, parent_mem is NULL, we allow value to be
4847 * set if there are no children.
4848 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004849 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004850 (val == 1 || val == 0)) {
4851 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004852 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004853 else
4854 retval = -EBUSY;
4855 } else
4856 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004857
4858out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004859 cgroup_unlock();
4860
4861 return retval;
4862}
4863
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004864
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004865static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004866 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004867{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004868 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004869 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004870
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004871 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004872 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004873 val += mem_cgroup_read_stat(iter, idx);
4874
4875 if (val < 0) /* race ? */
4876 val = 0;
4877 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004878}
4879
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004880static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004881{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004882 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004883
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004884 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004885 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004886 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004887 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004888 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004889 }
4890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004891 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4892 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004893
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004894 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004895 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004896
4897 return val << PAGE_SHIFT;
4898}
4899
Tejun Heoaf36f902012-04-01 12:09:55 -07004900static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4901 struct file *file, char __user *buf,
4902 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004903{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004904 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004905 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004906 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004907 int name, len;
4908 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004909
4910 type = MEMFILE_TYPE(cft->private);
4911 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004912
4913 if (!do_swap_account && type == _MEMSWAP)
4914 return -EOPNOTSUPP;
4915
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004916 switch (type) {
4917 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004918 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004919 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004920 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004921 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004922 break;
4923 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004924 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004925 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004926 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004927 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004928 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004929 case _KMEM:
4930 val = res_counter_read_u64(&memcg->kmem, name);
4931 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004932 default:
4933 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004934 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004935
4936 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4937 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004938}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004939
4940static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4941{
4942 int ret = -EINVAL;
4943#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004944 bool must_inc_static_branch = false;
4945
Glauber Costa510fc4e2012-12-18 14:21:47 -08004946 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4947 /*
4948 * For simplicity, we won't allow this to be disabled. It also can't
4949 * be changed if the cgroup has children already, or if tasks had
4950 * already joined.
4951 *
4952 * If tasks join before we set the limit, a person looking at
4953 * kmem.usage_in_bytes will have no way to determine when it took
4954 * place, which makes the value quite meaningless.
4955 *
4956 * After it first became limited, changes in the value of the limit are
4957 * of course permitted.
4958 *
4959 * Taking the cgroup_lock is really offensive, but it is so far the only
4960 * way to guarantee that no children will appear. There are plenty of
4961 * other offenders, and they should all go away. Fine grained locking
4962 * is probably the way to go here. When we are fully hierarchical, we
4963 * can also get rid of the use_hierarchy check.
4964 */
4965 cgroup_lock();
4966 mutex_lock(&set_limit_mutex);
4967 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4968 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4969 !list_empty(&cont->children))) {
4970 ret = -EBUSY;
4971 goto out;
4972 }
4973 ret = res_counter_set_limit(&memcg->kmem, val);
4974 VM_BUG_ON(ret);
4975
Glauber Costa55007d82012-12-18 14:22:38 -08004976 ret = memcg_update_cache_sizes(memcg);
4977 if (ret) {
4978 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
4979 goto out;
4980 }
Glauber Costaa8964b92012-12-18 14:22:09 -08004981 must_inc_static_branch = true;
Glauber Costa7de37682012-12-18 14:22:07 -08004982 /*
4983 * kmem charges can outlive the cgroup. In the case of slab
4984 * pages, for instance, a page contain objects from various
4985 * processes, so it is unfeasible to migrate them away. We
4986 * need to reference count the memcg because of that.
4987 */
4988 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004989 } else
4990 ret = res_counter_set_limit(&memcg->kmem, val);
4991out:
4992 mutex_unlock(&set_limit_mutex);
4993 cgroup_unlock();
Glauber Costaa8964b92012-12-18 14:22:09 -08004994
4995 /*
4996 * We are by now familiar with the fact that we can't inc the static
4997 * branch inside cgroup_lock. See disarm functions for details. A
4998 * worker here is overkill, but also wrong: After the limit is set, we
4999 * must start accounting right away. Since this operation can't fail,
5000 * we can safely defer it to here - no rollback will be needed.
5001 *
5002 * The boolean used to control this is also safe, because
5003 * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be
5004 * able to set it to true;
5005 */
5006 if (must_inc_static_branch) {
5007 static_key_slow_inc(&memcg_kmem_enabled_key);
5008 /*
5009 * setting the active bit after the inc will guarantee no one
5010 * starts accounting before all call sites are patched
5011 */
5012 memcg_kmem_set_active(memcg);
5013 }
5014
Glauber Costa510fc4e2012-12-18 14:21:47 -08005015#endif
5016 return ret;
5017}
5018
Glauber Costa55007d82012-12-18 14:22:38 -08005019static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005020{
Glauber Costa55007d82012-12-18 14:22:38 -08005021 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005022 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5023 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005024 goto out;
5025
Glauber Costa510fc4e2012-12-18 14:21:47 -08005026 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costa7de37682012-12-18 14:22:07 -08005027#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08005028 /*
5029 * When that happen, we need to disable the static branch only on those
5030 * memcgs that enabled it. To achieve this, we would be forced to
5031 * complicate the code by keeping track of which memcgs were the ones
5032 * that actually enabled limits, and which ones got it from its
5033 * parents.
5034 *
5035 * It is a lot simpler just to do static_key_slow_inc() on every child
5036 * that is accounted.
5037 */
Glauber Costa55007d82012-12-18 14:22:38 -08005038 if (!memcg_kmem_is_active(memcg))
5039 goto out;
5040
5041 /*
5042 * destroy(), called if we fail, will issue static_key_slow_inc() and
5043 * mem_cgroup_put() if kmem is enabled. We have to either call them
5044 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5045 * this more consistent, since it always leads to the same destroy path
5046 */
5047 mem_cgroup_get(memcg);
5048 static_key_slow_inc(&memcg_kmem_enabled_key);
5049
5050 mutex_lock(&set_limit_mutex);
5051 ret = memcg_update_cache_sizes(memcg);
5052 mutex_unlock(&set_limit_mutex);
Glauber Costa7de37682012-12-18 14:22:07 -08005053#endif
Glauber Costa55007d82012-12-18 14:22:38 -08005054out:
5055 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005056}
5057
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005058/*
5059 * The user of this function is...
5060 * RES_LIMIT.
5061 */
Paul Menage856c13a2008-07-25 01:47:04 -07005062static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5063 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005064{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005065 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005066 enum res_type type;
5067 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005068 unsigned long long val;
5069 int ret;
5070
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005071 type = MEMFILE_TYPE(cft->private);
5072 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005073
5074 if (!do_swap_account && type == _MEMSWAP)
5075 return -EOPNOTSUPP;
5076
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005077 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005078 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005079 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5080 ret = -EINVAL;
5081 break;
5082 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005083 /* This function does all necessary parse...reuse it */
5084 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005085 if (ret)
5086 break;
5087 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005088 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005089 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005090 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005091 else if (type == _KMEM)
5092 ret = memcg_update_kmem_limit(cont, val);
5093 else
5094 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005095 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005096 case RES_SOFT_LIMIT:
5097 ret = res_counter_memparse_write_strategy(buffer, &val);
5098 if (ret)
5099 break;
5100 /*
5101 * For memsw, soft limits are hard to implement in terms
5102 * of semantics, for now, we support soft limits for
5103 * control without swap
5104 */
5105 if (type == _MEM)
5106 ret = res_counter_set_soft_limit(&memcg->res, val);
5107 else
5108 ret = -EINVAL;
5109 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005110 default:
5111 ret = -EINVAL; /* should be BUG() ? */
5112 break;
5113 }
5114 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005115}
5116
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005117static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5118 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5119{
5120 struct cgroup *cgroup;
5121 unsigned long long min_limit, min_memsw_limit, tmp;
5122
5123 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5124 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5125 cgroup = memcg->css.cgroup;
5126 if (!memcg->use_hierarchy)
5127 goto out;
5128
5129 while (cgroup->parent) {
5130 cgroup = cgroup->parent;
5131 memcg = mem_cgroup_from_cont(cgroup);
5132 if (!memcg->use_hierarchy)
5133 break;
5134 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5135 min_limit = min(min_limit, tmp);
5136 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5137 min_memsw_limit = min(min_memsw_limit, tmp);
5138 }
5139out:
5140 *mem_limit = min_limit;
5141 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005142}
5143
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005144static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005145{
Tejun Heoaf36f902012-04-01 12:09:55 -07005146 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005147 int name;
5148 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005149
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005150 type = MEMFILE_TYPE(event);
5151 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005152
5153 if (!do_swap_account && type == _MEMSWAP)
5154 return -EOPNOTSUPP;
5155
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005156 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005157 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005158 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005159 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005160 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005161 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005162 else if (type == _KMEM)
5163 res_counter_reset_max(&memcg->kmem);
5164 else
5165 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005166 break;
5167 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005168 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005169 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005170 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005171 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005172 else if (type == _KMEM)
5173 res_counter_reset_failcnt(&memcg->kmem);
5174 else
5175 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005176 break;
5177 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005178
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005179 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005180}
5181
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005182static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5183 struct cftype *cft)
5184{
5185 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5186}
5187
Daisuke Nishimura02491442010-03-10 15:22:17 -08005188#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005189static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5190 struct cftype *cft, u64 val)
5191{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005192 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005193
5194 if (val >= (1 << NR_MOVE_TYPE))
5195 return -EINVAL;
5196 /*
5197 * We check this value several times in both in can_attach() and
5198 * attach(), so we need cgroup lock to prevent this value from being
5199 * inconsistent.
5200 */
5201 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005202 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005203 cgroup_unlock();
5204
5205 return 0;
5206}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005207#else
5208static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5209 struct cftype *cft, u64 val)
5210{
5211 return -ENOSYS;
5212}
5213#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005214
Ying Han406eb0c2011-05-26 16:25:37 -07005215#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005216static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005217 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005218{
5219 int nid;
5220 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5221 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005222 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005223
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005224 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005225 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005226 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005227 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005228 seq_printf(m, " N%d=%lu", nid, node_nr);
5229 }
5230 seq_putc(m, '\n');
5231
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005232 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005233 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005234 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005235 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005236 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005237 seq_printf(m, " N%d=%lu", nid, node_nr);
5238 }
5239 seq_putc(m, '\n');
5240
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005241 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005242 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005243 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005244 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005245 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005246 seq_printf(m, " N%d=%lu", nid, node_nr);
5247 }
5248 seq_putc(m, '\n');
5249
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005250 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005251 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005252 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005253 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005254 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005255 seq_printf(m, " N%d=%lu", nid, node_nr);
5256 }
5257 seq_putc(m, '\n');
5258 return 0;
5259}
5260#endif /* CONFIG_NUMA */
5261
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005262static inline void mem_cgroup_lru_names_not_uptodate(void)
5263{
5264 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5265}
5266
Wanpeng Liab215882012-07-31 16:43:09 -07005267static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005268 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005269{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005270 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005271 struct mem_cgroup *mi;
5272 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005273
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005274 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005275 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005276 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005277 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5278 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005279 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005280
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005281 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5282 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5283 mem_cgroup_read_events(memcg, i));
5284
5285 for (i = 0; i < NR_LRU_LISTS; i++)
5286 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5287 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5288
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005289 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005290 {
5291 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005292 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005293 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005294 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005295 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5296 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005297 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005298
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005299 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5300 long long val = 0;
5301
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005302 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005303 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005304 for_each_mem_cgroup_tree(mi, memcg)
5305 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5306 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5307 }
5308
5309 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5310 unsigned long long val = 0;
5311
5312 for_each_mem_cgroup_tree(mi, memcg)
5313 val += mem_cgroup_read_events(mi, i);
5314 seq_printf(m, "total_%s %llu\n",
5315 mem_cgroup_events_names[i], val);
5316 }
5317
5318 for (i = 0; i < NR_LRU_LISTS; i++) {
5319 unsigned long long val = 0;
5320
5321 for_each_mem_cgroup_tree(mi, memcg)
5322 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5323 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005324 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005325
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005326#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005327 {
5328 int nid, zid;
5329 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005330 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005331 unsigned long recent_rotated[2] = {0, 0};
5332 unsigned long recent_scanned[2] = {0, 0};
5333
5334 for_each_online_node(nid)
5335 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005336 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005337 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005338
Hugh Dickins89abfab2012-05-29 15:06:53 -07005339 recent_rotated[0] += rstat->recent_rotated[0];
5340 recent_rotated[1] += rstat->recent_rotated[1];
5341 recent_scanned[0] += rstat->recent_scanned[0];
5342 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005343 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005344 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5345 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5346 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5347 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005348 }
5349#endif
5350
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005351 return 0;
5352}
5353
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005354static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5355{
5356 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5357
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005358 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005359}
5360
5361static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5362 u64 val)
5363{
5364 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5365 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005366
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005367 if (val > 100)
5368 return -EINVAL;
5369
5370 if (cgrp->parent == NULL)
5371 return -EINVAL;
5372
5373 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005374
5375 cgroup_lock();
5376
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005377 /* If under hierarchy, only empty-root can set this value */
5378 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08005379 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
5380 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005381 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005382 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005383
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005384 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005385
Li Zefan068b38c2009-01-15 13:51:26 -08005386 cgroup_unlock();
5387
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005388 return 0;
5389}
5390
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005391static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5392{
5393 struct mem_cgroup_threshold_ary *t;
5394 u64 usage;
5395 int i;
5396
5397 rcu_read_lock();
5398 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005399 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005400 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005401 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005402
5403 if (!t)
5404 goto unlock;
5405
5406 usage = mem_cgroup_usage(memcg, swap);
5407
5408 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005409 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005410 * If it's not true, a threshold was crossed after last
5411 * call of __mem_cgroup_threshold().
5412 */
Phil Carmody5407a562010-05-26 14:42:42 -07005413 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005414
5415 /*
5416 * Iterate backward over array of thresholds starting from
5417 * current_threshold and check if a threshold is crossed.
5418 * If none of thresholds below usage is crossed, we read
5419 * only one element of the array here.
5420 */
5421 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5422 eventfd_signal(t->entries[i].eventfd, 1);
5423
5424 /* i = current_threshold + 1 */
5425 i++;
5426
5427 /*
5428 * Iterate forward over array of thresholds starting from
5429 * current_threshold+1 and check if a threshold is crossed.
5430 * If none of thresholds above usage is crossed, we read
5431 * only one element of the array here.
5432 */
5433 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5434 eventfd_signal(t->entries[i].eventfd, 1);
5435
5436 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005437 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005438unlock:
5439 rcu_read_unlock();
5440}
5441
5442static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5443{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005444 while (memcg) {
5445 __mem_cgroup_threshold(memcg, false);
5446 if (do_swap_account)
5447 __mem_cgroup_threshold(memcg, true);
5448
5449 memcg = parent_mem_cgroup(memcg);
5450 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005451}
5452
5453static int compare_thresholds(const void *a, const void *b)
5454{
5455 const struct mem_cgroup_threshold *_a = a;
5456 const struct mem_cgroup_threshold *_b = b;
5457
5458 return _a->threshold - _b->threshold;
5459}
5460
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005461static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005462{
5463 struct mem_cgroup_eventfd_list *ev;
5464
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005465 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005466 eventfd_signal(ev->eventfd, 1);
5467 return 0;
5468}
5469
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005470static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005471{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005472 struct mem_cgroup *iter;
5473
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005474 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005475 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005476}
5477
5478static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5479 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005480{
5481 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005482 struct mem_cgroup_thresholds *thresholds;
5483 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005484 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005485 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005486 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005487
5488 ret = res_counter_memparse_write_strategy(args, &threshold);
5489 if (ret)
5490 return ret;
5491
5492 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005493
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005494 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005495 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005496 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005497 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005498 else
5499 BUG();
5500
5501 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5502
5503 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005504 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005505 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5506
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005507 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005508
5509 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005510 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005511 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005512 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005513 ret = -ENOMEM;
5514 goto unlock;
5515 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005516 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005517
5518 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005519 if (thresholds->primary) {
5520 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005521 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005522 }
5523
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005524 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005525 new->entries[size - 1].eventfd = eventfd;
5526 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005527
5528 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005529 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005530 compare_thresholds, NULL);
5531
5532 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005533 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005534 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005535 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005536 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005537 * new->current_threshold will not be used until
5538 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005539 * it here.
5540 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005541 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005542 } else
5543 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005544 }
5545
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005546 /* Free old spare buffer and save old primary buffer as spare */
5547 kfree(thresholds->spare);
5548 thresholds->spare = thresholds->primary;
5549
5550 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005551
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005552 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005553 synchronize_rcu();
5554
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005555unlock:
5556 mutex_unlock(&memcg->thresholds_lock);
5557
5558 return ret;
5559}
5560
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005561static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005562 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005563{
5564 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005565 struct mem_cgroup_thresholds *thresholds;
5566 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005567 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005568 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005569 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005570
5571 mutex_lock(&memcg->thresholds_lock);
5572 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005573 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005574 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005575 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005576 else
5577 BUG();
5578
Anton Vorontsov371528c2012-02-24 05:14:46 +04005579 if (!thresholds->primary)
5580 goto unlock;
5581
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005582 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5583
5584 /* Check if a threshold crossed before removing */
5585 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5586
5587 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005588 size = 0;
5589 for (i = 0; i < thresholds->primary->size; i++) {
5590 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005591 size++;
5592 }
5593
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005594 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005595
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005596 /* Set thresholds array to NULL if we don't have thresholds */
5597 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005598 kfree(new);
5599 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005600 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005601 }
5602
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005603 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005604
5605 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005606 new->current_threshold = -1;
5607 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5608 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005609 continue;
5610
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005611 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005612 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005613 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005614 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005615 * until rcu_assign_pointer(), so it's safe to increment
5616 * it here.
5617 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005618 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005619 }
5620 j++;
5621 }
5622
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005623swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005624 /* Swap primary and spare array */
5625 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005626 /* If all events are unregistered, free the spare array */
5627 if (!new) {
5628 kfree(thresholds->spare);
5629 thresholds->spare = NULL;
5630 }
5631
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005632 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005633
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005634 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005635 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005636unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005637 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005638}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005639
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005640static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5641 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5642{
5643 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5644 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005645 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005646
5647 BUG_ON(type != _OOM_TYPE);
5648 event = kmalloc(sizeof(*event), GFP_KERNEL);
5649 if (!event)
5650 return -ENOMEM;
5651
Michal Hocko1af8efe2011-07-26 16:08:24 -07005652 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005653
5654 event->eventfd = eventfd;
5655 list_add(&event->list, &memcg->oom_notify);
5656
5657 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005658 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005659 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005660 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005661
5662 return 0;
5663}
5664
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005665static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005666 struct cftype *cft, struct eventfd_ctx *eventfd)
5667{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005668 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005669 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005670 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005671
5672 BUG_ON(type != _OOM_TYPE);
5673
Michal Hocko1af8efe2011-07-26 16:08:24 -07005674 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005675
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005676 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005677 if (ev->eventfd == eventfd) {
5678 list_del(&ev->list);
5679 kfree(ev);
5680 }
5681 }
5682
Michal Hocko1af8efe2011-07-26 16:08:24 -07005683 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005684}
5685
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005686static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5687 struct cftype *cft, struct cgroup_map_cb *cb)
5688{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005689 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005690
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005691 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005692
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005693 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005694 cb->fill(cb, "under_oom", 1);
5695 else
5696 cb->fill(cb, "under_oom", 0);
5697 return 0;
5698}
5699
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005700static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5701 struct cftype *cft, u64 val)
5702{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005703 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005704 struct mem_cgroup *parent;
5705
5706 /* cannot set to root cgroup and only 0 and 1 are allowed */
5707 if (!cgrp->parent || !((val == 0) || (val == 1)))
5708 return -EINVAL;
5709
5710 parent = mem_cgroup_from_cont(cgrp->parent);
5711
5712 cgroup_lock();
5713 /* oom-kill-disable is a flag for subhierarchy. */
5714 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005715 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005716 cgroup_unlock();
5717 return -EINVAL;
5718 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005719 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005720 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005721 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005722 cgroup_unlock();
5723 return 0;
5724}
5725
Andrew Mortonc255a452012-07-31 16:43:02 -07005726#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005727static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005728{
Glauber Costa55007d82012-12-18 14:22:38 -08005729 int ret;
5730
Glauber Costa2633d7a2012-12-18 14:22:34 -08005731 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005732 ret = memcg_propagate_kmem(memcg);
5733 if (ret)
5734 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005735
Glauber Costa1d62e432012-04-09 19:36:33 -03005736 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005737};
5738
Glauber Costa1d62e432012-04-09 19:36:33 -03005739static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005740{
Glauber Costa1d62e432012-04-09 19:36:33 -03005741 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005742
5743 memcg_kmem_mark_dead(memcg);
5744
5745 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5746 return;
5747
5748 /*
5749 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5750 * path here, being careful not to race with memcg_uncharge_kmem: it is
5751 * possible that the charges went down to 0 between mark_dead and the
5752 * res_counter read, so in that case, we don't need the put
5753 */
5754 if (memcg_kmem_test_and_clear_dead(memcg))
5755 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005756}
Glauber Costae5671df2011-12-11 21:47:01 +00005757#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005758static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005759{
5760 return 0;
5761}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005762
Glauber Costa1d62e432012-04-09 19:36:33 -03005763static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005764{
5765}
Glauber Costae5671df2011-12-11 21:47:01 +00005766#endif
5767
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005768static struct cftype mem_cgroup_files[] = {
5769 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005770 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005771 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005772 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005773 .register_event = mem_cgroup_usage_register_event,
5774 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005775 },
5776 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005777 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005778 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005779 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005780 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005781 },
5782 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005783 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005784 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005785 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005786 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005787 },
5788 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005789 .name = "soft_limit_in_bytes",
5790 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5791 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005792 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005793 },
5794 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005795 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005796 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005797 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005798 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005799 },
Balbir Singh8697d332008-02-07 00:13:59 -08005800 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005801 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005802 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005803 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005804 {
5805 .name = "force_empty",
5806 .trigger = mem_cgroup_force_empty_write,
5807 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005808 {
5809 .name = "use_hierarchy",
5810 .write_u64 = mem_cgroup_hierarchy_write,
5811 .read_u64 = mem_cgroup_hierarchy_read,
5812 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005813 {
5814 .name = "swappiness",
5815 .read_u64 = mem_cgroup_swappiness_read,
5816 .write_u64 = mem_cgroup_swappiness_write,
5817 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005818 {
5819 .name = "move_charge_at_immigrate",
5820 .read_u64 = mem_cgroup_move_charge_read,
5821 .write_u64 = mem_cgroup_move_charge_write,
5822 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005823 {
5824 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005825 .read_map = mem_cgroup_oom_control_read,
5826 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005827 .register_event = mem_cgroup_oom_register_event,
5828 .unregister_event = mem_cgroup_oom_unregister_event,
5829 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5830 },
Ying Han406eb0c2011-05-26 16:25:37 -07005831#ifdef CONFIG_NUMA
5832 {
5833 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005834 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005835 },
5836#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005837#ifdef CONFIG_MEMCG_KMEM
5838 {
5839 .name = "kmem.limit_in_bytes",
5840 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5841 .write_string = mem_cgroup_write,
5842 .read = mem_cgroup_read,
5843 },
5844 {
5845 .name = "kmem.usage_in_bytes",
5846 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5847 .read = mem_cgroup_read,
5848 },
5849 {
5850 .name = "kmem.failcnt",
5851 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5852 .trigger = mem_cgroup_reset,
5853 .read = mem_cgroup_read,
5854 },
5855 {
5856 .name = "kmem.max_usage_in_bytes",
5857 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5858 .trigger = mem_cgroup_reset,
5859 .read = mem_cgroup_read,
5860 },
Glauber Costa749c5412012-12-18 14:23:01 -08005861#ifdef CONFIG_SLABINFO
5862 {
5863 .name = "kmem.slabinfo",
5864 .read_seq_string = mem_cgroup_slabinfo_read,
5865 },
5866#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005867#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005868 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005869};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005870
Michal Hocko2d110852013-02-22 16:34:43 -08005871#ifdef CONFIG_MEMCG_SWAP
5872static struct cftype memsw_cgroup_files[] = {
5873 {
5874 .name = "memsw.usage_in_bytes",
5875 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5876 .read = mem_cgroup_read,
5877 .register_event = mem_cgroup_usage_register_event,
5878 .unregister_event = mem_cgroup_usage_unregister_event,
5879 },
5880 {
5881 .name = "memsw.max_usage_in_bytes",
5882 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5883 .trigger = mem_cgroup_reset,
5884 .read = mem_cgroup_read,
5885 },
5886 {
5887 .name = "memsw.limit_in_bytes",
5888 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5889 .write_string = mem_cgroup_write,
5890 .read = mem_cgroup_read,
5891 },
5892 {
5893 .name = "memsw.failcnt",
5894 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5895 .trigger = mem_cgroup_reset,
5896 .read = mem_cgroup_read,
5897 },
5898 { }, /* terminate */
5899};
5900#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005901static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005902{
5903 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005904 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005905 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005906 /*
5907 * This routine is called against possible nodes.
5908 * But it's BUG to call kmalloc() against offline node.
5909 *
5910 * TODO: this routine can waste much memory for nodes which will
5911 * never be onlined. It's better to use memory hotplug callback
5912 * function.
5913 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005914 if (!node_state(node, N_NORMAL_MEMORY))
5915 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005916 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005917 if (!pn)
5918 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005919
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005920 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5921 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005922 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005923 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005924 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005925 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005926 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005927 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005928 return 0;
5929}
5930
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005931static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005932{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005933 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005934}
5935
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005936static struct mem_cgroup *mem_cgroup_alloc(void)
5937{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005938 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005939 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005940
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005941 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005942 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005943 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005944 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005945 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005946
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005947 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005948 return NULL;
5949
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005950 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5951 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005952 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005953 spin_lock_init(&memcg->pcp_counter_lock);
5954 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005955
5956out_free:
5957 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005958 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005959 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005960 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005961 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005962}
5963
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005964/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005965 * At destroying mem_cgroup, references from swap_cgroup can remain.
5966 * (scanning all at force_empty is too costly...)
5967 *
5968 * Instead of clearing all references at force_empty, we remember
5969 * the number of reference from swap_cgroup and free mem_cgroup when
5970 * it goes down to 0.
5971 *
5972 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005973 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005974
5975static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005976{
Glauber Costac8b2a362012-12-18 14:22:13 -08005977 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005978 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005979
Glauber Costac8b2a362012-12-18 14:22:13 -08005980 mem_cgroup_remove_from_trees(memcg);
5981 free_css_id(&mem_cgroup_subsys, &memcg->css);
5982
5983 for_each_node(node)
5984 free_mem_cgroup_per_zone_info(memcg, node);
5985
5986 free_percpu(memcg->stat);
5987
Glauber Costa3f134612012-05-29 15:07:11 -07005988 /*
5989 * We need to make sure that (at least for now), the jump label
5990 * destruction code runs outside of the cgroup lock. This is because
5991 * get_online_cpus(), which is called from the static_branch update,
5992 * can't be called inside the cgroup_lock. cpusets are the ones
5993 * enforcing this dependency, so if they ever change, we might as well.
5994 *
5995 * schedule_work() will guarantee this happens. Be careful if you need
5996 * to move this code around, and make sure it is outside
5997 * the cgroup_lock.
5998 */
Glauber Costaa8964b92012-12-18 14:22:09 -08005999 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006000 if (size < PAGE_SIZE)
6001 kfree(memcg);
6002 else
6003 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006004}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006005
Glauber Costac8b2a362012-12-18 14:22:13 -08006006
6007/*
6008 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
6009 * but in process context. The work_freeing structure is overlaid
6010 * on the rcu_freeing structure, which itself is overlaid on memsw.
6011 */
6012static void free_work(struct work_struct *work)
6013{
6014 struct mem_cgroup *memcg;
6015
6016 memcg = container_of(work, struct mem_cgroup, work_freeing);
6017 __mem_cgroup_free(memcg);
6018}
6019
Glauber Costa3afe36b2012-05-29 15:07:10 -07006020static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006021{
6022 struct mem_cgroup *memcg;
6023
6024 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006025 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006026 schedule_work(&memcg->work_freeing);
6027}
6028
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006029static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006030{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006031 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006032}
6033
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006034static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006035{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006036 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6037 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006038 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006039 if (parent)
6040 mem_cgroup_put(parent);
6041 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006042}
6043
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006044static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006045{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006046 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006047}
6048
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006049/*
6050 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6051 */
Glauber Costae1aab162011-12-11 21:47:03 +00006052struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006053{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006054 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006055 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006056 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006057}
Glauber Costae1aab162011-12-11 21:47:03 +00006058EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006059
Balbir Singhf64c3f52009-09-23 15:56:37 -07006060static int mem_cgroup_soft_limit_tree_init(void)
6061{
6062 struct mem_cgroup_tree_per_node *rtpn;
6063 struct mem_cgroup_tree_per_zone *rtpz;
6064 int tmp, node, zone;
6065
Bob Liu3ed28fa2012-01-12 17:19:04 -08006066 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006067 tmp = node;
6068 if (!node_state(node, N_NORMAL_MEMORY))
6069 tmp = -1;
6070 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
6071 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08006072 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006073
6074 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6075
6076 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6077 rtpz = &rtpn->rb_tree_per_zone[zone];
6078 rtpz->rb_root = RB_ROOT;
6079 spin_lock_init(&rtpz->lock);
6080 }
6081 }
6082 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08006083
6084err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08006085 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08006086 if (!soft_limit_tree.rb_tree_per_node[node])
6087 break;
6088 kfree(soft_limit_tree.rb_tree_per_node[node]);
6089 soft_limit_tree.rb_tree_per_node[node] = NULL;
6090 }
6091 return 1;
6092
Balbir Singhf64c3f52009-09-23 15:56:37 -07006093}
6094
Li Zefan0eb253e2009-01-15 13:51:25 -08006095static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006096mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006097{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006098 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006099 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006100 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006101
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006102 memcg = mem_cgroup_alloc();
6103 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006104 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006105
Bob Liu3ed28fa2012-01-12 17:19:04 -08006106 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006107 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006108 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006109
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006110 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006111 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006112 int cpu;
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006113 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006114 if (mem_cgroup_soft_limit_tree_init())
6115 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08006116 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006117 for_each_possible_cpu(cpu) {
6118 struct memcg_stock_pcp *stock =
6119 &per_cpu(memcg_stock, cpu);
6120 INIT_WORK(&stock->work, drain_local_stock);
6121 }
Balbir Singh18f59ea2009-01-07 18:08:07 -08006122 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006123 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006124 memcg->use_hierarchy = parent->use_hierarchy;
6125 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006126 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006127
Balbir Singh18f59ea2009-01-07 18:08:07 -08006128 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006129 res_counter_init(&memcg->res, &parent->res);
6130 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006131 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006132
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006133 /*
6134 * We increment refcnt of the parent to ensure that we can
6135 * safely access it on res_counter_charge/uncharge.
6136 * This refcnt will be decremented when freeing this
6137 * mem_cgroup(see mem_cgroup_put).
6138 */
6139 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006140 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006141 res_counter_init(&memcg->res, NULL);
6142 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006143 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006144 /*
6145 * Deeper hierachy with use_hierarchy == false doesn't make
6146 * much sense so let cgroup subsystem know about this
6147 * unfortunate state in our controller.
6148 */
6149 if (parent && parent != root_mem_cgroup)
6150 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006151 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006152 memcg->last_scanned_node = MAX_NUMNODES;
6153 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08006154
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08006155 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006156 memcg->swappiness = mem_cgroup_swappiness(parent);
6157 atomic_set(&memcg->refcnt, 1);
6158 memcg->move_charge_at_immigrate = 0;
6159 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07006160 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03006161
6162 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
6163 if (error) {
6164 /*
6165 * We call put now because our (and parent's) refcnts
6166 * are already in place. mem_cgroup_put() will internally
6167 * call __mem_cgroup_free, so return directly
6168 */
6169 mem_cgroup_put(memcg);
6170 return ERR_PTR(error);
6171 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006172 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006173free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006174 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006175 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006176}
6177
Tejun Heo92fb9742012-11-19 08:13:38 -08006178static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006179{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006180 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006181
Michal Hockoab5196c2012-10-26 13:37:32 +02006182 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006183 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006184}
6185
Tejun Heo92fb9742012-11-19 08:13:38 -08006186static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006187{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006188 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006189
Glauber Costa1d62e432012-04-09 19:36:33 -03006190 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006191
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006192 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006193}
6194
Daisuke Nishimura02491442010-03-10 15:22:17 -08006195#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006196/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006197#define PRECHARGE_COUNT_AT_ONCE 256
6198static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006199{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006200 int ret = 0;
6201 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006202 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006203
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006204 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006205 mc.precharge += count;
6206 /* we don't need css_get for root */
6207 return ret;
6208 }
6209 /* try to charge at once */
6210 if (count > 1) {
6211 struct res_counter *dummy;
6212 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006213 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006214 * by cgroup_lock_live_cgroup() that it is not removed and we
6215 * are still under the same cgroup_mutex. So we can postpone
6216 * css_get().
6217 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006218 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006219 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006220 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006221 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006222 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006223 goto one_by_one;
6224 }
6225 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006226 return ret;
6227 }
6228one_by_one:
6229 /* fall back to one by one charge */
6230 while (count--) {
6231 if (signal_pending(current)) {
6232 ret = -EINTR;
6233 break;
6234 }
6235 if (!batch_count--) {
6236 batch_count = PRECHARGE_COUNT_AT_ONCE;
6237 cond_resched();
6238 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006239 ret = __mem_cgroup_try_charge(NULL,
6240 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006241 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006242 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006243 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006244 mc.precharge++;
6245 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006246 return ret;
6247}
6248
6249/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006250 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006251 * @vma: the vma the pte to be checked belongs
6252 * @addr: the address corresponding to the pte to be checked
6253 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006254 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006255 *
6256 * Returns
6257 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6258 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6259 * move charge. if @target is not NULL, the page is stored in target->page
6260 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006261 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6262 * target for charge migration. if @target is not NULL, the entry is stored
6263 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006264 *
6265 * Called with pte lock held.
6266 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006267union mc_target {
6268 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006269 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006270};
6271
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006272enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006273 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006274 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006275 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006276};
6277
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006278static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6279 unsigned long addr, pte_t ptent)
6280{
6281 struct page *page = vm_normal_page(vma, addr, ptent);
6282
6283 if (!page || !page_mapped(page))
6284 return NULL;
6285 if (PageAnon(page)) {
6286 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006287 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006288 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006289 } else if (!move_file())
6290 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006291 return NULL;
6292 if (!get_page_unless_zero(page))
6293 return NULL;
6294
6295 return page;
6296}
6297
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006298#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006299static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6300 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6301{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006302 struct page *page = NULL;
6303 swp_entry_t ent = pte_to_swp_entry(ptent);
6304
6305 if (!move_anon() || non_swap_entry(ent))
6306 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006307 /*
6308 * Because lookup_swap_cache() updates some statistics counter,
6309 * we call find_get_page() with swapper_space directly.
6310 */
Shaohua Li33806f02013-02-22 16:34:37 -08006311 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006312 if (do_swap_account)
6313 entry->val = ent.val;
6314
6315 return page;
6316}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006317#else
6318static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6319 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6320{
6321 return NULL;
6322}
6323#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006324
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006325static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6326 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6327{
6328 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006329 struct address_space *mapping;
6330 pgoff_t pgoff;
6331
6332 if (!vma->vm_file) /* anonymous vma */
6333 return NULL;
6334 if (!move_file())
6335 return NULL;
6336
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006337 mapping = vma->vm_file->f_mapping;
6338 if (pte_none(ptent))
6339 pgoff = linear_page_index(vma, addr);
6340 else /* pte_file(ptent) is true */
6341 pgoff = pte_to_pgoff(ptent);
6342
6343 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006344 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006345
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006346#ifdef CONFIG_SWAP
6347 /* shmem/tmpfs may report page out on swap: account for that too. */
6348 if (radix_tree_exceptional_entry(page)) {
6349 swp_entry_t swap = radix_to_swp_entry(page);
6350 if (do_swap_account)
6351 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006352 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006353 }
6354#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006355 return page;
6356}
6357
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006358static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006359 unsigned long addr, pte_t ptent, union mc_target *target)
6360{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006361 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006362 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006363 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006364 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006365
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006366 if (pte_present(ptent))
6367 page = mc_handle_present_pte(vma, addr, ptent);
6368 else if (is_swap_pte(ptent))
6369 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006370 else if (pte_none(ptent) || pte_file(ptent))
6371 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006372
6373 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006374 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006375 if (page) {
6376 pc = lookup_page_cgroup(page);
6377 /*
6378 * Do only loose check w/o page_cgroup lock.
6379 * mem_cgroup_move_account() checks the pc is valid or not under
6380 * the lock.
6381 */
6382 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6383 ret = MC_TARGET_PAGE;
6384 if (target)
6385 target->page = page;
6386 }
6387 if (!ret || !target)
6388 put_page(page);
6389 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006390 /* There is a swap entry and a page doesn't exist or isn't charged */
6391 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006392 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006393 ret = MC_TARGET_SWAP;
6394 if (target)
6395 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006396 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006397 return ret;
6398}
6399
Naoya Horiguchi12724852012-03-21 16:34:28 -07006400#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6401/*
6402 * We don't consider swapping or file mapped pages because THP does not
6403 * support them for now.
6404 * Caller should make sure that pmd_trans_huge(pmd) is true.
6405 */
6406static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6407 unsigned long addr, pmd_t pmd, union mc_target *target)
6408{
6409 struct page *page = NULL;
6410 struct page_cgroup *pc;
6411 enum mc_target_type ret = MC_TARGET_NONE;
6412
6413 page = pmd_page(pmd);
6414 VM_BUG_ON(!page || !PageHead(page));
6415 if (!move_anon())
6416 return ret;
6417 pc = lookup_page_cgroup(page);
6418 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6419 ret = MC_TARGET_PAGE;
6420 if (target) {
6421 get_page(page);
6422 target->page = page;
6423 }
6424 }
6425 return ret;
6426}
6427#else
6428static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6429 unsigned long addr, pmd_t pmd, union mc_target *target)
6430{
6431 return MC_TARGET_NONE;
6432}
6433#endif
6434
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006435static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6436 unsigned long addr, unsigned long end,
6437 struct mm_walk *walk)
6438{
6439 struct vm_area_struct *vma = walk->private;
6440 pte_t *pte;
6441 spinlock_t *ptl;
6442
Naoya Horiguchi12724852012-03-21 16:34:28 -07006443 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6444 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6445 mc.precharge += HPAGE_PMD_NR;
6446 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006447 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006448 }
Dave Hansen03319322011-03-22 16:32:56 -07006449
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006450 if (pmd_trans_unstable(pmd))
6451 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006452 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6453 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006454 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006455 mc.precharge++; /* increment precharge temporarily */
6456 pte_unmap_unlock(pte - 1, ptl);
6457 cond_resched();
6458
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006459 return 0;
6460}
6461
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006462static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6463{
6464 unsigned long precharge;
6465 struct vm_area_struct *vma;
6466
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006467 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006468 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6469 struct mm_walk mem_cgroup_count_precharge_walk = {
6470 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6471 .mm = mm,
6472 .private = vma,
6473 };
6474 if (is_vm_hugetlb_page(vma))
6475 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006476 walk_page_range(vma->vm_start, vma->vm_end,
6477 &mem_cgroup_count_precharge_walk);
6478 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006479 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006480
6481 precharge = mc.precharge;
6482 mc.precharge = 0;
6483
6484 return precharge;
6485}
6486
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006487static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6488{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006489 unsigned long precharge = mem_cgroup_count_precharge(mm);
6490
6491 VM_BUG_ON(mc.moving_task);
6492 mc.moving_task = current;
6493 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006494}
6495
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006496/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6497static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006498{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006499 struct mem_cgroup *from = mc.from;
6500 struct mem_cgroup *to = mc.to;
6501
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006502 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006503 if (mc.precharge) {
6504 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6505 mc.precharge = 0;
6506 }
6507 /*
6508 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6509 * we must uncharge here.
6510 */
6511 if (mc.moved_charge) {
6512 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6513 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006514 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006515 /* we must fixup refcnts and charges */
6516 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006517 /* uncharge swap account from the old cgroup */
6518 if (!mem_cgroup_is_root(mc.from))
6519 res_counter_uncharge(&mc.from->memsw,
6520 PAGE_SIZE * mc.moved_swap);
6521 __mem_cgroup_put(mc.from, mc.moved_swap);
6522
6523 if (!mem_cgroup_is_root(mc.to)) {
6524 /*
6525 * we charged both to->res and to->memsw, so we should
6526 * uncharge to->res.
6527 */
6528 res_counter_uncharge(&mc.to->res,
6529 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006530 }
6531 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006532 mc.moved_swap = 0;
6533 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006534 memcg_oom_recover(from);
6535 memcg_oom_recover(to);
6536 wake_up_all(&mc.waitq);
6537}
6538
6539static void mem_cgroup_clear_mc(void)
6540{
6541 struct mem_cgroup *from = mc.from;
6542
6543 /*
6544 * we must clear moving_task before waking up waiters at the end of
6545 * task migration.
6546 */
6547 mc.moving_task = NULL;
6548 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006549 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006550 mc.from = NULL;
6551 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006552 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006553 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006554}
6555
Li Zefan761b3ef52012-01-31 13:47:36 +08006556static int mem_cgroup_can_attach(struct cgroup *cgroup,
6557 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006558{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006559 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006560 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006561 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006562
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006563 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006564 struct mm_struct *mm;
6565 struct mem_cgroup *from = mem_cgroup_from_task(p);
6566
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006567 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006568
6569 mm = get_task_mm(p);
6570 if (!mm)
6571 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006572 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006573 if (mm->owner == p) {
6574 VM_BUG_ON(mc.from);
6575 VM_BUG_ON(mc.to);
6576 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006577 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006578 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006579 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006580 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006581 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006582 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006583 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006584 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006585
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006586 ret = mem_cgroup_precharge_mc(mm);
6587 if (ret)
6588 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006589 }
6590 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006591 }
6592 return ret;
6593}
6594
Li Zefan761b3ef52012-01-31 13:47:36 +08006595static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6596 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006597{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006598 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006599}
6600
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006601static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6602 unsigned long addr, unsigned long end,
6603 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006604{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006605 int ret = 0;
6606 struct vm_area_struct *vma = walk->private;
6607 pte_t *pte;
6608 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006609 enum mc_target_type target_type;
6610 union mc_target target;
6611 struct page *page;
6612 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006613
Naoya Horiguchi12724852012-03-21 16:34:28 -07006614 /*
6615 * We don't take compound_lock() here but no race with splitting thp
6616 * happens because:
6617 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6618 * under splitting, which means there's no concurrent thp split,
6619 * - if another thread runs into split_huge_page() just after we
6620 * entered this if-block, the thread must wait for page table lock
6621 * to be unlocked in __split_huge_page_splitting(), where the main
6622 * part of thp split is not executed yet.
6623 */
6624 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006625 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006626 spin_unlock(&vma->vm_mm->page_table_lock);
6627 return 0;
6628 }
6629 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6630 if (target_type == MC_TARGET_PAGE) {
6631 page = target.page;
6632 if (!isolate_lru_page(page)) {
6633 pc = lookup_page_cgroup(page);
6634 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006635 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006636 mc.precharge -= HPAGE_PMD_NR;
6637 mc.moved_charge += HPAGE_PMD_NR;
6638 }
6639 putback_lru_page(page);
6640 }
6641 put_page(page);
6642 }
6643 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006644 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006645 }
6646
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006647 if (pmd_trans_unstable(pmd))
6648 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006649retry:
6650 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6651 for (; addr != end; addr += PAGE_SIZE) {
6652 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006653 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006654
6655 if (!mc.precharge)
6656 break;
6657
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006658 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006659 case MC_TARGET_PAGE:
6660 page = target.page;
6661 if (isolate_lru_page(page))
6662 goto put;
6663 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006664 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006665 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006666 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006667 /* we uncharge from mc.from later. */
6668 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006669 }
6670 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006671put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006672 put_page(page);
6673 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006674 case MC_TARGET_SWAP:
6675 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006676 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006677 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006678 /* we fixup refcnts and charges later. */
6679 mc.moved_swap++;
6680 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006681 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006682 default:
6683 break;
6684 }
6685 }
6686 pte_unmap_unlock(pte - 1, ptl);
6687 cond_resched();
6688
6689 if (addr != end) {
6690 /*
6691 * We have consumed all precharges we got in can_attach().
6692 * We try charge one by one, but don't do any additional
6693 * charges to mc.to if we have failed in charge once in attach()
6694 * phase.
6695 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006696 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006697 if (!ret)
6698 goto retry;
6699 }
6700
6701 return ret;
6702}
6703
6704static void mem_cgroup_move_charge(struct mm_struct *mm)
6705{
6706 struct vm_area_struct *vma;
6707
6708 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006709retry:
6710 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6711 /*
6712 * Someone who are holding the mmap_sem might be waiting in
6713 * waitq. So we cancel all extra charges, wake up all waiters,
6714 * and retry. Because we cancel precharges, we might not be able
6715 * to move enough charges, but moving charge is a best-effort
6716 * feature anyway, so it wouldn't be a big problem.
6717 */
6718 __mem_cgroup_clear_mc();
6719 cond_resched();
6720 goto retry;
6721 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006722 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6723 int ret;
6724 struct mm_walk mem_cgroup_move_charge_walk = {
6725 .pmd_entry = mem_cgroup_move_charge_pte_range,
6726 .mm = mm,
6727 .private = vma,
6728 };
6729 if (is_vm_hugetlb_page(vma))
6730 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006731 ret = walk_page_range(vma->vm_start, vma->vm_end,
6732 &mem_cgroup_move_charge_walk);
6733 if (ret)
6734 /*
6735 * means we have consumed all precharges and failed in
6736 * doing additional charge. Just abandon here.
6737 */
6738 break;
6739 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006740 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006741}
6742
Li Zefan761b3ef52012-01-31 13:47:36 +08006743static void mem_cgroup_move_task(struct cgroup *cont,
6744 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006745{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006746 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006747 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006748
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006749 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006750 if (mc.to)
6751 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006752 mmput(mm);
6753 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006754 if (mc.to)
6755 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006756}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006757#else /* !CONFIG_MMU */
Li Zefan761b3ef52012-01-31 13:47:36 +08006758static int mem_cgroup_can_attach(struct cgroup *cgroup,
6759 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006760{
6761 return 0;
6762}
Li Zefan761b3ef52012-01-31 13:47:36 +08006763static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6764 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006765{
6766}
Li Zefan761b3ef52012-01-31 13:47:36 +08006767static void mem_cgroup_move_task(struct cgroup *cont,
6768 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006769{
6770}
6771#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006772
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006773struct cgroup_subsys mem_cgroup_subsys = {
6774 .name = "memory",
6775 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006776 .css_alloc = mem_cgroup_css_alloc,
6777 .css_offline = mem_cgroup_css_offline,
6778 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006779 .can_attach = mem_cgroup_can_attach,
6780 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006781 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006782 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006783 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006784 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006785};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006786
Andrew Mortonc255a452012-07-31 16:43:02 -07006787#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006788static int __init enable_swap_account(char *s)
6789{
6790 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006791 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006792 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006793 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006794 really_do_swap_account = 0;
6795 return 1;
6796}
Michal Hockoa2c89902011-05-24 17:12:50 -07006797__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006798
Michal Hocko2d110852013-02-22 16:34:43 -08006799static void __init memsw_file_init(void)
6800{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006801 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006802}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006803
6804static void __init enable_swap_cgroup(void)
6805{
6806 if (!mem_cgroup_disabled() && really_do_swap_account) {
6807 do_swap_account = 1;
6808 memsw_file_init();
6809 }
6810}
6811
Michal Hocko2d110852013-02-22 16:34:43 -08006812#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006813static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006814{
6815}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006816#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006817
6818/*
6819 * The rest of init is performed during ->css_alloc() for root css which
6820 * happens before initcalls. hotcpu_notifier() can't be done together as
6821 * it would introduce circular locking by adding cgroup_lock -> cpu hotplug
6822 * dependency. Do it from a subsys_initcall().
6823 */
6824static int __init mem_cgroup_init(void)
6825{
6826 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006827 enable_swap_cgroup();
Michal Hocko2d110852013-02-22 16:34:43 -08006828 return 0;
6829}
6830subsys_initcall(mem_cgroup_init);