blob: 661a2c679f64854ffa89090c1a8da59f428c381c [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 Hiroyuki453a9bf2011-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/*
Glauber Costaee5e8472013-02-22 16:34:50 -0800419 * Types of charges to be moved. "move_charge_at_immitgrate" and
420 * "immigrate_flags" are treated as a left-shifted bitmap of these types.
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800421 */
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;
Glauber Costaee5e8472013-02-22 16:34:50 -0800433 unsigned long immigrate_flags;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800434 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800435 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800436 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800437 struct task_struct *moving_task; /* a task moving charges */
438 wait_queue_head_t waitq; /* a waitq for other context */
439} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700440 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800441 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
442};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800443
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700444static bool move_anon(void)
445{
Glauber Costaee5e8472013-02-22 16:34:50 -0800446 return test_bit(MOVE_CHARGE_TYPE_ANON, &mc.immigrate_flags);
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700447}
448
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700449static bool move_file(void)
450{
Glauber Costaee5e8472013-02-22 16:34:50 -0800451 return test_bit(MOVE_CHARGE_TYPE_FILE, &mc.immigrate_flags);
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700452}
453
Balbir Singh4e416952009-09-23 15:56:39 -0700454/*
455 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
456 * limit reclaim to prevent infinite loops, if they ever occur.
457 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700458#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
459#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700460
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800461enum charge_type {
462 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700463 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800464 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700465 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700466 NR_CHARGE_TYPE,
467};
468
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800469/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800470enum res_type {
471 _MEM,
472 _MEMSWAP,
473 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800474 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800475};
476
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700477#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
478#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800479#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700480/* Used for OOM nofiier */
481#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800482
Balbir Singh75822b42009-09-23 15:56:38 -0700483/*
484 * Reclaim flags for mem_cgroup_hierarchical_reclaim
485 */
486#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
487#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
488#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
489#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
490
Glauber Costa09998212013-02-22 16:34:55 -0800491/*
492 * The memcg_create_mutex will be held whenever a new cgroup is created.
493 * As a consequence, any change that needs to protect against new child cgroups
494 * appearing has to hold it as well.
495 */
496static DEFINE_MUTEX(memcg_create_mutex);
497
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700498static void mem_cgroup_get(struct mem_cgroup *memcg);
499static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000500
Wanpeng Lib2145142012-07-31 16:46:01 -0700501static inline
502struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
503{
504 return container_of(s, struct mem_cgroup, css);
505}
506
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700507static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
508{
509 return (memcg == root_mem_cgroup);
510}
511
Glauber Costae1aab162011-12-11 21:47:03 +0000512/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700513#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000514
Glauber Costae1aab162011-12-11 21:47:03 +0000515void sock_update_memcg(struct sock *sk)
516{
Glauber Costa376be5f2012-01-20 04:57:14 +0000517 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000518 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700519 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000520
521 BUG_ON(!sk->sk_prot->proto_cgroup);
522
Glauber Costaf3f511e2012-01-05 20:16:39 +0000523 /* Socket cloning can throw us here with sk_cgrp already
524 * filled. It won't however, necessarily happen from
525 * process context. So the test for root memcg given
526 * the current task's memcg won't help us in this case.
527 *
528 * Respecting the original socket's memcg is a better
529 * decision in this case.
530 */
531 if (sk->sk_cgrp) {
532 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
533 mem_cgroup_get(sk->sk_cgrp->memcg);
534 return;
535 }
536
Glauber Costae1aab162011-12-11 21:47:03 +0000537 rcu_read_lock();
538 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700539 cg_proto = sk->sk_prot->proto_cgroup(memcg);
540 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000541 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700542 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000543 }
544 rcu_read_unlock();
545 }
546}
547EXPORT_SYMBOL(sock_update_memcg);
548
549void sock_release_memcg(struct sock *sk)
550{
Glauber Costa376be5f2012-01-20 04:57:14 +0000551 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000552 struct mem_cgroup *memcg;
553 WARN_ON(!sk->sk_cgrp->memcg);
554 memcg = sk->sk_cgrp->memcg;
555 mem_cgroup_put(memcg);
556 }
557}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000558
559struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
560{
561 if (!memcg || mem_cgroup_is_root(memcg))
562 return NULL;
563
564 return &memcg->tcp_mem.cg_proto;
565}
566EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000567
Glauber Costa3f134612012-05-29 15:07:11 -0700568static void disarm_sock_keys(struct mem_cgroup *memcg)
569{
570 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
571 return;
572 static_key_slow_dec(&memcg_socket_limit_enabled);
573}
574#else
575static void disarm_sock_keys(struct mem_cgroup *memcg)
576{
577}
578#endif
579
Glauber Costaa8964b92012-12-18 14:22:09 -0800580#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800581/*
582 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
583 * There are two main reasons for not using the css_id for this:
584 * 1) this works better in sparse environments, where we have a lot of memcgs,
585 * but only a few kmem-limited. Or also, if we have, for instance, 200
586 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
587 * 200 entry array for that.
588 *
589 * 2) In order not to violate the cgroup API, we would like to do all memory
590 * allocation in ->create(). At that point, we haven't yet allocated the
591 * css_id. Having a separate index prevents us from messing with the cgroup
592 * core for this
593 *
594 * The current size of the caches array is stored in
595 * memcg_limited_groups_array_size. It will double each time we have to
596 * increase it.
597 */
598static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800599int memcg_limited_groups_array_size;
600
Glauber Costa55007d82012-12-18 14:22:38 -0800601/*
602 * MIN_SIZE is different than 1, because we would like to avoid going through
603 * the alloc/free process all the time. In a small machine, 4 kmem-limited
604 * cgroups is a reasonable guess. In the future, it could be a parameter or
605 * tunable, but that is strictly not necessary.
606 *
607 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
608 * this constant directly from cgroup, but it is understandable that this is
609 * better kept as an internal representation in cgroup.c. In any case, the
610 * css_id space is not getting any smaller, and we don't have to necessarily
611 * increase ours as well if it increases.
612 */
613#define MEMCG_CACHES_MIN_SIZE 4
614#define MEMCG_CACHES_MAX_SIZE 65535
615
Glauber Costad7f25f82012-12-18 14:22:40 -0800616/*
617 * A lot of the calls to the cache allocation functions are expected to be
618 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
619 * conditional to this static branch, we'll have to allow modules that does
620 * kmem_cache_alloc and the such to see this symbol as well
621 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800622struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800623EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800624
625static void disarm_kmem_keys(struct mem_cgroup *memcg)
626{
Glauber Costa55007d82012-12-18 14:22:38 -0800627 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800628 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800629 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
630 }
Glauber Costabea207c2012-12-18 14:22:11 -0800631 /*
632 * This check can't live in kmem destruction function,
633 * since the charges will outlive the cgroup
634 */
635 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800636}
637#else
638static void disarm_kmem_keys(struct mem_cgroup *memcg)
639{
640}
641#endif /* CONFIG_MEMCG_KMEM */
642
643static void disarm_static_keys(struct mem_cgroup *memcg)
644{
645 disarm_sock_keys(memcg);
646 disarm_kmem_keys(memcg);
647}
648
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700649static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800650
Balbir Singhf64c3f52009-09-23 15:56:37 -0700651static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700652mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700653{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800654 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700655 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700656}
657
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700658struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100659{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700660 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100661}
662
Balbir Singhf64c3f52009-09-23 15:56:37 -0700663static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700664page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700665{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700666 int nid = page_to_nid(page);
667 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700668
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700669 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700670}
671
672static struct mem_cgroup_tree_per_zone *
673soft_limit_tree_node_zone(int nid, int zid)
674{
675 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
676}
677
678static struct mem_cgroup_tree_per_zone *
679soft_limit_tree_from_page(struct page *page)
680{
681 int nid = page_to_nid(page);
682 int zid = page_zonenum(page);
683
684 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
685}
686
687static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700688__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700689 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700690 struct mem_cgroup_tree_per_zone *mctz,
691 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700692{
693 struct rb_node **p = &mctz->rb_root.rb_node;
694 struct rb_node *parent = NULL;
695 struct mem_cgroup_per_zone *mz_node;
696
697 if (mz->on_tree)
698 return;
699
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700700 mz->usage_in_excess = new_usage_in_excess;
701 if (!mz->usage_in_excess)
702 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700703 while (*p) {
704 parent = *p;
705 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
706 tree_node);
707 if (mz->usage_in_excess < mz_node->usage_in_excess)
708 p = &(*p)->rb_left;
709 /*
710 * We can't avoid mem cgroups that are over their soft
711 * limit by the same amount
712 */
713 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
714 p = &(*p)->rb_right;
715 }
716 rb_link_node(&mz->tree_node, parent, p);
717 rb_insert_color(&mz->tree_node, &mctz->rb_root);
718 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700719}
720
721static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700722__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700723 struct mem_cgroup_per_zone *mz,
724 struct mem_cgroup_tree_per_zone *mctz)
725{
726 if (!mz->on_tree)
727 return;
728 rb_erase(&mz->tree_node, &mctz->rb_root);
729 mz->on_tree = false;
730}
731
732static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700733mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700734 struct mem_cgroup_per_zone *mz,
735 struct mem_cgroup_tree_per_zone *mctz)
736{
737 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700738 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700739 spin_unlock(&mctz->lock);
740}
741
Balbir Singhf64c3f52009-09-23 15:56:37 -0700742
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700743static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700744{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700745 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700746 struct mem_cgroup_per_zone *mz;
747 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700748 int nid = page_to_nid(page);
749 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700750 mctz = soft_limit_tree_from_page(page);
751
752 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700753 * Necessary to update all ancestors when hierarchy is used.
754 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700755 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700756 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
757 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
758 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700759 /*
760 * We have to update the tree if mz is on RB-tree or
761 * mem is over its softlimit.
762 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700763 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700764 spin_lock(&mctz->lock);
765 /* if on-tree, remove it */
766 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700767 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700768 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700769 * Insert again. mz->usage_in_excess will be updated.
770 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700771 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700772 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700773 spin_unlock(&mctz->lock);
774 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700775 }
776}
777
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700778static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700779{
780 int node, zone;
781 struct mem_cgroup_per_zone *mz;
782 struct mem_cgroup_tree_per_zone *mctz;
783
Bob Liu3ed28fa2012-01-12 17:19:04 -0800784 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700785 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700786 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700787 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700788 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700789 }
790 }
791}
792
Balbir Singh4e416952009-09-23 15:56:39 -0700793static struct mem_cgroup_per_zone *
794__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
795{
796 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700797 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700798
799retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700800 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700801 rightmost = rb_last(&mctz->rb_root);
802 if (!rightmost)
803 goto done; /* Nothing to reclaim from */
804
805 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
806 /*
807 * Remove the node now but someone else can add it back,
808 * we will to add it back at the end of reclaim to its correct
809 * position in the tree.
810 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700811 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
812 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
813 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700814 goto retry;
815done:
816 return mz;
817}
818
819static struct mem_cgroup_per_zone *
820mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
821{
822 struct mem_cgroup_per_zone *mz;
823
824 spin_lock(&mctz->lock);
825 mz = __mem_cgroup_largest_soft_limit_node(mctz);
826 spin_unlock(&mctz->lock);
827 return mz;
828}
829
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700830/*
831 * Implementation Note: reading percpu statistics for memcg.
832 *
833 * Both of vmstat[] and percpu_counter has threshold and do periodic
834 * synchronization to implement "quick" read. There are trade-off between
835 * reading cost and precision of value. Then, we may have a chance to implement
836 * a periodic synchronizion of counter in memcg's counter.
837 *
838 * But this _read() function is used for user interface now. The user accounts
839 * memory usage by memory cgroup and he _always_ requires exact value because
840 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
841 * have to visit all online cpus and make sum. So, for now, unnecessary
842 * synchronization is not implemented. (just implemented for cpu hotplug)
843 *
844 * If there are kernel internal actions which can make use of some not-exact
845 * value, and reading all cpu value can be performance bottleneck in some
846 * common workload, threashold and synchonization as vmstat[] should be
847 * implemented.
848 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700849static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700850 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800851{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700852 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800853 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800854
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700855 get_online_cpus();
856 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700857 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700858#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700859 spin_lock(&memcg->pcp_counter_lock);
860 val += memcg->nocpu_base.count[idx];
861 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700862#endif
863 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800864 return val;
865}
866
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700867static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700868 bool charge)
869{
870 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700871 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700872}
873
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700874static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700875 enum mem_cgroup_events_index idx)
876{
877 unsigned long val = 0;
878 int cpu;
879
880 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700881 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700882#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700883 spin_lock(&memcg->pcp_counter_lock);
884 val += memcg->nocpu_base.events[idx];
885 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700886#endif
887 return val;
888}
889
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700890static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700891 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800892{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800893 preempt_disable();
894
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700895 /*
896 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
897 * counted as CACHE even if it's on ANON LRU.
898 */
899 if (anon)
900 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700901 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800902 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700903 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700904 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700905
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800906 /* pagein of a big page is an event. So, ignore page size */
907 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700908 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800909 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700910 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800911 nr_pages = -nr_pages; /* for event */
912 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800913
Johannes Weiner13114712012-05-29 15:07:07 -0700914 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800915
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800916 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800917}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800918
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700919unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700920mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700921{
922 struct mem_cgroup_per_zone *mz;
923
924 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
925 return mz->lru_size[lru];
926}
927
928static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700929mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700930 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700931{
932 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700933 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700934 unsigned long ret = 0;
935
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700936 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700937
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700938 for_each_lru(lru) {
939 if (BIT(lru) & lru_mask)
940 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700941 }
942 return ret;
943}
944
945static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700946mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700947 int nid, unsigned int lru_mask)
948{
Ying Han889976d2011-05-26 16:25:33 -0700949 u64 total = 0;
950 int zid;
951
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700952 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700953 total += mem_cgroup_zone_nr_lru_pages(memcg,
954 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700955
Ying Han889976d2011-05-26 16:25:33 -0700956 return total;
957}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700958
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700959static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700960 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800961{
Ying Han889976d2011-05-26 16:25:33 -0700962 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800963 u64 total = 0;
964
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800965 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700966 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800967 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800968}
969
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800970static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
971 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800972{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700973 unsigned long val, next;
974
Johannes Weiner13114712012-05-29 15:07:07 -0700975 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700976 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700977 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800978 if ((long)next - (long)val < 0) {
979 switch (target) {
980 case MEM_CGROUP_TARGET_THRESH:
981 next = val + THRESHOLDS_EVENTS_TARGET;
982 break;
983 case MEM_CGROUP_TARGET_SOFTLIMIT:
984 next = val + SOFTLIMIT_EVENTS_TARGET;
985 break;
986 case MEM_CGROUP_TARGET_NUMAINFO:
987 next = val + NUMAINFO_EVENTS_TARGET;
988 break;
989 default:
990 break;
991 }
992 __this_cpu_write(memcg->stat->targets[target], next);
993 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700994 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800995 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800996}
997
998/*
999 * Check events in order.
1000 *
1001 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001002static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001003{
Steven Rostedt47994012011-11-02 13:38:33 -07001004 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001005 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001006 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1007 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001008 bool do_softlimit;
1009 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001010
1011 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1012 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001013#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001014 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1015 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001016#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001017 preempt_enable();
1018
1019 mem_cgroup_threshold(memcg);
1020 if (unlikely(do_softlimit))
1021 mem_cgroup_update_tree(memcg, page);
1022#if MAX_NUMNODES > 1
1023 if (unlikely(do_numainfo))
1024 atomic_inc(&memcg->numainfo_events);
1025#endif
1026 } else
1027 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001028}
1029
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001030struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001031{
Wanpeng Lib2145142012-07-31 16:46:01 -07001032 return mem_cgroup_from_css(
1033 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001034}
1035
Balbir Singhcf475ad2008-04-29 01:00:16 -07001036struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001037{
Balbir Singh31a78f22008-09-28 23:09:31 +01001038 /*
1039 * mm_update_next_owner() may clear mm->owner to NULL
1040 * if it races with swapoff, page migration, etc.
1041 * So this can be called with p == NULL.
1042 */
1043 if (unlikely(!p))
1044 return NULL;
1045
Wanpeng Lib2145142012-07-31 16:46:01 -07001046 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001047}
1048
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001049struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001050{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001051 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001052
1053 if (!mm)
1054 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001055 /*
1056 * Because we have no locks, mm->owner's may be being moved to other
1057 * cgroup. We use css_tryget() here even if this looks
1058 * pessimistic (rather than adding locks here).
1059 */
1060 rcu_read_lock();
1061 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001062 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1063 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001064 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001065 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001066 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001067 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001068}
1069
Johannes Weiner56600482012-01-12 17:17:59 -08001070/**
1071 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1072 * @root: hierarchy root
1073 * @prev: previously returned memcg, NULL on first invocation
1074 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1075 *
1076 * Returns references to children of the hierarchy below @root, or
1077 * @root itself, or %NULL after a full round-trip.
1078 *
1079 * Caller must pass the return value in @prev on subsequent
1080 * invocations for reference counting, or use mem_cgroup_iter_break()
1081 * to cancel a hierarchy walk before the round-trip is complete.
1082 *
1083 * Reclaimers can specify a zone and a priority level in @reclaim to
1084 * divide up the memcgs in the hierarchy among all concurrent
1085 * reclaimers operating on the same zone and priority.
1086 */
1087struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1088 struct mem_cgroup *prev,
1089 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001090{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001091 struct mem_cgroup *memcg = NULL;
1092 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001093
Johannes Weiner56600482012-01-12 17:17:59 -08001094 if (mem_cgroup_disabled())
1095 return NULL;
1096
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001097 if (!root)
1098 root = root_mem_cgroup;
1099
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001100 if (prev && !reclaim)
1101 id = css_id(&prev->css);
1102
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001103 if (!root->use_hierarchy && root != root_mem_cgroup) {
1104 if (prev)
Michal Hockoc40046f2013-04-29 15:07:14 -07001105 goto out_css_put;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001106 return root;
1107 }
1108
1109 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001110 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001111 struct cgroup_subsys_state *css;
1112
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001113 if (reclaim) {
1114 int nid = zone_to_nid(reclaim->zone);
1115 int zid = zone_idx(reclaim->zone);
1116 struct mem_cgroup_per_zone *mz;
1117
1118 mz = mem_cgroup_zoneinfo(root, nid, zid);
1119 iter = &mz->reclaim_iter[reclaim->priority];
1120 if (prev && reclaim->generation != iter->generation)
Michal Hockoc40046f2013-04-29 15:07:14 -07001121 goto out_css_put;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001122 id = iter->position;
1123 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001124
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001125 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001126 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1127 if (css) {
1128 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001129 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001130 } else
1131 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001132 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001133
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001134 if (reclaim) {
1135 iter->position = id;
1136 if (!css)
1137 iter->generation++;
1138 else if (!prev && memcg)
1139 reclaim->generation = iter->generation;
1140 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001141
1142 if (prev && !css)
Michal Hockoc40046f2013-04-29 15:07:14 -07001143 goto out_css_put;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001144 }
Michal Hockoc40046f2013-04-29 15:07:14 -07001145out_css_put:
1146 if (prev && prev != root)
1147 css_put(&prev->css);
1148
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001149 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001150}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001151
Johannes Weiner56600482012-01-12 17:17:59 -08001152/**
1153 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1154 * @root: hierarchy root
1155 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1156 */
1157void mem_cgroup_iter_break(struct mem_cgroup *root,
1158 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001159{
1160 if (!root)
1161 root = root_mem_cgroup;
1162 if (prev && prev != root)
1163 css_put(&prev->css);
1164}
1165
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001166/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001167 * Iteration constructs for visiting all cgroups (under a tree). If
1168 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1169 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001170 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001171#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001172 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001173 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001174 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001175
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001176#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001177 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001178 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001179 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001180
David Rientjes68ae5642012-12-12 13:51:57 -08001181void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001182{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001183 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001184
Ying Han456f9982011-05-26 16:25:38 -07001185 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001186 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1187 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001188 goto out;
1189
1190 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001191 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001192 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1193 break;
1194 case PGMAJFAULT:
1195 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001196 break;
1197 default:
1198 BUG();
1199 }
1200out:
1201 rcu_read_unlock();
1202}
David Rientjes68ae5642012-12-12 13:51:57 -08001203EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001204
Johannes Weiner925b7672012-01-12 17:18:15 -08001205/**
1206 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1207 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001208 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001209 *
1210 * Returns the lru list vector holding pages for the given @zone and
1211 * @mem. This can be the global zone lruvec, if the memory controller
1212 * is disabled.
1213 */
1214struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1215 struct mem_cgroup *memcg)
1216{
1217 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001218 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001219
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001220 if (mem_cgroup_disabled()) {
1221 lruvec = &zone->lruvec;
1222 goto out;
1223 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001224
1225 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001226 lruvec = &mz->lruvec;
1227out:
1228 /*
1229 * Since a node can be onlined after the mem_cgroup was created,
1230 * we have to be prepared to initialize lruvec->zone here;
1231 * and if offlined then reonlined, we need to reinitialize it.
1232 */
1233 if (unlikely(lruvec->zone != zone))
1234 lruvec->zone = zone;
1235 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001236}
1237
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001238/*
1239 * Following LRU functions are allowed to be used without PCG_LOCK.
1240 * Operations are called by routine of global LRU independently from memcg.
1241 * What we have to take care of here is validness of pc->mem_cgroup.
1242 *
1243 * Changes to pc->mem_cgroup happens when
1244 * 1. charge
1245 * 2. moving account
1246 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1247 * It is added to LRU before charge.
1248 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1249 * When moving account, the page is not on LRU. It's isolated.
1250 */
1251
Johannes Weiner925b7672012-01-12 17:18:15 -08001252/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001253 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001254 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001255 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001256 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001257struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001258{
1259 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001260 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001261 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001262 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001263
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001264 if (mem_cgroup_disabled()) {
1265 lruvec = &zone->lruvec;
1266 goto out;
1267 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001268
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001269 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001270 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001271
1272 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001273 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001274 * an uncharged page off lru does nothing to secure
1275 * its former mem_cgroup from sudden removal.
1276 *
1277 * Our caller holds lru_lock, and PageCgroupUsed is updated
1278 * under page_cgroup lock: between them, they make all uses
1279 * of pc->mem_cgroup safe.
1280 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001281 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001282 pc->mem_cgroup = memcg = root_mem_cgroup;
1283
Johannes Weiner925b7672012-01-12 17:18:15 -08001284 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001285 lruvec = &mz->lruvec;
1286out:
1287 /*
1288 * Since a node can be onlined after the mem_cgroup was created,
1289 * we have to be prepared to initialize lruvec->zone here;
1290 * and if offlined then reonlined, we need to reinitialize it.
1291 */
1292 if (unlikely(lruvec->zone != zone))
1293 lruvec->zone = zone;
1294 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001295}
1296
1297/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001298 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1299 * @lruvec: mem_cgroup per zone lru vector
1300 * @lru: index of lru list the page is sitting on
1301 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001302 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001303 * This function must be called when a page is added to or removed from an
1304 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001305 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001306void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1307 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001308{
1309 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001310 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001311
1312 if (mem_cgroup_disabled())
1313 return;
1314
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001315 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1316 lru_size = mz->lru_size + lru;
1317 *lru_size += nr_pages;
1318 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001319}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001320
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001321/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001322 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001323 * hierarchy subtree
1324 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001325bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1326 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001327{
Johannes Weiner91c637342012-05-29 15:06:24 -07001328 if (root_memcg == memcg)
1329 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001330 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001331 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001332 return css_is_ancestor(&memcg->css, &root_memcg->css);
1333}
1334
1335static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1336 struct mem_cgroup *memcg)
1337{
1338 bool ret;
1339
Johannes Weiner91c637342012-05-29 15:06:24 -07001340 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001341 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001342 rcu_read_unlock();
1343 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001344}
1345
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001346int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001347{
1348 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001349 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001350 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001351
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001352 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001353 if (p) {
1354 curr = try_get_mem_cgroup_from_mm(p->mm);
1355 task_unlock(p);
1356 } else {
1357 /*
1358 * All threads may have already detached their mm's, but the oom
1359 * killer still needs to detect if they have already been oom
1360 * killed to prevent needlessly killing additional tasks.
1361 */
1362 task_lock(task);
1363 curr = mem_cgroup_from_task(task);
1364 if (curr)
1365 css_get(&curr->css);
1366 task_unlock(task);
1367 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001368 if (!curr)
1369 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001370 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001371 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001372 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001373 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1374 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001375 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001376 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001377 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001378 return ret;
1379}
1380
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001381int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001382{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001383 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001384 unsigned long inactive;
1385 unsigned long active;
1386 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001387
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001388 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1389 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001390
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001391 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1392 if (gb)
1393 inactive_ratio = int_sqrt(10 * gb);
1394 else
1395 inactive_ratio = 1;
1396
Johannes Weiner9b272972011-11-02 13:38:23 -07001397 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001398}
1399
Balbir Singh6d61ef42009-01-07 18:08:06 -08001400#define mem_cgroup_from_res_counter(counter, member) \
1401 container_of(counter, struct mem_cgroup, member)
1402
Johannes Weiner19942822011-02-01 15:52:43 -08001403/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001404 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001405 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001406 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001407 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001408 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001409 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001410static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001411{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001412 unsigned long long margin;
1413
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001414 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001415 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001416 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001417 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001418}
1419
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001420int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001421{
1422 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001423
1424 /* root ? */
1425 if (cgrp->parent == NULL)
1426 return vm_swappiness;
1427
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001428 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001429}
1430
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001431/*
1432 * memcg->moving_account is used for checking possibility that some thread is
1433 * calling move_account(). When a thread on CPU-A starts moving pages under
1434 * a memcg, other threads should check memcg->moving_account under
1435 * rcu_read_lock(), like this:
1436 *
1437 * CPU-A CPU-B
1438 * rcu_read_lock()
1439 * memcg->moving_account+1 if (memcg->mocing_account)
1440 * take heavy locks.
1441 * synchronize_rcu() update something.
1442 * rcu_read_unlock()
1443 * start move here.
1444 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001445
1446/* for quick checking without looking up memcg */
1447atomic_t memcg_moving __read_mostly;
1448
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001449static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001450{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001451 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001452 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001453 synchronize_rcu();
1454}
1455
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001456static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001457{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001458 /*
1459 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1460 * We check NULL in callee rather than caller.
1461 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001462 if (memcg) {
1463 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001464 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001465 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001466}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001467
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001468/*
1469 * 2 routines for checking "mem" is under move_account() or not.
1470 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001471 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1472 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001473 * pc->mem_cgroup may be overwritten.
1474 *
1475 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1476 * under hierarchy of moving cgroups. This is for
1477 * waiting at hith-memory prressure caused by "move".
1478 */
1479
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001480static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001481{
1482 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001483 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001484}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001485
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001486static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001487{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001488 struct mem_cgroup *from;
1489 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001490 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001491 /*
1492 * Unlike task_move routines, we access mc.to, mc.from not under
1493 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1494 */
1495 spin_lock(&mc.lock);
1496 from = mc.from;
1497 to = mc.to;
1498 if (!from)
1499 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001500
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001501 ret = mem_cgroup_same_or_subtree(memcg, from)
1502 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001503unlock:
1504 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001505 return ret;
1506}
1507
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001508static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001509{
1510 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001511 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001512 DEFINE_WAIT(wait);
1513 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1514 /* moving charge context might have finished. */
1515 if (mc.moving_task)
1516 schedule();
1517 finish_wait(&mc.waitq, &wait);
1518 return true;
1519 }
1520 }
1521 return false;
1522}
1523
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001524/*
1525 * Take this lock when
1526 * - a code tries to modify page's memcg while it's USED.
1527 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001528 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001529 */
1530static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1531 unsigned long *flags)
1532{
1533 spin_lock_irqsave(&memcg->move_lock, *flags);
1534}
1535
1536static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1537 unsigned long *flags)
1538{
1539 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1540}
1541
Sha Zhengju58cf1882013-02-22 16:32:05 -08001542#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001543/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001544 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001545 * @memcg: The memory cgroup that went over limit
1546 * @p: Task that is going to be killed
1547 *
1548 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1549 * enabled
1550 */
1551void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1552{
1553 struct cgroup *task_cgrp;
1554 struct cgroup *mem_cgrp;
1555 /*
1556 * Need a buffer in BSS, can't rely on allocations. The code relies
1557 * on the assumption that OOM is serialized for memory controller.
1558 * If this assumption is broken, revisit this code.
1559 */
1560 static char memcg_name[PATH_MAX];
1561 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001562 struct mem_cgroup *iter;
1563 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001564
Sha Zhengju58cf1882013-02-22 16:32:05 -08001565 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001566 return;
1567
Balbir Singhe2224322009-04-02 16:57:39 -07001568 rcu_read_lock();
1569
1570 mem_cgrp = memcg->css.cgroup;
1571 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1572
1573 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1574 if (ret < 0) {
1575 /*
1576 * Unfortunately, we are unable to convert to a useful name
1577 * But we'll still print out the usage information
1578 */
1579 rcu_read_unlock();
1580 goto done;
1581 }
1582 rcu_read_unlock();
1583
Andrew Mortond0451972013-02-22 16:32:06 -08001584 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001585
1586 rcu_read_lock();
1587 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1588 if (ret < 0) {
1589 rcu_read_unlock();
1590 goto done;
1591 }
1592 rcu_read_unlock();
1593
1594 /*
1595 * Continues from above, so we don't need an KERN_ level
1596 */
Andrew Mortond0451972013-02-22 16:32:06 -08001597 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001598done:
1599
Andrew Mortond0451972013-02-22 16:32:06 -08001600 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001601 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1602 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1603 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001604 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001605 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1606 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1607 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001608 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001609 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1610 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1611 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001612
1613 for_each_mem_cgroup_tree(iter, memcg) {
1614 pr_info("Memory cgroup stats");
1615
1616 rcu_read_lock();
1617 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1618 if (!ret)
1619 pr_cont(" for %s", memcg_name);
1620 rcu_read_unlock();
1621 pr_cont(":");
1622
1623 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1624 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1625 continue;
1626 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1627 K(mem_cgroup_read_stat(iter, i)));
1628 }
1629
1630 for (i = 0; i < NR_LRU_LISTS; i++)
1631 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1632 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1633
1634 pr_cont("\n");
1635 }
Balbir Singhe2224322009-04-02 16:57:39 -07001636}
1637
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001638/*
1639 * This function returns the number of memcg under hierarchy tree. Returns
1640 * 1(self count) if no children.
1641 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001642static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001643{
1644 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001645 struct mem_cgroup *iter;
1646
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001647 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001648 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001649 return num;
1650}
1651
Balbir Singh6d61ef42009-01-07 18:08:06 -08001652/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001653 * Return the memory (and swap, if configured) limit for a memcg.
1654 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001655static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001656{
1657 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001658
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001659 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001660
David Rientjesa63d83f2010-08-09 17:19:46 -07001661 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001662 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001663 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001664 if (mem_cgroup_swappiness(memcg)) {
1665 u64 memsw;
1666
1667 limit += total_swap_pages << PAGE_SHIFT;
1668 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1669
1670 /*
1671 * If memsw is finite and limits the amount of swap space
1672 * available to this memcg, return that limit.
1673 */
1674 limit = min(limit, memsw);
1675 }
1676
1677 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001678}
1679
David Rientjes19965462012-12-11 16:00:26 -08001680static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1681 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001682{
1683 struct mem_cgroup *iter;
1684 unsigned long chosen_points = 0;
1685 unsigned long totalpages;
1686 unsigned int points = 0;
1687 struct task_struct *chosen = NULL;
1688
David Rientjes876aafb2012-07-31 16:43:48 -07001689 /*
1690 * If current has a pending SIGKILL, then automatically select it. The
1691 * goal is to allow it to allocate so that it may quickly exit and free
1692 * its memory.
1693 */
1694 if (fatal_signal_pending(current)) {
1695 set_thread_flag(TIF_MEMDIE);
1696 return;
1697 }
1698
1699 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001700 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1701 for_each_mem_cgroup_tree(iter, memcg) {
1702 struct cgroup *cgroup = iter->css.cgroup;
1703 struct cgroup_iter it;
1704 struct task_struct *task;
1705
1706 cgroup_iter_start(cgroup, &it);
1707 while ((task = cgroup_iter_next(cgroup, &it))) {
1708 switch (oom_scan_process_thread(task, totalpages, NULL,
1709 false)) {
1710 case OOM_SCAN_SELECT:
1711 if (chosen)
1712 put_task_struct(chosen);
1713 chosen = task;
1714 chosen_points = ULONG_MAX;
1715 get_task_struct(chosen);
1716 /* fall through */
1717 case OOM_SCAN_CONTINUE:
1718 continue;
1719 case OOM_SCAN_ABORT:
1720 cgroup_iter_end(cgroup, &it);
1721 mem_cgroup_iter_break(memcg, iter);
1722 if (chosen)
1723 put_task_struct(chosen);
1724 return;
1725 case OOM_SCAN_OK:
1726 break;
1727 };
1728 points = oom_badness(task, memcg, NULL, totalpages);
1729 if (points > chosen_points) {
1730 if (chosen)
1731 put_task_struct(chosen);
1732 chosen = task;
1733 chosen_points = points;
1734 get_task_struct(chosen);
1735 }
1736 }
1737 cgroup_iter_end(cgroup, &it);
1738 }
1739
1740 if (!chosen)
1741 return;
1742 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001743 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1744 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001745}
1746
Johannes Weiner56600482012-01-12 17:17:59 -08001747static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1748 gfp_t gfp_mask,
1749 unsigned long flags)
1750{
1751 unsigned long total = 0;
1752 bool noswap = false;
1753 int loop;
1754
1755 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1756 noswap = true;
1757 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1758 noswap = true;
1759
1760 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1761 if (loop)
1762 drain_all_stock_async(memcg);
1763 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1764 /*
1765 * Allow limit shrinkers, which are triggered directly
1766 * by userspace, to catch signals and stop reclaim
1767 * after minimal progress, regardless of the margin.
1768 */
1769 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1770 break;
1771 if (mem_cgroup_margin(memcg))
1772 break;
1773 /*
1774 * If nothing was reclaimed after two attempts, there
1775 * may be no reclaimable pages in this hierarchy.
1776 */
1777 if (loop && !total)
1778 break;
1779 }
1780 return total;
1781}
1782
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001783/**
1784 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001785 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001786 * @nid: the node ID to be checked.
1787 * @noswap : specify true here if the user wants flle only information.
1788 *
1789 * This function returns whether the specified memcg contains any
1790 * reclaimable pages on a node. Returns true if there are any reclaimable
1791 * pages in the node.
1792 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001793static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001794 int nid, bool noswap)
1795{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001796 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001797 return true;
1798 if (noswap || !total_swap_pages)
1799 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001800 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001801 return true;
1802 return false;
1803
1804}
Ying Han889976d2011-05-26 16:25:33 -07001805#if MAX_NUMNODES > 1
1806
1807/*
1808 * Always updating the nodemask is not very good - even if we have an empty
1809 * list or the wrong list here, we can start from some node and traverse all
1810 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1811 *
1812 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001813static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001814{
1815 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001816 /*
1817 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1818 * pagein/pageout changes since the last update.
1819 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001820 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001821 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001822 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001823 return;
1824
Ying Han889976d2011-05-26 16:25:33 -07001825 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001826 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001827
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001828 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001829
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001830 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1831 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001832 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001833
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001834 atomic_set(&memcg->numainfo_events, 0);
1835 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001836}
1837
1838/*
1839 * Selecting a node where we start reclaim from. Because what we need is just
1840 * reducing usage counter, start from anywhere is O,K. Considering
1841 * memory reclaim from current node, there are pros. and cons.
1842 *
1843 * Freeing memory from current node means freeing memory from a node which
1844 * we'll use or we've used. So, it may make LRU bad. And if several threads
1845 * hit limits, it will see a contention on a node. But freeing from remote
1846 * node means more costs for memory reclaim because of memory latency.
1847 *
1848 * Now, we use round-robin. Better algorithm is welcomed.
1849 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001850int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001851{
1852 int node;
1853
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001854 mem_cgroup_may_update_nodemask(memcg);
1855 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001856
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001857 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001858 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001859 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001860 /*
1861 * We call this when we hit limit, not when pages are added to LRU.
1862 * No LRU may hold pages because all pages are UNEVICTABLE or
1863 * memcg is too small and all pages are not on LRU. In that case,
1864 * we use curret node.
1865 */
1866 if (unlikely(node == MAX_NUMNODES))
1867 node = numa_node_id();
1868
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001869 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001870 return node;
1871}
1872
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001873/*
1874 * Check all nodes whether it contains reclaimable pages or not.
1875 * For quick scan, we make use of scan_nodes. This will allow us to skip
1876 * unused nodes. But scan_nodes is lazily updated and may not cotain
1877 * enough new information. We need to do double check.
1878 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001879static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001880{
1881 int nid;
1882
1883 /*
1884 * quick check...making use of scan_node.
1885 * We can skip unused nodes.
1886 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001887 if (!nodes_empty(memcg->scan_nodes)) {
1888 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001889 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001890 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001891
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001892 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001893 return true;
1894 }
1895 }
1896 /*
1897 * Check rest of nodes.
1898 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001899 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001900 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001901 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001902 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001903 return true;
1904 }
1905 return false;
1906}
1907
Ying Han889976d2011-05-26 16:25:33 -07001908#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001909int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001910{
1911 return 0;
1912}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001913
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001914static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001915{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001916 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001917}
Ying Han889976d2011-05-26 16:25:33 -07001918#endif
1919
Johannes Weiner56600482012-01-12 17:17:59 -08001920static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1921 struct zone *zone,
1922 gfp_t gfp_mask,
1923 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001924{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001925 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001926 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001927 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001928 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001929 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001930 struct mem_cgroup_reclaim_cookie reclaim = {
1931 .zone = zone,
1932 .priority = 0,
1933 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001934
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001935 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001936
Balbir Singh4e416952009-09-23 15:56:39 -07001937 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001938 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001939 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001940 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001941 if (loop >= 2) {
1942 /*
1943 * If we have not been able to reclaim
1944 * anything, it might because there are
1945 * no reclaimable pages under this hierarchy
1946 */
Johannes Weiner56600482012-01-12 17:17:59 -08001947 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001948 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001949 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001950 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001951 * excess >> 2 is not to excessive so as to
1952 * reclaim too much, nor too less that we keep
1953 * coming back to reclaim from this cgroup
1954 */
1955 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001956 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001957 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001958 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001959 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001960 }
Johannes Weiner56600482012-01-12 17:17:59 -08001961 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001962 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001963 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1964 zone, &nr_scanned);
1965 *total_scanned += nr_scanned;
1966 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001967 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001968 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001969 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001970 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001971}
1972
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001973/*
1974 * Check OOM-Killer is already running under our hierarchy.
1975 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001976 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001977 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001978static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001979{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001980 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001981
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001982 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001983 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001984 /*
1985 * this subtree of our hierarchy is already locked
1986 * so we cannot give a lock.
1987 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001988 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001989 mem_cgroup_iter_break(memcg, iter);
1990 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001991 } else
1992 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001993 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001994
Michal Hocko79dfdac2011-07-26 16:08:23 -07001995 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001996 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001997
1998 /*
1999 * OK, we failed to lock the whole subtree so we have to clean up
2000 * what we set up to the failing subtree
2001 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002002 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002003 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002004 mem_cgroup_iter_break(memcg, iter);
2005 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002006 }
2007 iter->oom_lock = false;
2008 }
Johannes Weiner23751be2011-08-25 15:59:16 -07002009 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002010}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002011
Michal Hocko79dfdac2011-07-26 16:08:23 -07002012/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07002013 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07002014 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002015static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002016{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002017 struct mem_cgroup *iter;
2018
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002019 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002020 iter->oom_lock = false;
2021 return 0;
2022}
2023
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002024static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002025{
2026 struct mem_cgroup *iter;
2027
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002028 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002029 atomic_inc(&iter->under_oom);
2030}
2031
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002032static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002033{
2034 struct mem_cgroup *iter;
2035
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002036 /*
2037 * When a new child is created while the hierarchy is under oom,
2038 * mem_cgroup_oom_lock() may not be called. We have to use
2039 * atomic_add_unless() here.
2040 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002041 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002042 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002043}
2044
Michal Hocko1af8efe2011-07-26 16:08:24 -07002045static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002046static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2047
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002048struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002049 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002050 wait_queue_t wait;
2051};
2052
2053static int memcg_oom_wake_function(wait_queue_t *wait,
2054 unsigned mode, int sync, void *arg)
2055{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002056 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2057 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002058 struct oom_wait_info *oom_wait_info;
2059
2060 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002061 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002062
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002063 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002064 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002065 * Then we can use css_is_ancestor without taking care of RCU.
2066 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002067 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2068 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002069 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002070 return autoremove_wake_function(wait, mode, sync, arg);
2071}
2072
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002073static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002074{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002075 /* for filtering, pass "memcg" as argument. */
2076 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002077}
2078
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002079static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002080{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002081 if (memcg && atomic_read(&memcg->under_oom))
2082 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002083}
2084
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002085/*
2086 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2087 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002088static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2089 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002090{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002091 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002092 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002093
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002094 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002095 owait.wait.flags = 0;
2096 owait.wait.func = memcg_oom_wake_function;
2097 owait.wait.private = current;
2098 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002099 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002100 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002101
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002102 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002103 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002104 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002105 /*
2106 * Even if signal_pending(), we can't quit charge() loop without
2107 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2108 * under OOM is always welcomed, use TASK_KILLABLE here.
2109 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002110 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002111 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002112 need_to_kill = false;
2113 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002114 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002115 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002116
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002117 if (need_to_kill) {
2118 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002119 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002120 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002121 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002122 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002123 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002124 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002125 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002126 mem_cgroup_oom_unlock(memcg);
2127 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002128 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002129
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002130 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002131
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002132 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2133 return false;
2134 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002135 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002136 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002137}
2138
Balbir Singhd69b0422009-06-17 16:26:34 -07002139/*
2140 * Currently used to update mapped file statistics, but the routine can be
2141 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002142 *
2143 * Notes: Race condition
2144 *
2145 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2146 * it tends to be costly. But considering some conditions, we doesn't need
2147 * to do so _always_.
2148 *
2149 * Considering "charge", lock_page_cgroup() is not required because all
2150 * file-stat operations happen after a page is attached to radix-tree. There
2151 * are no race with "charge".
2152 *
2153 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2154 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2155 * if there are race with "uncharge". Statistics itself is properly handled
2156 * by flags.
2157 *
2158 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002159 * small, we check mm->moving_account and detect there are possibility of race
2160 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002161 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002162
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002163void __mem_cgroup_begin_update_page_stat(struct page *page,
2164 bool *locked, unsigned long *flags)
2165{
2166 struct mem_cgroup *memcg;
2167 struct page_cgroup *pc;
2168
2169 pc = lookup_page_cgroup(page);
2170again:
2171 memcg = pc->mem_cgroup;
2172 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2173 return;
2174 /*
2175 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002176 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002177 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002178 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002179 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002180 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002181 return;
2182
2183 move_lock_mem_cgroup(memcg, flags);
2184 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2185 move_unlock_mem_cgroup(memcg, flags);
2186 goto again;
2187 }
2188 *locked = true;
2189}
2190
2191void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2192{
2193 struct page_cgroup *pc = lookup_page_cgroup(page);
2194
2195 /*
2196 * It's guaranteed that pc->mem_cgroup never changes while
2197 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002198 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002199 */
2200 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2201}
2202
Greg Thelen2a7106f2011-01-13 15:47:37 -08002203void mem_cgroup_update_page_stat(struct page *page,
2204 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002205{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002206 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002207 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002208 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002209
Johannes Weinercfa44942012-01-12 17:18:38 -08002210 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002211 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002212
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002213 memcg = pc->mem_cgroup;
2214 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002215 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002216
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002217 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002218 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002219 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002220 break;
2221 default:
2222 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002223 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002224
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002225 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002226}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002227
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002228/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002229 * size of first charge trial. "32" comes from vmscan.c's magic value.
2230 * TODO: maybe necessary to use big numbers in big irons.
2231 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002232#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002233struct memcg_stock_pcp {
2234 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002235 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002236 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002237 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002238#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002239};
2240static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002241static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002242
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002243/**
2244 * consume_stock: Try to consume stocked charge on this cpu.
2245 * @memcg: memcg to consume from.
2246 * @nr_pages: how many pages to charge.
2247 *
2248 * The charges will only happen if @memcg matches the current cpu's memcg
2249 * stock, and at least @nr_pages are available in that stock. Failure to
2250 * service an allocation will refill the stock.
2251 *
2252 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002253 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002254static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002255{
2256 struct memcg_stock_pcp *stock;
2257 bool ret = true;
2258
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002259 if (nr_pages > CHARGE_BATCH)
2260 return false;
2261
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002262 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002263 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2264 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002265 else /* need to call res_counter_charge */
2266 ret = false;
2267 put_cpu_var(memcg_stock);
2268 return ret;
2269}
2270
2271/*
2272 * Returns stocks cached in percpu to res_counter and reset cached information.
2273 */
2274static void drain_stock(struct memcg_stock_pcp *stock)
2275{
2276 struct mem_cgroup *old = stock->cached;
2277
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002278 if (stock->nr_pages) {
2279 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2280
2281 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002282 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002283 res_counter_uncharge(&old->memsw, bytes);
2284 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002285 }
2286 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002287}
2288
2289/*
2290 * This must be called under preempt disabled or must be called by
2291 * a thread which is pinned to local cpu.
2292 */
2293static void drain_local_stock(struct work_struct *dummy)
2294{
2295 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2296 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002297 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002298}
2299
Michal Hockoe4777492013-02-22 16:35:40 -08002300static void __init memcg_stock_init(void)
2301{
2302 int cpu;
2303
2304 for_each_possible_cpu(cpu) {
2305 struct memcg_stock_pcp *stock =
2306 &per_cpu(memcg_stock, cpu);
2307 INIT_WORK(&stock->work, drain_local_stock);
2308 }
2309}
2310
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002311/*
2312 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002313 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002314 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002315static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002316{
2317 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2318
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002319 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002320 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002321 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002322 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002323 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002324 put_cpu_var(memcg_stock);
2325}
2326
2327/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002328 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002329 * of the hierarchy under it. sync flag says whether we should block
2330 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002331 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002332static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002333{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002334 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002335
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002336 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002337 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002338 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002339 for_each_online_cpu(cpu) {
2340 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002341 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002342
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002343 memcg = stock->cached;
2344 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002345 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002346 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002347 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002348 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2349 if (cpu == curcpu)
2350 drain_local_stock(&stock->work);
2351 else
2352 schedule_work_on(cpu, &stock->work);
2353 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002354 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002355 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002356
2357 if (!sync)
2358 goto out;
2359
2360 for_each_online_cpu(cpu) {
2361 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002362 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002363 flush_work(&stock->work);
2364 }
2365out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002366 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002367}
2368
2369/*
2370 * Tries to drain stocked charges in other cpus. This function is asynchronous
2371 * and just put a work per cpu for draining localy on each cpu. Caller can
2372 * expects some charges will be back to res_counter later but cannot wait for
2373 * it.
2374 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002375static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002376{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002377 /*
2378 * If someone calls draining, avoid adding more kworker runs.
2379 */
2380 if (!mutex_trylock(&percpu_charge_mutex))
2381 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002382 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002383 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002384}
2385
2386/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002387static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002388{
2389 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002390 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002391 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002392 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002393}
2394
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002395/*
2396 * This function drains percpu counter value from DEAD cpu and
2397 * move it to local cpu. Note that this function can be preempted.
2398 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002399static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002400{
2401 int i;
2402
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002403 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002404 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002405 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002406
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002407 per_cpu(memcg->stat->count[i], cpu) = 0;
2408 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002409 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002410 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002411 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002412
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002413 per_cpu(memcg->stat->events[i], cpu) = 0;
2414 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002415 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002416 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002417}
2418
2419static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002420 unsigned long action,
2421 void *hcpu)
2422{
2423 int cpu = (unsigned long)hcpu;
2424 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002425 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002426
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002427 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002428 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002429
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002430 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002431 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002432
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002433 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002434 mem_cgroup_drain_pcp_counter(iter, cpu);
2435
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002436 stock = &per_cpu(memcg_stock, cpu);
2437 drain_stock(stock);
2438 return NOTIFY_OK;
2439}
2440
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002441
2442/* See __mem_cgroup_try_charge() for details */
2443enum {
2444 CHARGE_OK, /* success */
2445 CHARGE_RETRY, /* need to retry but retry is not bad */
2446 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2447 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2448 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2449};
2450
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002451static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002452 unsigned int nr_pages, unsigned int min_pages,
2453 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002454{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002455 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002456 struct mem_cgroup *mem_over_limit;
2457 struct res_counter *fail_res;
2458 unsigned long flags = 0;
2459 int ret;
2460
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002461 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002462
2463 if (likely(!ret)) {
2464 if (!do_swap_account)
2465 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002466 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002467 if (likely(!ret))
2468 return CHARGE_OK;
2469
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002470 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002471 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2472 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2473 } else
2474 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002475 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002476 * Never reclaim on behalf of optional batching, retry with a
2477 * single page instead.
2478 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002479 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002480 return CHARGE_RETRY;
2481
2482 if (!(gfp_mask & __GFP_WAIT))
2483 return CHARGE_WOULDBLOCK;
2484
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002485 if (gfp_mask & __GFP_NORETRY)
2486 return CHARGE_NOMEM;
2487
Johannes Weiner56600482012-01-12 17:17:59 -08002488 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002489 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002490 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002491 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002492 * Even though the limit is exceeded at this point, reclaim
2493 * may have been able to free some pages. Retry the charge
2494 * before killing the task.
2495 *
2496 * Only for regular pages, though: huge pages are rather
2497 * unlikely to succeed so close to the limit, and we fall back
2498 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002499 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002500 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002501 return CHARGE_RETRY;
2502
2503 /*
2504 * At task move, charge accounts can be doubly counted. So, it's
2505 * better to wait until the end of task_move if something is going on.
2506 */
2507 if (mem_cgroup_wait_acct_move(mem_over_limit))
2508 return CHARGE_RETRY;
2509
2510 /* If we don't need to call oom-killer at el, return immediately */
2511 if (!oom_check)
2512 return CHARGE_NOMEM;
2513 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002514 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002515 return CHARGE_OOM_DIE;
2516
2517 return CHARGE_RETRY;
2518}
2519
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002520/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002521 * __mem_cgroup_try_charge() does
2522 * 1. detect memcg to be charged against from passed *mm and *ptr,
2523 * 2. update res_counter
2524 * 3. call memory reclaim if necessary.
2525 *
2526 * In some special case, if the task is fatal, fatal_signal_pending() or
2527 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2528 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2529 * as possible without any hazards. 2: all pages should have a valid
2530 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2531 * pointer, that is treated as a charge to root_mem_cgroup.
2532 *
2533 * So __mem_cgroup_try_charge() will return
2534 * 0 ... on success, filling *ptr with a valid memcg pointer.
2535 * -ENOMEM ... charge failure because of resource limits.
2536 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2537 *
2538 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2539 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002540 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002541static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002542 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002543 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002544 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002545 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002546{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002547 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002548 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002549 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002550 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002551
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002552 /*
2553 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2554 * in system level. So, allow to go ahead dying process in addition to
2555 * MEMDIE process.
2556 */
2557 if (unlikely(test_thread_flag(TIF_MEMDIE)
2558 || fatal_signal_pending(current)))
2559 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002560
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002561 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002562 * We always charge the cgroup the mm_struct belongs to.
2563 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002564 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002565 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002566 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002567 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002568 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002569again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002570 if (*ptr) { /* css should be a valid one */
2571 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002572 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002573 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002574 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002575 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002576 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002577 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002578 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002579
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002580 rcu_read_lock();
2581 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002582 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002583 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002584 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002585 * race with swapoff. Then, we have small risk of mis-accouning.
2586 * But such kind of mis-account by race always happens because
2587 * we don't have cgroup_mutex(). It's overkill and we allo that
2588 * small race, here.
2589 * (*) swapoff at el will charge against mm-struct not against
2590 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002591 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002592 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002593 if (!memcg)
2594 memcg = root_mem_cgroup;
2595 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002596 rcu_read_unlock();
2597 goto done;
2598 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002599 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002600 /*
2601 * It seems dagerous to access memcg without css_get().
2602 * But considering how consume_stok works, it's not
2603 * necessary. If consume_stock success, some charges
2604 * from this memcg are cached on this cpu. So, we
2605 * don't need to call css_get()/css_tryget() before
2606 * calling consume_stock().
2607 */
2608 rcu_read_unlock();
2609 goto done;
2610 }
2611 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002612 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002613 rcu_read_unlock();
2614 goto again;
2615 }
2616 rcu_read_unlock();
2617 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002618
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002619 do {
2620 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002621
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002622 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002623 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002624 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002625 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002626 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002627
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002628 oom_check = false;
2629 if (oom && !nr_oom_retries) {
2630 oom_check = true;
2631 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2632 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002633
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002634 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2635 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002636 switch (ret) {
2637 case CHARGE_OK:
2638 break;
2639 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002640 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002641 css_put(&memcg->css);
2642 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002643 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002644 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002645 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002646 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002647 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002648 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002649 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002650 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002651 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002652 /* If oom, we never return -ENOMEM */
2653 nr_oom_retries--;
2654 break;
2655 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002656 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002657 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002658 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002659 } while (ret != CHARGE_OK);
2660
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002661 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002662 refill_stock(memcg, batch - nr_pages);
2663 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002664done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002665 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002666 return 0;
2667nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002668 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002669 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002670bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002671 *ptr = root_mem_cgroup;
2672 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002673}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002674
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002675/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002676 * Somemtimes we have to undo a charge we got by try_charge().
2677 * This function is for that and do uncharge, put css's refcnt.
2678 * gotten by try_charge().
2679 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002680static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002681 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002682{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002683 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002684 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002685
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002686 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002687 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002688 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002689 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002690}
2691
2692/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002693 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2694 * This is useful when moving usage to parent cgroup.
2695 */
2696static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2697 unsigned int nr_pages)
2698{
2699 unsigned long bytes = nr_pages * PAGE_SIZE;
2700
2701 if (mem_cgroup_is_root(memcg))
2702 return;
2703
2704 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2705 if (do_swap_account)
2706 res_counter_uncharge_until(&memcg->memsw,
2707 memcg->memsw.parent, bytes);
2708}
2709
2710/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002711 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002712 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2713 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2714 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002715 */
2716static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2717{
2718 struct cgroup_subsys_state *css;
2719
2720 /* ID 0 is unused ID */
2721 if (!id)
2722 return NULL;
2723 css = css_lookup(&mem_cgroup_subsys, id);
2724 if (!css)
2725 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002726 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002727}
2728
Wu Fengguange42d9d52009-12-16 12:19:59 +01002729struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002730{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002731 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002732 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002733 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002734 swp_entry_t ent;
2735
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002736 VM_BUG_ON(!PageLocked(page));
2737
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002738 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002739 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002740 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002741 memcg = pc->mem_cgroup;
2742 if (memcg && !css_tryget(&memcg->css))
2743 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002744 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002745 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002746 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002747 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002748 memcg = mem_cgroup_lookup(id);
2749 if (memcg && !css_tryget(&memcg->css))
2750 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002751 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002752 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002753 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002754 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002755}
2756
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002757static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002758 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002759 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002760 enum charge_type ctype,
2761 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002762{
Johannes Weinerce587e62012-04-24 20:22:33 +02002763 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002764 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002765 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002766 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002767 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002768
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002769 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002770 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002771 /*
2772 * we don't need page_cgroup_lock about tail pages, becase they are not
2773 * accessed by any other context at this point.
2774 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002775
2776 /*
2777 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2778 * may already be on some other mem_cgroup's LRU. Take care of it.
2779 */
2780 if (lrucare) {
2781 zone = page_zone(page);
2782 spin_lock_irq(&zone->lru_lock);
2783 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002784 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002785 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002786 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002787 was_on_lru = true;
2788 }
2789 }
2790
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002791 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002792 /*
2793 * We access a page_cgroup asynchronously without lock_page_cgroup().
2794 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2795 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2796 * before USED bit, we need memory barrier here.
2797 * See mem_cgroup_add_lru_list(), etc.
2798 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002799 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002800 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002801
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002802 if (lrucare) {
2803 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002804 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002805 VM_BUG_ON(PageLRU(page));
2806 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002807 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002808 }
2809 spin_unlock_irq(&zone->lru_lock);
2810 }
2811
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002812 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002813 anon = true;
2814 else
2815 anon = false;
2816
2817 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002818 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002819
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002820 /*
2821 * "charge_statistics" updated event counter. Then, check it.
2822 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2823 * if they exceeds softlimit.
2824 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002825 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002826}
2827
Glauber Costa7cf27982012-12-18 14:22:55 -08002828static DEFINE_MUTEX(set_limit_mutex);
2829
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002830#ifdef CONFIG_MEMCG_KMEM
2831static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2832{
2833 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2834 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2835}
2836
Glauber Costa1f458cb2012-12-18 14:22:50 -08002837/*
2838 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2839 * in the memcg_cache_params struct.
2840 */
2841static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2842{
2843 struct kmem_cache *cachep;
2844
2845 VM_BUG_ON(p->is_root_cache);
2846 cachep = p->root_cache;
2847 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2848}
2849
Glauber Costa749c5412012-12-18 14:23:01 -08002850#ifdef CONFIG_SLABINFO
2851static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2852 struct seq_file *m)
2853{
2854 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2855 struct memcg_cache_params *params;
2856
2857 if (!memcg_can_account_kmem(memcg))
2858 return -EIO;
2859
2860 print_slabinfo_header(m);
2861
2862 mutex_lock(&memcg->slab_caches_mutex);
2863 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2864 cache_show(memcg_params_to_cache(params), m);
2865 mutex_unlock(&memcg->slab_caches_mutex);
2866
2867 return 0;
2868}
2869#endif
2870
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002871static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2872{
2873 struct res_counter *fail_res;
2874 struct mem_cgroup *_memcg;
2875 int ret = 0;
2876 bool may_oom;
2877
2878 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2879 if (ret)
2880 return ret;
2881
2882 /*
2883 * Conditions under which we can wait for the oom_killer. Those are
2884 * the same conditions tested by the core page allocator
2885 */
2886 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2887
2888 _memcg = memcg;
2889 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2890 &_memcg, may_oom);
2891
2892 if (ret == -EINTR) {
2893 /*
2894 * __mem_cgroup_try_charge() chosed to bypass to root due to
2895 * OOM kill or fatal signal. Since our only options are to
2896 * either fail the allocation or charge it to this cgroup, do
2897 * it as a temporary condition. But we can't fail. From a
2898 * kmem/slab perspective, the cache has already been selected,
2899 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2900 * our minds.
2901 *
2902 * This condition will only trigger if the task entered
2903 * memcg_charge_kmem in a sane state, but was OOM-killed during
2904 * __mem_cgroup_try_charge() above. Tasks that were already
2905 * dying when the allocation triggers should have been already
2906 * directed to the root cgroup in memcontrol.h
2907 */
2908 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2909 if (do_swap_account)
2910 res_counter_charge_nofail(&memcg->memsw, size,
2911 &fail_res);
2912 ret = 0;
2913 } else if (ret)
2914 res_counter_uncharge(&memcg->kmem, size);
2915
2916 return ret;
2917}
2918
2919static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2920{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002921 res_counter_uncharge(&memcg->res, size);
2922 if (do_swap_account)
2923 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002924
2925 /* Not down to 0 */
2926 if (res_counter_uncharge(&memcg->kmem, size))
2927 return;
2928
2929 if (memcg_kmem_test_and_clear_dead(memcg))
2930 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002931}
2932
Glauber Costa2633d7a2012-12-18 14:22:34 -08002933void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2934{
2935 if (!memcg)
2936 return;
2937
2938 mutex_lock(&memcg->slab_caches_mutex);
2939 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2940 mutex_unlock(&memcg->slab_caches_mutex);
2941}
2942
2943/*
2944 * helper for acessing a memcg's index. It will be used as an index in the
2945 * child cache array in kmem_cache, and also to derive its name. This function
2946 * will return -1 when this is not a kmem-limited memcg.
2947 */
2948int memcg_cache_id(struct mem_cgroup *memcg)
2949{
2950 return memcg ? memcg->kmemcg_id : -1;
2951}
2952
Glauber Costa55007d82012-12-18 14:22:38 -08002953/*
2954 * This ends up being protected by the set_limit mutex, during normal
2955 * operation, because that is its main call site.
2956 *
2957 * But when we create a new cache, we can call this as well if its parent
2958 * is kmem-limited. That will have to hold set_limit_mutex as well.
2959 */
2960int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2961{
2962 int num, ret;
2963
2964 num = ida_simple_get(&kmem_limited_groups,
2965 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2966 if (num < 0)
2967 return num;
2968 /*
2969 * After this point, kmem_accounted (that we test atomically in
2970 * the beginning of this conditional), is no longer 0. This
2971 * guarantees only one process will set the following boolean
2972 * to true. We don't need test_and_set because we're protected
2973 * by the set_limit_mutex anyway.
2974 */
2975 memcg_kmem_set_activated(memcg);
2976
2977 ret = memcg_update_all_caches(num+1);
2978 if (ret) {
2979 ida_simple_remove(&kmem_limited_groups, num);
2980 memcg_kmem_clear_activated(memcg);
2981 return ret;
2982 }
2983
2984 memcg->kmemcg_id = num;
2985 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2986 mutex_init(&memcg->slab_caches_mutex);
2987 return 0;
2988}
2989
2990static size_t memcg_caches_array_size(int num_groups)
2991{
2992 ssize_t size;
2993 if (num_groups <= 0)
2994 return 0;
2995
2996 size = 2 * num_groups;
2997 if (size < MEMCG_CACHES_MIN_SIZE)
2998 size = MEMCG_CACHES_MIN_SIZE;
2999 else if (size > MEMCG_CACHES_MAX_SIZE)
3000 size = MEMCG_CACHES_MAX_SIZE;
3001
3002 return size;
3003}
3004
3005/*
3006 * We should update the current array size iff all caches updates succeed. This
3007 * can only be done from the slab side. The slab mutex needs to be held when
3008 * calling this.
3009 */
3010void memcg_update_array_size(int num)
3011{
3012 if (num > memcg_limited_groups_array_size)
3013 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3014}
3015
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003016static void kmem_cache_destroy_work_func(struct work_struct *w);
3017
Glauber Costa55007d82012-12-18 14:22:38 -08003018int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3019{
3020 struct memcg_cache_params *cur_params = s->memcg_params;
3021
3022 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3023
3024 if (num_groups > memcg_limited_groups_array_size) {
3025 int i;
3026 ssize_t size = memcg_caches_array_size(num_groups);
3027
3028 size *= sizeof(void *);
3029 size += sizeof(struct memcg_cache_params);
3030
3031 s->memcg_params = kzalloc(size, GFP_KERNEL);
3032 if (!s->memcg_params) {
3033 s->memcg_params = cur_params;
3034 return -ENOMEM;
3035 }
3036
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003037 INIT_WORK(&s->memcg_params->destroy,
3038 kmem_cache_destroy_work_func);
Glauber Costa55007d82012-12-18 14:22:38 -08003039 s->memcg_params->is_root_cache = true;
3040
3041 /*
3042 * There is the chance it will be bigger than
3043 * memcg_limited_groups_array_size, if we failed an allocation
3044 * in a cache, in which case all caches updated before it, will
3045 * have a bigger array.
3046 *
3047 * But if that is the case, the data after
3048 * memcg_limited_groups_array_size is certainly unused
3049 */
3050 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3051 if (!cur_params->memcg_caches[i])
3052 continue;
3053 s->memcg_params->memcg_caches[i] =
3054 cur_params->memcg_caches[i];
3055 }
3056
3057 /*
3058 * Ideally, we would wait until all caches succeed, and only
3059 * then free the old one. But this is not worth the extra
3060 * pointer per-cache we'd have to have for this.
3061 *
3062 * It is not a big deal if some caches are left with a size
3063 * bigger than the others. And all updates will reset this
3064 * anyway.
3065 */
3066 kfree(cur_params);
3067 }
3068 return 0;
3069}
3070
Glauber Costa943a4512012-12-18 14:23:03 -08003071int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3072 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003073{
3074 size_t size = sizeof(struct memcg_cache_params);
3075
3076 if (!memcg_kmem_enabled())
3077 return 0;
3078
Glauber Costa55007d82012-12-18 14:22:38 -08003079 if (!memcg)
3080 size += memcg_limited_groups_array_size * sizeof(void *);
3081
Glauber Costa2633d7a2012-12-18 14:22:34 -08003082 s->memcg_params = kzalloc(size, GFP_KERNEL);
3083 if (!s->memcg_params)
3084 return -ENOMEM;
3085
Konstantin Khlebnikov15cf17d2013-03-08 12:43:36 -08003086 INIT_WORK(&s->memcg_params->destroy,
3087 kmem_cache_destroy_work_func);
Glauber Costa943a4512012-12-18 14:23:03 -08003088 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003089 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003090 s->memcg_params->root_cache = root_cache;
Glauber Costa4ba902b2013-02-12 13:46:22 -08003091 } else
3092 s->memcg_params->is_root_cache = true;
3093
Glauber Costa2633d7a2012-12-18 14:22:34 -08003094 return 0;
3095}
3096
3097void memcg_release_cache(struct kmem_cache *s)
3098{
Glauber Costad7f25f82012-12-18 14:22:40 -08003099 struct kmem_cache *root;
3100 struct mem_cgroup *memcg;
3101 int id;
3102
3103 /*
3104 * This happens, for instance, when a root cache goes away before we
3105 * add any memcg.
3106 */
3107 if (!s->memcg_params)
3108 return;
3109
3110 if (s->memcg_params->is_root_cache)
3111 goto out;
3112
3113 memcg = s->memcg_params->memcg;
3114 id = memcg_cache_id(memcg);
3115
3116 root = s->memcg_params->root_cache;
3117 root->memcg_params->memcg_caches[id] = NULL;
3118 mem_cgroup_put(memcg);
3119
3120 mutex_lock(&memcg->slab_caches_mutex);
3121 list_del(&s->memcg_params->list);
3122 mutex_unlock(&memcg->slab_caches_mutex);
3123
3124out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003125 kfree(s->memcg_params);
3126}
3127
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003128/*
3129 * During the creation a new cache, we need to disable our accounting mechanism
3130 * altogether. This is true even if we are not creating, but rather just
3131 * enqueing new caches to be created.
3132 *
3133 * This is because that process will trigger allocations; some visible, like
3134 * explicit kmallocs to auxiliary data structures, name strings and internal
3135 * cache structures; some well concealed, like INIT_WORK() that can allocate
3136 * objects during debug.
3137 *
3138 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3139 * to it. This may not be a bounded recursion: since the first cache creation
3140 * failed to complete (waiting on the allocation), we'll just try to create the
3141 * cache again, failing at the same point.
3142 *
3143 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3144 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3145 * inside the following two functions.
3146 */
3147static inline void memcg_stop_kmem_account(void)
3148{
3149 VM_BUG_ON(!current->mm);
3150 current->memcg_kmem_skip_account++;
3151}
3152
3153static inline void memcg_resume_kmem_account(void)
3154{
3155 VM_BUG_ON(!current->mm);
3156 current->memcg_kmem_skip_account--;
3157}
3158
Glauber Costa1f458cb2012-12-18 14:22:50 -08003159static void kmem_cache_destroy_work_func(struct work_struct *w)
3160{
3161 struct kmem_cache *cachep;
3162 struct memcg_cache_params *p;
3163
3164 p = container_of(w, struct memcg_cache_params, destroy);
3165
3166 cachep = memcg_params_to_cache(p);
3167
Glauber Costa22933152012-12-18 14:22:59 -08003168 /*
3169 * If we get down to 0 after shrink, we could delete right away.
3170 * However, memcg_release_pages() already puts us back in the workqueue
3171 * in that case. If we proceed deleting, we'll get a dangling
3172 * reference, and removing the object from the workqueue in that case
3173 * is unnecessary complication. We are not a fast path.
3174 *
3175 * Note that this case is fundamentally different from racing with
3176 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3177 * kmem_cache_shrink, not only we would be reinserting a dead cache
3178 * into the queue, but doing so from inside the worker racing to
3179 * destroy it.
3180 *
3181 * So if we aren't down to zero, we'll just schedule a worker and try
3182 * again
3183 */
3184 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3185 kmem_cache_shrink(cachep);
3186 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3187 return;
3188 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003189 kmem_cache_destroy(cachep);
3190}
3191
3192void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3193{
3194 if (!cachep->memcg_params->dead)
3195 return;
3196
3197 /*
Glauber Costa22933152012-12-18 14:22:59 -08003198 * There are many ways in which we can get here.
3199 *
3200 * We can get to a memory-pressure situation while the delayed work is
3201 * still pending to run. The vmscan shrinkers can then release all
3202 * cache memory and get us to destruction. If this is the case, we'll
3203 * be executed twice, which is a bug (the second time will execute over
3204 * bogus data). In this case, cancelling the work should be fine.
3205 *
3206 * But we can also get here from the worker itself, if
3207 * kmem_cache_shrink is enough to shake all the remaining objects and
3208 * get the page count to 0. In this case, we'll deadlock if we try to
3209 * cancel the work (the worker runs with an internal lock held, which
3210 * is the same lock we would hold for cancel_work_sync().)
3211 *
3212 * Since we can't possibly know who got us here, just refrain from
3213 * running if there is already work pending
3214 */
3215 if (work_pending(&cachep->memcg_params->destroy))
3216 return;
3217 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003218 * We have to defer the actual destroying to a workqueue, because
3219 * we might currently be in a context that cannot sleep.
3220 */
3221 schedule_work(&cachep->memcg_params->destroy);
3222}
3223
Glauber Costad7f25f82012-12-18 14:22:40 -08003224static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s)
3225{
3226 char *name;
3227 struct dentry *dentry;
3228
3229 rcu_read_lock();
3230 dentry = rcu_dereference(memcg->css.cgroup->dentry);
3231 rcu_read_unlock();
3232
3233 BUG_ON(dentry == NULL);
3234
3235 name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name,
3236 memcg_cache_id(memcg), dentry->d_name.name);
3237
3238 return name;
3239}
3240
3241static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3242 struct kmem_cache *s)
3243{
3244 char *name;
3245 struct kmem_cache *new;
3246
3247 name = memcg_cache_name(memcg, s);
3248 if (!name)
3249 return NULL;
3250
3251 new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align,
Glauber Costa943a4512012-12-18 14:23:03 -08003252 (s->flags & ~SLAB_PANIC), s->ctor, s);
Glauber Costad7f25f82012-12-18 14:22:40 -08003253
Glauber Costad79923f2012-12-18 14:22:48 -08003254 if (new)
3255 new->allocflags |= __GFP_KMEMCG;
3256
Glauber Costad7f25f82012-12-18 14:22:40 -08003257 kfree(name);
3258 return new;
3259}
3260
3261/*
3262 * This lock protects updaters, not readers. We want readers to be as fast as
3263 * they can, and they will either see NULL or a valid cache value. Our model
3264 * allow them to see NULL, in which case the root memcg will be selected.
3265 *
3266 * We need this lock because multiple allocations to the same cache from a non
3267 * will span more than one worker. Only one of them can create the cache.
3268 */
3269static DEFINE_MUTEX(memcg_cache_mutex);
3270static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3271 struct kmem_cache *cachep)
3272{
3273 struct kmem_cache *new_cachep;
3274 int idx;
3275
3276 BUG_ON(!memcg_can_account_kmem(memcg));
3277
3278 idx = memcg_cache_id(memcg);
3279
3280 mutex_lock(&memcg_cache_mutex);
3281 new_cachep = cachep->memcg_params->memcg_caches[idx];
3282 if (new_cachep)
3283 goto out;
3284
3285 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003286 if (new_cachep == NULL) {
3287 new_cachep = cachep;
3288 goto out;
3289 }
3290
3291 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003292 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003293
3294 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3295 /*
3296 * the readers won't lock, make sure everybody sees the updated value,
3297 * so they won't put stuff in the queue again for no reason
3298 */
3299 wmb();
3300out:
3301 mutex_unlock(&memcg_cache_mutex);
3302 return new_cachep;
3303}
3304
Glauber Costa7cf27982012-12-18 14:22:55 -08003305void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3306{
3307 struct kmem_cache *c;
3308 int i;
3309
3310 if (!s->memcg_params)
3311 return;
3312 if (!s->memcg_params->is_root_cache)
3313 return;
3314
3315 /*
3316 * If the cache is being destroyed, we trust that there is no one else
3317 * requesting objects from it. Even if there are, the sanity checks in
3318 * kmem_cache_destroy should caught this ill-case.
3319 *
3320 * Still, we don't want anyone else freeing memcg_caches under our
3321 * noses, which can happen if a new memcg comes to life. As usual,
3322 * we'll take the set_limit_mutex to protect ourselves against this.
3323 */
3324 mutex_lock(&set_limit_mutex);
3325 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3326 c = s->memcg_params->memcg_caches[i];
3327 if (!c)
3328 continue;
3329
3330 /*
3331 * We will now manually delete the caches, so to avoid races
3332 * we need to cancel all pending destruction workers and
3333 * proceed with destruction ourselves.
3334 *
3335 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3336 * and that could spawn the workers again: it is likely that
3337 * the cache still have active pages until this very moment.
3338 * This would lead us back to mem_cgroup_destroy_cache.
3339 *
3340 * But that will not execute at all if the "dead" flag is not
3341 * set, so flip it down to guarantee we are in control.
3342 */
3343 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003344 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003345 kmem_cache_destroy(c);
3346 }
3347 mutex_unlock(&set_limit_mutex);
3348}
3349
Glauber Costad7f25f82012-12-18 14:22:40 -08003350struct create_work {
3351 struct mem_cgroup *memcg;
3352 struct kmem_cache *cachep;
3353 struct work_struct work;
3354};
3355
Glauber Costa1f458cb2012-12-18 14:22:50 -08003356static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3357{
3358 struct kmem_cache *cachep;
3359 struct memcg_cache_params *params;
3360
3361 if (!memcg_kmem_is_active(memcg))
3362 return;
3363
3364 mutex_lock(&memcg->slab_caches_mutex);
3365 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3366 cachep = memcg_params_to_cache(params);
3367 cachep->memcg_params->dead = true;
Glauber Costa1f458cb2012-12-18 14:22:50 -08003368 schedule_work(&cachep->memcg_params->destroy);
3369 }
3370 mutex_unlock(&memcg->slab_caches_mutex);
3371}
3372
Glauber Costad7f25f82012-12-18 14:22:40 -08003373static void memcg_create_cache_work_func(struct work_struct *w)
3374{
3375 struct create_work *cw;
3376
3377 cw = container_of(w, struct create_work, work);
3378 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3379 /* Drop the reference gotten when we enqueued. */
3380 css_put(&cw->memcg->css);
3381 kfree(cw);
3382}
3383
3384/*
3385 * Enqueue the creation of a per-memcg kmem_cache.
3386 * Called with rcu_read_lock.
3387 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003388static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3389 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003390{
3391 struct create_work *cw;
3392
3393 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3394 if (cw == NULL)
3395 return;
3396
3397 /* The corresponding put will be done in the workqueue. */
3398 if (!css_tryget(&memcg->css)) {
3399 kfree(cw);
3400 return;
3401 }
3402
3403 cw->memcg = memcg;
3404 cw->cachep = cachep;
3405
3406 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3407 schedule_work(&cw->work);
3408}
3409
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003410static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3411 struct kmem_cache *cachep)
3412{
3413 /*
3414 * We need to stop accounting when we kmalloc, because if the
3415 * corresponding kmalloc cache is not yet created, the first allocation
3416 * in __memcg_create_cache_enqueue will recurse.
3417 *
3418 * However, it is better to enclose the whole function. Depending on
3419 * the debugging options enabled, INIT_WORK(), for instance, can
3420 * trigger an allocation. This too, will make us recurse. Because at
3421 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3422 * the safest choice is to do it like this, wrapping the whole function.
3423 */
3424 memcg_stop_kmem_account();
3425 __memcg_create_cache_enqueue(memcg, cachep);
3426 memcg_resume_kmem_account();
3427}
Glauber Costad7f25f82012-12-18 14:22:40 -08003428/*
3429 * Return the kmem_cache we're supposed to use for a slab allocation.
3430 * We try to use the current memcg's version of the cache.
3431 *
3432 * If the cache does not exist yet, if we are the first user of it,
3433 * we either create it immediately, if possible, or create it asynchronously
3434 * in a workqueue.
3435 * In the latter case, we will let the current allocation go through with
3436 * the original cache.
3437 *
3438 * Can't be called in interrupt context or from kernel threads.
3439 * This function needs to be called with rcu_read_lock() held.
3440 */
3441struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3442 gfp_t gfp)
3443{
3444 struct mem_cgroup *memcg;
3445 int idx;
3446
3447 VM_BUG_ON(!cachep->memcg_params);
3448 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3449
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003450 if (!current->mm || current->memcg_kmem_skip_account)
3451 return cachep;
3452
Glauber Costad7f25f82012-12-18 14:22:40 -08003453 rcu_read_lock();
3454 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3455 rcu_read_unlock();
3456
3457 if (!memcg_can_account_kmem(memcg))
3458 return cachep;
3459
3460 idx = memcg_cache_id(memcg);
3461
3462 /*
3463 * barrier to mare sure we're always seeing the up to date value. The
3464 * code updating memcg_caches will issue a write barrier to match this.
3465 */
3466 read_barrier_depends();
3467 if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) {
3468 /*
3469 * If we are in a safe context (can wait, and not in interrupt
3470 * context), we could be be predictable and return right away.
3471 * This would guarantee that the allocation being performed
3472 * already belongs in the new cache.
3473 *
3474 * However, there are some clashes that can arrive from locking.
3475 * For instance, because we acquire the slab_mutex while doing
3476 * kmem_cache_dup, this means no further allocation could happen
3477 * with the slab_mutex held.
3478 *
3479 * Also, because cache creation issue get_online_cpus(), this
3480 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3481 * that ends up reversed during cpu hotplug. (cpuset allocates
3482 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3483 * better to defer everything.
3484 */
3485 memcg_create_cache_enqueue(memcg, cachep);
3486 return cachep;
3487 }
3488
3489 return cachep->memcg_params->memcg_caches[idx];
3490}
3491EXPORT_SYMBOL(__memcg_kmem_get_cache);
3492
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003493/*
3494 * We need to verify if the allocation against current->mm->owner's memcg is
3495 * possible for the given order. But the page is not allocated yet, so we'll
3496 * need a further commit step to do the final arrangements.
3497 *
3498 * It is possible for the task to switch cgroups in this mean time, so at
3499 * commit time, we can't rely on task conversion any longer. We'll then use
3500 * the handle argument to return to the caller which cgroup we should commit
3501 * against. We could also return the memcg directly and avoid the pointer
3502 * passing, but a boolean return value gives better semantics considering
3503 * the compiled-out case as well.
3504 *
3505 * Returning true means the allocation is possible.
3506 */
3507bool
3508__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3509{
3510 struct mem_cgroup *memcg;
3511 int ret;
3512
3513 *_memcg = NULL;
3514 memcg = try_get_mem_cgroup_from_mm(current->mm);
3515
3516 /*
3517 * very rare case described in mem_cgroup_from_task. Unfortunately there
3518 * isn't much we can do without complicating this too much, and it would
3519 * be gfp-dependent anyway. Just let it go
3520 */
3521 if (unlikely(!memcg))
3522 return true;
3523
3524 if (!memcg_can_account_kmem(memcg)) {
3525 css_put(&memcg->css);
3526 return true;
3527 }
3528
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003529 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3530 if (!ret)
3531 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003532
3533 css_put(&memcg->css);
3534 return (ret == 0);
3535}
3536
3537void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3538 int order)
3539{
3540 struct page_cgroup *pc;
3541
3542 VM_BUG_ON(mem_cgroup_is_root(memcg));
3543
3544 /* The page allocation failed. Revert */
3545 if (!page) {
3546 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003547 return;
3548 }
3549
3550 pc = lookup_page_cgroup(page);
3551 lock_page_cgroup(pc);
3552 pc->mem_cgroup = memcg;
3553 SetPageCgroupUsed(pc);
3554 unlock_page_cgroup(pc);
3555}
3556
3557void __memcg_kmem_uncharge_pages(struct page *page, int order)
3558{
3559 struct mem_cgroup *memcg = NULL;
3560 struct page_cgroup *pc;
3561
3562
3563 pc = lookup_page_cgroup(page);
3564 /*
3565 * Fast unlocked return. Theoretically might have changed, have to
3566 * check again after locking.
3567 */
3568 if (!PageCgroupUsed(pc))
3569 return;
3570
3571 lock_page_cgroup(pc);
3572 if (PageCgroupUsed(pc)) {
3573 memcg = pc->mem_cgroup;
3574 ClearPageCgroupUsed(pc);
3575 }
3576 unlock_page_cgroup(pc);
3577
3578 /*
3579 * We trust that only if there is a memcg associated with the page, it
3580 * is a valid allocation
3581 */
3582 if (!memcg)
3583 return;
3584
3585 VM_BUG_ON(mem_cgroup_is_root(memcg));
3586 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003587}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003588#else
3589static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3590{
3591}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003592#endif /* CONFIG_MEMCG_KMEM */
3593
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003594#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3595
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003596#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003597/*
3598 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003599 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3600 * charge/uncharge will be never happen and move_account() is done under
3601 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003602 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003603void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003604{
3605 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003606 struct page_cgroup *pc;
3607 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003608
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003609 if (mem_cgroup_disabled())
3610 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003611 for (i = 1; i < HPAGE_PMD_NR; i++) {
3612 pc = head_pc + i;
3613 pc->mem_cgroup = head_pc->mem_cgroup;
3614 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003615 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3616 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003617}
Hugh Dickins12d27102012-01-12 17:19:52 -08003618#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003619
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003620/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003621 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003622 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003623 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003624 * @pc: page_cgroup of the page.
3625 * @from: mem_cgroup which the page is moved from.
3626 * @to: mem_cgroup which the page is moved to. @from != @to.
3627 *
3628 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003629 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003630 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003631 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003632 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3633 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003634 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003635static int mem_cgroup_move_account(struct page *page,
3636 unsigned int nr_pages,
3637 struct page_cgroup *pc,
3638 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003639 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003640{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003641 unsigned long flags;
3642 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003643 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003644
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003645 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003646 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003647 /*
3648 * The page is isolated from LRU. So, collapse function
3649 * will not handle this page. But page splitting can happen.
3650 * Do this check under compound_page_lock(). The caller should
3651 * hold it.
3652 */
3653 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003654 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003655 goto out;
3656
3657 lock_page_cgroup(pc);
3658
3659 ret = -EINVAL;
3660 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3661 goto unlock;
3662
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003663 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003664
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003665 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003666 /* Update mapped_file data for mem_cgroup */
3667 preempt_disable();
3668 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3669 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3670 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003671 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003672 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003673
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003674 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003675 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003676 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003677 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003678 ret = 0;
3679unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003680 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003681 /*
3682 * check events
3683 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003684 memcg_check_events(to, page);
3685 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003686out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003687 return ret;
3688}
3689
Michal Hocko2ef37d32012-10-26 13:37:30 +02003690/**
3691 * mem_cgroup_move_parent - moves page to the parent group
3692 * @page: the page to move
3693 * @pc: page_cgroup of the page
3694 * @child: page's cgroup
3695 *
3696 * move charges to its parent or the root cgroup if the group has no
3697 * parent (aka use_hierarchy==0).
3698 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3699 * mem_cgroup_move_account fails) the failure is always temporary and
3700 * it signals a race with a page removal/uncharge or migration. In the
3701 * first case the page is on the way out and it will vanish from the LRU
3702 * on the next attempt and the call should be retried later.
3703 * Isolation from the LRU fails only if page has been isolated from
3704 * the LRU since we looked at it and that usually means either global
3705 * reclaim or migration going on. The page will either get back to the
3706 * LRU or vanish.
3707 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3708 * (!PageCgroupUsed) or moved to a different group. The page will
3709 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003710 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003711static int mem_cgroup_move_parent(struct page *page,
3712 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003713 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003714{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003715 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003716 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003717 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003718 int ret;
3719
Michal Hockod8423012012-10-26 13:37:29 +02003720 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003721
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003722 ret = -EBUSY;
3723 if (!get_page_unless_zero(page))
3724 goto out;
3725 if (isolate_lru_page(page))
3726 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003727
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003728 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003729
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003730 parent = parent_mem_cgroup(child);
3731 /*
3732 * If no parent, move charges to root cgroup.
3733 */
3734 if (!parent)
3735 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003736
Michal Hocko2ef37d32012-10-26 13:37:30 +02003737 if (nr_pages > 1) {
3738 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003739 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003740 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003741
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003742 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003743 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003744 if (!ret)
3745 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003746
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003747 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003748 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003749 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003750put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003751 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003752out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753 return ret;
3754}
3755
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003756/*
3757 * Charge the memory controller for page usage.
3758 * Return
3759 * 0 if the charge was successful
3760 * < 0 if the cgroup is over its limit
3761 */
3762static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003763 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003764{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003765 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003766 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003767 bool oom = true;
3768 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003769
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003770 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003771 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003772 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003773 /*
3774 * Never OOM-kill a process for a huge page. The
3775 * fault handler will fall back to regular pages.
3776 */
3777 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003778 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003779
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003780 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003781 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003782 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003783 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003784 return 0;
3785}
3786
3787int mem_cgroup_newpage_charge(struct page *page,
3788 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003789{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003790 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003791 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003792 VM_BUG_ON(page_mapped(page));
3793 VM_BUG_ON(page->mapping && !PageAnon(page));
3794 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003795 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003796 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003797}
3798
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003799/*
3800 * While swap-in, try_charge -> commit or cancel, the page is locked.
3801 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003802 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003803 * "commit()" or removed by "cancel()"
3804 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003805static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3806 struct page *page,
3807 gfp_t mask,
3808 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003809{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003810 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003811 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003812 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003813
Johannes Weiner90deb782012-07-31 16:45:47 -07003814 pc = lookup_page_cgroup(page);
3815 /*
3816 * Every swap fault against a single page tries to charge the
3817 * page, bail as early as possible. shmem_unuse() encounters
3818 * already charged pages, too. The USED bit is protected by
3819 * the page lock, which serializes swap cache removal, which
3820 * in turn serializes uncharging.
3821 */
3822 if (PageCgroupUsed(pc))
3823 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003824 if (!do_swap_account)
3825 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003826 memcg = try_get_mem_cgroup_from_page(page);
3827 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003828 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003829 *memcgp = memcg;
3830 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003831 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003832 if (ret == -EINTR)
3833 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003834 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003835charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003836 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3837 if (ret == -EINTR)
3838 ret = 0;
3839 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003840}
3841
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003842int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3843 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3844{
3845 *memcgp = NULL;
3846 if (mem_cgroup_disabled())
3847 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003848 /*
3849 * A racing thread's fault, or swapoff, may have already
3850 * updated the pte, and even removed page from swap cache: in
3851 * those cases unuse_pte()'s pte_same() test will fail; but
3852 * there's also a KSM case which does need to charge the page.
3853 */
3854 if (!PageSwapCache(page)) {
3855 int ret;
3856
3857 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3858 if (ret == -EINTR)
3859 ret = 0;
3860 return ret;
3861 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003862 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3863}
3864
Johannes Weiner827a03d2012-07-31 16:45:36 -07003865void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3866{
3867 if (mem_cgroup_disabled())
3868 return;
3869 if (!memcg)
3870 return;
3871 __mem_cgroup_cancel_charge(memcg, 1);
3872}
3873
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003874static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003875__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003876 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003877{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003878 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003879 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003880 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003881 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003882
Johannes Weinerce587e62012-04-24 20:22:33 +02003883 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003884 /*
3885 * Now swap is on-memory. This means this page may be
3886 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003887 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3888 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3889 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003890 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003891 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003892 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003893 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003894 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003895}
3896
Johannes Weiner72835c82012-01-12 17:18:32 -08003897void mem_cgroup_commit_charge_swapin(struct page *page,
3898 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003899{
Johannes Weiner72835c82012-01-12 17:18:32 -08003900 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003901 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003902}
3903
Johannes Weiner827a03d2012-07-31 16:45:36 -07003904int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3905 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003906{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003907 struct mem_cgroup *memcg = NULL;
3908 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3909 int ret;
3910
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003911 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003912 return 0;
3913 if (PageCompound(page))
3914 return 0;
3915
Johannes Weiner827a03d2012-07-31 16:45:36 -07003916 if (!PageSwapCache(page))
3917 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3918 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003919 ret = __mem_cgroup_try_charge_swapin(mm, page,
3920 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003921 if (!ret)
3922 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3923 }
3924 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003925}
3926
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003927static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003928 unsigned int nr_pages,
3929 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003930{
3931 struct memcg_batch_info *batch = NULL;
3932 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003933
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003934 /* If swapout, usage of swap doesn't decrease */
3935 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3936 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003937
3938 batch = &current->memcg_batch;
3939 /*
3940 * In usual, we do css_get() when we remember memcg pointer.
3941 * But in this case, we keep res->usage until end of a series of
3942 * uncharges. Then, it's ok to ignore memcg's refcnt.
3943 */
3944 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003945 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003946 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003947 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003948 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003949 * the same cgroup and we have chance to coalesce uncharges.
3950 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3951 * because we want to do uncharge as soon as possible.
3952 */
3953
3954 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3955 goto direct_uncharge;
3956
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003957 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003958 goto direct_uncharge;
3959
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003960 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003961 * In typical case, batch->memcg == mem. This means we can
3962 * merge a series of uncharges to an uncharge of res_counter.
3963 * If not, we uncharge res_counter ony by one.
3964 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003965 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003966 goto direct_uncharge;
3967 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003968 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003969 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003970 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003971 return;
3972direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003973 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003974 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003975 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3976 if (unlikely(batch->memcg != memcg))
3977 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003978}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003979
Balbir Singh8697d332008-02-07 00:13:59 -08003980/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003981 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003982 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003983static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003984__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3985 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003986{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003987 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003988 unsigned int nr_pages = 1;
3989 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003990 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003991
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003992 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003993 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003994
Johannes Weiner0c59b892012-07-31 16:45:31 -07003995 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003996
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003997 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003998 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003999 VM_BUG_ON(!PageTransHuge(page));
4000 }
Balbir Singh8697d332008-02-07 00:13:59 -08004001 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08004002 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08004003 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004004 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004005 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004006 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08004007
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004008 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004009
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004010 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004011
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004012 if (!PageCgroupUsed(pc))
4013 goto unlock_out;
4014
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004015 anon = PageAnon(page);
4016
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004017 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004018 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004019 /*
4020 * Generally PageAnon tells if it's the anon statistics to be
4021 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4022 * used before page reached the stage of being marked PageAnon.
4023 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004024 anon = true;
4025 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004026 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004027 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004028 if (page_mapped(page))
4029 goto unlock_out;
4030 /*
4031 * Pages under migration may not be uncharged. But
4032 * end_migration() /must/ be the one uncharging the
4033 * unused post-migration page and so it has to call
4034 * here with the migration bit still set. See the
4035 * res_counter handling below.
4036 */
4037 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004038 goto unlock_out;
4039 break;
4040 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4041 if (!PageAnon(page)) { /* Shared memory */
4042 if (page->mapping && !page_is_file_cache(page))
4043 goto unlock_out;
4044 } else if (page_mapped(page)) /* Anon */
4045 goto unlock_out;
4046 break;
4047 default:
4048 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004049 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004050
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004051 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004052
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004053 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004054 /*
4055 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4056 * freed from LRU. This is safe because uncharged page is expected not
4057 * to be reused (freed soon). Exception is SwapCache, it's handled by
4058 * special functions.
4059 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004060
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004061 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004062 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004063 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004064 * will never be freed.
4065 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004066 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004067 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004068 mem_cgroup_swap_statistics(memcg, true);
4069 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004070 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004071 /*
4072 * Migration does not charge the res_counter for the
4073 * replacement page, so leave it alone when phasing out the
4074 * page that is unused after the migration.
4075 */
4076 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004077 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004078
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004079 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004080
4081unlock_out:
4082 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004083 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004084}
4085
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004086void mem_cgroup_uncharge_page(struct page *page)
4087{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004088 /* early check. */
4089 if (page_mapped(page))
4090 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004091 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07004092 if (PageSwapCache(page))
4093 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004094 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004095}
4096
4097void mem_cgroup_uncharge_cache_page(struct page *page)
4098{
4099 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004100 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004101 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004102}
4103
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004104/*
4105 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4106 * In that cases, pages are freed continuously and we can expect pages
4107 * are in the same memcg. All these calls itself limits the number of
4108 * pages freed at once, then uncharge_start/end() is called properly.
4109 * This may be called prural(2) times in a context,
4110 */
4111
4112void mem_cgroup_uncharge_start(void)
4113{
4114 current->memcg_batch.do_batch++;
4115 /* We can do nest. */
4116 if (current->memcg_batch.do_batch == 1) {
4117 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004118 current->memcg_batch.nr_pages = 0;
4119 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004120 }
4121}
4122
4123void mem_cgroup_uncharge_end(void)
4124{
4125 struct memcg_batch_info *batch = &current->memcg_batch;
4126
4127 if (!batch->do_batch)
4128 return;
4129
4130 batch->do_batch--;
4131 if (batch->do_batch) /* If stacked, do nothing. */
4132 return;
4133
4134 if (!batch->memcg)
4135 return;
4136 /*
4137 * This "batch->memcg" is valid without any css_get/put etc...
4138 * bacause we hide charges behind us.
4139 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004140 if (batch->nr_pages)
4141 res_counter_uncharge(&batch->memcg->res,
4142 batch->nr_pages * PAGE_SIZE);
4143 if (batch->memsw_nr_pages)
4144 res_counter_uncharge(&batch->memcg->memsw,
4145 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004146 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004147 /* forget this pointer (for sanity check) */
4148 batch->memcg = NULL;
4149}
4150
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004151#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004152/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004153 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004154 * memcg information is recorded to swap_cgroup of "ent"
4155 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004156void
4157mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004158{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004159 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004160 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004161
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004162 if (!swapout) /* this was a swap cache but the swap is unused ! */
4163 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4164
Johannes Weiner0030f532012-07-31 16:45:25 -07004165 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004166
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004167 /*
4168 * record memcg information, if swapout && memcg != NULL,
4169 * mem_cgroup_get() was called in uncharge().
4170 */
4171 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004172 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004173}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004174#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004175
Andrew Mortonc255a452012-07-31 16:43:02 -07004176#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004177/*
4178 * called from swap_entry_free(). remove record in swap_cgroup and
4179 * uncharge "memsw" account.
4180 */
4181void mem_cgroup_uncharge_swap(swp_entry_t ent)
4182{
4183 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004184 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004185
4186 if (!do_swap_account)
4187 return;
4188
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004189 id = swap_cgroup_record(ent, 0);
4190 rcu_read_lock();
4191 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004192 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004193 /*
4194 * We uncharge this because swap is freed.
4195 * This memcg can be obsolete one. We avoid calling css_tryget
4196 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004197 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004198 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004199 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004200 mem_cgroup_put(memcg);
4201 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004202 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004203}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004204
4205/**
4206 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4207 * @entry: swap entry to be moved
4208 * @from: mem_cgroup which the entry is moved from
4209 * @to: mem_cgroup which the entry is moved to
4210 *
4211 * It succeeds only when the swap_cgroup's record for this entry is the same
4212 * as the mem_cgroup's id of @from.
4213 *
4214 * Returns 0 on success, -EINVAL on failure.
4215 *
4216 * The caller must have charged to @to, IOW, called res_counter_charge() about
4217 * both res and memsw, and called css_get().
4218 */
4219static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004220 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004221{
4222 unsigned short old_id, new_id;
4223
4224 old_id = css_id(&from->css);
4225 new_id = css_id(&to->css);
4226
4227 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004228 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004229 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004230 /*
4231 * This function is only called from task migration context now.
4232 * It postpones res_counter and refcount handling till the end
4233 * of task migration(mem_cgroup_clear_mc()) for performance
4234 * improvement. But we cannot postpone mem_cgroup_get(to)
4235 * because if the process that has been moved to @to does
4236 * swap-in, the refcount of @to might be decreased to 0.
4237 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004238 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004239 return 0;
4240 }
4241 return -EINVAL;
4242}
4243#else
4244static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004245 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004246{
4247 return -EINVAL;
4248}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004249#endif
4250
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004251/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004252 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4253 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004254 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004255void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4256 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004257{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004258 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004259 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004260 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004261 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004262
Johannes Weiner72835c82012-01-12 17:18:32 -08004263 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004264
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004265 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004266 return;
Balbir Singh40779602008-04-04 14:29:59 -07004267
Mel Gormanb32967f2012-11-19 12:35:47 +00004268 if (PageTransHuge(page))
4269 nr_pages <<= compound_order(page);
4270
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004271 pc = lookup_page_cgroup(page);
4272 lock_page_cgroup(pc);
4273 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004274 memcg = pc->mem_cgroup;
4275 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004276 /*
4277 * At migrating an anonymous page, its mapcount goes down
4278 * to 0 and uncharge() will be called. But, even if it's fully
4279 * unmapped, migration may fail and this page has to be
4280 * charged again. We set MIGRATION flag here and delay uncharge
4281 * until end_migration() is called
4282 *
4283 * Corner Case Thinking
4284 * A)
4285 * When the old page was mapped as Anon and it's unmap-and-freed
4286 * while migration was ongoing.
4287 * If unmap finds the old page, uncharge() of it will be delayed
4288 * until end_migration(). If unmap finds a new page, it's
4289 * uncharged when it make mapcount to be 1->0. If unmap code
4290 * finds swap_migration_entry, the new page will not be mapped
4291 * and end_migration() will find it(mapcount==0).
4292 *
4293 * B)
4294 * When the old page was mapped but migraion fails, the kernel
4295 * remaps it. A charge for it is kept by MIGRATION flag even
4296 * if mapcount goes down to 0. We can do remap successfully
4297 * without charging it again.
4298 *
4299 * C)
4300 * The "old" page is under lock_page() until the end of
4301 * migration, so, the old page itself will not be swapped-out.
4302 * If the new page is swapped out before end_migraton, our
4303 * hook to usual swap-out path will catch the event.
4304 */
4305 if (PageAnon(page))
4306 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004307 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004308 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004309 /*
4310 * If the page is not charged at this point,
4311 * we return here.
4312 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004313 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004314 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004315
Johannes Weiner72835c82012-01-12 17:18:32 -08004316 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004317 /*
4318 * We charge new page before it's used/mapped. So, even if unlock_page()
4319 * is called before end_migration, we can catch all events on this new
4320 * page. In the case new page is migrated but not remapped, new page's
4321 * mapcount will be finally 0 and we call uncharge in end_migration().
4322 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004323 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004324 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004325 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004326 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004327 /*
4328 * The page is committed to the memcg, but it's not actually
4329 * charged to the res_counter since we plan on replacing the
4330 * old one and only one page is going to be left afterwards.
4331 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004332 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004333}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004334
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004335/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004336void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004337 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004338{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004339 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004340 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004341 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004342
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004343 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004344 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004345
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004346 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004347 used = oldpage;
4348 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004349 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004350 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004351 unused = oldpage;
4352 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004353 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004354 __mem_cgroup_uncharge_common(unused,
4355 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4356 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4357 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004358 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004359 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004360 * We disallowed uncharge of pages under migration because mapcount
4361 * of the page goes down to zero, temporarly.
4362 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004363 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004364 pc = lookup_page_cgroup(oldpage);
4365 lock_page_cgroup(pc);
4366 ClearPageCgroupMigration(pc);
4367 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004368
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004369 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004370 * If a page is a file cache, radix-tree replacement is very atomic
4371 * and we can skip this check. When it was an Anon page, its mapcount
4372 * goes down to 0. But because we added MIGRATION flage, it's not
4373 * uncharged yet. There are several case but page->mapcount check
4374 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4375 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004376 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004377 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004378 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004379}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004380
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004381/*
4382 * At replace page cache, newpage is not under any memcg but it's on
4383 * LRU. So, this function doesn't touch res_counter but handles LRU
4384 * in correct way. Both pages are locked so we cannot race with uncharge.
4385 */
4386void mem_cgroup_replace_page_cache(struct page *oldpage,
4387 struct page *newpage)
4388{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004389 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004390 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004391 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004392
4393 if (mem_cgroup_disabled())
4394 return;
4395
4396 pc = lookup_page_cgroup(oldpage);
4397 /* fix accounting on old pages */
4398 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004399 if (PageCgroupUsed(pc)) {
4400 memcg = pc->mem_cgroup;
4401 mem_cgroup_charge_statistics(memcg, false, -1);
4402 ClearPageCgroupUsed(pc);
4403 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004404 unlock_page_cgroup(pc);
4405
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004406 /*
4407 * When called from shmem_replace_page(), in some cases the
4408 * oldpage has already been charged, and in some cases not.
4409 */
4410 if (!memcg)
4411 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004412 /*
4413 * Even if newpage->mapping was NULL before starting replacement,
4414 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4415 * LRU while we overwrite pc->mem_cgroup.
4416 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004417 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004418}
4419
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004420#ifdef CONFIG_DEBUG_VM
4421static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4422{
4423 struct page_cgroup *pc;
4424
4425 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004426 /*
4427 * Can be NULL while feeding pages into the page allocator for
4428 * the first time, i.e. during boot or memory hotplug;
4429 * or when mem_cgroup_disabled().
4430 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004431 if (likely(pc) && PageCgroupUsed(pc))
4432 return pc;
4433 return NULL;
4434}
4435
4436bool mem_cgroup_bad_page_check(struct page *page)
4437{
4438 if (mem_cgroup_disabled())
4439 return false;
4440
4441 return lookup_page_cgroup_used(page) != NULL;
4442}
4443
4444void mem_cgroup_print_bad_page(struct page *page)
4445{
4446 struct page_cgroup *pc;
4447
4448 pc = lookup_page_cgroup_used(page);
4449 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004450 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4451 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004452 }
4453}
4454#endif
4455
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004456static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004457 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004458{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004459 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004460 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004461 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004462 int children = mem_cgroup_count_children(memcg);
4463 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004464 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004465
4466 /*
4467 * For keeping hierarchical_reclaim simple, how long we should retry
4468 * is depends on callers. We set our retry-count to be function
4469 * of # of children which we should visit in this loop.
4470 */
4471 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4472
4473 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004474
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004475 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004476 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004477 if (signal_pending(current)) {
4478 ret = -EINTR;
4479 break;
4480 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004481 /*
4482 * Rather than hide all in some function, I do this in
4483 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004484 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004485 */
4486 mutex_lock(&set_limit_mutex);
4487 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4488 if (memswlimit < val) {
4489 ret = -EINVAL;
4490 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004491 break;
4492 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004493
4494 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4495 if (memlimit < val)
4496 enlarge = 1;
4497
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004498 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004499 if (!ret) {
4500 if (memswlimit == val)
4501 memcg->memsw_is_minimum = true;
4502 else
4503 memcg->memsw_is_minimum = false;
4504 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004505 mutex_unlock(&set_limit_mutex);
4506
4507 if (!ret)
4508 break;
4509
Johannes Weiner56600482012-01-12 17:17:59 -08004510 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4511 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004512 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4513 /* Usage is reduced ? */
4514 if (curusage >= oldusage)
4515 retry_count--;
4516 else
4517 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004518 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004519 if (!ret && enlarge)
4520 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004521
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004522 return ret;
4523}
4524
Li Zefan338c8432009-06-17 16:27:15 -07004525static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4526 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004527{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004528 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004529 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004530 int children = mem_cgroup_count_children(memcg);
4531 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004532 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004533
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004534 /* see mem_cgroup_resize_res_limit */
4535 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4536 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004537 while (retry_count) {
4538 if (signal_pending(current)) {
4539 ret = -EINTR;
4540 break;
4541 }
4542 /*
4543 * Rather than hide all in some function, I do this in
4544 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004545 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004546 */
4547 mutex_lock(&set_limit_mutex);
4548 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4549 if (memlimit > val) {
4550 ret = -EINVAL;
4551 mutex_unlock(&set_limit_mutex);
4552 break;
4553 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004554 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4555 if (memswlimit < val)
4556 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004557 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004558 if (!ret) {
4559 if (memlimit == val)
4560 memcg->memsw_is_minimum = true;
4561 else
4562 memcg->memsw_is_minimum = false;
4563 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004564 mutex_unlock(&set_limit_mutex);
4565
4566 if (!ret)
4567 break;
4568
Johannes Weiner56600482012-01-12 17:17:59 -08004569 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4570 MEM_CGROUP_RECLAIM_NOSWAP |
4571 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004572 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004573 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004574 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004575 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004576 else
4577 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004578 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004579 if (!ret && enlarge)
4580 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004581 return ret;
4582}
4583
Balbir Singh4e416952009-09-23 15:56:39 -07004584unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004585 gfp_t gfp_mask,
4586 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004587{
4588 unsigned long nr_reclaimed = 0;
4589 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4590 unsigned long reclaimed;
4591 int loop = 0;
4592 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004593 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004594 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004595
4596 if (order > 0)
4597 return 0;
4598
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004599 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004600 /*
4601 * This loop can run a while, specially if mem_cgroup's continuously
4602 * keep exceeding their soft limit and putting the system under
4603 * pressure
4604 */
4605 do {
4606 if (next_mz)
4607 mz = next_mz;
4608 else
4609 mz = mem_cgroup_largest_soft_limit_node(mctz);
4610 if (!mz)
4611 break;
4612
Ying Han0ae5e892011-05-26 16:25:25 -07004613 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004614 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004615 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004616 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004617 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004618 spin_lock(&mctz->lock);
4619
4620 /*
4621 * If we failed to reclaim anything from this memory cgroup
4622 * it is time to move on to the next cgroup
4623 */
4624 next_mz = NULL;
4625 if (!reclaimed) {
4626 do {
4627 /*
4628 * Loop until we find yet another one.
4629 *
4630 * By the time we get the soft_limit lock
4631 * again, someone might have aded the
4632 * group back on the RB tree. Iterate to
4633 * make sure we get a different mem.
4634 * mem_cgroup_largest_soft_limit_node returns
4635 * NULL if no other cgroup is present on
4636 * the tree
4637 */
4638 next_mz =
4639 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004640 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004641 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004642 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004643 break;
4644 } while (1);
4645 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004646 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4647 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004648 /*
4649 * One school of thought says that we should not add
4650 * back the node to the tree if reclaim returns 0.
4651 * But our reclaim could return 0, simply because due
4652 * to priority we are exposing a smaller subset of
4653 * memory to reclaim from. Consider this as a longer
4654 * term TODO.
4655 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004656 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004657 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004658 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004659 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004660 loop++;
4661 /*
4662 * Could not reclaim anything and there are no more
4663 * mem cgroups to try or we seem to be looping without
4664 * reclaiming anything.
4665 */
4666 if (!nr_reclaimed &&
4667 (next_mz == NULL ||
4668 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4669 break;
4670 } while (!nr_reclaimed);
4671 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004672 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004673 return nr_reclaimed;
4674}
4675
Michal Hocko2ef37d32012-10-26 13:37:30 +02004676/**
4677 * mem_cgroup_force_empty_list - clears LRU of a group
4678 * @memcg: group to clear
4679 * @node: NUMA node
4680 * @zid: zone id
4681 * @lru: lru to to clear
4682 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004683 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004684 * reclaim the pages page themselves - pages are moved to the parent (or root)
4685 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004686 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004687static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004688 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004689{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004690 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004691 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004692 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004693 struct page *busy;
4694 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004695
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004696 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004697 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4698 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004699
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004700 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004701 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004702 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004703 struct page *page;
4704
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004705 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004706 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004707 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004708 break;
4709 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004710 page = list_entry(list->prev, struct page, lru);
4711 if (busy == page) {
4712 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004713 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004714 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004715 continue;
4716 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004717 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004718
Johannes Weiner925b7672012-01-12 17:18:15 -08004719 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004720
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004721 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004722 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004723 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004724 cond_resched();
4725 } else
4726 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004727 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004728}
4729
4730/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004731 * make mem_cgroup's charge to be 0 if there is no task by moving
4732 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004733 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004734 *
4735 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004736 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004737static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004738{
Michal Hockoc26251f2012-10-26 13:37:28 +02004739 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004740 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004741
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004742 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004743 /* This is for making all *used* pages to be on LRU. */
4744 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004745 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004746 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004747 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004748 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004749 enum lru_list lru;
4750 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004751 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004752 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004753 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004754 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004755 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004756 mem_cgroup_end_move(memcg);
4757 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004758 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004759
Michal Hocko2ef37d32012-10-26 13:37:30 +02004760 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004761 * Kernel memory may not necessarily be trackable to a specific
4762 * process. So they are not migrated, and therefore we can't
4763 * expect their value to drop to 0 here.
4764 * Having res filled up with kmem only is enough.
4765 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004766 * This is a safety check because mem_cgroup_force_empty_list
4767 * could have raced with mem_cgroup_replace_page_cache callers
4768 * so the lru seemed empty but the page could have been added
4769 * right after the check. RES_USAGE should be safe as we always
4770 * charge before adding to the LRU.
4771 */
Glauber Costabea207c2012-12-18 14:22:11 -08004772 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4773 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4774 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004775}
4776
4777/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004778 * This mainly exists for tests during the setting of set of use_hierarchy.
4779 * Since this is the very setting we are changing, the current hierarchy value
4780 * is meaningless
4781 */
4782static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4783{
4784 struct cgroup *pos;
4785
4786 /* bounce at first found */
4787 cgroup_for_each_child(pos, memcg->css.cgroup)
4788 return true;
4789 return false;
4790}
4791
4792/*
Glauber Costa09998212013-02-22 16:34:55 -08004793 * Must be called with memcg_create_mutex held, unless the cgroup is guaranteed
4794 * to be already dead (as in mem_cgroup_force_empty, for instance). This is
Glauber Costab5f99b52013-02-22 16:34:53 -08004795 * from mem_cgroup_count_children(), in the sense that we don't really care how
4796 * many children we have; we only need to know if we have any. It also counts
4797 * any memcg without hierarchy as infertile.
4798 */
4799static inline bool memcg_has_children(struct mem_cgroup *memcg)
4800{
4801 return memcg->use_hierarchy && __memcg_has_children(memcg);
4802}
4803
4804/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004805 * Reclaims as many pages from the given memcg as possible and moves
4806 * the rest to the parent.
4807 *
4808 * Caller is responsible for holding css reference for memcg.
4809 */
4810static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4811{
4812 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4813 struct cgroup *cgrp = memcg->css.cgroup;
4814
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004815 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004816 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4817 return -EBUSY;
4818
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004819 /* we call try-to-free pages for make this cgroup empty */
4820 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004821 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004822 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004823 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004824
Michal Hockoc26251f2012-10-26 13:37:28 +02004825 if (signal_pending(current))
4826 return -EINTR;
4827
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004828 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004829 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004830 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004831 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004832 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004833 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004834 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004835
4836 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004837 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004838 mem_cgroup_reparent_charges(memcg);
4839
4840 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004841}
4842
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004843static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004844{
Michal Hockoc26251f2012-10-26 13:37:28 +02004845 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4846 int ret;
4847
Michal Hockod8423012012-10-26 13:37:29 +02004848 if (mem_cgroup_is_root(memcg))
4849 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004850 css_get(&memcg->css);
4851 ret = mem_cgroup_force_empty(memcg);
4852 css_put(&memcg->css);
4853
4854 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004855}
4856
4857
Balbir Singh18f59ea2009-01-07 18:08:07 -08004858static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4859{
4860 return mem_cgroup_from_cont(cont)->use_hierarchy;
4861}
4862
4863static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4864 u64 val)
4865{
4866 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004867 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004868 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004869 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004870
4871 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004872 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004873
Glauber Costa09998212013-02-22 16:34:55 -08004874 mutex_lock(&memcg_create_mutex);
Glauber Costa567fb432012-07-31 16:43:07 -07004875
4876 if (memcg->use_hierarchy == val)
4877 goto out;
4878
Balbir Singh18f59ea2009-01-07 18:08:07 -08004879 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004880 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004881 * in the child subtrees. If it is unset, then the change can
4882 * occur, provided the current cgroup has no children.
4883 *
4884 * For the root cgroup, parent_mem is NULL, we allow value to be
4885 * set if there are no children.
4886 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004887 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004888 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004889 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004890 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004891 else
4892 retval = -EBUSY;
4893 } else
4894 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004895
4896out:
Glauber Costa09998212013-02-22 16:34:55 -08004897 mutex_unlock(&memcg_create_mutex);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004898
4899 return retval;
4900}
4901
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004902
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004903static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004904 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004905{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004906 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004907 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004908
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004909 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004910 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004911 val += mem_cgroup_read_stat(iter, idx);
4912
4913 if (val < 0) /* race ? */
4914 val = 0;
4915 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004916}
4917
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004918static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004919{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004920 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004921
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004922 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004923 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004924 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004925 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004926 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004927 }
4928
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004929 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4930 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004931
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004932 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004933 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004934
4935 return val << PAGE_SHIFT;
4936}
4937
Tejun Heoaf36f902012-04-01 12:09:55 -07004938static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4939 struct file *file, char __user *buf,
4940 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004941{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004942 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004943 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004944 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004945 int name, len;
4946 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004947
4948 type = MEMFILE_TYPE(cft->private);
4949 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004950
4951 if (!do_swap_account && type == _MEMSWAP)
4952 return -EOPNOTSUPP;
4953
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004954 switch (type) {
4955 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004956 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004957 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004958 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004959 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004960 break;
4961 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004962 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004963 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004964 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004965 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004966 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004967 case _KMEM:
4968 val = res_counter_read_u64(&memcg->kmem, name);
4969 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004970 default:
4971 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004972 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004973
4974 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4975 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004976}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004977
4978static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4979{
4980 int ret = -EINVAL;
4981#ifdef CONFIG_MEMCG_KMEM
4982 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4983 /*
4984 * For simplicity, we won't allow this to be disabled. It also can't
4985 * be changed if the cgroup has children already, or if tasks had
4986 * already joined.
4987 *
4988 * If tasks join before we set the limit, a person looking at
4989 * kmem.usage_in_bytes will have no way to determine when it took
4990 * place, which makes the value quite meaningless.
4991 *
4992 * After it first became limited, changes in the value of the limit are
4993 * of course permitted.
Glauber Costa510fc4e2012-12-18 14:21:47 -08004994 */
Glauber Costa09998212013-02-22 16:34:55 -08004995 mutex_lock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004996 mutex_lock(&set_limit_mutex);
4997 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004998 if (cgroup_task_count(cont) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004999 ret = -EBUSY;
5000 goto out;
5001 }
5002 ret = res_counter_set_limit(&memcg->kmem, val);
5003 VM_BUG_ON(ret);
5004
Glauber Costa55007d82012-12-18 14:22:38 -08005005 ret = memcg_update_cache_sizes(memcg);
5006 if (ret) {
5007 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
5008 goto out;
5009 }
Glauber Costa692e89a2013-02-22 16:34:56 -08005010 static_key_slow_inc(&memcg_kmem_enabled_key);
5011 /*
5012 * setting the active bit after the inc will guarantee no one
5013 * starts accounting before all call sites are patched
5014 */
5015 memcg_kmem_set_active(memcg);
5016
Glauber Costa7de37682012-12-18 14:22:07 -08005017 /*
5018 * kmem charges can outlive the cgroup. In the case of slab
5019 * pages, for instance, a page contain objects from various
5020 * processes, so it is unfeasible to migrate them away. We
5021 * need to reference count the memcg because of that.
5022 */
5023 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005024 } else
5025 ret = res_counter_set_limit(&memcg->kmem, val);
5026out:
5027 mutex_unlock(&set_limit_mutex);
Glauber Costa09998212013-02-22 16:34:55 -08005028 mutex_unlock(&memcg_create_mutex);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005029#endif
5030 return ret;
5031}
5032
Hugh Dickins6d0439902013-02-22 16:35:50 -08005033#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -08005034static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005035{
Glauber Costa55007d82012-12-18 14:22:38 -08005036 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005037 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5038 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005039 goto out;
5040
Glauber Costa510fc4e2012-12-18 14:21:47 -08005041 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costaa8964b92012-12-18 14:22:09 -08005042 /*
5043 * When that happen, we need to disable the static branch only on those
5044 * memcgs that enabled it. To achieve this, we would be forced to
5045 * complicate the code by keeping track of which memcgs were the ones
5046 * that actually enabled limits, and which ones got it from its
5047 * parents.
5048 *
5049 * It is a lot simpler just to do static_key_slow_inc() on every child
5050 * that is accounted.
5051 */
Glauber Costa55007d82012-12-18 14:22:38 -08005052 if (!memcg_kmem_is_active(memcg))
5053 goto out;
5054
5055 /*
5056 * destroy(), called if we fail, will issue static_key_slow_inc() and
5057 * mem_cgroup_put() if kmem is enabled. We have to either call them
5058 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5059 * this more consistent, since it always leads to the same destroy path
5060 */
5061 mem_cgroup_get(memcg);
5062 static_key_slow_inc(&memcg_kmem_enabled_key);
5063
5064 mutex_lock(&set_limit_mutex);
5065 ret = memcg_update_cache_sizes(memcg);
5066 mutex_unlock(&set_limit_mutex);
Glauber Costa55007d82012-12-18 14:22:38 -08005067out:
5068 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005069}
Hugh Dickins6d0439902013-02-22 16:35:50 -08005070#endif /* CONFIG_MEMCG_KMEM */
Glauber Costa510fc4e2012-12-18 14:21:47 -08005071
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005072/*
5073 * The user of this function is...
5074 * RES_LIMIT.
5075 */
Paul Menage856c13a2008-07-25 01:47:04 -07005076static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5077 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005078{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005079 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005080 enum res_type type;
5081 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005082 unsigned long long val;
5083 int ret;
5084
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005085 type = MEMFILE_TYPE(cft->private);
5086 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005087
5088 if (!do_swap_account && type == _MEMSWAP)
5089 return -EOPNOTSUPP;
5090
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005091 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005092 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005093 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5094 ret = -EINVAL;
5095 break;
5096 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005097 /* This function does all necessary parse...reuse it */
5098 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005099 if (ret)
5100 break;
5101 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005102 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005103 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005104 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005105 else if (type == _KMEM)
5106 ret = memcg_update_kmem_limit(cont, val);
5107 else
5108 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005109 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005110 case RES_SOFT_LIMIT:
5111 ret = res_counter_memparse_write_strategy(buffer, &val);
5112 if (ret)
5113 break;
5114 /*
5115 * For memsw, soft limits are hard to implement in terms
5116 * of semantics, for now, we support soft limits for
5117 * control without swap
5118 */
5119 if (type == _MEM)
5120 ret = res_counter_set_soft_limit(&memcg->res, val);
5121 else
5122 ret = -EINVAL;
5123 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005124 default:
5125 ret = -EINVAL; /* should be BUG() ? */
5126 break;
5127 }
5128 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005129}
5130
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005131static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5132 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5133{
5134 struct cgroup *cgroup;
5135 unsigned long long min_limit, min_memsw_limit, tmp;
5136
5137 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5138 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5139 cgroup = memcg->css.cgroup;
5140 if (!memcg->use_hierarchy)
5141 goto out;
5142
5143 while (cgroup->parent) {
5144 cgroup = cgroup->parent;
5145 memcg = mem_cgroup_from_cont(cgroup);
5146 if (!memcg->use_hierarchy)
5147 break;
5148 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5149 min_limit = min(min_limit, tmp);
5150 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5151 min_memsw_limit = min(min_memsw_limit, tmp);
5152 }
5153out:
5154 *mem_limit = min_limit;
5155 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005156}
5157
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005158static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005159{
Tejun Heoaf36f902012-04-01 12:09:55 -07005160 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005161 int name;
5162 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005163
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005164 type = MEMFILE_TYPE(event);
5165 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005166
5167 if (!do_swap_account && type == _MEMSWAP)
5168 return -EOPNOTSUPP;
5169
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005170 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005171 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005172 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005173 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005174 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005175 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005176 else if (type == _KMEM)
5177 res_counter_reset_max(&memcg->kmem);
5178 else
5179 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005180 break;
5181 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005182 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005183 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005184 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005185 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005186 else if (type == _KMEM)
5187 res_counter_reset_failcnt(&memcg->kmem);
5188 else
5189 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005190 break;
5191 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005192
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005193 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005194}
5195
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005196static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5197 struct cftype *cft)
5198{
5199 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5200}
5201
Daisuke Nishimura02491442010-03-10 15:22:17 -08005202#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005203static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5204 struct cftype *cft, u64 val)
5205{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005206 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005207
5208 if (val >= (1 << NR_MOVE_TYPE))
5209 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005210
Glauber Costaee5e8472013-02-22 16:34:50 -08005211 /*
5212 * No kind of locking is needed in here, because ->can_attach() will
5213 * check this value once in the beginning of the process, and then carry
5214 * on with stale data. This means that changes to this value will only
5215 * affect task migrations starting after the change.
5216 */
5217 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005218 return 0;
5219}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005220#else
5221static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5222 struct cftype *cft, u64 val)
5223{
5224 return -ENOSYS;
5225}
5226#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005227
Ying Han406eb0c2011-05-26 16:25:37 -07005228#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005229static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005230 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005231{
5232 int nid;
5233 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5234 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005235 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005236
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005237 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005238 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005239 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005240 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005241 seq_printf(m, " N%d=%lu", nid, node_nr);
5242 }
5243 seq_putc(m, '\n');
5244
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005245 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005246 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005247 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005248 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005249 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005250 seq_printf(m, " N%d=%lu", nid, node_nr);
5251 }
5252 seq_putc(m, '\n');
5253
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005254 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005255 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005256 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005257 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005258 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005259 seq_printf(m, " N%d=%lu", nid, node_nr);
5260 }
5261 seq_putc(m, '\n');
5262
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005263 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005264 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005265 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005266 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005267 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005268 seq_printf(m, " N%d=%lu", nid, node_nr);
5269 }
5270 seq_putc(m, '\n');
5271 return 0;
5272}
5273#endif /* CONFIG_NUMA */
5274
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005275static inline void mem_cgroup_lru_names_not_uptodate(void)
5276{
5277 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5278}
5279
Wanpeng Liab215882012-07-31 16:43:09 -07005280static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005281 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005282{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005283 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005284 struct mem_cgroup *mi;
5285 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005286
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005287 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005288 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005289 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005290 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5291 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005292 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005293
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005294 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5295 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5296 mem_cgroup_read_events(memcg, i));
5297
5298 for (i = 0; i < NR_LRU_LISTS; i++)
5299 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5300 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5301
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005302 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005303 {
5304 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005305 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005306 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005307 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005308 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5309 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005310 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005311
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005312 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5313 long long val = 0;
5314
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005315 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005316 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005317 for_each_mem_cgroup_tree(mi, memcg)
5318 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5319 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5320 }
5321
5322 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5323 unsigned long long val = 0;
5324
5325 for_each_mem_cgroup_tree(mi, memcg)
5326 val += mem_cgroup_read_events(mi, i);
5327 seq_printf(m, "total_%s %llu\n",
5328 mem_cgroup_events_names[i], val);
5329 }
5330
5331 for (i = 0; i < NR_LRU_LISTS; i++) {
5332 unsigned long long val = 0;
5333
5334 for_each_mem_cgroup_tree(mi, memcg)
5335 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5336 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005337 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005338
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005339#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005340 {
5341 int nid, zid;
5342 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005343 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005344 unsigned long recent_rotated[2] = {0, 0};
5345 unsigned long recent_scanned[2] = {0, 0};
5346
5347 for_each_online_node(nid)
5348 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005349 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005350 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005351
Hugh Dickins89abfab2012-05-29 15:06:53 -07005352 recent_rotated[0] += rstat->recent_rotated[0];
5353 recent_rotated[1] += rstat->recent_rotated[1];
5354 recent_scanned[0] += rstat->recent_scanned[0];
5355 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005356 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005357 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5358 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5359 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5360 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005361 }
5362#endif
5363
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005364 return 0;
5365}
5366
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005367static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5368{
5369 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5370
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005371 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005372}
5373
5374static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5375 u64 val)
5376{
5377 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5378 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005379
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005380 if (val > 100)
5381 return -EINVAL;
5382
5383 if (cgrp->parent == NULL)
5384 return -EINVAL;
5385
5386 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005387
Glauber Costa09998212013-02-22 16:34:55 -08005388 mutex_lock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005389
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005390 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005391 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005392 mutex_unlock(&memcg_create_mutex);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005393 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005394 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005395
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005396 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005397
Glauber Costa09998212013-02-22 16:34:55 -08005398 mutex_unlock(&memcg_create_mutex);
Li Zefan068b38c2009-01-15 13:51:26 -08005399
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005400 return 0;
5401}
5402
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005403static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5404{
5405 struct mem_cgroup_threshold_ary *t;
5406 u64 usage;
5407 int i;
5408
5409 rcu_read_lock();
5410 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005411 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005412 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005413 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005414
5415 if (!t)
5416 goto unlock;
5417
5418 usage = mem_cgroup_usage(memcg, swap);
5419
5420 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005421 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005422 * If it's not true, a threshold was crossed after last
5423 * call of __mem_cgroup_threshold().
5424 */
Phil Carmody5407a562010-05-26 14:42:42 -07005425 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005426
5427 /*
5428 * Iterate backward over array of thresholds starting from
5429 * current_threshold and check if a threshold is crossed.
5430 * If none of thresholds below usage is crossed, we read
5431 * only one element of the array here.
5432 */
5433 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5434 eventfd_signal(t->entries[i].eventfd, 1);
5435
5436 /* i = current_threshold + 1 */
5437 i++;
5438
5439 /*
5440 * Iterate forward over array of thresholds starting from
5441 * current_threshold+1 and check if a threshold is crossed.
5442 * If none of thresholds above usage is crossed, we read
5443 * only one element of the array here.
5444 */
5445 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5446 eventfd_signal(t->entries[i].eventfd, 1);
5447
5448 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005449 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005450unlock:
5451 rcu_read_unlock();
5452}
5453
5454static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5455{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005456 while (memcg) {
5457 __mem_cgroup_threshold(memcg, false);
5458 if (do_swap_account)
5459 __mem_cgroup_threshold(memcg, true);
5460
5461 memcg = parent_mem_cgroup(memcg);
5462 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005463}
5464
5465static int compare_thresholds(const void *a, const void *b)
5466{
5467 const struct mem_cgroup_threshold *_a = a;
5468 const struct mem_cgroup_threshold *_b = b;
5469
5470 return _a->threshold - _b->threshold;
5471}
5472
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005473static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005474{
5475 struct mem_cgroup_eventfd_list *ev;
5476
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005477 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005478 eventfd_signal(ev->eventfd, 1);
5479 return 0;
5480}
5481
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005482static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005483{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005484 struct mem_cgroup *iter;
5485
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005486 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005487 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005488}
5489
5490static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5491 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005492{
5493 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005494 struct mem_cgroup_thresholds *thresholds;
5495 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005496 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005497 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005498 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005499
5500 ret = res_counter_memparse_write_strategy(args, &threshold);
5501 if (ret)
5502 return ret;
5503
5504 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005505
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005506 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005507 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005508 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005509 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005510 else
5511 BUG();
5512
5513 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5514
5515 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005516 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005517 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5518
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005519 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005520
5521 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005522 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005523 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005524 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005525 ret = -ENOMEM;
5526 goto unlock;
5527 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005528 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005529
5530 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005531 if (thresholds->primary) {
5532 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005533 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005534 }
5535
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005536 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005537 new->entries[size - 1].eventfd = eventfd;
5538 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005539
5540 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005541 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005542 compare_thresholds, NULL);
5543
5544 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005545 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005546 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005547 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005548 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005549 * new->current_threshold will not be used until
5550 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005551 * it here.
5552 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005553 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005554 } else
5555 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005556 }
5557
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005558 /* Free old spare buffer and save old primary buffer as spare */
5559 kfree(thresholds->spare);
5560 thresholds->spare = thresholds->primary;
5561
5562 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005563
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005564 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005565 synchronize_rcu();
5566
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005567unlock:
5568 mutex_unlock(&memcg->thresholds_lock);
5569
5570 return ret;
5571}
5572
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005573static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005574 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005575{
5576 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005577 struct mem_cgroup_thresholds *thresholds;
5578 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005579 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005580 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005581 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005582
5583 mutex_lock(&memcg->thresholds_lock);
5584 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005585 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005586 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005587 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005588 else
5589 BUG();
5590
Anton Vorontsov371528c2012-02-24 05:14:46 +04005591 if (!thresholds->primary)
5592 goto unlock;
5593
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005594 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5595
5596 /* Check if a threshold crossed before removing */
5597 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5598
5599 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005600 size = 0;
5601 for (i = 0; i < thresholds->primary->size; i++) {
5602 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005603 size++;
5604 }
5605
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005606 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005607
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005608 /* Set thresholds array to NULL if we don't have thresholds */
5609 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005610 kfree(new);
5611 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005612 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005613 }
5614
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005615 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005616
5617 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005618 new->current_threshold = -1;
5619 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5620 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005621 continue;
5622
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005623 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005624 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005625 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005626 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005627 * until rcu_assign_pointer(), so it's safe to increment
5628 * it here.
5629 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005630 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005631 }
5632 j++;
5633 }
5634
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005635swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005636 /* Swap primary and spare array */
5637 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005638 /* If all events are unregistered, free the spare array */
5639 if (!new) {
5640 kfree(thresholds->spare);
5641 thresholds->spare = NULL;
5642 }
5643
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005644 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005645
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005646 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005647 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005648unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005649 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005650}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005651
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005652static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5653 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5654{
5655 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5656 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005657 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005658
5659 BUG_ON(type != _OOM_TYPE);
5660 event = kmalloc(sizeof(*event), GFP_KERNEL);
5661 if (!event)
5662 return -ENOMEM;
5663
Michal Hocko1af8efe2011-07-26 16:08:24 -07005664 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005665
5666 event->eventfd = eventfd;
5667 list_add(&event->list, &memcg->oom_notify);
5668
5669 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005670 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005671 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005672 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005673
5674 return 0;
5675}
5676
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005677static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005678 struct cftype *cft, struct eventfd_ctx *eventfd)
5679{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005680 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005681 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005682 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005683
5684 BUG_ON(type != _OOM_TYPE);
5685
Michal Hocko1af8efe2011-07-26 16:08:24 -07005686 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005687
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005688 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005689 if (ev->eventfd == eventfd) {
5690 list_del(&ev->list);
5691 kfree(ev);
5692 }
5693 }
5694
Michal Hocko1af8efe2011-07-26 16:08:24 -07005695 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005696}
5697
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005698static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5699 struct cftype *cft, struct cgroup_map_cb *cb)
5700{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005701 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005702
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005703 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005704
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005705 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005706 cb->fill(cb, "under_oom", 1);
5707 else
5708 cb->fill(cb, "under_oom", 0);
5709 return 0;
5710}
5711
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005712static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5713 struct cftype *cft, u64 val)
5714{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005715 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005716 struct mem_cgroup *parent;
5717
5718 /* cannot set to root cgroup and only 0 and 1 are allowed */
5719 if (!cgrp->parent || !((val == 0) || (val == 1)))
5720 return -EINVAL;
5721
5722 parent = mem_cgroup_from_cont(cgrp->parent);
5723
Glauber Costa09998212013-02-22 16:34:55 -08005724 mutex_lock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005725 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005726 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Glauber Costa09998212013-02-22 16:34:55 -08005727 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005728 return -EINVAL;
5729 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005730 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005731 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005732 memcg_oom_recover(memcg);
Glauber Costa09998212013-02-22 16:34:55 -08005733 mutex_unlock(&memcg_create_mutex);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005734 return 0;
5735}
5736
Andrew Mortonc255a452012-07-31 16:43:02 -07005737#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005738static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005739{
Glauber Costa55007d82012-12-18 14:22:38 -08005740 int ret;
5741
Glauber Costa2633d7a2012-12-18 14:22:34 -08005742 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005743 ret = memcg_propagate_kmem(memcg);
5744 if (ret)
5745 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005746
Glauber Costa1d62e432012-04-09 19:36:33 -03005747 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005748};
5749
Glauber Costa1d62e432012-04-09 19:36:33 -03005750static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005751{
Glauber Costa1d62e432012-04-09 19:36:33 -03005752 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005753
5754 memcg_kmem_mark_dead(memcg);
5755
5756 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5757 return;
5758
5759 /*
5760 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5761 * path here, being careful not to race with memcg_uncharge_kmem: it is
5762 * possible that the charges went down to 0 between mark_dead and the
5763 * res_counter read, so in that case, we don't need the put
5764 */
5765 if (memcg_kmem_test_and_clear_dead(memcg))
5766 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005767}
Glauber Costae5671df2011-12-11 21:47:01 +00005768#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005769static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005770{
5771 return 0;
5772}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005773
Glauber Costa1d62e432012-04-09 19:36:33 -03005774static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005775{
5776}
Glauber Costae5671df2011-12-11 21:47:01 +00005777#endif
5778
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005779static struct cftype mem_cgroup_files[] = {
5780 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005781 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005782 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005783 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005784 .register_event = mem_cgroup_usage_register_event,
5785 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005786 },
5787 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005788 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005789 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005790 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005791 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005792 },
5793 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005794 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005795 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005796 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005797 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005798 },
5799 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005800 .name = "soft_limit_in_bytes",
5801 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5802 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005803 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005804 },
5805 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005806 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005807 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005808 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005809 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005810 },
Balbir Singh8697d332008-02-07 00:13:59 -08005811 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005812 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005813 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005814 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005815 {
5816 .name = "force_empty",
5817 .trigger = mem_cgroup_force_empty_write,
5818 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005819 {
5820 .name = "use_hierarchy",
5821 .write_u64 = mem_cgroup_hierarchy_write,
5822 .read_u64 = mem_cgroup_hierarchy_read,
5823 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005824 {
5825 .name = "swappiness",
5826 .read_u64 = mem_cgroup_swappiness_read,
5827 .write_u64 = mem_cgroup_swappiness_write,
5828 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005829 {
5830 .name = "move_charge_at_immigrate",
5831 .read_u64 = mem_cgroup_move_charge_read,
5832 .write_u64 = mem_cgroup_move_charge_write,
5833 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005834 {
5835 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005836 .read_map = mem_cgroup_oom_control_read,
5837 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005838 .register_event = mem_cgroup_oom_register_event,
5839 .unregister_event = mem_cgroup_oom_unregister_event,
5840 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5841 },
Ying Han406eb0c2011-05-26 16:25:37 -07005842#ifdef CONFIG_NUMA
5843 {
5844 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005845 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005846 },
5847#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005848#ifdef CONFIG_MEMCG_KMEM
5849 {
5850 .name = "kmem.limit_in_bytes",
5851 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5852 .write_string = mem_cgroup_write,
5853 .read = mem_cgroup_read,
5854 },
5855 {
5856 .name = "kmem.usage_in_bytes",
5857 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5858 .read = mem_cgroup_read,
5859 },
5860 {
5861 .name = "kmem.failcnt",
5862 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5863 .trigger = mem_cgroup_reset,
5864 .read = mem_cgroup_read,
5865 },
5866 {
5867 .name = "kmem.max_usage_in_bytes",
5868 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5869 .trigger = mem_cgroup_reset,
5870 .read = mem_cgroup_read,
5871 },
Glauber Costa749c5412012-12-18 14:23:01 -08005872#ifdef CONFIG_SLABINFO
5873 {
5874 .name = "kmem.slabinfo",
5875 .read_seq_string = mem_cgroup_slabinfo_read,
5876 },
5877#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005878#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005879 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005880};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005881
Michal Hocko2d110852013-02-22 16:34:43 -08005882#ifdef CONFIG_MEMCG_SWAP
5883static struct cftype memsw_cgroup_files[] = {
5884 {
5885 .name = "memsw.usage_in_bytes",
5886 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5887 .read = mem_cgroup_read,
5888 .register_event = mem_cgroup_usage_register_event,
5889 .unregister_event = mem_cgroup_usage_unregister_event,
5890 },
5891 {
5892 .name = "memsw.max_usage_in_bytes",
5893 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5894 .trigger = mem_cgroup_reset,
5895 .read = mem_cgroup_read,
5896 },
5897 {
5898 .name = "memsw.limit_in_bytes",
5899 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5900 .write_string = mem_cgroup_write,
5901 .read = mem_cgroup_read,
5902 },
5903 {
5904 .name = "memsw.failcnt",
5905 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5906 .trigger = mem_cgroup_reset,
5907 .read = mem_cgroup_read,
5908 },
5909 { }, /* terminate */
5910};
5911#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005912static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005913{
5914 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005915 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005916 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005917 /*
5918 * This routine is called against possible nodes.
5919 * But it's BUG to call kmalloc() against offline node.
5920 *
5921 * TODO: this routine can waste much memory for nodes which will
5922 * never be onlined. It's better to use memory hotplug callback
5923 * function.
5924 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005925 if (!node_state(node, N_NORMAL_MEMORY))
5926 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005927 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005928 if (!pn)
5929 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005930
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005931 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5932 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005933 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005934 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005935 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005936 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005937 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005938 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005939 return 0;
5940}
5941
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005942static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005943{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005944 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005945}
5946
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005947static struct mem_cgroup *mem_cgroup_alloc(void)
5948{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005949 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005950 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005951
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005952 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005953 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005954 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005955 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005956 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005957
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005958 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005959 return NULL;
5960
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005961 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5962 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005963 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005964 spin_lock_init(&memcg->pcp_counter_lock);
5965 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005966
5967out_free:
5968 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005969 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005970 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005971 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005972 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005973}
5974
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005975/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005976 * At destroying mem_cgroup, references from swap_cgroup can remain.
5977 * (scanning all at force_empty is too costly...)
5978 *
5979 * Instead of clearing all references at force_empty, we remember
5980 * the number of reference from swap_cgroup and free mem_cgroup when
5981 * it goes down to 0.
5982 *
5983 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005984 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005985
5986static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005987{
Glauber Costac8b2a362012-12-18 14:22:13 -08005988 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005989 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07005990
Glauber Costac8b2a362012-12-18 14:22:13 -08005991 mem_cgroup_remove_from_trees(memcg);
5992 free_css_id(&mem_cgroup_subsys, &memcg->css);
5993
5994 for_each_node(node)
5995 free_mem_cgroup_per_zone_info(memcg, node);
5996
5997 free_percpu(memcg->stat);
5998
Glauber Costa3f134612012-05-29 15:07:11 -07005999 /*
6000 * We need to make sure that (at least for now), the jump label
6001 * destruction code runs outside of the cgroup lock. This is because
6002 * get_online_cpus(), which is called from the static_branch update,
6003 * can't be called inside the cgroup_lock. cpusets are the ones
6004 * enforcing this dependency, so if they ever change, we might as well.
6005 *
6006 * schedule_work() will guarantee this happens. Be careful if you need
6007 * to move this code around, and make sure it is outside
6008 * the cgroup_lock.
6009 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006010 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006011 if (size < PAGE_SIZE)
6012 kfree(memcg);
6013 else
6014 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006015}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006016
Glauber Costac8b2a362012-12-18 14:22:13 -08006017
6018/*
6019 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
6020 * but in process context. The work_freeing structure is overlaid
6021 * on the rcu_freeing structure, which itself is overlaid on memsw.
6022 */
6023static void free_work(struct work_struct *work)
6024{
6025 struct mem_cgroup *memcg;
6026
6027 memcg = container_of(work, struct mem_cgroup, work_freeing);
6028 __mem_cgroup_free(memcg);
6029}
6030
Glauber Costa3afe36b2012-05-29 15:07:10 -07006031static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006032{
6033 struct mem_cgroup *memcg;
6034
6035 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006036 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006037 schedule_work(&memcg->work_freeing);
6038}
6039
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006040static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006041{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006042 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006043}
6044
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006045static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006046{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006047 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6048 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006049 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006050 if (parent)
6051 mem_cgroup_put(parent);
6052 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006053}
6054
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006055static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006056{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006057 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006058}
6059
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006060/*
6061 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6062 */
Glauber Costae1aab162011-12-11 21:47:03 +00006063struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006064{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006065 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006066 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006067 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006068}
Glauber Costae1aab162011-12-11 21:47:03 +00006069EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006070
Michal Hocko8787a1d2013-02-22 16:35:39 -08006071static void __init mem_cgroup_soft_limit_tree_init(void)
Balbir Singhf64c3f52009-09-23 15:56:37 -07006072{
6073 struct mem_cgroup_tree_per_node *rtpn;
6074 struct mem_cgroup_tree_per_zone *rtpz;
6075 int tmp, node, zone;
6076
Bob Liu3ed28fa2012-01-12 17:19:04 -08006077 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006078 tmp = node;
6079 if (!node_state(node, N_NORMAL_MEMORY))
6080 tmp = -1;
6081 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
Michal Hocko8787a1d2013-02-22 16:35:39 -08006082 BUG_ON(!rtpn);
Balbir Singhf64c3f52009-09-23 15:56:37 -07006083
6084 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6085
6086 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6087 rtpz = &rtpn->rb_tree_per_zone[zone];
6088 rtpz->rb_root = RB_ROOT;
6089 spin_lock_init(&rtpz->lock);
6090 }
6091 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07006092}
6093
Li Zefan0eb253e2009-01-15 13:51:25 -08006094static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006095mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006096{
Glauber Costad142e3e2013-02-22 16:34:52 -08006097 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006098 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006099 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006100
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006101 memcg = mem_cgroup_alloc();
6102 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006103 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006104
Bob Liu3ed28fa2012-01-12 17:19:04 -08006105 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006106 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006107 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006108
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006109 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006110 if (cont->parent == NULL) {
Hillf Dantona41c58a2011-12-19 17:11:57 -08006111 root_mem_cgroup = memcg;
Glauber Costad142e3e2013-02-22 16:34:52 -08006112 res_counter_init(&memcg->res, NULL);
6113 res_counter_init(&memcg->memsw, NULL);
6114 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006115 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006116
Glauber Costad142e3e2013-02-22 16:34:52 -08006117 memcg->last_scanned_node = MAX_NUMNODES;
6118 INIT_LIST_HEAD(&memcg->oom_notify);
6119 atomic_set(&memcg->refcnt, 1);
6120 memcg->move_charge_at_immigrate = 0;
6121 mutex_init(&memcg->thresholds_lock);
6122 spin_lock_init(&memcg->move_lock);
6123
6124 return &memcg->css;
6125
6126free_out:
6127 __mem_cgroup_free(memcg);
6128 return ERR_PTR(error);
6129}
6130
6131static int
6132mem_cgroup_css_online(struct cgroup *cont)
6133{
6134 struct mem_cgroup *memcg, *parent;
6135 int error = 0;
6136
6137 if (!cont->parent)
6138 return 0;
6139
Glauber Costa09998212013-02-22 16:34:55 -08006140 mutex_lock(&memcg_create_mutex);
Glauber Costad142e3e2013-02-22 16:34:52 -08006141 memcg = mem_cgroup_from_cont(cont);
6142 parent = mem_cgroup_from_cont(cont->parent);
6143
6144 memcg->use_hierarchy = parent->use_hierarchy;
6145 memcg->oom_kill_disable = parent->oom_kill_disable;
6146 memcg->swappiness = mem_cgroup_swappiness(parent);
6147
6148 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006149 res_counter_init(&memcg->res, &parent->res);
6150 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006151 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006152
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006153 /*
6154 * We increment refcnt of the parent to ensure that we can
6155 * safely access it on res_counter_charge/uncharge.
6156 * This refcnt will be decremented when freeing this
6157 * mem_cgroup(see mem_cgroup_put).
6158 */
6159 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006160 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006161 res_counter_init(&memcg->res, NULL);
6162 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006163 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006164 /*
6165 * Deeper hierachy with use_hierarchy == false doesn't make
6166 * much sense so let cgroup subsystem know about this
6167 * unfortunate state in our controller.
6168 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006169 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006170 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006171 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006172
6173 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
Glauber Costa09998212013-02-22 16:34:55 -08006174 mutex_unlock(&memcg_create_mutex);
Glauber Costacbe128e32012-04-09 19:36:34 -03006175 if (error) {
6176 /*
6177 * We call put now because our (and parent's) refcnts
6178 * are already in place. mem_cgroup_put() will internally
6179 * call __mem_cgroup_free, so return directly
6180 */
6181 mem_cgroup_put(memcg);
Glauber Costae4715f02013-02-22 16:34:57 -08006182 if (parent->use_hierarchy)
6183 mem_cgroup_put(parent);
Glauber Costacbe128e32012-04-09 19:36:34 -03006184 }
Glauber Costad142e3e2013-02-22 16:34:52 -08006185 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006186}
6187
Tejun Heo92fb9742012-11-19 08:13:38 -08006188static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006189{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006190 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006191
Michal Hockoab5196c2012-10-26 13:37:32 +02006192 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006193 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006194}
6195
Tejun Heo92fb9742012-11-19 08:13:38 -08006196static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006197{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006198 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006199
Glauber Costa1d62e432012-04-09 19:36:33 -03006200 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006201
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006202 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006203}
6204
Daisuke Nishimura02491442010-03-10 15:22:17 -08006205#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006206/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006207#define PRECHARGE_COUNT_AT_ONCE 256
6208static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006209{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006210 int ret = 0;
6211 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006212 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006213
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006214 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006215 mc.precharge += count;
6216 /* we don't need css_get for root */
6217 return ret;
6218 }
6219 /* try to charge at once */
6220 if (count > 1) {
6221 struct res_counter *dummy;
6222 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006223 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006224 * by cgroup_lock_live_cgroup() that it is not removed and we
6225 * are still under the same cgroup_mutex. So we can postpone
6226 * css_get().
6227 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006228 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006229 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006230 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006231 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006232 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006233 goto one_by_one;
6234 }
6235 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006236 return ret;
6237 }
6238one_by_one:
6239 /* fall back to one by one charge */
6240 while (count--) {
6241 if (signal_pending(current)) {
6242 ret = -EINTR;
6243 break;
6244 }
6245 if (!batch_count--) {
6246 batch_count = PRECHARGE_COUNT_AT_ONCE;
6247 cond_resched();
6248 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006249 ret = __mem_cgroup_try_charge(NULL,
6250 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006251 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006252 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006253 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006254 mc.precharge++;
6255 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006256 return ret;
6257}
6258
6259/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006260 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006261 * @vma: the vma the pte to be checked belongs
6262 * @addr: the address corresponding to the pte to be checked
6263 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006264 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006265 *
6266 * Returns
6267 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6268 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6269 * move charge. if @target is not NULL, the page is stored in target->page
6270 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006271 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6272 * target for charge migration. if @target is not NULL, the entry is stored
6273 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006274 *
6275 * Called with pte lock held.
6276 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006277union mc_target {
6278 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006279 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006280};
6281
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006282enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006283 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006284 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006285 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006286};
6287
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006288static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6289 unsigned long addr, pte_t ptent)
6290{
6291 struct page *page = vm_normal_page(vma, addr, ptent);
6292
6293 if (!page || !page_mapped(page))
6294 return NULL;
6295 if (PageAnon(page)) {
6296 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006297 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006298 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006299 } else if (!move_file())
6300 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006301 return NULL;
6302 if (!get_page_unless_zero(page))
6303 return NULL;
6304
6305 return page;
6306}
6307
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006308#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006309static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6310 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6311{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006312 struct page *page = NULL;
6313 swp_entry_t ent = pte_to_swp_entry(ptent);
6314
6315 if (!move_anon() || non_swap_entry(ent))
6316 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006317 /*
6318 * Because lookup_swap_cache() updates some statistics counter,
6319 * we call find_get_page() with swapper_space directly.
6320 */
Shaohua Li33806f02013-02-22 16:34:37 -08006321 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006322 if (do_swap_account)
6323 entry->val = ent.val;
6324
6325 return page;
6326}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006327#else
6328static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6329 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6330{
6331 return NULL;
6332}
6333#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006334
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006335static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6336 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6337{
6338 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006339 struct address_space *mapping;
6340 pgoff_t pgoff;
6341
6342 if (!vma->vm_file) /* anonymous vma */
6343 return NULL;
6344 if (!move_file())
6345 return NULL;
6346
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006347 mapping = vma->vm_file->f_mapping;
6348 if (pte_none(ptent))
6349 pgoff = linear_page_index(vma, addr);
6350 else /* pte_file(ptent) is true */
6351 pgoff = pte_to_pgoff(ptent);
6352
6353 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006354 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006355
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006356#ifdef CONFIG_SWAP
6357 /* shmem/tmpfs may report page out on swap: account for that too. */
6358 if (radix_tree_exceptional_entry(page)) {
6359 swp_entry_t swap = radix_to_swp_entry(page);
6360 if (do_swap_account)
6361 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006362 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006363 }
6364#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006365 return page;
6366}
6367
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006368static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006369 unsigned long addr, pte_t ptent, union mc_target *target)
6370{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006371 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006372 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006373 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006374 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006375
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006376 if (pte_present(ptent))
6377 page = mc_handle_present_pte(vma, addr, ptent);
6378 else if (is_swap_pte(ptent))
6379 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006380 else if (pte_none(ptent) || pte_file(ptent))
6381 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006382
6383 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006384 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006385 if (page) {
6386 pc = lookup_page_cgroup(page);
6387 /*
6388 * Do only loose check w/o page_cgroup lock.
6389 * mem_cgroup_move_account() checks the pc is valid or not under
6390 * the lock.
6391 */
6392 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6393 ret = MC_TARGET_PAGE;
6394 if (target)
6395 target->page = page;
6396 }
6397 if (!ret || !target)
6398 put_page(page);
6399 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006400 /* There is a swap entry and a page doesn't exist or isn't charged */
6401 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006402 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006403 ret = MC_TARGET_SWAP;
6404 if (target)
6405 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006406 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006407 return ret;
6408}
6409
Naoya Horiguchi12724852012-03-21 16:34:28 -07006410#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6411/*
6412 * We don't consider swapping or file mapped pages because THP does not
6413 * support them for now.
6414 * Caller should make sure that pmd_trans_huge(pmd) is true.
6415 */
6416static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6417 unsigned long addr, pmd_t pmd, union mc_target *target)
6418{
6419 struct page *page = NULL;
6420 struct page_cgroup *pc;
6421 enum mc_target_type ret = MC_TARGET_NONE;
6422
6423 page = pmd_page(pmd);
6424 VM_BUG_ON(!page || !PageHead(page));
6425 if (!move_anon())
6426 return ret;
6427 pc = lookup_page_cgroup(page);
6428 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6429 ret = MC_TARGET_PAGE;
6430 if (target) {
6431 get_page(page);
6432 target->page = page;
6433 }
6434 }
6435 return ret;
6436}
6437#else
6438static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6439 unsigned long addr, pmd_t pmd, union mc_target *target)
6440{
6441 return MC_TARGET_NONE;
6442}
6443#endif
6444
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006445static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6446 unsigned long addr, unsigned long end,
6447 struct mm_walk *walk)
6448{
6449 struct vm_area_struct *vma = walk->private;
6450 pte_t *pte;
6451 spinlock_t *ptl;
6452
Naoya Horiguchi12724852012-03-21 16:34:28 -07006453 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6454 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6455 mc.precharge += HPAGE_PMD_NR;
6456 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006457 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006458 }
Dave Hansen03319322011-03-22 16:32:56 -07006459
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006460 if (pmd_trans_unstable(pmd))
6461 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006462 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6463 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006464 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006465 mc.precharge++; /* increment precharge temporarily */
6466 pte_unmap_unlock(pte - 1, ptl);
6467 cond_resched();
6468
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006469 return 0;
6470}
6471
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006472static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6473{
6474 unsigned long precharge;
6475 struct vm_area_struct *vma;
6476
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006477 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006478 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6479 struct mm_walk mem_cgroup_count_precharge_walk = {
6480 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6481 .mm = mm,
6482 .private = vma,
6483 };
6484 if (is_vm_hugetlb_page(vma))
6485 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006486 walk_page_range(vma->vm_start, vma->vm_end,
6487 &mem_cgroup_count_precharge_walk);
6488 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006489 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006490
6491 precharge = mc.precharge;
6492 mc.precharge = 0;
6493
6494 return precharge;
6495}
6496
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006497static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6498{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006499 unsigned long precharge = mem_cgroup_count_precharge(mm);
6500
6501 VM_BUG_ON(mc.moving_task);
6502 mc.moving_task = current;
6503 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006504}
6505
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006506/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6507static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006508{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006509 struct mem_cgroup *from = mc.from;
6510 struct mem_cgroup *to = mc.to;
6511
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006512 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006513 if (mc.precharge) {
6514 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6515 mc.precharge = 0;
6516 }
6517 /*
6518 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6519 * we must uncharge here.
6520 */
6521 if (mc.moved_charge) {
6522 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6523 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006524 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006525 /* we must fixup refcnts and charges */
6526 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006527 /* uncharge swap account from the old cgroup */
6528 if (!mem_cgroup_is_root(mc.from))
6529 res_counter_uncharge(&mc.from->memsw,
6530 PAGE_SIZE * mc.moved_swap);
6531 __mem_cgroup_put(mc.from, mc.moved_swap);
6532
6533 if (!mem_cgroup_is_root(mc.to)) {
6534 /*
6535 * we charged both to->res and to->memsw, so we should
6536 * uncharge to->res.
6537 */
6538 res_counter_uncharge(&mc.to->res,
6539 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006540 }
6541 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006542 mc.moved_swap = 0;
6543 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006544 memcg_oom_recover(from);
6545 memcg_oom_recover(to);
6546 wake_up_all(&mc.waitq);
6547}
6548
6549static void mem_cgroup_clear_mc(void)
6550{
6551 struct mem_cgroup *from = mc.from;
6552
6553 /*
6554 * we must clear moving_task before waking up waiters at the end of
6555 * task migration.
6556 */
6557 mc.moving_task = NULL;
6558 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006559 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006560 mc.from = NULL;
6561 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006562 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006563 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006564}
6565
Li Zefan761b3ef2012-01-31 13:47:36 +08006566static int mem_cgroup_can_attach(struct cgroup *cgroup,
6567 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006568{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006569 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006570 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006571 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Glauber Costaee5e8472013-02-22 16:34:50 -08006572 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006573
Glauber Costaee5e8472013-02-22 16:34:50 -08006574 /*
6575 * We are now commited to this value whatever it is. Changes in this
6576 * tunable will only affect upcoming migrations, not the current one.
6577 * So we need to save it, and keep it going.
6578 */
6579 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6580 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006581 struct mm_struct *mm;
6582 struct mem_cgroup *from = mem_cgroup_from_task(p);
6583
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006584 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006585
6586 mm = get_task_mm(p);
6587 if (!mm)
6588 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006589 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006590 if (mm->owner == p) {
6591 VM_BUG_ON(mc.from);
6592 VM_BUG_ON(mc.to);
6593 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006594 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006595 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006596 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006597 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006598 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006599 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006600 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006601 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006602 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006603
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006604 ret = mem_cgroup_precharge_mc(mm);
6605 if (ret)
6606 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006607 }
6608 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006609 }
6610 return ret;
6611}
6612
Li Zefan761b3ef2012-01-31 13:47:36 +08006613static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6614 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006615{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006616 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006617}
6618
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006619static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6620 unsigned long addr, unsigned long end,
6621 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006622{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006623 int ret = 0;
6624 struct vm_area_struct *vma = walk->private;
6625 pte_t *pte;
6626 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006627 enum mc_target_type target_type;
6628 union mc_target target;
6629 struct page *page;
6630 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006631
Naoya Horiguchi12724852012-03-21 16:34:28 -07006632 /*
6633 * We don't take compound_lock() here but no race with splitting thp
6634 * happens because:
6635 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6636 * under splitting, which means there's no concurrent thp split,
6637 * - if another thread runs into split_huge_page() just after we
6638 * entered this if-block, the thread must wait for page table lock
6639 * to be unlocked in __split_huge_page_splitting(), where the main
6640 * part of thp split is not executed yet.
6641 */
6642 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006643 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006644 spin_unlock(&vma->vm_mm->page_table_lock);
6645 return 0;
6646 }
6647 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6648 if (target_type == MC_TARGET_PAGE) {
6649 page = target.page;
6650 if (!isolate_lru_page(page)) {
6651 pc = lookup_page_cgroup(page);
6652 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006653 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006654 mc.precharge -= HPAGE_PMD_NR;
6655 mc.moved_charge += HPAGE_PMD_NR;
6656 }
6657 putback_lru_page(page);
6658 }
6659 put_page(page);
6660 }
6661 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006662 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006663 }
6664
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006665 if (pmd_trans_unstable(pmd))
6666 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006667retry:
6668 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6669 for (; addr != end; addr += PAGE_SIZE) {
6670 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006671 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006672
6673 if (!mc.precharge)
6674 break;
6675
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006676 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006677 case MC_TARGET_PAGE:
6678 page = target.page;
6679 if (isolate_lru_page(page))
6680 goto put;
6681 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006682 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006683 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006684 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006685 /* we uncharge from mc.from later. */
6686 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006687 }
6688 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006689put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006690 put_page(page);
6691 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006692 case MC_TARGET_SWAP:
6693 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006694 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006695 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006696 /* we fixup refcnts and charges later. */
6697 mc.moved_swap++;
6698 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006699 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006700 default:
6701 break;
6702 }
6703 }
6704 pte_unmap_unlock(pte - 1, ptl);
6705 cond_resched();
6706
6707 if (addr != end) {
6708 /*
6709 * We have consumed all precharges we got in can_attach().
6710 * We try charge one by one, but don't do any additional
6711 * charges to mc.to if we have failed in charge once in attach()
6712 * phase.
6713 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006714 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006715 if (!ret)
6716 goto retry;
6717 }
6718
6719 return ret;
6720}
6721
6722static void mem_cgroup_move_charge(struct mm_struct *mm)
6723{
6724 struct vm_area_struct *vma;
6725
6726 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006727retry:
6728 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6729 /*
6730 * Someone who are holding the mmap_sem might be waiting in
6731 * waitq. So we cancel all extra charges, wake up all waiters,
6732 * and retry. Because we cancel precharges, we might not be able
6733 * to move enough charges, but moving charge is a best-effort
6734 * feature anyway, so it wouldn't be a big problem.
6735 */
6736 __mem_cgroup_clear_mc();
6737 cond_resched();
6738 goto retry;
6739 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006740 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6741 int ret;
6742 struct mm_walk mem_cgroup_move_charge_walk = {
6743 .pmd_entry = mem_cgroup_move_charge_pte_range,
6744 .mm = mm,
6745 .private = vma,
6746 };
6747 if (is_vm_hugetlb_page(vma))
6748 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006749 ret = walk_page_range(vma->vm_start, vma->vm_end,
6750 &mem_cgroup_move_charge_walk);
6751 if (ret)
6752 /*
6753 * means we have consumed all precharges and failed in
6754 * doing additional charge. Just abandon here.
6755 */
6756 break;
6757 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006758 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006759}
6760
Li Zefan761b3ef2012-01-31 13:47:36 +08006761static void mem_cgroup_move_task(struct cgroup *cont,
6762 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006763{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006764 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006765 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006766
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006767 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006768 if (mc.to)
6769 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006770 mmput(mm);
6771 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006772 if (mc.to)
6773 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006774}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006775#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006776static int mem_cgroup_can_attach(struct cgroup *cgroup,
6777 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006778{
6779 return 0;
6780}
Li Zefan761b3ef2012-01-31 13:47:36 +08006781static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6782 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006783{
6784}
Li Zefan761b3ef2012-01-31 13:47:36 +08006785static void mem_cgroup_move_task(struct cgroup *cont,
6786 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006787{
6788}
6789#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006790
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006791struct cgroup_subsys mem_cgroup_subsys = {
6792 .name = "memory",
6793 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006794 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006795 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006796 .css_offline = mem_cgroup_css_offline,
6797 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006798 .can_attach = mem_cgroup_can_attach,
6799 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006800 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006801 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006802 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006803 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006804};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006805
Andrew Mortonc255a452012-07-31 16:43:02 -07006806#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006807static int __init enable_swap_account(char *s)
6808{
6809 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006810 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006811 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006812 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006813 really_do_swap_account = 0;
6814 return 1;
6815}
Michal Hockoa2c89902011-05-24 17:12:50 -07006816__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006817
Michal Hocko2d110852013-02-22 16:34:43 -08006818static void __init memsw_file_init(void)
6819{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006820 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006821}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006822
6823static void __init enable_swap_cgroup(void)
6824{
6825 if (!mem_cgroup_disabled() && really_do_swap_account) {
6826 do_swap_account = 1;
6827 memsw_file_init();
6828 }
6829}
6830
Michal Hocko2d110852013-02-22 16:34:43 -08006831#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006832static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006833{
6834}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006835#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006836
6837/*
Michal Hocko10813122013-02-22 16:35:41 -08006838 * subsys_initcall() for memory controller.
6839 *
6840 * Some parts like hotcpu_notifier() have to be initialized from this context
6841 * because of lock dependencies (cgroup_lock -> cpu hotplug) but basically
6842 * everything that doesn't depend on a specific mem_cgroup structure should
6843 * be initialized from here.
Michal Hocko2d110852013-02-22 16:34:43 -08006844 */
6845static int __init mem_cgroup_init(void)
6846{
6847 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006848 enable_swap_cgroup();
Michal Hocko8787a1d2013-02-22 16:35:39 -08006849 mem_cgroup_soft_limit_tree_init();
Michal Hockoe4777492013-02-22 16:35:40 -08006850 memcg_stock_init();
Michal Hocko2d110852013-02-22 16:34:43 -08006851 return 0;
6852}
6853subsys_initcall(mem_cgroup_init);