blob: 2bc3fbe931549bd728034b4c014531119c97e1e3 [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
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700491static void mem_cgroup_get(struct mem_cgroup *memcg);
492static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000493
Wanpeng Lib2145142012-07-31 16:46:01 -0700494static inline
495struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
496{
497 return container_of(s, struct mem_cgroup, css);
498}
499
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700500static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
501{
502 return (memcg == root_mem_cgroup);
503}
504
Glauber Costae1aab162011-12-11 21:47:03 +0000505/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700506#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000507
Glauber Costae1aab162011-12-11 21:47:03 +0000508void sock_update_memcg(struct sock *sk)
509{
Glauber Costa376be5f2012-01-20 04:57:14 +0000510 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000511 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700512 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000513
514 BUG_ON(!sk->sk_prot->proto_cgroup);
515
Glauber Costaf3f511e2012-01-05 20:16:39 +0000516 /* Socket cloning can throw us here with sk_cgrp already
517 * filled. It won't however, necessarily happen from
518 * process context. So the test for root memcg given
519 * the current task's memcg won't help us in this case.
520 *
521 * Respecting the original socket's memcg is a better
522 * decision in this case.
523 */
524 if (sk->sk_cgrp) {
525 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
526 mem_cgroup_get(sk->sk_cgrp->memcg);
527 return;
528 }
529
Glauber Costae1aab162011-12-11 21:47:03 +0000530 rcu_read_lock();
531 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700532 cg_proto = sk->sk_prot->proto_cgroup(memcg);
533 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000534 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700535 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000536 }
537 rcu_read_unlock();
538 }
539}
540EXPORT_SYMBOL(sock_update_memcg);
541
542void sock_release_memcg(struct sock *sk)
543{
Glauber Costa376be5f2012-01-20 04:57:14 +0000544 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000545 struct mem_cgroup *memcg;
546 WARN_ON(!sk->sk_cgrp->memcg);
547 memcg = sk->sk_cgrp->memcg;
548 mem_cgroup_put(memcg);
549 }
550}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000551
552struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
553{
554 if (!memcg || mem_cgroup_is_root(memcg))
555 return NULL;
556
557 return &memcg->tcp_mem.cg_proto;
558}
559EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000560
Glauber Costa3f134612012-05-29 15:07:11 -0700561static void disarm_sock_keys(struct mem_cgroup *memcg)
562{
563 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
564 return;
565 static_key_slow_dec(&memcg_socket_limit_enabled);
566}
567#else
568static void disarm_sock_keys(struct mem_cgroup *memcg)
569{
570}
571#endif
572
Glauber Costaa8964b92012-12-18 14:22:09 -0800573#ifdef CONFIG_MEMCG_KMEM
Glauber Costa55007d82012-12-18 14:22:38 -0800574/*
575 * This will be the memcg's index in each cache's ->memcg_params->memcg_caches.
576 * There are two main reasons for not using the css_id for this:
577 * 1) this works better in sparse environments, where we have a lot of memcgs,
578 * but only a few kmem-limited. Or also, if we have, for instance, 200
579 * memcgs, and none but the 200th is kmem-limited, we'd have to have a
580 * 200 entry array for that.
581 *
582 * 2) In order not to violate the cgroup API, we would like to do all memory
583 * allocation in ->create(). At that point, we haven't yet allocated the
584 * css_id. Having a separate index prevents us from messing with the cgroup
585 * core for this
586 *
587 * The current size of the caches array is stored in
588 * memcg_limited_groups_array_size. It will double each time we have to
589 * increase it.
590 */
591static DEFINE_IDA(kmem_limited_groups);
Glauber Costa749c5412012-12-18 14:23:01 -0800592int memcg_limited_groups_array_size;
593
Glauber Costa55007d82012-12-18 14:22:38 -0800594/*
595 * MIN_SIZE is different than 1, because we would like to avoid going through
596 * the alloc/free process all the time. In a small machine, 4 kmem-limited
597 * cgroups is a reasonable guess. In the future, it could be a parameter or
598 * tunable, but that is strictly not necessary.
599 *
600 * MAX_SIZE should be as large as the number of css_ids. Ideally, we could get
601 * this constant directly from cgroup, but it is understandable that this is
602 * better kept as an internal representation in cgroup.c. In any case, the
603 * css_id space is not getting any smaller, and we don't have to necessarily
604 * increase ours as well if it increases.
605 */
606#define MEMCG_CACHES_MIN_SIZE 4
607#define MEMCG_CACHES_MAX_SIZE 65535
608
Glauber Costad7f25f82012-12-18 14:22:40 -0800609/*
610 * A lot of the calls to the cache allocation functions are expected to be
611 * inlined by the compiler. Since the calls to memcg_kmem_get_cache are
612 * conditional to this static branch, we'll have to allow modules that does
613 * kmem_cache_alloc and the such to see this symbol as well
614 */
Glauber Costaa8964b92012-12-18 14:22:09 -0800615struct static_key memcg_kmem_enabled_key;
Glauber Costad7f25f82012-12-18 14:22:40 -0800616EXPORT_SYMBOL(memcg_kmem_enabled_key);
Glauber Costaa8964b92012-12-18 14:22:09 -0800617
618static void disarm_kmem_keys(struct mem_cgroup *memcg)
619{
Glauber Costa55007d82012-12-18 14:22:38 -0800620 if (memcg_kmem_is_active(memcg)) {
Glauber Costaa8964b92012-12-18 14:22:09 -0800621 static_key_slow_dec(&memcg_kmem_enabled_key);
Glauber Costa55007d82012-12-18 14:22:38 -0800622 ida_simple_remove(&kmem_limited_groups, memcg->kmemcg_id);
623 }
Glauber Costabea207c2012-12-18 14:22:11 -0800624 /*
625 * This check can't live in kmem destruction function,
626 * since the charges will outlive the cgroup
627 */
628 WARN_ON(res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0);
Glauber Costaa8964b92012-12-18 14:22:09 -0800629}
630#else
631static void disarm_kmem_keys(struct mem_cgroup *memcg)
632{
633}
634#endif /* CONFIG_MEMCG_KMEM */
635
636static void disarm_static_keys(struct mem_cgroup *memcg)
637{
638 disarm_sock_keys(memcg);
639 disarm_kmem_keys(memcg);
640}
641
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700642static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800643
Balbir Singhf64c3f52009-09-23 15:56:37 -0700644static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700645mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700646{
Glauber Costa45cf7eb2013-02-22 16:34:49 -0800647 VM_BUG_ON((unsigned)nid >= nr_node_ids);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700648 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700649}
650
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700651struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100652{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700653 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100654}
655
Balbir Singhf64c3f52009-09-23 15:56:37 -0700656static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700657page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700658{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700659 int nid = page_to_nid(page);
660 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700661
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700662 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700663}
664
665static struct mem_cgroup_tree_per_zone *
666soft_limit_tree_node_zone(int nid, int zid)
667{
668 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
669}
670
671static struct mem_cgroup_tree_per_zone *
672soft_limit_tree_from_page(struct page *page)
673{
674 int nid = page_to_nid(page);
675 int zid = page_zonenum(page);
676
677 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
678}
679
680static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700681__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700682 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700683 struct mem_cgroup_tree_per_zone *mctz,
684 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700685{
686 struct rb_node **p = &mctz->rb_root.rb_node;
687 struct rb_node *parent = NULL;
688 struct mem_cgroup_per_zone *mz_node;
689
690 if (mz->on_tree)
691 return;
692
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700693 mz->usage_in_excess = new_usage_in_excess;
694 if (!mz->usage_in_excess)
695 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700696 while (*p) {
697 parent = *p;
698 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
699 tree_node);
700 if (mz->usage_in_excess < mz_node->usage_in_excess)
701 p = &(*p)->rb_left;
702 /*
703 * We can't avoid mem cgroups that are over their soft
704 * limit by the same amount
705 */
706 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
707 p = &(*p)->rb_right;
708 }
709 rb_link_node(&mz->tree_node, parent, p);
710 rb_insert_color(&mz->tree_node, &mctz->rb_root);
711 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700712}
713
714static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700715__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700716 struct mem_cgroup_per_zone *mz,
717 struct mem_cgroup_tree_per_zone *mctz)
718{
719 if (!mz->on_tree)
720 return;
721 rb_erase(&mz->tree_node, &mctz->rb_root);
722 mz->on_tree = false;
723}
724
725static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700726mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700727 struct mem_cgroup_per_zone *mz,
728 struct mem_cgroup_tree_per_zone *mctz)
729{
730 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700731 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700732 spin_unlock(&mctz->lock);
733}
734
Balbir Singhf64c3f52009-09-23 15:56:37 -0700735
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700736static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700737{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700738 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700739 struct mem_cgroup_per_zone *mz;
740 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700741 int nid = page_to_nid(page);
742 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700743 mctz = soft_limit_tree_from_page(page);
744
745 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700746 * Necessary to update all ancestors when hierarchy is used.
747 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700748 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700749 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
750 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
751 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700752 /*
753 * We have to update the tree if mz is on RB-tree or
754 * mem is over its softlimit.
755 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700756 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700757 spin_lock(&mctz->lock);
758 /* if on-tree, remove it */
759 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700760 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700761 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700762 * Insert again. mz->usage_in_excess will be updated.
763 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700764 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700765 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700766 spin_unlock(&mctz->lock);
767 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700768 }
769}
770
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700771static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700772{
773 int node, zone;
774 struct mem_cgroup_per_zone *mz;
775 struct mem_cgroup_tree_per_zone *mctz;
776
Bob Liu3ed28fa2012-01-12 17:19:04 -0800777 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700778 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700779 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700780 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700781 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700782 }
783 }
784}
785
Balbir Singh4e416952009-09-23 15:56:39 -0700786static struct mem_cgroup_per_zone *
787__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
788{
789 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700790 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700791
792retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700793 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700794 rightmost = rb_last(&mctz->rb_root);
795 if (!rightmost)
796 goto done; /* Nothing to reclaim from */
797
798 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
799 /*
800 * Remove the node now but someone else can add it back,
801 * we will to add it back at the end of reclaim to its correct
802 * position in the tree.
803 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700804 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
805 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
806 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700807 goto retry;
808done:
809 return mz;
810}
811
812static struct mem_cgroup_per_zone *
813mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
814{
815 struct mem_cgroup_per_zone *mz;
816
817 spin_lock(&mctz->lock);
818 mz = __mem_cgroup_largest_soft_limit_node(mctz);
819 spin_unlock(&mctz->lock);
820 return mz;
821}
822
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700823/*
824 * Implementation Note: reading percpu statistics for memcg.
825 *
826 * Both of vmstat[] and percpu_counter has threshold and do periodic
827 * synchronization to implement "quick" read. There are trade-off between
828 * reading cost and precision of value. Then, we may have a chance to implement
829 * a periodic synchronizion of counter in memcg's counter.
830 *
831 * But this _read() function is used for user interface now. The user accounts
832 * memory usage by memory cgroup and he _always_ requires exact value because
833 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
834 * have to visit all online cpus and make sum. So, for now, unnecessary
835 * synchronization is not implemented. (just implemented for cpu hotplug)
836 *
837 * If there are kernel internal actions which can make use of some not-exact
838 * value, and reading all cpu value can be performance bottleneck in some
839 * common workload, threashold and synchonization as vmstat[] should be
840 * implemented.
841 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700842static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700843 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800844{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700845 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800846 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800847
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700848 get_online_cpus();
849 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700850 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700851#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700852 spin_lock(&memcg->pcp_counter_lock);
853 val += memcg->nocpu_base.count[idx];
854 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700855#endif
856 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800857 return val;
858}
859
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700860static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700861 bool charge)
862{
863 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700864 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700865}
866
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700867static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700868 enum mem_cgroup_events_index idx)
869{
870 unsigned long val = 0;
871 int cpu;
872
873 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700874 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700875#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700876 spin_lock(&memcg->pcp_counter_lock);
877 val += memcg->nocpu_base.events[idx];
878 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700879#endif
880 return val;
881}
882
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700883static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700884 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800885{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800886 preempt_disable();
887
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700888 /*
889 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
890 * counted as CACHE even if it's on ANON LRU.
891 */
892 if (anon)
893 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700894 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800895 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700896 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700897 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700898
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800899 /* pagein of a big page is an event. So, ignore page size */
900 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700901 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800902 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700903 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800904 nr_pages = -nr_pages; /* for event */
905 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800906
Johannes Weiner13114712012-05-29 15:07:07 -0700907 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800908
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800909 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800910}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800911
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700912unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700913mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700914{
915 struct mem_cgroup_per_zone *mz;
916
917 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
918 return mz->lru_size[lru];
919}
920
921static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700922mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700923 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700924{
925 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700926 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700927 unsigned long ret = 0;
928
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700929 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700930
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700931 for_each_lru(lru) {
932 if (BIT(lru) & lru_mask)
933 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700934 }
935 return ret;
936}
937
938static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700939mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700940 int nid, unsigned int lru_mask)
941{
Ying Han889976d2011-05-26 16:25:33 -0700942 u64 total = 0;
943 int zid;
944
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700945 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700946 total += mem_cgroup_zone_nr_lru_pages(memcg,
947 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700948
Ying Han889976d2011-05-26 16:25:33 -0700949 return total;
950}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700951
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700952static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700953 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800954{
Ying Han889976d2011-05-26 16:25:33 -0700955 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800956 u64 total = 0;
957
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800958 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700959 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800960 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800961}
962
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800963static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
964 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800965{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700966 unsigned long val, next;
967
Johannes Weiner13114712012-05-29 15:07:07 -0700968 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700969 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700970 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800971 if ((long)next - (long)val < 0) {
972 switch (target) {
973 case MEM_CGROUP_TARGET_THRESH:
974 next = val + THRESHOLDS_EVENTS_TARGET;
975 break;
976 case MEM_CGROUP_TARGET_SOFTLIMIT:
977 next = val + SOFTLIMIT_EVENTS_TARGET;
978 break;
979 case MEM_CGROUP_TARGET_NUMAINFO:
980 next = val + NUMAINFO_EVENTS_TARGET;
981 break;
982 default:
983 break;
984 }
985 __this_cpu_write(memcg->stat->targets[target], next);
986 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700987 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800988 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800989}
990
991/*
992 * Check events in order.
993 *
994 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700995static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800996{
Steven Rostedt47994012011-11-02 13:38:33 -0700997 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800998 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800999 if (unlikely(mem_cgroup_event_ratelimit(memcg,
1000 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -08001001 bool do_softlimit;
1002 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001003
1004 do_softlimit = mem_cgroup_event_ratelimit(memcg,
1005 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001006#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001007 do_numainfo = mem_cgroup_event_ratelimit(memcg,
1008 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001009#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -08001010 preempt_enable();
1011
1012 mem_cgroup_threshold(memcg);
1013 if (unlikely(do_softlimit))
1014 mem_cgroup_update_tree(memcg, page);
1015#if MAX_NUMNODES > 1
1016 if (unlikely(do_numainfo))
1017 atomic_inc(&memcg->numainfo_events);
1018#endif
1019 } else
1020 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001021}
1022
Glauber Costad1a4c0b2011-12-11 21:47:04 +00001023struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001024{
Wanpeng Lib2145142012-07-31 16:46:01 -07001025 return mem_cgroup_from_css(
1026 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001027}
1028
Balbir Singhcf475ad2008-04-29 01:00:16 -07001029struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001030{
Balbir Singh31a78f22008-09-28 23:09:31 +01001031 /*
1032 * mm_update_next_owner() may clear mm->owner to NULL
1033 * if it races with swapoff, page migration, etc.
1034 * So this can be called with p == NULL.
1035 */
1036 if (unlikely(!p))
1037 return NULL;
1038
Wanpeng Lib2145142012-07-31 16:46:01 -07001039 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001040}
1041
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07001042struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001043{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001044 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001045
1046 if (!mm)
1047 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001048 /*
1049 * Because we have no locks, mm->owner's may be being moved to other
1050 * cgroup. We use css_tryget() here even if this looks
1051 * pessimistic (rather than adding locks here).
1052 */
1053 rcu_read_lock();
1054 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001055 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1056 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001057 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001058 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001059 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001060 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001061}
1062
Johannes Weiner56600482012-01-12 17:17:59 -08001063/**
1064 * mem_cgroup_iter - iterate over memory cgroup hierarchy
1065 * @root: hierarchy root
1066 * @prev: previously returned memcg, NULL on first invocation
1067 * @reclaim: cookie for shared reclaim walks, NULL for full walks
1068 *
1069 * Returns references to children of the hierarchy below @root, or
1070 * @root itself, or %NULL after a full round-trip.
1071 *
1072 * Caller must pass the return value in @prev on subsequent
1073 * invocations for reference counting, or use mem_cgroup_iter_break()
1074 * to cancel a hierarchy walk before the round-trip is complete.
1075 *
1076 * Reclaimers can specify a zone and a priority level in @reclaim to
1077 * divide up the memcgs in the hierarchy among all concurrent
1078 * reclaimers operating on the same zone and priority.
1079 */
1080struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
1081 struct mem_cgroup *prev,
1082 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07001083{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001084 struct mem_cgroup *memcg = NULL;
1085 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001086
Johannes Weiner56600482012-01-12 17:17:59 -08001087 if (mem_cgroup_disabled())
1088 return NULL;
1089
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07001090 if (!root)
1091 root = root_mem_cgroup;
1092
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001093 if (prev && !reclaim)
1094 id = css_id(&prev->css);
1095
1096 if (prev && prev != root)
1097 css_put(&prev->css);
1098
1099 if (!root->use_hierarchy && root != root_mem_cgroup) {
1100 if (prev)
1101 return NULL;
1102 return root;
1103 }
1104
1105 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001106 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001107 struct cgroup_subsys_state *css;
1108
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001109 if (reclaim) {
1110 int nid = zone_to_nid(reclaim->zone);
1111 int zid = zone_idx(reclaim->zone);
1112 struct mem_cgroup_per_zone *mz;
1113
1114 mz = mem_cgroup_zoneinfo(root, nid, zid);
1115 iter = &mz->reclaim_iter[reclaim->priority];
1116 if (prev && reclaim->generation != iter->generation)
1117 return NULL;
1118 id = iter->position;
1119 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001120
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001121 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001122 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1123 if (css) {
1124 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001125 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001126 } else
1127 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001128 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001129
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001130 if (reclaim) {
1131 iter->position = id;
1132 if (!css)
1133 iter->generation++;
1134 else if (!prev && memcg)
1135 reclaim->generation = iter->generation;
1136 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001137
1138 if (prev && !css)
1139 return NULL;
1140 }
1141 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001142}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001143
Johannes Weiner56600482012-01-12 17:17:59 -08001144/**
1145 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1146 * @root: hierarchy root
1147 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1148 */
1149void mem_cgroup_iter_break(struct mem_cgroup *root,
1150 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001151{
1152 if (!root)
1153 root = root_mem_cgroup;
1154 if (prev && prev != root)
1155 css_put(&prev->css);
1156}
1157
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001158/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001159 * Iteration constructs for visiting all cgroups (under a tree). If
1160 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1161 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001162 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001163#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001164 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001165 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001166 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001167
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001168#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001169 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001170 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001171 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001172
David Rientjes68ae5642012-12-12 13:51:57 -08001173void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001174{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001175 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001176
Ying Han456f9982011-05-26 16:25:38 -07001177 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001178 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1179 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001180 goto out;
1181
1182 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001183 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001184 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1185 break;
1186 case PGMAJFAULT:
1187 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001188 break;
1189 default:
1190 BUG();
1191 }
1192out:
1193 rcu_read_unlock();
1194}
David Rientjes68ae5642012-12-12 13:51:57 -08001195EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001196
Johannes Weiner925b7672012-01-12 17:18:15 -08001197/**
1198 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1199 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001200 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001201 *
1202 * Returns the lru list vector holding pages for the given @zone and
1203 * @mem. This can be the global zone lruvec, if the memory controller
1204 * is disabled.
1205 */
1206struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1207 struct mem_cgroup *memcg)
1208{
1209 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001210 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001211
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001212 if (mem_cgroup_disabled()) {
1213 lruvec = &zone->lruvec;
1214 goto out;
1215 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001216
1217 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001218 lruvec = &mz->lruvec;
1219out:
1220 /*
1221 * Since a node can be onlined after the mem_cgroup was created,
1222 * we have to be prepared to initialize lruvec->zone here;
1223 * and if offlined then reonlined, we need to reinitialize it.
1224 */
1225 if (unlikely(lruvec->zone != zone))
1226 lruvec->zone = zone;
1227 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001228}
1229
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001230/*
1231 * Following LRU functions are allowed to be used without PCG_LOCK.
1232 * Operations are called by routine of global LRU independently from memcg.
1233 * What we have to take care of here is validness of pc->mem_cgroup.
1234 *
1235 * Changes to pc->mem_cgroup happens when
1236 * 1. charge
1237 * 2. moving account
1238 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1239 * It is added to LRU before charge.
1240 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1241 * When moving account, the page is not on LRU. It's isolated.
1242 */
1243
Johannes Weiner925b7672012-01-12 17:18:15 -08001244/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001245 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001246 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001247 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001248 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001249struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001250{
1251 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001252 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001253 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001254 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001255
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001256 if (mem_cgroup_disabled()) {
1257 lruvec = &zone->lruvec;
1258 goto out;
1259 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001260
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001261 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001262 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001263
1264 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001265 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001266 * an uncharged page off lru does nothing to secure
1267 * its former mem_cgroup from sudden removal.
1268 *
1269 * Our caller holds lru_lock, and PageCgroupUsed is updated
1270 * under page_cgroup lock: between them, they make all uses
1271 * of pc->mem_cgroup safe.
1272 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001273 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001274 pc->mem_cgroup = memcg = root_mem_cgroup;
1275
Johannes Weiner925b7672012-01-12 17:18:15 -08001276 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001277 lruvec = &mz->lruvec;
1278out:
1279 /*
1280 * Since a node can be onlined after the mem_cgroup was created,
1281 * we have to be prepared to initialize lruvec->zone here;
1282 * and if offlined then reonlined, we need to reinitialize it.
1283 */
1284 if (unlikely(lruvec->zone != zone))
1285 lruvec->zone = zone;
1286 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001287}
1288
1289/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001290 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1291 * @lruvec: mem_cgroup per zone lru vector
1292 * @lru: index of lru list the page is sitting on
1293 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001294 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001295 * This function must be called when a page is added to or removed from an
1296 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001297 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001298void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1299 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001300{
1301 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001302 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001303
1304 if (mem_cgroup_disabled())
1305 return;
1306
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001307 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1308 lru_size = mz->lru_size + lru;
1309 *lru_size += nr_pages;
1310 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001311}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001312
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001313/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001314 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001315 * hierarchy subtree
1316 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001317bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1318 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001319{
Johannes Weiner91c637342012-05-29 15:06:24 -07001320 if (root_memcg == memcg)
1321 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001322 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001323 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001324 return css_is_ancestor(&memcg->css, &root_memcg->css);
1325}
1326
1327static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1328 struct mem_cgroup *memcg)
1329{
1330 bool ret;
1331
Johannes Weiner91c637342012-05-29 15:06:24 -07001332 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001333 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001334 rcu_read_unlock();
1335 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001336}
1337
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001338int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001339{
1340 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001341 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001342 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001343
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001344 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001345 if (p) {
1346 curr = try_get_mem_cgroup_from_mm(p->mm);
1347 task_unlock(p);
1348 } else {
1349 /*
1350 * All threads may have already detached their mm's, but the oom
1351 * killer still needs to detect if they have already been oom
1352 * killed to prevent needlessly killing additional tasks.
1353 */
1354 task_lock(task);
1355 curr = mem_cgroup_from_task(task);
1356 if (curr)
1357 css_get(&curr->css);
1358 task_unlock(task);
1359 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001360 if (!curr)
1361 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001362 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001363 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001364 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001365 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1366 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001367 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001368 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001369 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001370 return ret;
1371}
1372
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001373int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001374{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001375 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001376 unsigned long inactive;
1377 unsigned long active;
1378 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001379
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001380 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1381 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001382
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001383 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1384 if (gb)
1385 inactive_ratio = int_sqrt(10 * gb);
1386 else
1387 inactive_ratio = 1;
1388
Johannes Weiner9b272972011-11-02 13:38:23 -07001389 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001390}
1391
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001392int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001393{
1394 unsigned long active;
1395 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001396
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001397 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1398 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001399
1400 return (active > inactive);
1401}
1402
Balbir Singh6d61ef42009-01-07 18:08:06 -08001403#define mem_cgroup_from_res_counter(counter, member) \
1404 container_of(counter, struct mem_cgroup, member)
1405
Johannes Weiner19942822011-02-01 15:52:43 -08001406/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001407 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001408 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001409 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001410 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001411 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001412 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001413static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001414{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001415 unsigned long long margin;
1416
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001417 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001418 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001419 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001420 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001421}
1422
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001423int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001424{
1425 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001426
1427 /* root ? */
1428 if (cgrp->parent == NULL)
1429 return vm_swappiness;
1430
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001431 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001432}
1433
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001434/*
1435 * memcg->moving_account is used for checking possibility that some thread is
1436 * calling move_account(). When a thread on CPU-A starts moving pages under
1437 * a memcg, other threads should check memcg->moving_account under
1438 * rcu_read_lock(), like this:
1439 *
1440 * CPU-A CPU-B
1441 * rcu_read_lock()
1442 * memcg->moving_account+1 if (memcg->mocing_account)
1443 * take heavy locks.
1444 * synchronize_rcu() update something.
1445 * rcu_read_unlock()
1446 * start move here.
1447 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001448
1449/* for quick checking without looking up memcg */
1450atomic_t memcg_moving __read_mostly;
1451
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001452static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001453{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001454 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001455 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001456 synchronize_rcu();
1457}
1458
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001459static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001460{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001461 /*
1462 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1463 * We check NULL in callee rather than caller.
1464 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001465 if (memcg) {
1466 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001467 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001468 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001469}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001470
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001471/*
1472 * 2 routines for checking "mem" is under move_account() or not.
1473 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001474 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1475 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001476 * pc->mem_cgroup may be overwritten.
1477 *
1478 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1479 * under hierarchy of moving cgroups. This is for
1480 * waiting at hith-memory prressure caused by "move".
1481 */
1482
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001483static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001484{
1485 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001486 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001487}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001488
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001489static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001490{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001491 struct mem_cgroup *from;
1492 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001493 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001494 /*
1495 * Unlike task_move routines, we access mc.to, mc.from not under
1496 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1497 */
1498 spin_lock(&mc.lock);
1499 from = mc.from;
1500 to = mc.to;
1501 if (!from)
1502 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001503
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001504 ret = mem_cgroup_same_or_subtree(memcg, from)
1505 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001506unlock:
1507 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001508 return ret;
1509}
1510
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001511static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001512{
1513 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001514 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001515 DEFINE_WAIT(wait);
1516 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1517 /* moving charge context might have finished. */
1518 if (mc.moving_task)
1519 schedule();
1520 finish_wait(&mc.waitq, &wait);
1521 return true;
1522 }
1523 }
1524 return false;
1525}
1526
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001527/*
1528 * Take this lock when
1529 * - a code tries to modify page's memcg while it's USED.
1530 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001531 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001532 */
1533static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1534 unsigned long *flags)
1535{
1536 spin_lock_irqsave(&memcg->move_lock, *flags);
1537}
1538
1539static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1540 unsigned long *flags)
1541{
1542 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1543}
1544
Sha Zhengju58cf1882013-02-22 16:32:05 -08001545#define K(x) ((x) << (PAGE_SHIFT-10))
Balbir Singhe2224322009-04-02 16:57:39 -07001546/**
Sha Zhengju58cf1882013-02-22 16:32:05 -08001547 * mem_cgroup_print_oom_info: Print OOM information relevant to memory controller.
Balbir Singhe2224322009-04-02 16:57:39 -07001548 * @memcg: The memory cgroup that went over limit
1549 * @p: Task that is going to be killed
1550 *
1551 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1552 * enabled
1553 */
1554void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1555{
1556 struct cgroup *task_cgrp;
1557 struct cgroup *mem_cgrp;
1558 /*
1559 * Need a buffer in BSS, can't rely on allocations. The code relies
1560 * on the assumption that OOM is serialized for memory controller.
1561 * If this assumption is broken, revisit this code.
1562 */
1563 static char memcg_name[PATH_MAX];
1564 int ret;
Sha Zhengju58cf1882013-02-22 16:32:05 -08001565 struct mem_cgroup *iter;
1566 unsigned int i;
Balbir Singhe2224322009-04-02 16:57:39 -07001567
Sha Zhengju58cf1882013-02-22 16:32:05 -08001568 if (!p)
Balbir Singhe2224322009-04-02 16:57:39 -07001569 return;
1570
Balbir Singhe2224322009-04-02 16:57:39 -07001571 rcu_read_lock();
1572
1573 mem_cgrp = memcg->css.cgroup;
1574 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1575
1576 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1577 if (ret < 0) {
1578 /*
1579 * Unfortunately, we are unable to convert to a useful name
1580 * But we'll still print out the usage information
1581 */
1582 rcu_read_unlock();
1583 goto done;
1584 }
1585 rcu_read_unlock();
1586
Andrew Mortond0451972013-02-22 16:32:06 -08001587 pr_info("Task in %s killed", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001588
1589 rcu_read_lock();
1590 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1591 if (ret < 0) {
1592 rcu_read_unlock();
1593 goto done;
1594 }
1595 rcu_read_unlock();
1596
1597 /*
1598 * Continues from above, so we don't need an KERN_ level
1599 */
Andrew Mortond0451972013-02-22 16:32:06 -08001600 pr_cont(" as a result of limit of %s\n", memcg_name);
Balbir Singhe2224322009-04-02 16:57:39 -07001601done:
1602
Andrew Mortond0451972013-02-22 16:32:06 -08001603 pr_info("memory: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001604 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1605 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1606 res_counter_read_u64(&memcg->res, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001607 pr_info("memory+swap: usage %llukB, limit %llukB, failcnt %llu\n",
Balbir Singhe2224322009-04-02 16:57:39 -07001608 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1609 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1610 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Andrew Mortond0451972013-02-22 16:32:06 -08001611 pr_info("kmem: usage %llukB, limit %llukB, failcnt %llu\n",
Glauber Costa510fc4e2012-12-18 14:21:47 -08001612 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1613 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1614 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Sha Zhengju58cf1882013-02-22 16:32:05 -08001615
1616 for_each_mem_cgroup_tree(iter, memcg) {
1617 pr_info("Memory cgroup stats");
1618
1619 rcu_read_lock();
1620 ret = cgroup_path(iter->css.cgroup, memcg_name, PATH_MAX);
1621 if (!ret)
1622 pr_cont(" for %s", memcg_name);
1623 rcu_read_unlock();
1624 pr_cont(":");
1625
1626 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
1627 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
1628 continue;
1629 pr_cont(" %s:%ldKB", mem_cgroup_stat_names[i],
1630 K(mem_cgroup_read_stat(iter, i)));
1631 }
1632
1633 for (i = 0; i < NR_LRU_LISTS; i++)
1634 pr_cont(" %s:%luKB", mem_cgroup_lru_names[i],
1635 K(mem_cgroup_nr_lru_pages(iter, BIT(i))));
1636
1637 pr_cont("\n");
1638 }
Balbir Singhe2224322009-04-02 16:57:39 -07001639}
1640
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001641/*
1642 * This function returns the number of memcg under hierarchy tree. Returns
1643 * 1(self count) if no children.
1644 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001645static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001646{
1647 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001648 struct mem_cgroup *iter;
1649
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001650 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001651 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001652 return num;
1653}
1654
Balbir Singh6d61ef42009-01-07 18:08:06 -08001655/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001656 * Return the memory (and swap, if configured) limit for a memcg.
1657 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001658static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001659{
1660 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001661
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001662 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001663
David Rientjesa63d83f2010-08-09 17:19:46 -07001664 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001665 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001666 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001667 if (mem_cgroup_swappiness(memcg)) {
1668 u64 memsw;
1669
1670 limit += total_swap_pages << PAGE_SHIFT;
1671 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1672
1673 /*
1674 * If memsw is finite and limits the amount of swap space
1675 * available to this memcg, return that limit.
1676 */
1677 limit = min(limit, memsw);
1678 }
1679
1680 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001681}
1682
David Rientjes19965462012-12-11 16:00:26 -08001683static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1684 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001685{
1686 struct mem_cgroup *iter;
1687 unsigned long chosen_points = 0;
1688 unsigned long totalpages;
1689 unsigned int points = 0;
1690 struct task_struct *chosen = NULL;
1691
David Rientjes876aafb2012-07-31 16:43:48 -07001692 /*
1693 * If current has a pending SIGKILL, then automatically select it. The
1694 * goal is to allow it to allocate so that it may quickly exit and free
1695 * its memory.
1696 */
1697 if (fatal_signal_pending(current)) {
1698 set_thread_flag(TIF_MEMDIE);
1699 return;
1700 }
1701
1702 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001703 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1704 for_each_mem_cgroup_tree(iter, memcg) {
1705 struct cgroup *cgroup = iter->css.cgroup;
1706 struct cgroup_iter it;
1707 struct task_struct *task;
1708
1709 cgroup_iter_start(cgroup, &it);
1710 while ((task = cgroup_iter_next(cgroup, &it))) {
1711 switch (oom_scan_process_thread(task, totalpages, NULL,
1712 false)) {
1713 case OOM_SCAN_SELECT:
1714 if (chosen)
1715 put_task_struct(chosen);
1716 chosen = task;
1717 chosen_points = ULONG_MAX;
1718 get_task_struct(chosen);
1719 /* fall through */
1720 case OOM_SCAN_CONTINUE:
1721 continue;
1722 case OOM_SCAN_ABORT:
1723 cgroup_iter_end(cgroup, &it);
1724 mem_cgroup_iter_break(memcg, iter);
1725 if (chosen)
1726 put_task_struct(chosen);
1727 return;
1728 case OOM_SCAN_OK:
1729 break;
1730 };
1731 points = oom_badness(task, memcg, NULL, totalpages);
1732 if (points > chosen_points) {
1733 if (chosen)
1734 put_task_struct(chosen);
1735 chosen = task;
1736 chosen_points = points;
1737 get_task_struct(chosen);
1738 }
1739 }
1740 cgroup_iter_end(cgroup, &it);
1741 }
1742
1743 if (!chosen)
1744 return;
1745 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001746 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1747 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001748}
1749
Johannes Weiner56600482012-01-12 17:17:59 -08001750static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1751 gfp_t gfp_mask,
1752 unsigned long flags)
1753{
1754 unsigned long total = 0;
1755 bool noswap = false;
1756 int loop;
1757
1758 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1759 noswap = true;
1760 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1761 noswap = true;
1762
1763 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1764 if (loop)
1765 drain_all_stock_async(memcg);
1766 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1767 /*
1768 * Allow limit shrinkers, which are triggered directly
1769 * by userspace, to catch signals and stop reclaim
1770 * after minimal progress, regardless of the margin.
1771 */
1772 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1773 break;
1774 if (mem_cgroup_margin(memcg))
1775 break;
1776 /*
1777 * If nothing was reclaimed after two attempts, there
1778 * may be no reclaimable pages in this hierarchy.
1779 */
1780 if (loop && !total)
1781 break;
1782 }
1783 return total;
1784}
1785
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001786/**
1787 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001788 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001789 * @nid: the node ID to be checked.
1790 * @noswap : specify true here if the user wants flle only information.
1791 *
1792 * This function returns whether the specified memcg contains any
1793 * reclaimable pages on a node. Returns true if there are any reclaimable
1794 * pages in the node.
1795 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001796static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001797 int nid, bool noswap)
1798{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001799 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001800 return true;
1801 if (noswap || !total_swap_pages)
1802 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001803 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001804 return true;
1805 return false;
1806
1807}
Ying Han889976d2011-05-26 16:25:33 -07001808#if MAX_NUMNODES > 1
1809
1810/*
1811 * Always updating the nodemask is not very good - even if we have an empty
1812 * list or the wrong list here, we can start from some node and traverse all
1813 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1814 *
1815 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001816static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001817{
1818 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001819 /*
1820 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1821 * pagein/pageout changes since the last update.
1822 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001823 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001824 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001825 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001826 return;
1827
Ying Han889976d2011-05-26 16:25:33 -07001828 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001829 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001830
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001831 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001832
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001833 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1834 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001835 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001836
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001837 atomic_set(&memcg->numainfo_events, 0);
1838 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001839}
1840
1841/*
1842 * Selecting a node where we start reclaim from. Because what we need is just
1843 * reducing usage counter, start from anywhere is O,K. Considering
1844 * memory reclaim from current node, there are pros. and cons.
1845 *
1846 * Freeing memory from current node means freeing memory from a node which
1847 * we'll use or we've used. So, it may make LRU bad. And if several threads
1848 * hit limits, it will see a contention on a node. But freeing from remote
1849 * node means more costs for memory reclaim because of memory latency.
1850 *
1851 * Now, we use round-robin. Better algorithm is welcomed.
1852 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001853int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001854{
1855 int node;
1856
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001857 mem_cgroup_may_update_nodemask(memcg);
1858 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001859
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001860 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001861 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001862 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001863 /*
1864 * We call this when we hit limit, not when pages are added to LRU.
1865 * No LRU may hold pages because all pages are UNEVICTABLE or
1866 * memcg is too small and all pages are not on LRU. In that case,
1867 * we use curret node.
1868 */
1869 if (unlikely(node == MAX_NUMNODES))
1870 node = numa_node_id();
1871
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001872 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001873 return node;
1874}
1875
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001876/*
1877 * Check all nodes whether it contains reclaimable pages or not.
1878 * For quick scan, we make use of scan_nodes. This will allow us to skip
1879 * unused nodes. But scan_nodes is lazily updated and may not cotain
1880 * enough new information. We need to do double check.
1881 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001882static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001883{
1884 int nid;
1885
1886 /*
1887 * quick check...making use of scan_node.
1888 * We can skip unused nodes.
1889 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001890 if (!nodes_empty(memcg->scan_nodes)) {
1891 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001892 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001893 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001894
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001895 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001896 return true;
1897 }
1898 }
1899 /*
1900 * Check rest of nodes.
1901 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001902 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001903 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001904 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001905 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001906 return true;
1907 }
1908 return false;
1909}
1910
Ying Han889976d2011-05-26 16:25:33 -07001911#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001912int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001913{
1914 return 0;
1915}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001916
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001917static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001918{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001919 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001920}
Ying Han889976d2011-05-26 16:25:33 -07001921#endif
1922
Johannes Weiner56600482012-01-12 17:17:59 -08001923static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1924 struct zone *zone,
1925 gfp_t gfp_mask,
1926 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001927{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001928 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001929 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001930 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001931 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001932 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001933 struct mem_cgroup_reclaim_cookie reclaim = {
1934 .zone = zone,
1935 .priority = 0,
1936 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001937
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001938 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001939
Balbir Singh4e416952009-09-23 15:56:39 -07001940 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001941 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001942 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001943 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001944 if (loop >= 2) {
1945 /*
1946 * If we have not been able to reclaim
1947 * anything, it might because there are
1948 * no reclaimable pages under this hierarchy
1949 */
Johannes Weiner56600482012-01-12 17:17:59 -08001950 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001951 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001952 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001953 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001954 * excess >> 2 is not to excessive so as to
1955 * reclaim too much, nor too less that we keep
1956 * coming back to reclaim from this cgroup
1957 */
1958 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001959 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001960 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001961 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001962 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001963 }
Johannes Weiner56600482012-01-12 17:17:59 -08001964 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001965 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001966 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1967 zone, &nr_scanned);
1968 *total_scanned += nr_scanned;
1969 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001970 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001971 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001972 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001973 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001974}
1975
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001976/*
1977 * Check OOM-Killer is already running under our hierarchy.
1978 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001979 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001980 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001981static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001982{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001983 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001984
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001985 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001986 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001987 /*
1988 * this subtree of our hierarchy is already locked
1989 * so we cannot give a lock.
1990 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001991 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001992 mem_cgroup_iter_break(memcg, iter);
1993 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001994 } else
1995 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001996 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001997
Michal Hocko79dfdac2011-07-26 16:08:23 -07001998 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001999 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002000
2001 /*
2002 * OK, we failed to lock the whole subtree so we have to clean up
2003 * what we set up to the failing subtree
2004 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002005 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07002006 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002007 mem_cgroup_iter_break(memcg, iter);
2008 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07002009 }
2010 iter->oom_lock = false;
2011 }
Johannes Weiner23751be2011-08-25 15:59:16 -07002012 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002013}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002014
Michal Hocko79dfdac2011-07-26 16:08:23 -07002015/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07002016 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07002017 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002018static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002019{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07002020 struct mem_cgroup *iter;
2021
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002022 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002023 iter->oom_lock = false;
2024 return 0;
2025}
2026
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002027static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002028{
2029 struct mem_cgroup *iter;
2030
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002031 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002032 atomic_inc(&iter->under_oom);
2033}
2034
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002035static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002036{
2037 struct mem_cgroup *iter;
2038
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002039 /*
2040 * When a new child is created while the hierarchy is under oom,
2041 * mem_cgroup_oom_lock() may not be called. We have to use
2042 * atomic_add_unless() here.
2043 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002044 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07002045 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002046}
2047
Michal Hocko1af8efe2011-07-26 16:08:24 -07002048static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002049static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
2050
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002051struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002052 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002053 wait_queue_t wait;
2054};
2055
2056static int memcg_oom_wake_function(wait_queue_t *wait,
2057 unsigned mode, int sync, void *arg)
2058{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002059 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
2060 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002061 struct oom_wait_info *oom_wait_info;
2062
2063 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002064 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002065
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002066 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002067 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002068 * Then we can use css_is_ancestor without taking care of RCU.
2069 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002070 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
2071 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002072 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002073 return autoremove_wake_function(wait, mode, sync, arg);
2074}
2075
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002076static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002077{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002078 /* for filtering, pass "memcg" as argument. */
2079 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002080}
2081
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002082static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002083{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002084 if (memcg && atomic_read(&memcg->under_oom))
2085 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002086}
2087
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002088/*
2089 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
2090 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07002091static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
2092 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002093{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002094 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002095 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002096
Hugh Dickinsd79154b2012-03-21 16:34:18 -07002097 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002098 owait.wait.flags = 0;
2099 owait.wait.func = memcg_oom_wake_function;
2100 owait.wait.private = current;
2101 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002102 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002103 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002104
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002105 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07002106 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002107 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002108 /*
2109 * Even if signal_pending(), we can't quit charge() loop without
2110 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
2111 * under OOM is always welcomed, use TASK_KILLABLE here.
2112 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002113 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002114 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002115 need_to_kill = false;
2116 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002117 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002118 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002119
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002120 if (need_to_kill) {
2121 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07002122 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002123 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002124 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07002125 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002126 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07002127 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002128 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002129 mem_cgroup_oom_unlock(memcg);
2130 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07002131 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002132
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002133 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002134
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002135 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2136 return false;
2137 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002138 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002139 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002140}
2141
Balbir Singhd69b0422009-06-17 16:26:34 -07002142/*
2143 * Currently used to update mapped file statistics, but the routine can be
2144 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002145 *
2146 * Notes: Race condition
2147 *
2148 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2149 * it tends to be costly. But considering some conditions, we doesn't need
2150 * to do so _always_.
2151 *
2152 * Considering "charge", lock_page_cgroup() is not required because all
2153 * file-stat operations happen after a page is attached to radix-tree. There
2154 * are no race with "charge".
2155 *
2156 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2157 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2158 * if there are race with "uncharge". Statistics itself is properly handled
2159 * by flags.
2160 *
2161 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002162 * small, we check mm->moving_account and detect there are possibility of race
2163 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002164 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002165
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002166void __mem_cgroup_begin_update_page_stat(struct page *page,
2167 bool *locked, unsigned long *flags)
2168{
2169 struct mem_cgroup *memcg;
2170 struct page_cgroup *pc;
2171
2172 pc = lookup_page_cgroup(page);
2173again:
2174 memcg = pc->mem_cgroup;
2175 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2176 return;
2177 /*
2178 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002179 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002180 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002181 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002182 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002183 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002184 return;
2185
2186 move_lock_mem_cgroup(memcg, flags);
2187 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2188 move_unlock_mem_cgroup(memcg, flags);
2189 goto again;
2190 }
2191 *locked = true;
2192}
2193
2194void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2195{
2196 struct page_cgroup *pc = lookup_page_cgroup(page);
2197
2198 /*
2199 * It's guaranteed that pc->mem_cgroup never changes while
2200 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002201 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002202 */
2203 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2204}
2205
Greg Thelen2a7106f2011-01-13 15:47:37 -08002206void mem_cgroup_update_page_stat(struct page *page,
2207 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002208{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002209 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002210 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002211 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002212
Johannes Weinercfa44942012-01-12 17:18:38 -08002213 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002214 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002215
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002216 memcg = pc->mem_cgroup;
2217 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002218 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002219
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002220 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002221 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002222 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002223 break;
2224 default:
2225 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002226 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002227
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002228 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002229}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002230
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002231/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002232 * size of first charge trial. "32" comes from vmscan.c's magic value.
2233 * TODO: maybe necessary to use big numbers in big irons.
2234 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002235#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002236struct memcg_stock_pcp {
2237 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002238 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002239 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002240 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002241#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002242};
2243static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002244static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002245
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002246/**
2247 * consume_stock: Try to consume stocked charge on this cpu.
2248 * @memcg: memcg to consume from.
2249 * @nr_pages: how many pages to charge.
2250 *
2251 * The charges will only happen if @memcg matches the current cpu's memcg
2252 * stock, and at least @nr_pages are available in that stock. Failure to
2253 * service an allocation will refill the stock.
2254 *
2255 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002256 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002257static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002258{
2259 struct memcg_stock_pcp *stock;
2260 bool ret = true;
2261
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002262 if (nr_pages > CHARGE_BATCH)
2263 return false;
2264
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002265 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002266 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2267 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002268 else /* need to call res_counter_charge */
2269 ret = false;
2270 put_cpu_var(memcg_stock);
2271 return ret;
2272}
2273
2274/*
2275 * Returns stocks cached in percpu to res_counter and reset cached information.
2276 */
2277static void drain_stock(struct memcg_stock_pcp *stock)
2278{
2279 struct mem_cgroup *old = stock->cached;
2280
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002281 if (stock->nr_pages) {
2282 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2283
2284 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002285 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002286 res_counter_uncharge(&old->memsw, bytes);
2287 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002288 }
2289 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002290}
2291
2292/*
2293 * This must be called under preempt disabled or must be called by
2294 * a thread which is pinned to local cpu.
2295 */
2296static void drain_local_stock(struct work_struct *dummy)
2297{
2298 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2299 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002300 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002301}
2302
2303/*
2304 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002305 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002306 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002307static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002308{
2309 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2310
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002311 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002312 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002313 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002314 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002315 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002316 put_cpu_var(memcg_stock);
2317}
2318
2319/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002320 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002321 * of the hierarchy under it. sync flag says whether we should block
2322 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002323 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002324static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002325{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002326 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002327
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002328 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002329 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002330 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002331 for_each_online_cpu(cpu) {
2332 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002333 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002334
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002335 memcg = stock->cached;
2336 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002337 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002338 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002339 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002340 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2341 if (cpu == curcpu)
2342 drain_local_stock(&stock->work);
2343 else
2344 schedule_work_on(cpu, &stock->work);
2345 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002346 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002347 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002348
2349 if (!sync)
2350 goto out;
2351
2352 for_each_online_cpu(cpu) {
2353 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002354 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002355 flush_work(&stock->work);
2356 }
2357out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002358 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002359}
2360
2361/*
2362 * Tries to drain stocked charges in other cpus. This function is asynchronous
2363 * and just put a work per cpu for draining localy on each cpu. Caller can
2364 * expects some charges will be back to res_counter later but cannot wait for
2365 * it.
2366 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002367static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002368{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002369 /*
2370 * If someone calls draining, avoid adding more kworker runs.
2371 */
2372 if (!mutex_trylock(&percpu_charge_mutex))
2373 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002374 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002375 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002376}
2377
2378/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002379static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002380{
2381 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002382 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002383 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002384 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002385}
2386
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002387/*
2388 * This function drains percpu counter value from DEAD cpu and
2389 * move it to local cpu. Note that this function can be preempted.
2390 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002391static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002392{
2393 int i;
2394
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002395 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002396 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002397 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002398
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002399 per_cpu(memcg->stat->count[i], cpu) = 0;
2400 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002401 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002402 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002403 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002404
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002405 per_cpu(memcg->stat->events[i], cpu) = 0;
2406 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002407 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002408 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002409}
2410
2411static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002412 unsigned long action,
2413 void *hcpu)
2414{
2415 int cpu = (unsigned long)hcpu;
2416 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002417 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002418
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002419 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002420 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002421
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002422 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002423 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002424
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002425 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002426 mem_cgroup_drain_pcp_counter(iter, cpu);
2427
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002428 stock = &per_cpu(memcg_stock, cpu);
2429 drain_stock(stock);
2430 return NOTIFY_OK;
2431}
2432
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002433
2434/* See __mem_cgroup_try_charge() for details */
2435enum {
2436 CHARGE_OK, /* success */
2437 CHARGE_RETRY, /* need to retry but retry is not bad */
2438 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2439 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2440 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2441};
2442
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002443static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002444 unsigned int nr_pages, unsigned int min_pages,
2445 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002446{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002447 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002448 struct mem_cgroup *mem_over_limit;
2449 struct res_counter *fail_res;
2450 unsigned long flags = 0;
2451 int ret;
2452
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002453 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002454
2455 if (likely(!ret)) {
2456 if (!do_swap_account)
2457 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002459 if (likely(!ret))
2460 return CHARGE_OK;
2461
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002462 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002463 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2464 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2465 } else
2466 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002467 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002468 * Never reclaim on behalf of optional batching, retry with a
2469 * single page instead.
2470 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002471 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002472 return CHARGE_RETRY;
2473
2474 if (!(gfp_mask & __GFP_WAIT))
2475 return CHARGE_WOULDBLOCK;
2476
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002477 if (gfp_mask & __GFP_NORETRY)
2478 return CHARGE_NOMEM;
2479
Johannes Weiner56600482012-01-12 17:17:59 -08002480 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002481 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002482 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002483 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002484 * Even though the limit is exceeded at this point, reclaim
2485 * may have been able to free some pages. Retry the charge
2486 * before killing the task.
2487 *
2488 * Only for regular pages, though: huge pages are rather
2489 * unlikely to succeed so close to the limit, and we fall back
2490 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002491 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002492 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002493 return CHARGE_RETRY;
2494
2495 /*
2496 * At task move, charge accounts can be doubly counted. So, it's
2497 * better to wait until the end of task_move if something is going on.
2498 */
2499 if (mem_cgroup_wait_acct_move(mem_over_limit))
2500 return CHARGE_RETRY;
2501
2502 /* If we don't need to call oom-killer at el, return immediately */
2503 if (!oom_check)
2504 return CHARGE_NOMEM;
2505 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002506 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002507 return CHARGE_OOM_DIE;
2508
2509 return CHARGE_RETRY;
2510}
2511
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002512/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002513 * __mem_cgroup_try_charge() does
2514 * 1. detect memcg to be charged against from passed *mm and *ptr,
2515 * 2. update res_counter
2516 * 3. call memory reclaim if necessary.
2517 *
2518 * In some special case, if the task is fatal, fatal_signal_pending() or
2519 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2520 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2521 * as possible without any hazards. 2: all pages should have a valid
2522 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2523 * pointer, that is treated as a charge to root_mem_cgroup.
2524 *
2525 * So __mem_cgroup_try_charge() will return
2526 * 0 ... on success, filling *ptr with a valid memcg pointer.
2527 * -ENOMEM ... charge failure because of resource limits.
2528 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2529 *
2530 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2531 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002532 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002533static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002534 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002535 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002536 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002537 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002538{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002539 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002540 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002541 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002542 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002543
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002544 /*
2545 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2546 * in system level. So, allow to go ahead dying process in addition to
2547 * MEMDIE process.
2548 */
2549 if (unlikely(test_thread_flag(TIF_MEMDIE)
2550 || fatal_signal_pending(current)))
2551 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002552
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002553 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002554 * We always charge the cgroup the mm_struct belongs to.
2555 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002556 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002557 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002558 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002559 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002560 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002561again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002562 if (*ptr) { /* css should be a valid one */
2563 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002564 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002565 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002566 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002567 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002568 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002569 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002570 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002571
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002572 rcu_read_lock();
2573 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002574 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002575 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002576 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002577 * race with swapoff. Then, we have small risk of mis-accouning.
2578 * But such kind of mis-account by race always happens because
2579 * we don't have cgroup_mutex(). It's overkill and we allo that
2580 * small race, here.
2581 * (*) swapoff at el will charge against mm-struct not against
2582 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002583 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002584 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002585 if (!memcg)
2586 memcg = root_mem_cgroup;
2587 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002588 rcu_read_unlock();
2589 goto done;
2590 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002591 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002592 /*
2593 * It seems dagerous to access memcg without css_get().
2594 * But considering how consume_stok works, it's not
2595 * necessary. If consume_stock success, some charges
2596 * from this memcg are cached on this cpu. So, we
2597 * don't need to call css_get()/css_tryget() before
2598 * calling consume_stock().
2599 */
2600 rcu_read_unlock();
2601 goto done;
2602 }
2603 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002604 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002605 rcu_read_unlock();
2606 goto again;
2607 }
2608 rcu_read_unlock();
2609 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002610
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002611 do {
2612 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002613
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002614 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002615 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002616 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002617 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002618 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002619
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002620 oom_check = false;
2621 if (oom && !nr_oom_retries) {
2622 oom_check = true;
2623 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2624 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002625
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002626 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2627 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002628 switch (ret) {
2629 case CHARGE_OK:
2630 break;
2631 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002632 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002633 css_put(&memcg->css);
2634 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002635 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002636 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002637 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002638 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002639 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002640 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002641 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002642 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002643 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002644 /* If oom, we never return -ENOMEM */
2645 nr_oom_retries--;
2646 break;
2647 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002648 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002649 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002650 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002651 } while (ret != CHARGE_OK);
2652
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002653 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002654 refill_stock(memcg, batch - nr_pages);
2655 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002656done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002657 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002658 return 0;
2659nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002660 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002661 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002662bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002663 *ptr = root_mem_cgroup;
2664 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002665}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002666
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002667/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002668 * Somemtimes we have to undo a charge we got by try_charge().
2669 * This function is for that and do uncharge, put css's refcnt.
2670 * gotten by try_charge().
2671 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002672static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002673 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002674{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002675 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002676 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002677
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002678 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002679 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002680 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002681 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002682}
2683
2684/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002685 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2686 * This is useful when moving usage to parent cgroup.
2687 */
2688static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2689 unsigned int nr_pages)
2690{
2691 unsigned long bytes = nr_pages * PAGE_SIZE;
2692
2693 if (mem_cgroup_is_root(memcg))
2694 return;
2695
2696 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2697 if (do_swap_account)
2698 res_counter_uncharge_until(&memcg->memsw,
2699 memcg->memsw.parent, bytes);
2700}
2701
2702/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002703 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002704 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2705 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2706 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002707 */
2708static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2709{
2710 struct cgroup_subsys_state *css;
2711
2712 /* ID 0 is unused ID */
2713 if (!id)
2714 return NULL;
2715 css = css_lookup(&mem_cgroup_subsys, id);
2716 if (!css)
2717 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002718 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002719}
2720
Wu Fengguange42d9d52009-12-16 12:19:59 +01002721struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002722{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002723 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002724 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002725 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002726 swp_entry_t ent;
2727
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002728 VM_BUG_ON(!PageLocked(page));
2729
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002730 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002731 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002732 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002733 memcg = pc->mem_cgroup;
2734 if (memcg && !css_tryget(&memcg->css))
2735 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002736 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002737 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002738 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002739 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002740 memcg = mem_cgroup_lookup(id);
2741 if (memcg && !css_tryget(&memcg->css))
2742 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002743 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002744 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002745 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002746 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002747}
2748
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002749static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002750 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002751 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002752 enum charge_type ctype,
2753 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002754{
Johannes Weinerce587e62012-04-24 20:22:33 +02002755 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002756 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002757 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002758 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002759 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002760
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002761 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002762 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002763 /*
2764 * we don't need page_cgroup_lock about tail pages, becase they are not
2765 * accessed by any other context at this point.
2766 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002767
2768 /*
2769 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2770 * may already be on some other mem_cgroup's LRU. Take care of it.
2771 */
2772 if (lrucare) {
2773 zone = page_zone(page);
2774 spin_lock_irq(&zone->lru_lock);
2775 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002776 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002777 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002778 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002779 was_on_lru = true;
2780 }
2781 }
2782
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002783 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002784 /*
2785 * We access a page_cgroup asynchronously without lock_page_cgroup().
2786 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2787 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2788 * before USED bit, we need memory barrier here.
2789 * See mem_cgroup_add_lru_list(), etc.
2790 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002791 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002792 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002793
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002794 if (lrucare) {
2795 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002796 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002797 VM_BUG_ON(PageLRU(page));
2798 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002799 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002800 }
2801 spin_unlock_irq(&zone->lru_lock);
2802 }
2803
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002804 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002805 anon = true;
2806 else
2807 anon = false;
2808
2809 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002810 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002811
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002812 /*
2813 * "charge_statistics" updated event counter. Then, check it.
2814 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2815 * if they exceeds softlimit.
2816 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002817 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002818}
2819
Glauber Costa7cf27982012-12-18 14:22:55 -08002820static DEFINE_MUTEX(set_limit_mutex);
2821
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002822#ifdef CONFIG_MEMCG_KMEM
2823static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2824{
2825 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2826 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2827}
2828
Glauber Costa1f458cb2012-12-18 14:22:50 -08002829/*
2830 * This is a bit cumbersome, but it is rarely used and avoids a backpointer
2831 * in the memcg_cache_params struct.
2832 */
2833static struct kmem_cache *memcg_params_to_cache(struct memcg_cache_params *p)
2834{
2835 struct kmem_cache *cachep;
2836
2837 VM_BUG_ON(p->is_root_cache);
2838 cachep = p->root_cache;
2839 return cachep->memcg_params->memcg_caches[memcg_cache_id(p->memcg)];
2840}
2841
Glauber Costa749c5412012-12-18 14:23:01 -08002842#ifdef CONFIG_SLABINFO
2843static int mem_cgroup_slabinfo_read(struct cgroup *cont, struct cftype *cft,
2844 struct seq_file *m)
2845{
2846 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2847 struct memcg_cache_params *params;
2848
2849 if (!memcg_can_account_kmem(memcg))
2850 return -EIO;
2851
2852 print_slabinfo_header(m);
2853
2854 mutex_lock(&memcg->slab_caches_mutex);
2855 list_for_each_entry(params, &memcg->memcg_slab_caches, list)
2856 cache_show(memcg_params_to_cache(params), m);
2857 mutex_unlock(&memcg->slab_caches_mutex);
2858
2859 return 0;
2860}
2861#endif
2862
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002863static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2864{
2865 struct res_counter *fail_res;
2866 struct mem_cgroup *_memcg;
2867 int ret = 0;
2868 bool may_oom;
2869
2870 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2871 if (ret)
2872 return ret;
2873
2874 /*
2875 * Conditions under which we can wait for the oom_killer. Those are
2876 * the same conditions tested by the core page allocator
2877 */
2878 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2879
2880 _memcg = memcg;
2881 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2882 &_memcg, may_oom);
2883
2884 if (ret == -EINTR) {
2885 /*
2886 * __mem_cgroup_try_charge() chosed to bypass to root due to
2887 * OOM kill or fatal signal. Since our only options are to
2888 * either fail the allocation or charge it to this cgroup, do
2889 * it as a temporary condition. But we can't fail. From a
2890 * kmem/slab perspective, the cache has already been selected,
2891 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2892 * our minds.
2893 *
2894 * This condition will only trigger if the task entered
2895 * memcg_charge_kmem in a sane state, but was OOM-killed during
2896 * __mem_cgroup_try_charge() above. Tasks that were already
2897 * dying when the allocation triggers should have been already
2898 * directed to the root cgroup in memcontrol.h
2899 */
2900 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2901 if (do_swap_account)
2902 res_counter_charge_nofail(&memcg->memsw, size,
2903 &fail_res);
2904 ret = 0;
2905 } else if (ret)
2906 res_counter_uncharge(&memcg->kmem, size);
2907
2908 return ret;
2909}
2910
2911static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2912{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002913 res_counter_uncharge(&memcg->res, size);
2914 if (do_swap_account)
2915 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002916
2917 /* Not down to 0 */
2918 if (res_counter_uncharge(&memcg->kmem, size))
2919 return;
2920
2921 if (memcg_kmem_test_and_clear_dead(memcg))
2922 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002923}
2924
Glauber Costa2633d7a2012-12-18 14:22:34 -08002925void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep)
2926{
2927 if (!memcg)
2928 return;
2929
2930 mutex_lock(&memcg->slab_caches_mutex);
2931 list_add(&cachep->memcg_params->list, &memcg->memcg_slab_caches);
2932 mutex_unlock(&memcg->slab_caches_mutex);
2933}
2934
2935/*
2936 * helper for acessing a memcg's index. It will be used as an index in the
2937 * child cache array in kmem_cache, and also to derive its name. This function
2938 * will return -1 when this is not a kmem-limited memcg.
2939 */
2940int memcg_cache_id(struct mem_cgroup *memcg)
2941{
2942 return memcg ? memcg->kmemcg_id : -1;
2943}
2944
Glauber Costa55007d82012-12-18 14:22:38 -08002945/*
2946 * This ends up being protected by the set_limit mutex, during normal
2947 * operation, because that is its main call site.
2948 *
2949 * But when we create a new cache, we can call this as well if its parent
2950 * is kmem-limited. That will have to hold set_limit_mutex as well.
2951 */
2952int memcg_update_cache_sizes(struct mem_cgroup *memcg)
2953{
2954 int num, ret;
2955
2956 num = ida_simple_get(&kmem_limited_groups,
2957 0, MEMCG_CACHES_MAX_SIZE, GFP_KERNEL);
2958 if (num < 0)
2959 return num;
2960 /*
2961 * After this point, kmem_accounted (that we test atomically in
2962 * the beginning of this conditional), is no longer 0. This
2963 * guarantees only one process will set the following boolean
2964 * to true. We don't need test_and_set because we're protected
2965 * by the set_limit_mutex anyway.
2966 */
2967 memcg_kmem_set_activated(memcg);
2968
2969 ret = memcg_update_all_caches(num+1);
2970 if (ret) {
2971 ida_simple_remove(&kmem_limited_groups, num);
2972 memcg_kmem_clear_activated(memcg);
2973 return ret;
2974 }
2975
2976 memcg->kmemcg_id = num;
2977 INIT_LIST_HEAD(&memcg->memcg_slab_caches);
2978 mutex_init(&memcg->slab_caches_mutex);
2979 return 0;
2980}
2981
2982static size_t memcg_caches_array_size(int num_groups)
2983{
2984 ssize_t size;
2985 if (num_groups <= 0)
2986 return 0;
2987
2988 size = 2 * num_groups;
2989 if (size < MEMCG_CACHES_MIN_SIZE)
2990 size = MEMCG_CACHES_MIN_SIZE;
2991 else if (size > MEMCG_CACHES_MAX_SIZE)
2992 size = MEMCG_CACHES_MAX_SIZE;
2993
2994 return size;
2995}
2996
2997/*
2998 * We should update the current array size iff all caches updates succeed. This
2999 * can only be done from the slab side. The slab mutex needs to be held when
3000 * calling this.
3001 */
3002void memcg_update_array_size(int num)
3003{
3004 if (num > memcg_limited_groups_array_size)
3005 memcg_limited_groups_array_size = memcg_caches_array_size(num);
3006}
3007
3008int memcg_update_cache_size(struct kmem_cache *s, int num_groups)
3009{
3010 struct memcg_cache_params *cur_params = s->memcg_params;
3011
3012 VM_BUG_ON(s->memcg_params && !s->memcg_params->is_root_cache);
3013
3014 if (num_groups > memcg_limited_groups_array_size) {
3015 int i;
3016 ssize_t size = memcg_caches_array_size(num_groups);
3017
3018 size *= sizeof(void *);
3019 size += sizeof(struct memcg_cache_params);
3020
3021 s->memcg_params = kzalloc(size, GFP_KERNEL);
3022 if (!s->memcg_params) {
3023 s->memcg_params = cur_params;
3024 return -ENOMEM;
3025 }
3026
3027 s->memcg_params->is_root_cache = true;
3028
3029 /*
3030 * There is the chance it will be bigger than
3031 * memcg_limited_groups_array_size, if we failed an allocation
3032 * in a cache, in which case all caches updated before it, will
3033 * have a bigger array.
3034 *
3035 * But if that is the case, the data after
3036 * memcg_limited_groups_array_size is certainly unused
3037 */
3038 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3039 if (!cur_params->memcg_caches[i])
3040 continue;
3041 s->memcg_params->memcg_caches[i] =
3042 cur_params->memcg_caches[i];
3043 }
3044
3045 /*
3046 * Ideally, we would wait until all caches succeed, and only
3047 * then free the old one. But this is not worth the extra
3048 * pointer per-cache we'd have to have for this.
3049 *
3050 * It is not a big deal if some caches are left with a size
3051 * bigger than the others. And all updates will reset this
3052 * anyway.
3053 */
3054 kfree(cur_params);
3055 }
3056 return 0;
3057}
3058
Glauber Costa943a4512012-12-18 14:23:03 -08003059int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
3060 struct kmem_cache *root_cache)
Glauber Costa2633d7a2012-12-18 14:22:34 -08003061{
3062 size_t size = sizeof(struct memcg_cache_params);
3063
3064 if (!memcg_kmem_enabled())
3065 return 0;
3066
Glauber Costa55007d82012-12-18 14:22:38 -08003067 if (!memcg)
3068 size += memcg_limited_groups_array_size * sizeof(void *);
3069
Glauber Costa2633d7a2012-12-18 14:22:34 -08003070 s->memcg_params = kzalloc(size, GFP_KERNEL);
3071 if (!s->memcg_params)
3072 return -ENOMEM;
3073
Glauber Costa943a4512012-12-18 14:23:03 -08003074 if (memcg) {
Glauber Costa2633d7a2012-12-18 14:22:34 -08003075 s->memcg_params->memcg = memcg;
Glauber Costa943a4512012-12-18 14:23:03 -08003076 s->memcg_params->root_cache = root_cache;
Glauber Costa4ba902b2013-02-12 13:46:22 -08003077 } else
3078 s->memcg_params->is_root_cache = true;
3079
Glauber Costa2633d7a2012-12-18 14:22:34 -08003080 return 0;
3081}
3082
3083void memcg_release_cache(struct kmem_cache *s)
3084{
Glauber Costad7f25f82012-12-18 14:22:40 -08003085 struct kmem_cache *root;
3086 struct mem_cgroup *memcg;
3087 int id;
3088
3089 /*
3090 * This happens, for instance, when a root cache goes away before we
3091 * add any memcg.
3092 */
3093 if (!s->memcg_params)
3094 return;
3095
3096 if (s->memcg_params->is_root_cache)
3097 goto out;
3098
3099 memcg = s->memcg_params->memcg;
3100 id = memcg_cache_id(memcg);
3101
3102 root = s->memcg_params->root_cache;
3103 root->memcg_params->memcg_caches[id] = NULL;
3104 mem_cgroup_put(memcg);
3105
3106 mutex_lock(&memcg->slab_caches_mutex);
3107 list_del(&s->memcg_params->list);
3108 mutex_unlock(&memcg->slab_caches_mutex);
3109
3110out:
Glauber Costa2633d7a2012-12-18 14:22:34 -08003111 kfree(s->memcg_params);
3112}
3113
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003114/*
3115 * During the creation a new cache, we need to disable our accounting mechanism
3116 * altogether. This is true even if we are not creating, but rather just
3117 * enqueing new caches to be created.
3118 *
3119 * This is because that process will trigger allocations; some visible, like
3120 * explicit kmallocs to auxiliary data structures, name strings and internal
3121 * cache structures; some well concealed, like INIT_WORK() that can allocate
3122 * objects during debug.
3123 *
3124 * If any allocation happens during memcg_kmem_get_cache, we will recurse back
3125 * to it. This may not be a bounded recursion: since the first cache creation
3126 * failed to complete (waiting on the allocation), we'll just try to create the
3127 * cache again, failing at the same point.
3128 *
3129 * memcg_kmem_get_cache is prepared to abort after seeing a positive count of
3130 * memcg_kmem_skip_account. So we enclose anything that might allocate memory
3131 * inside the following two functions.
3132 */
3133static inline void memcg_stop_kmem_account(void)
3134{
3135 VM_BUG_ON(!current->mm);
3136 current->memcg_kmem_skip_account++;
3137}
3138
3139static inline void memcg_resume_kmem_account(void)
3140{
3141 VM_BUG_ON(!current->mm);
3142 current->memcg_kmem_skip_account--;
3143}
3144
Glauber Costa1f458cb2012-12-18 14:22:50 -08003145static void kmem_cache_destroy_work_func(struct work_struct *w)
3146{
3147 struct kmem_cache *cachep;
3148 struct memcg_cache_params *p;
3149
3150 p = container_of(w, struct memcg_cache_params, destroy);
3151
3152 cachep = memcg_params_to_cache(p);
3153
Glauber Costa22933152012-12-18 14:22:59 -08003154 /*
3155 * If we get down to 0 after shrink, we could delete right away.
3156 * However, memcg_release_pages() already puts us back in the workqueue
3157 * in that case. If we proceed deleting, we'll get a dangling
3158 * reference, and removing the object from the workqueue in that case
3159 * is unnecessary complication. We are not a fast path.
3160 *
3161 * Note that this case is fundamentally different from racing with
3162 * shrink_slab(): if memcg_cgroup_destroy_cache() is called in
3163 * kmem_cache_shrink, not only we would be reinserting a dead cache
3164 * into the queue, but doing so from inside the worker racing to
3165 * destroy it.
3166 *
3167 * So if we aren't down to zero, we'll just schedule a worker and try
3168 * again
3169 */
3170 if (atomic_read(&cachep->memcg_params->nr_pages) != 0) {
3171 kmem_cache_shrink(cachep);
3172 if (atomic_read(&cachep->memcg_params->nr_pages) == 0)
3173 return;
3174 } else
Glauber Costa1f458cb2012-12-18 14:22:50 -08003175 kmem_cache_destroy(cachep);
3176}
3177
3178void mem_cgroup_destroy_cache(struct kmem_cache *cachep)
3179{
3180 if (!cachep->memcg_params->dead)
3181 return;
3182
3183 /*
Glauber Costa22933152012-12-18 14:22:59 -08003184 * There are many ways in which we can get here.
3185 *
3186 * We can get to a memory-pressure situation while the delayed work is
3187 * still pending to run. The vmscan shrinkers can then release all
3188 * cache memory and get us to destruction. If this is the case, we'll
3189 * be executed twice, which is a bug (the second time will execute over
3190 * bogus data). In this case, cancelling the work should be fine.
3191 *
3192 * But we can also get here from the worker itself, if
3193 * kmem_cache_shrink is enough to shake all the remaining objects and
3194 * get the page count to 0. In this case, we'll deadlock if we try to
3195 * cancel the work (the worker runs with an internal lock held, which
3196 * is the same lock we would hold for cancel_work_sync().)
3197 *
3198 * Since we can't possibly know who got us here, just refrain from
3199 * running if there is already work pending
3200 */
3201 if (work_pending(&cachep->memcg_params->destroy))
3202 return;
3203 /*
Glauber Costa1f458cb2012-12-18 14:22:50 -08003204 * We have to defer the actual destroying to a workqueue, because
3205 * we might currently be in a context that cannot sleep.
3206 */
3207 schedule_work(&cachep->memcg_params->destroy);
3208}
3209
Glauber Costad7f25f82012-12-18 14:22:40 -08003210static char *memcg_cache_name(struct mem_cgroup *memcg, struct kmem_cache *s)
3211{
3212 char *name;
3213 struct dentry *dentry;
3214
3215 rcu_read_lock();
3216 dentry = rcu_dereference(memcg->css.cgroup->dentry);
3217 rcu_read_unlock();
3218
3219 BUG_ON(dentry == NULL);
3220
3221 name = kasprintf(GFP_KERNEL, "%s(%d:%s)", s->name,
3222 memcg_cache_id(memcg), dentry->d_name.name);
3223
3224 return name;
3225}
3226
3227static struct kmem_cache *kmem_cache_dup(struct mem_cgroup *memcg,
3228 struct kmem_cache *s)
3229{
3230 char *name;
3231 struct kmem_cache *new;
3232
3233 name = memcg_cache_name(memcg, s);
3234 if (!name)
3235 return NULL;
3236
3237 new = kmem_cache_create_memcg(memcg, name, s->object_size, s->align,
Glauber Costa943a4512012-12-18 14:23:03 -08003238 (s->flags & ~SLAB_PANIC), s->ctor, s);
Glauber Costad7f25f82012-12-18 14:22:40 -08003239
Glauber Costad79923f2012-12-18 14:22:48 -08003240 if (new)
3241 new->allocflags |= __GFP_KMEMCG;
3242
Glauber Costad7f25f82012-12-18 14:22:40 -08003243 kfree(name);
3244 return new;
3245}
3246
3247/*
3248 * This lock protects updaters, not readers. We want readers to be as fast as
3249 * they can, and they will either see NULL or a valid cache value. Our model
3250 * allow them to see NULL, in which case the root memcg will be selected.
3251 *
3252 * We need this lock because multiple allocations to the same cache from a non
3253 * will span more than one worker. Only one of them can create the cache.
3254 */
3255static DEFINE_MUTEX(memcg_cache_mutex);
3256static struct kmem_cache *memcg_create_kmem_cache(struct mem_cgroup *memcg,
3257 struct kmem_cache *cachep)
3258{
3259 struct kmem_cache *new_cachep;
3260 int idx;
3261
3262 BUG_ON(!memcg_can_account_kmem(memcg));
3263
3264 idx = memcg_cache_id(memcg);
3265
3266 mutex_lock(&memcg_cache_mutex);
3267 new_cachep = cachep->memcg_params->memcg_caches[idx];
3268 if (new_cachep)
3269 goto out;
3270
3271 new_cachep = kmem_cache_dup(memcg, cachep);
Glauber Costad7f25f82012-12-18 14:22:40 -08003272 if (new_cachep == NULL) {
3273 new_cachep = cachep;
3274 goto out;
3275 }
3276
3277 mem_cgroup_get(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003278 atomic_set(&new_cachep->memcg_params->nr_pages , 0);
Glauber Costad7f25f82012-12-18 14:22:40 -08003279
3280 cachep->memcg_params->memcg_caches[idx] = new_cachep;
3281 /*
3282 * the readers won't lock, make sure everybody sees the updated value,
3283 * so they won't put stuff in the queue again for no reason
3284 */
3285 wmb();
3286out:
3287 mutex_unlock(&memcg_cache_mutex);
3288 return new_cachep;
3289}
3290
Glauber Costa7cf27982012-12-18 14:22:55 -08003291void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
3292{
3293 struct kmem_cache *c;
3294 int i;
3295
3296 if (!s->memcg_params)
3297 return;
3298 if (!s->memcg_params->is_root_cache)
3299 return;
3300
3301 /*
3302 * If the cache is being destroyed, we trust that there is no one else
3303 * requesting objects from it. Even if there are, the sanity checks in
3304 * kmem_cache_destroy should caught this ill-case.
3305 *
3306 * Still, we don't want anyone else freeing memcg_caches under our
3307 * noses, which can happen if a new memcg comes to life. As usual,
3308 * we'll take the set_limit_mutex to protect ourselves against this.
3309 */
3310 mutex_lock(&set_limit_mutex);
3311 for (i = 0; i < memcg_limited_groups_array_size; i++) {
3312 c = s->memcg_params->memcg_caches[i];
3313 if (!c)
3314 continue;
3315
3316 /*
3317 * We will now manually delete the caches, so to avoid races
3318 * we need to cancel all pending destruction workers and
3319 * proceed with destruction ourselves.
3320 *
3321 * kmem_cache_destroy() will call kmem_cache_shrink internally,
3322 * and that could spawn the workers again: it is likely that
3323 * the cache still have active pages until this very moment.
3324 * This would lead us back to mem_cgroup_destroy_cache.
3325 *
3326 * But that will not execute at all if the "dead" flag is not
3327 * set, so flip it down to guarantee we are in control.
3328 */
3329 c->memcg_params->dead = false;
Glauber Costa22933152012-12-18 14:22:59 -08003330 cancel_work_sync(&c->memcg_params->destroy);
Glauber Costa7cf27982012-12-18 14:22:55 -08003331 kmem_cache_destroy(c);
3332 }
3333 mutex_unlock(&set_limit_mutex);
3334}
3335
Glauber Costad7f25f82012-12-18 14:22:40 -08003336struct create_work {
3337 struct mem_cgroup *memcg;
3338 struct kmem_cache *cachep;
3339 struct work_struct work;
3340};
3341
Glauber Costa1f458cb2012-12-18 14:22:50 -08003342static void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3343{
3344 struct kmem_cache *cachep;
3345 struct memcg_cache_params *params;
3346
3347 if (!memcg_kmem_is_active(memcg))
3348 return;
3349
3350 mutex_lock(&memcg->slab_caches_mutex);
3351 list_for_each_entry(params, &memcg->memcg_slab_caches, list) {
3352 cachep = memcg_params_to_cache(params);
3353 cachep->memcg_params->dead = true;
3354 INIT_WORK(&cachep->memcg_params->destroy,
Glauber Costa22933152012-12-18 14:22:59 -08003355 kmem_cache_destroy_work_func);
Glauber Costa1f458cb2012-12-18 14:22:50 -08003356 schedule_work(&cachep->memcg_params->destroy);
3357 }
3358 mutex_unlock(&memcg->slab_caches_mutex);
3359}
3360
Glauber Costad7f25f82012-12-18 14:22:40 -08003361static void memcg_create_cache_work_func(struct work_struct *w)
3362{
3363 struct create_work *cw;
3364
3365 cw = container_of(w, struct create_work, work);
3366 memcg_create_kmem_cache(cw->memcg, cw->cachep);
3367 /* Drop the reference gotten when we enqueued. */
3368 css_put(&cw->memcg->css);
3369 kfree(cw);
3370}
3371
3372/*
3373 * Enqueue the creation of a per-memcg kmem_cache.
3374 * Called with rcu_read_lock.
3375 */
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003376static void __memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3377 struct kmem_cache *cachep)
Glauber Costad7f25f82012-12-18 14:22:40 -08003378{
3379 struct create_work *cw;
3380
3381 cw = kmalloc(sizeof(struct create_work), GFP_NOWAIT);
3382 if (cw == NULL)
3383 return;
3384
3385 /* The corresponding put will be done in the workqueue. */
3386 if (!css_tryget(&memcg->css)) {
3387 kfree(cw);
3388 return;
3389 }
3390
3391 cw->memcg = memcg;
3392 cw->cachep = cachep;
3393
3394 INIT_WORK(&cw->work, memcg_create_cache_work_func);
3395 schedule_work(&cw->work);
3396}
3397
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003398static void memcg_create_cache_enqueue(struct mem_cgroup *memcg,
3399 struct kmem_cache *cachep)
3400{
3401 /*
3402 * We need to stop accounting when we kmalloc, because if the
3403 * corresponding kmalloc cache is not yet created, the first allocation
3404 * in __memcg_create_cache_enqueue will recurse.
3405 *
3406 * However, it is better to enclose the whole function. Depending on
3407 * the debugging options enabled, INIT_WORK(), for instance, can
3408 * trigger an allocation. This too, will make us recurse. Because at
3409 * this point we can't allow ourselves back into memcg_kmem_get_cache,
3410 * the safest choice is to do it like this, wrapping the whole function.
3411 */
3412 memcg_stop_kmem_account();
3413 __memcg_create_cache_enqueue(memcg, cachep);
3414 memcg_resume_kmem_account();
3415}
Glauber Costad7f25f82012-12-18 14:22:40 -08003416/*
3417 * Return the kmem_cache we're supposed to use for a slab allocation.
3418 * We try to use the current memcg's version of the cache.
3419 *
3420 * If the cache does not exist yet, if we are the first user of it,
3421 * we either create it immediately, if possible, or create it asynchronously
3422 * in a workqueue.
3423 * In the latter case, we will let the current allocation go through with
3424 * the original cache.
3425 *
3426 * Can't be called in interrupt context or from kernel threads.
3427 * This function needs to be called with rcu_read_lock() held.
3428 */
3429struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep,
3430 gfp_t gfp)
3431{
3432 struct mem_cgroup *memcg;
3433 int idx;
3434
3435 VM_BUG_ON(!cachep->memcg_params);
3436 VM_BUG_ON(!cachep->memcg_params->is_root_cache);
3437
Glauber Costa0e9d92f2012-12-18 14:22:42 -08003438 if (!current->mm || current->memcg_kmem_skip_account)
3439 return cachep;
3440
Glauber Costad7f25f82012-12-18 14:22:40 -08003441 rcu_read_lock();
3442 memcg = mem_cgroup_from_task(rcu_dereference(current->mm->owner));
3443 rcu_read_unlock();
3444
3445 if (!memcg_can_account_kmem(memcg))
3446 return cachep;
3447
3448 idx = memcg_cache_id(memcg);
3449
3450 /*
3451 * barrier to mare sure we're always seeing the up to date value. The
3452 * code updating memcg_caches will issue a write barrier to match this.
3453 */
3454 read_barrier_depends();
3455 if (unlikely(cachep->memcg_params->memcg_caches[idx] == NULL)) {
3456 /*
3457 * If we are in a safe context (can wait, and not in interrupt
3458 * context), we could be be predictable and return right away.
3459 * This would guarantee that the allocation being performed
3460 * already belongs in the new cache.
3461 *
3462 * However, there are some clashes that can arrive from locking.
3463 * For instance, because we acquire the slab_mutex while doing
3464 * kmem_cache_dup, this means no further allocation could happen
3465 * with the slab_mutex held.
3466 *
3467 * Also, because cache creation issue get_online_cpus(), this
3468 * creates a lock chain: memcg_slab_mutex -> cpu_hotplug_mutex,
3469 * that ends up reversed during cpu hotplug. (cpuset allocates
3470 * a bunch of GFP_KERNEL memory during cpuup). Due to all that,
3471 * better to defer everything.
3472 */
3473 memcg_create_cache_enqueue(memcg, cachep);
3474 return cachep;
3475 }
3476
3477 return cachep->memcg_params->memcg_caches[idx];
3478}
3479EXPORT_SYMBOL(__memcg_kmem_get_cache);
3480
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003481/*
3482 * We need to verify if the allocation against current->mm->owner's memcg is
3483 * possible for the given order. But the page is not allocated yet, so we'll
3484 * need a further commit step to do the final arrangements.
3485 *
3486 * It is possible for the task to switch cgroups in this mean time, so at
3487 * commit time, we can't rely on task conversion any longer. We'll then use
3488 * the handle argument to return to the caller which cgroup we should commit
3489 * against. We could also return the memcg directly and avoid the pointer
3490 * passing, but a boolean return value gives better semantics considering
3491 * the compiled-out case as well.
3492 *
3493 * Returning true means the allocation is possible.
3494 */
3495bool
3496__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
3497{
3498 struct mem_cgroup *memcg;
3499 int ret;
3500
3501 *_memcg = NULL;
3502 memcg = try_get_mem_cgroup_from_mm(current->mm);
3503
3504 /*
3505 * very rare case described in mem_cgroup_from_task. Unfortunately there
3506 * isn't much we can do without complicating this too much, and it would
3507 * be gfp-dependent anyway. Just let it go
3508 */
3509 if (unlikely(!memcg))
3510 return true;
3511
3512 if (!memcg_can_account_kmem(memcg)) {
3513 css_put(&memcg->css);
3514 return true;
3515 }
3516
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003517 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
3518 if (!ret)
3519 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003520
3521 css_put(&memcg->css);
3522 return (ret == 0);
3523}
3524
3525void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
3526 int order)
3527{
3528 struct page_cgroup *pc;
3529
3530 VM_BUG_ON(mem_cgroup_is_root(memcg));
3531
3532 /* The page allocation failed. Revert */
3533 if (!page) {
3534 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003535 return;
3536 }
3537
3538 pc = lookup_page_cgroup(page);
3539 lock_page_cgroup(pc);
3540 pc->mem_cgroup = memcg;
3541 SetPageCgroupUsed(pc);
3542 unlock_page_cgroup(pc);
3543}
3544
3545void __memcg_kmem_uncharge_pages(struct page *page, int order)
3546{
3547 struct mem_cgroup *memcg = NULL;
3548 struct page_cgroup *pc;
3549
3550
3551 pc = lookup_page_cgroup(page);
3552 /*
3553 * Fast unlocked return. Theoretically might have changed, have to
3554 * check again after locking.
3555 */
3556 if (!PageCgroupUsed(pc))
3557 return;
3558
3559 lock_page_cgroup(pc);
3560 if (PageCgroupUsed(pc)) {
3561 memcg = pc->mem_cgroup;
3562 ClearPageCgroupUsed(pc);
3563 }
3564 unlock_page_cgroup(pc);
3565
3566 /*
3567 * We trust that only if there is a memcg associated with the page, it
3568 * is a valid allocation
3569 */
3570 if (!memcg)
3571 return;
3572
3573 VM_BUG_ON(mem_cgroup_is_root(memcg));
3574 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003575}
Glauber Costa1f458cb2012-12-18 14:22:50 -08003576#else
3577static inline void mem_cgroup_destroy_all_caches(struct mem_cgroup *memcg)
3578{
3579}
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08003580#endif /* CONFIG_MEMCG_KMEM */
3581
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003582#ifdef CONFIG_TRANSPARENT_HUGEPAGE
3583
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07003584#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003585/*
3586 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003587 * zone->lru_lock, 'splitting on pmd' and compound_lock.
3588 * charge/uncharge will be never happen and move_account() is done under
3589 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003590 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003591void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003592{
3593 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003594 struct page_cgroup *pc;
3595 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003596
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08003597 if (mem_cgroup_disabled())
3598 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003599 for (i = 1; i < HPAGE_PMD_NR; i++) {
3600 pc = head_pc + i;
3601 pc->mem_cgroup = head_pc->mem_cgroup;
3602 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08003603 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
3604 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003605}
Hugh Dickins12d27102012-01-12 17:19:52 -08003606#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08003607
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003608/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07003609 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07003610 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003611 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003612 * @pc: page_cgroup of the page.
3613 * @from: mem_cgroup which the page is moved from.
3614 * @to: mem_cgroup which the page is moved to. @from != @to.
3615 *
3616 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003617 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003618 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003619 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003620 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
3621 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003622 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003623static int mem_cgroup_move_account(struct page *page,
3624 unsigned int nr_pages,
3625 struct page_cgroup *pc,
3626 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003627 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003628{
Johannes Weinerde3638d2011-03-23 16:42:28 -07003629 unsigned long flags;
3630 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003631 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003632
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003633 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07003634 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07003635 /*
3636 * The page is isolated from LRU. So, collapse function
3637 * will not handle this page. But page splitting can happen.
3638 * Do this check under compound_page_lock(). The caller should
3639 * hold it.
3640 */
3641 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003642 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07003643 goto out;
3644
3645 lock_page_cgroup(pc);
3646
3647 ret = -EINVAL;
3648 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
3649 goto unlock;
3650
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003651 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003652
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003653 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003654 /* Update mapped_file data for mem_cgroup */
3655 preempt_disable();
3656 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3657 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
3658 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07003659 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003660 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07003661
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003662 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003663 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003664 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07003665 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003666 ret = 0;
3667unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003668 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08003669 /*
3670 * check events
3671 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003672 memcg_check_events(to, page);
3673 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07003674out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003675 return ret;
3676}
3677
Michal Hocko2ef37d32012-10-26 13:37:30 +02003678/**
3679 * mem_cgroup_move_parent - moves page to the parent group
3680 * @page: the page to move
3681 * @pc: page_cgroup of the page
3682 * @child: page's cgroup
3683 *
3684 * move charges to its parent or the root cgroup if the group has no
3685 * parent (aka use_hierarchy==0).
3686 * Although this might fail (get_page_unless_zero, isolate_lru_page or
3687 * mem_cgroup_move_account fails) the failure is always temporary and
3688 * it signals a race with a page removal/uncharge or migration. In the
3689 * first case the page is on the way out and it will vanish from the LRU
3690 * on the next attempt and the call should be retried later.
3691 * Isolation from the LRU fails only if page has been isolated from
3692 * the LRU since we looked at it and that usually means either global
3693 * reclaim or migration going on. The page will either get back to the
3694 * LRU or vanish.
3695 * Finaly mem_cgroup_move_account fails only if the page got uncharged
3696 * (!PageCgroupUsed) or moved to a different group. The page will
3697 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003698 */
Johannes Weiner5564e882011-03-23 16:42:29 -07003699static int mem_cgroup_move_parent(struct page *page,
3700 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07003701 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003702{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003703 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003704 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07003705 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003706 int ret;
3707
Michal Hockod8423012012-10-26 13:37:29 +02003708 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003709
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003710 ret = -EBUSY;
3711 if (!get_page_unless_zero(page))
3712 goto out;
3713 if (isolate_lru_page(page))
3714 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08003715
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003716 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003717
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003718 parent = parent_mem_cgroup(child);
3719 /*
3720 * If no parent, move charges to root cgroup.
3721 */
3722 if (!parent)
3723 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003724
Michal Hocko2ef37d32012-10-26 13:37:30 +02003725 if (nr_pages > 1) {
3726 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003727 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02003728 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003729
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003730 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003731 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003732 if (!ret)
3733 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003734
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003735 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003736 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003737 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003738put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003739 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003740out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003741 return ret;
3742}
3743
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003744/*
3745 * Charge the memory controller for page usage.
3746 * Return
3747 * 0 if the charge was successful
3748 * < 0 if the cgroup is over its limit
3749 */
3750static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003751 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003752{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003753 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003754 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003755 bool oom = true;
3756 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003757
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003758 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003759 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003760 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003761 /*
3762 * Never OOM-kill a process for a huge page. The
3763 * fault handler will fall back to regular pages.
3764 */
3765 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003766 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003767
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003768 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003769 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003770 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003771 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003772 return 0;
3773}
3774
3775int mem_cgroup_newpage_charge(struct page *page,
3776 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003777{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003778 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003779 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003780 VM_BUG_ON(page_mapped(page));
3781 VM_BUG_ON(page->mapping && !PageAnon(page));
3782 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003783 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003784 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003785}
3786
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003787/*
3788 * While swap-in, try_charge -> commit or cancel, the page is locked.
3789 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003790 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003791 * "commit()" or removed by "cancel()"
3792 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003793static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3794 struct page *page,
3795 gfp_t mask,
3796 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003797{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003798 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003799 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003800 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003801
Johannes Weiner90deb782012-07-31 16:45:47 -07003802 pc = lookup_page_cgroup(page);
3803 /*
3804 * Every swap fault against a single page tries to charge the
3805 * page, bail as early as possible. shmem_unuse() encounters
3806 * already charged pages, too. The USED bit is protected by
3807 * the page lock, which serializes swap cache removal, which
3808 * in turn serializes uncharging.
3809 */
3810 if (PageCgroupUsed(pc))
3811 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003812 if (!do_swap_account)
3813 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003814 memcg = try_get_mem_cgroup_from_page(page);
3815 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003816 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003817 *memcgp = memcg;
3818 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003819 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003820 if (ret == -EINTR)
3821 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003822 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003823charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003824 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3825 if (ret == -EINTR)
3826 ret = 0;
3827 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003828}
3829
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003830int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3831 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3832{
3833 *memcgp = NULL;
3834 if (mem_cgroup_disabled())
3835 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003836 /*
3837 * A racing thread's fault, or swapoff, may have already
3838 * updated the pte, and even removed page from swap cache: in
3839 * those cases unuse_pte()'s pte_same() test will fail; but
3840 * there's also a KSM case which does need to charge the page.
3841 */
3842 if (!PageSwapCache(page)) {
3843 int ret;
3844
3845 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3846 if (ret == -EINTR)
3847 ret = 0;
3848 return ret;
3849 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003850 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3851}
3852
Johannes Weiner827a03d2012-07-31 16:45:36 -07003853void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3854{
3855 if (mem_cgroup_disabled())
3856 return;
3857 if (!memcg)
3858 return;
3859 __mem_cgroup_cancel_charge(memcg, 1);
3860}
3861
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003862static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003863__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003864 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003865{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003866 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003867 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003868 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003869 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003870
Johannes Weinerce587e62012-04-24 20:22:33 +02003871 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003872 /*
3873 * Now swap is on-memory. This means this page may be
3874 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003875 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3876 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3877 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003878 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003879 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003880 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003881 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003882 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003883}
3884
Johannes Weiner72835c82012-01-12 17:18:32 -08003885void mem_cgroup_commit_charge_swapin(struct page *page,
3886 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003887{
Johannes Weiner72835c82012-01-12 17:18:32 -08003888 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003889 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003890}
3891
Johannes Weiner827a03d2012-07-31 16:45:36 -07003892int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3893 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003894{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003895 struct mem_cgroup *memcg = NULL;
3896 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3897 int ret;
3898
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003899 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003900 return 0;
3901 if (PageCompound(page))
3902 return 0;
3903
Johannes Weiner827a03d2012-07-31 16:45:36 -07003904 if (!PageSwapCache(page))
3905 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3906 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003907 ret = __mem_cgroup_try_charge_swapin(mm, page,
3908 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003909 if (!ret)
3910 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3911 }
3912 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003913}
3914
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003915static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003916 unsigned int nr_pages,
3917 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003918{
3919 struct memcg_batch_info *batch = NULL;
3920 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003921
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003922 /* If swapout, usage of swap doesn't decrease */
3923 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3924 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003925
3926 batch = &current->memcg_batch;
3927 /*
3928 * In usual, we do css_get() when we remember memcg pointer.
3929 * But in this case, we keep res->usage until end of a series of
3930 * uncharges. Then, it's ok to ignore memcg's refcnt.
3931 */
3932 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003933 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003934 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003935 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003936 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003937 * the same cgroup and we have chance to coalesce uncharges.
3938 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3939 * because we want to do uncharge as soon as possible.
3940 */
3941
3942 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3943 goto direct_uncharge;
3944
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003945 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003946 goto direct_uncharge;
3947
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003948 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003949 * In typical case, batch->memcg == mem. This means we can
3950 * merge a series of uncharges to an uncharge of res_counter.
3951 * If not, we uncharge res_counter ony by one.
3952 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003953 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003954 goto direct_uncharge;
3955 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003956 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003957 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003958 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003959 return;
3960direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003961 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003962 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003963 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3964 if (unlikely(batch->memcg != memcg))
3965 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003966}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003967
Balbir Singh8697d332008-02-07 00:13:59 -08003968/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003969 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003970 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003971static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003972__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3973 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003974{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003975 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003976 unsigned int nr_pages = 1;
3977 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003978 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003979
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003980 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003981 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003982
Johannes Weiner0c59b892012-07-31 16:45:31 -07003983 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003984
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003985 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003986 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003987 VM_BUG_ON(!PageTransHuge(page));
3988 }
Balbir Singh8697d332008-02-07 00:13:59 -08003989 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003990 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003991 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003992 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003993 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003994 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003995
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003996 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003997
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003998 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003999
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004000 if (!PageCgroupUsed(pc))
4001 goto unlock_out;
4002
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004003 anon = PageAnon(page);
4004
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004005 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004006 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07004007 /*
4008 * Generally PageAnon tells if it's the anon statistics to be
4009 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
4010 * used before page reached the stage of being marked PageAnon.
4011 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004012 anon = true;
4013 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004014 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004015 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07004016 if (page_mapped(page))
4017 goto unlock_out;
4018 /*
4019 * Pages under migration may not be uncharged. But
4020 * end_migration() /must/ be the one uncharging the
4021 * unused post-migration page and so it has to call
4022 * here with the migration bit still set. See the
4023 * res_counter handling below.
4024 */
4025 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004026 goto unlock_out;
4027 break;
4028 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
4029 if (!PageAnon(page)) { /* Shared memory */
4030 if (page->mapping && !page_is_file_cache(page))
4031 goto unlock_out;
4032 } else if (page_mapped(page)) /* Anon */
4033 goto unlock_out;
4034 break;
4035 default:
4036 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004037 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004038
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004039 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004040
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004041 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08004042 /*
4043 * pc->mem_cgroup is not cleared here. It will be accessed when it's
4044 * freed from LRU. This is safe because uncharged page is expected not
4045 * to be reused (freed soon). Exception is SwapCache, it's handled by
4046 * special functions.
4047 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004048
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004049 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004050 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004051 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004052 * will never be freed.
4053 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004054 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004055 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004056 mem_cgroup_swap_statistics(memcg, true);
4057 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004058 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004059 /*
4060 * Migration does not charge the res_counter for the
4061 * replacement page, so leave it alone when phasing out the
4062 * page that is unused after the migration.
4063 */
4064 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004065 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004066
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004067 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004068
4069unlock_out:
4070 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004071 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08004072}
4073
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004074void mem_cgroup_uncharge_page(struct page *page)
4075{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004076 /* early check. */
4077 if (page_mapped(page))
4078 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08004079 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07004080 if (PageSwapCache(page))
4081 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07004082 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004083}
4084
4085void mem_cgroup_uncharge_cache_page(struct page *page)
4086{
4087 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07004088 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07004089 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004090}
4091
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004092/*
4093 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
4094 * In that cases, pages are freed continuously and we can expect pages
4095 * are in the same memcg. All these calls itself limits the number of
4096 * pages freed at once, then uncharge_start/end() is called properly.
4097 * This may be called prural(2) times in a context,
4098 */
4099
4100void mem_cgroup_uncharge_start(void)
4101{
4102 current->memcg_batch.do_batch++;
4103 /* We can do nest. */
4104 if (current->memcg_batch.do_batch == 1) {
4105 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004106 current->memcg_batch.nr_pages = 0;
4107 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004108 }
4109}
4110
4111void mem_cgroup_uncharge_end(void)
4112{
4113 struct memcg_batch_info *batch = &current->memcg_batch;
4114
4115 if (!batch->do_batch)
4116 return;
4117
4118 batch->do_batch--;
4119 if (batch->do_batch) /* If stacked, do nothing. */
4120 return;
4121
4122 if (!batch->memcg)
4123 return;
4124 /*
4125 * This "batch->memcg" is valid without any css_get/put etc...
4126 * bacause we hide charges behind us.
4127 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07004128 if (batch->nr_pages)
4129 res_counter_uncharge(&batch->memcg->res,
4130 batch->nr_pages * PAGE_SIZE);
4131 if (batch->memsw_nr_pages)
4132 res_counter_uncharge(&batch->memcg->memsw,
4133 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004134 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08004135 /* forget this pointer (for sanity check) */
4136 batch->memcg = NULL;
4137}
4138
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004139#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004140/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004141 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004142 * memcg information is recorded to swap_cgroup of "ent"
4143 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004144void
4145mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004146{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004147 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004148 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004149
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004150 if (!swapout) /* this was a swap cache but the swap is unused ! */
4151 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
4152
Johannes Weiner0030f532012-07-31 16:45:25 -07004153 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07004154
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07004155 /*
4156 * record memcg information, if swapout && memcg != NULL,
4157 * mem_cgroup_get() was called in uncharge().
4158 */
4159 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004160 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004161}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07004162#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08004163
Andrew Mortonc255a452012-07-31 16:43:02 -07004164#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004165/*
4166 * called from swap_entry_free(). remove record in swap_cgroup and
4167 * uncharge "memsw" account.
4168 */
4169void mem_cgroup_uncharge_swap(swp_entry_t ent)
4170{
4171 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004172 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004173
4174 if (!do_swap_account)
4175 return;
4176
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004177 id = swap_cgroup_record(ent, 0);
4178 rcu_read_lock();
4179 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004180 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004181 /*
4182 * We uncharge this because swap is freed.
4183 * This memcg can be obsolete one. We avoid calling css_tryget
4184 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004185 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07004186 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004187 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004188 mem_cgroup_put(memcg);
4189 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07004190 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004191}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004192
4193/**
4194 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
4195 * @entry: swap entry to be moved
4196 * @from: mem_cgroup which the entry is moved from
4197 * @to: mem_cgroup which the entry is moved to
4198 *
4199 * It succeeds only when the swap_cgroup's record for this entry is the same
4200 * as the mem_cgroup's id of @from.
4201 *
4202 * Returns 0 on success, -EINVAL on failure.
4203 *
4204 * The caller must have charged to @to, IOW, called res_counter_charge() about
4205 * both res and memsw, and called css_get().
4206 */
4207static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004208 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004209{
4210 unsigned short old_id, new_id;
4211
4212 old_id = css_id(&from->css);
4213 new_id = css_id(&to->css);
4214
4215 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004216 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004217 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004218 /*
4219 * This function is only called from task migration context now.
4220 * It postpones res_counter and refcount handling till the end
4221 * of task migration(mem_cgroup_clear_mc()) for performance
4222 * improvement. But we cannot postpone mem_cgroup_get(to)
4223 * because if the process that has been moved to @to does
4224 * swap-in, the refcount of @to might be decreased to 0.
4225 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08004226 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08004227 return 0;
4228 }
4229 return -EINVAL;
4230}
4231#else
4232static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07004233 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004234{
4235 return -EINVAL;
4236}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004237#endif
4238
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004239/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004240 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
4241 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004242 */
Johannes Weiner0030f532012-07-31 16:45:25 -07004243void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
4244 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004245{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004246 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00004247 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07004248 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004249 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004250
Johannes Weiner72835c82012-01-12 17:18:32 -08004251 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07004252
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004253 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07004254 return;
Balbir Singh40779602008-04-04 14:29:59 -07004255
Mel Gormanb32967f2012-11-19 12:35:47 +00004256 if (PageTransHuge(page))
4257 nr_pages <<= compound_order(page);
4258
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004259 pc = lookup_page_cgroup(page);
4260 lock_page_cgroup(pc);
4261 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004262 memcg = pc->mem_cgroup;
4263 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004264 /*
4265 * At migrating an anonymous page, its mapcount goes down
4266 * to 0 and uncharge() will be called. But, even if it's fully
4267 * unmapped, migration may fail and this page has to be
4268 * charged again. We set MIGRATION flag here and delay uncharge
4269 * until end_migration() is called
4270 *
4271 * Corner Case Thinking
4272 * A)
4273 * When the old page was mapped as Anon and it's unmap-and-freed
4274 * while migration was ongoing.
4275 * If unmap finds the old page, uncharge() of it will be delayed
4276 * until end_migration(). If unmap finds a new page, it's
4277 * uncharged when it make mapcount to be 1->0. If unmap code
4278 * finds swap_migration_entry, the new page will not be mapped
4279 * and end_migration() will find it(mapcount==0).
4280 *
4281 * B)
4282 * When the old page was mapped but migraion fails, the kernel
4283 * remaps it. A charge for it is kept by MIGRATION flag even
4284 * if mapcount goes down to 0. We can do remap successfully
4285 * without charging it again.
4286 *
4287 * C)
4288 * The "old" page is under lock_page() until the end of
4289 * migration, so, the old page itself will not be swapped-out.
4290 * If the new page is swapped out before end_migraton, our
4291 * hook to usual swap-out path will catch the event.
4292 */
4293 if (PageAnon(page))
4294 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08004295 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004296 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004297 /*
4298 * If the page is not charged at this point,
4299 * we return here.
4300 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004301 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07004302 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004303
Johannes Weiner72835c82012-01-12 17:18:32 -08004304 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004305 /*
4306 * We charge new page before it's used/mapped. So, even if unlock_page()
4307 * is called before end_migration, we can catch all events on this new
4308 * page. In the case new page is migrated but not remapped, new page's
4309 * mapcount will be finally 0 and we call uncharge in end_migration().
4310 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004311 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07004312 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004313 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07004314 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07004315 /*
4316 * The page is committed to the memcg, but it's not actually
4317 * charged to the res_counter since we plan on replacing the
4318 * old one and only one page is going to be left afterwards.
4319 */
Mel Gormanb32967f2012-11-19 12:35:47 +00004320 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004321}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08004322
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004323/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004324void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004325 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07004326{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004327 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004328 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004329 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004330
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004331 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004332 return;
Tejun Heob25ed602012-11-05 09:16:59 -08004333
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08004334 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004335 used = oldpage;
4336 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004337 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004338 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004339 unused = oldpage;
4340 }
Johannes Weiner0030f532012-07-31 16:45:25 -07004341 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07004342 __mem_cgroup_uncharge_common(unused,
4343 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
4344 : MEM_CGROUP_CHARGE_TYPE_CACHE,
4345 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07004346 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004347 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004348 * We disallowed uncharge of pages under migration because mapcount
4349 * of the page goes down to zero, temporarly.
4350 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07004351 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004352 pc = lookup_page_cgroup(oldpage);
4353 lock_page_cgroup(pc);
4354 ClearPageCgroupMigration(pc);
4355 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004356
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004357 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004358 * If a page is a file cache, radix-tree replacement is very atomic
4359 * and we can skip this check. When it was an Anon page, its mapcount
4360 * goes down to 0. But because we added MIGRATION flage, it's not
4361 * uncharged yet. There are several case but page->mapcount check
4362 * and USED bit check in mem_cgroup_uncharge_page() will do enough
4363 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08004364 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07004365 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07004366 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08004367}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004368
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004369/*
4370 * At replace page cache, newpage is not under any memcg but it's on
4371 * LRU. So, this function doesn't touch res_counter but handles LRU
4372 * in correct way. Both pages are locked so we cannot race with uncharge.
4373 */
4374void mem_cgroup_replace_page_cache(struct page *oldpage,
4375 struct page *newpage)
4376{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004377 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004378 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004379 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004380
4381 if (mem_cgroup_disabled())
4382 return;
4383
4384 pc = lookup_page_cgroup(oldpage);
4385 /* fix accounting on old pages */
4386 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004387 if (PageCgroupUsed(pc)) {
4388 memcg = pc->mem_cgroup;
4389 mem_cgroup_charge_statistics(memcg, false, -1);
4390 ClearPageCgroupUsed(pc);
4391 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004392 unlock_page_cgroup(pc);
4393
Hugh Dickinsbde05d12012-05-29 15:06:38 -07004394 /*
4395 * When called from shmem_replace_page(), in some cases the
4396 * oldpage has already been charged, and in some cases not.
4397 */
4398 if (!memcg)
4399 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004400 /*
4401 * Even if newpage->mapping was NULL before starting replacement,
4402 * the newpage may be on LRU(or pagevec for LRU) already. We lock
4403 * LRU while we overwrite pc->mem_cgroup.
4404 */
Johannes Weinerce587e62012-04-24 20:22:33 +02004405 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08004406}
4407
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004408#ifdef CONFIG_DEBUG_VM
4409static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
4410{
4411 struct page_cgroup *pc;
4412
4413 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08004414 /*
4415 * Can be NULL while feeding pages into the page allocator for
4416 * the first time, i.e. during boot or memory hotplug;
4417 * or when mem_cgroup_disabled().
4418 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004419 if (likely(pc) && PageCgroupUsed(pc))
4420 return pc;
4421 return NULL;
4422}
4423
4424bool mem_cgroup_bad_page_check(struct page *page)
4425{
4426 if (mem_cgroup_disabled())
4427 return false;
4428
4429 return lookup_page_cgroup_used(page) != NULL;
4430}
4431
4432void mem_cgroup_print_bad_page(struct page *page)
4433{
4434 struct page_cgroup *pc;
4435
4436 pc = lookup_page_cgroup_used(page);
4437 if (pc) {
Andrew Mortond0451972013-02-22 16:32:06 -08004438 pr_alert("pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
4439 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07004440 }
4441}
4442#endif
4443
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08004444static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004445 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004446{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004447 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004448 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004449 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004450 int children = mem_cgroup_count_children(memcg);
4451 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004452 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004453
4454 /*
4455 * For keeping hierarchical_reclaim simple, how long we should retry
4456 * is depends on callers. We set our retry-count to be function
4457 * of # of children which we should visit in this loop.
4458 */
4459 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
4460
4461 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004462
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004463 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004464 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004465 if (signal_pending(current)) {
4466 ret = -EINTR;
4467 break;
4468 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004469 /*
4470 * Rather than hide all in some function, I do this in
4471 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004472 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004473 */
4474 mutex_lock(&set_limit_mutex);
4475 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4476 if (memswlimit < val) {
4477 ret = -EINVAL;
4478 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004479 break;
4480 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004481
4482 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4483 if (memlimit < val)
4484 enlarge = 1;
4485
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004486 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004487 if (!ret) {
4488 if (memswlimit == val)
4489 memcg->memsw_is_minimum = true;
4490 else
4491 memcg->memsw_is_minimum = false;
4492 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004493 mutex_unlock(&set_limit_mutex);
4494
4495 if (!ret)
4496 break;
4497
Johannes Weiner56600482012-01-12 17:17:59 -08004498 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4499 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004500 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
4501 /* Usage is reduced ? */
4502 if (curusage >= oldusage)
4503 retry_count--;
4504 else
4505 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004506 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004507 if (!ret && enlarge)
4508 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08004509
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004510 return ret;
4511}
4512
Li Zefan338c8432009-06-17 16:27:15 -07004513static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
4514 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004515{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004516 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004517 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004518 int children = mem_cgroup_count_children(memcg);
4519 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004520 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004521
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004522 /* see mem_cgroup_resize_res_limit */
4523 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
4524 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004525 while (retry_count) {
4526 if (signal_pending(current)) {
4527 ret = -EINTR;
4528 break;
4529 }
4530 /*
4531 * Rather than hide all in some function, I do this in
4532 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07004533 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004534 */
4535 mutex_lock(&set_limit_mutex);
4536 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4537 if (memlimit > val) {
4538 ret = -EINVAL;
4539 mutex_unlock(&set_limit_mutex);
4540 break;
4541 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004542 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4543 if (memswlimit < val)
4544 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004545 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07004546 if (!ret) {
4547 if (memlimit == val)
4548 memcg->memsw_is_minimum = true;
4549 else
4550 memcg->memsw_is_minimum = false;
4551 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004552 mutex_unlock(&set_limit_mutex);
4553
4554 if (!ret)
4555 break;
4556
Johannes Weiner56600482012-01-12 17:17:59 -08004557 mem_cgroup_reclaim(memcg, GFP_KERNEL,
4558 MEM_CGROUP_RECLAIM_NOSWAP |
4559 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004560 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004561 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004562 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004563 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07004564 else
4565 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004566 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004567 if (!ret && enlarge)
4568 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004569 return ret;
4570}
4571
Balbir Singh4e416952009-09-23 15:56:39 -07004572unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07004573 gfp_t gfp_mask,
4574 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07004575{
4576 unsigned long nr_reclaimed = 0;
4577 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
4578 unsigned long reclaimed;
4579 int loop = 0;
4580 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004581 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07004582 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004583
4584 if (order > 0)
4585 return 0;
4586
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07004587 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07004588 /*
4589 * This loop can run a while, specially if mem_cgroup's continuously
4590 * keep exceeding their soft limit and putting the system under
4591 * pressure
4592 */
4593 do {
4594 if (next_mz)
4595 mz = next_mz;
4596 else
4597 mz = mem_cgroup_largest_soft_limit_node(mctz);
4598 if (!mz)
4599 break;
4600
Ying Han0ae5e892011-05-26 16:25:25 -07004601 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004602 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08004603 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07004604 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07004605 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07004606 spin_lock(&mctz->lock);
4607
4608 /*
4609 * If we failed to reclaim anything from this memory cgroup
4610 * it is time to move on to the next cgroup
4611 */
4612 next_mz = NULL;
4613 if (!reclaimed) {
4614 do {
4615 /*
4616 * Loop until we find yet another one.
4617 *
4618 * By the time we get the soft_limit lock
4619 * again, someone might have aded the
4620 * group back on the RB tree. Iterate to
4621 * make sure we get a different mem.
4622 * mem_cgroup_largest_soft_limit_node returns
4623 * NULL if no other cgroup is present on
4624 * the tree
4625 */
4626 next_mz =
4627 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004628 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004629 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07004630 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07004631 break;
4632 } while (1);
4633 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004634 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
4635 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07004636 /*
4637 * One school of thought says that we should not add
4638 * back the node to the tree if reclaim returns 0.
4639 * But our reclaim could return 0, simply because due
4640 * to priority we are exposing a smaller subset of
4641 * memory to reclaim from. Consider this as a longer
4642 * term TODO.
4643 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07004644 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004645 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07004646 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004647 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004648 loop++;
4649 /*
4650 * Could not reclaim anything and there are no more
4651 * mem cgroups to try or we seem to be looping without
4652 * reclaiming anything.
4653 */
4654 if (!nr_reclaimed &&
4655 (next_mz == NULL ||
4656 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
4657 break;
4658 } while (!nr_reclaimed);
4659 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004660 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07004661 return nr_reclaimed;
4662}
4663
Michal Hocko2ef37d32012-10-26 13:37:30 +02004664/**
4665 * mem_cgroup_force_empty_list - clears LRU of a group
4666 * @memcg: group to clear
4667 * @node: NUMA node
4668 * @zid: zone id
4669 * @lru: lru to to clear
4670 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004671 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02004672 * reclaim the pages page themselves - pages are moved to the parent (or root)
4673 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004674 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02004675static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004676 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004677{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004678 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004679 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004680 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08004681 struct page *busy;
4682 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08004683
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004684 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004685 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
4686 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004687
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004688 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004689 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08004690 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07004691 struct page *page;
4692
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004693 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004694 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004695 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004696 break;
4697 }
Johannes Weiner925b7672012-01-12 17:18:15 -08004698 page = list_entry(list->prev, struct page, lru);
4699 if (busy == page) {
4700 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08004701 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004702 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004703 continue;
4704 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004705 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004706
Johannes Weiner925b7672012-01-12 17:18:15 -08004707 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07004708
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07004709 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004710 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08004711 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004712 cond_resched();
4713 } else
4714 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02004715 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004716}
4717
4718/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004719 * make mem_cgroup's charge to be 0 if there is no task by moving
4720 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004721 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02004722 *
4723 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004724 */
Michal Hockoab5196c2012-10-26 13:37:32 +02004725static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004726{
Michal Hockoc26251f2012-10-26 13:37:28 +02004727 int node, zid;
Glauber Costabea207c2012-12-18 14:22:11 -08004728 u64 usage;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004729
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004730 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004731 /* This is for making all *used* pages to be on LRU. */
4732 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004733 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004734 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004735 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004736 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004737 enum lru_list lru;
4738 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004739 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004740 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004741 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004742 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004743 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004744 mem_cgroup_end_move(memcg);
4745 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004746 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004747
Michal Hocko2ef37d32012-10-26 13:37:30 +02004748 /*
Glauber Costabea207c2012-12-18 14:22:11 -08004749 * Kernel memory may not necessarily be trackable to a specific
4750 * process. So they are not migrated, and therefore we can't
4751 * expect their value to drop to 0 here.
4752 * Having res filled up with kmem only is enough.
4753 *
Michal Hocko2ef37d32012-10-26 13:37:30 +02004754 * This is a safety check because mem_cgroup_force_empty_list
4755 * could have raced with mem_cgroup_replace_page_cache callers
4756 * so the lru seemed empty but the page could have been added
4757 * right after the check. RES_USAGE should be safe as we always
4758 * charge before adding to the LRU.
4759 */
Glauber Costabea207c2012-12-18 14:22:11 -08004760 usage = res_counter_read_u64(&memcg->res, RES_USAGE) -
4761 res_counter_read_u64(&memcg->kmem, RES_USAGE);
4762 } while (usage > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004763}
4764
4765/*
Glauber Costab5f99b52013-02-22 16:34:53 -08004766 * This mainly exists for tests during the setting of set of use_hierarchy.
4767 * Since this is the very setting we are changing, the current hierarchy value
4768 * is meaningless
4769 */
4770static inline bool __memcg_has_children(struct mem_cgroup *memcg)
4771{
4772 struct cgroup *pos;
4773
4774 /* bounce at first found */
4775 cgroup_for_each_child(pos, memcg->css.cgroup)
4776 return true;
4777 return false;
4778}
4779
4780/*
4781 * Must be called with cgroup_lock held, unless the cgroup is guaranteed to be
4782 * already dead (in mem_cgroup_force_empty(), for instance). This is different
4783 * from mem_cgroup_count_children(), in the sense that we don't really care how
4784 * many children we have; we only need to know if we have any. It also counts
4785 * any memcg without hierarchy as infertile.
4786 */
4787static inline bool memcg_has_children(struct mem_cgroup *memcg)
4788{
4789 return memcg->use_hierarchy && __memcg_has_children(memcg);
4790}
4791
4792/*
Michal Hockoc26251f2012-10-26 13:37:28 +02004793 * Reclaims as many pages from the given memcg as possible and moves
4794 * the rest to the parent.
4795 *
4796 * Caller is responsible for holding css reference for memcg.
4797 */
4798static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4799{
4800 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4801 struct cgroup *cgrp = memcg->css.cgroup;
4802
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004803 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004804 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4805 return -EBUSY;
4806
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004807 /* we call try-to-free pages for make this cgroup empty */
4808 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004809 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004810 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004811 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004812
Michal Hockoc26251f2012-10-26 13:37:28 +02004813 if (signal_pending(current))
4814 return -EINTR;
4815
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004816 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004817 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004818 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004819 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004820 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004821 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004822 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004823
4824 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004825 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004826 mem_cgroup_reparent_charges(memcg);
4827
4828 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004829}
4830
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004831static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004832{
Michal Hockoc26251f2012-10-26 13:37:28 +02004833 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4834 int ret;
4835
Michal Hockod8423012012-10-26 13:37:29 +02004836 if (mem_cgroup_is_root(memcg))
4837 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004838 css_get(&memcg->css);
4839 ret = mem_cgroup_force_empty(memcg);
4840 css_put(&memcg->css);
4841
4842 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004843}
4844
4845
Balbir Singh18f59ea2009-01-07 18:08:07 -08004846static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4847{
4848 return mem_cgroup_from_cont(cont)->use_hierarchy;
4849}
4850
4851static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4852 u64 val)
4853{
4854 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004855 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004856 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004857 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004858
4859 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004860 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004861
4862 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004863
4864 if (memcg->use_hierarchy == val)
4865 goto out;
4866
Balbir Singh18f59ea2009-01-07 18:08:07 -08004867 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004868 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004869 * in the child subtrees. If it is unset, then the change can
4870 * occur, provided the current cgroup has no children.
4871 *
4872 * For the root cgroup, parent_mem is NULL, we allow value to be
4873 * set if there are no children.
4874 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004875 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004876 (val == 1 || val == 0)) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004877 if (!__memcg_has_children(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004878 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004879 else
4880 retval = -EBUSY;
4881 } else
4882 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004883
4884out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004885 cgroup_unlock();
4886
4887 return retval;
4888}
4889
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004891static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004892 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004893{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004894 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004895 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004896
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004897 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004898 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004899 val += mem_cgroup_read_stat(iter, idx);
4900
4901 if (val < 0) /* race ? */
4902 val = 0;
4903 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004904}
4905
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004906static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004907{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004908 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004909
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004910 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004911 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004912 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004913 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004914 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004915 }
4916
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004917 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4918 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004919
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004920 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004921 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004922
4923 return val << PAGE_SHIFT;
4924}
4925
Tejun Heoaf36f902012-04-01 12:09:55 -07004926static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4927 struct file *file, char __user *buf,
4928 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004929{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004930 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004931 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004932 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004933 int name, len;
4934 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004935
4936 type = MEMFILE_TYPE(cft->private);
4937 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004938
4939 if (!do_swap_account && type == _MEMSWAP)
4940 return -EOPNOTSUPP;
4941
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004942 switch (type) {
4943 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004944 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004945 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004946 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004947 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004948 break;
4949 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004950 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004951 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004952 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004953 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004954 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004955 case _KMEM:
4956 val = res_counter_read_u64(&memcg->kmem, name);
4957 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004958 default:
4959 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004960 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004961
4962 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4963 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004964}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004965
4966static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4967{
4968 int ret = -EINVAL;
4969#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08004970 bool must_inc_static_branch = false;
4971
Glauber Costa510fc4e2012-12-18 14:21:47 -08004972 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4973 /*
4974 * For simplicity, we won't allow this to be disabled. It also can't
4975 * be changed if the cgroup has children already, or if tasks had
4976 * already joined.
4977 *
4978 * If tasks join before we set the limit, a person looking at
4979 * kmem.usage_in_bytes will have no way to determine when it took
4980 * place, which makes the value quite meaningless.
4981 *
4982 * After it first became limited, changes in the value of the limit are
4983 * of course permitted.
4984 *
4985 * Taking the cgroup_lock is really offensive, but it is so far the only
4986 * way to guarantee that no children will appear. There are plenty of
4987 * other offenders, and they should all go away. Fine grained locking
4988 * is probably the way to go here. When we are fully hierarchical, we
4989 * can also get rid of the use_hierarchy check.
4990 */
4991 cgroup_lock();
4992 mutex_lock(&set_limit_mutex);
4993 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
Glauber Costab5f99b52013-02-22 16:34:53 -08004994 if (cgroup_task_count(cont) || memcg_has_children(memcg)) {
Glauber Costa510fc4e2012-12-18 14:21:47 -08004995 ret = -EBUSY;
4996 goto out;
4997 }
4998 ret = res_counter_set_limit(&memcg->kmem, val);
4999 VM_BUG_ON(ret);
5000
Glauber Costa55007d82012-12-18 14:22:38 -08005001 ret = memcg_update_cache_sizes(memcg);
5002 if (ret) {
5003 res_counter_set_limit(&memcg->kmem, RESOURCE_MAX);
5004 goto out;
5005 }
Glauber Costaa8964b92012-12-18 14:22:09 -08005006 must_inc_static_branch = true;
Glauber Costa7de37682012-12-18 14:22:07 -08005007 /*
5008 * kmem charges can outlive the cgroup. In the case of slab
5009 * pages, for instance, a page contain objects from various
5010 * processes, so it is unfeasible to migrate them away. We
5011 * need to reference count the memcg because of that.
5012 */
5013 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005014 } else
5015 ret = res_counter_set_limit(&memcg->kmem, val);
5016out:
5017 mutex_unlock(&set_limit_mutex);
5018 cgroup_unlock();
Glauber Costaa8964b92012-12-18 14:22:09 -08005019
5020 /*
5021 * We are by now familiar with the fact that we can't inc the static
5022 * branch inside cgroup_lock. See disarm functions for details. A
5023 * worker here is overkill, but also wrong: After the limit is set, we
5024 * must start accounting right away. Since this operation can't fail,
5025 * we can safely defer it to here - no rollback will be needed.
5026 *
5027 * The boolean used to control this is also safe, because
5028 * KMEM_ACCOUNTED_ACTIVATED guarantees that only one process will be
5029 * able to set it to true;
5030 */
5031 if (must_inc_static_branch) {
5032 static_key_slow_inc(&memcg_kmem_enabled_key);
5033 /*
5034 * setting the active bit after the inc will guarantee no one
5035 * starts accounting before all call sites are patched
5036 */
5037 memcg_kmem_set_active(memcg);
5038 }
5039
Glauber Costa510fc4e2012-12-18 14:21:47 -08005040#endif
5041 return ret;
5042}
5043
Glauber Costa55007d82012-12-18 14:22:38 -08005044static int memcg_propagate_kmem(struct mem_cgroup *memcg)
Glauber Costa510fc4e2012-12-18 14:21:47 -08005045{
Glauber Costa55007d82012-12-18 14:22:38 -08005046 int ret = 0;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005047 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5048 if (!parent)
Glauber Costa55007d82012-12-18 14:22:38 -08005049 goto out;
5050
Glauber Costa510fc4e2012-12-18 14:21:47 -08005051 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costa7de37682012-12-18 14:22:07 -08005052#ifdef CONFIG_MEMCG_KMEM
Glauber Costaa8964b92012-12-18 14:22:09 -08005053 /*
5054 * When that happen, we need to disable the static branch only on those
5055 * memcgs that enabled it. To achieve this, we would be forced to
5056 * complicate the code by keeping track of which memcgs were the ones
5057 * that actually enabled limits, and which ones got it from its
5058 * parents.
5059 *
5060 * It is a lot simpler just to do static_key_slow_inc() on every child
5061 * that is accounted.
5062 */
Glauber Costa55007d82012-12-18 14:22:38 -08005063 if (!memcg_kmem_is_active(memcg))
5064 goto out;
5065
5066 /*
5067 * destroy(), called if we fail, will issue static_key_slow_inc() and
5068 * mem_cgroup_put() if kmem is enabled. We have to either call them
5069 * unconditionally, or clear the KMEM_ACTIVE flag. I personally find
5070 * this more consistent, since it always leads to the same destroy path
5071 */
5072 mem_cgroup_get(memcg);
5073 static_key_slow_inc(&memcg_kmem_enabled_key);
5074
5075 mutex_lock(&set_limit_mutex);
5076 ret = memcg_update_cache_sizes(memcg);
5077 mutex_unlock(&set_limit_mutex);
Glauber Costa7de37682012-12-18 14:22:07 -08005078#endif
Glauber Costa55007d82012-12-18 14:22:38 -08005079out:
5080 return ret;
Glauber Costa510fc4e2012-12-18 14:21:47 -08005081}
5082
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005083/*
5084 * The user of this function is...
5085 * RES_LIMIT.
5086 */
Paul Menage856c13a2008-07-25 01:47:04 -07005087static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
5088 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005089{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005090 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005091 enum res_type type;
5092 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005093 unsigned long long val;
5094 int ret;
5095
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005096 type = MEMFILE_TYPE(cft->private);
5097 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07005098
5099 if (!do_swap_account && type == _MEMSWAP)
5100 return -EOPNOTSUPP;
5101
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005102 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005103 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07005104 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
5105 ret = -EINVAL;
5106 break;
5107 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005108 /* This function does all necessary parse...reuse it */
5109 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005110 if (ret)
5111 break;
5112 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005113 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005114 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005115 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005116 else if (type == _KMEM)
5117 ret = memcg_update_kmem_limit(cont, val);
5118 else
5119 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005120 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07005121 case RES_SOFT_LIMIT:
5122 ret = res_counter_memparse_write_strategy(buffer, &val);
5123 if (ret)
5124 break;
5125 /*
5126 * For memsw, soft limits are hard to implement in terms
5127 * of semantics, for now, we support soft limits for
5128 * control without swap
5129 */
5130 if (type == _MEM)
5131 ret = res_counter_set_soft_limit(&memcg->res, val);
5132 else
5133 ret = -EINVAL;
5134 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07005135 default:
5136 ret = -EINVAL; /* should be BUG() ? */
5137 break;
5138 }
5139 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005140}
5141
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005142static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
5143 unsigned long long *mem_limit, unsigned long long *memsw_limit)
5144{
5145 struct cgroup *cgroup;
5146 unsigned long long min_limit, min_memsw_limit, tmp;
5147
5148 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
5149 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5150 cgroup = memcg->css.cgroup;
5151 if (!memcg->use_hierarchy)
5152 goto out;
5153
5154 while (cgroup->parent) {
5155 cgroup = cgroup->parent;
5156 memcg = mem_cgroup_from_cont(cgroup);
5157 if (!memcg->use_hierarchy)
5158 break;
5159 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
5160 min_limit = min(min_limit, tmp);
5161 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
5162 min_memsw_limit = min(min_memsw_limit, tmp);
5163 }
5164out:
5165 *mem_limit = min_limit;
5166 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005167}
5168
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005169static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005170{
Tejun Heoaf36f902012-04-01 12:09:55 -07005171 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08005172 int name;
5173 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005174
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005175 type = MEMFILE_TYPE(event);
5176 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07005177
5178 if (!do_swap_account && type == _MEMSWAP)
5179 return -EOPNOTSUPP;
5180
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005181 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005182 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005183 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005184 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005185 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005186 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005187 else if (type == _KMEM)
5188 res_counter_reset_max(&memcg->kmem);
5189 else
5190 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005191 break;
5192 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005193 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005194 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005195 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005196 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005197 else if (type == _KMEM)
5198 res_counter_reset_failcnt(&memcg->kmem);
5199 else
5200 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005201 break;
5202 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07005203
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07005204 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005205}
5206
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005207static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
5208 struct cftype *cft)
5209{
5210 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
5211}
5212
Daisuke Nishimura02491442010-03-10 15:22:17 -08005213#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005214static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5215 struct cftype *cft, u64 val)
5216{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005217 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005218
5219 if (val >= (1 << NR_MOVE_TYPE))
5220 return -EINVAL;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005221
Glauber Costaee5e8472013-02-22 16:34:50 -08005222 /*
5223 * No kind of locking is needed in here, because ->can_attach() will
5224 * check this value once in the beginning of the process, and then carry
5225 * on with stale data. This means that changes to this value will only
5226 * affect task migrations starting after the change.
5227 */
5228 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005229 return 0;
5230}
Daisuke Nishimura02491442010-03-10 15:22:17 -08005231#else
5232static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
5233 struct cftype *cft, u64 val)
5234{
5235 return -ENOSYS;
5236}
5237#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005238
Ying Han406eb0c2011-05-26 16:25:37 -07005239#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07005240static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07005241 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07005242{
5243 int nid;
5244 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
5245 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005246 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07005247
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005248 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005249 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005250 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005251 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07005252 seq_printf(m, " N%d=%lu", nid, node_nr);
5253 }
5254 seq_putc(m, '\n');
5255
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005256 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005257 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005258 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005259 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005260 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07005261 seq_printf(m, " N%d=%lu", nid, node_nr);
5262 }
5263 seq_putc(m, '\n');
5264
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005265 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005266 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005267 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005268 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005269 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07005270 seq_printf(m, " N%d=%lu", nid, node_nr);
5271 }
5272 seq_putc(m, '\n');
5273
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005274 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005275 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08005276 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005277 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07005278 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07005279 seq_printf(m, " N%d=%lu", nid, node_nr);
5280 }
5281 seq_putc(m, '\n');
5282 return 0;
5283}
5284#endif /* CONFIG_NUMA */
5285
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005286static inline void mem_cgroup_lru_names_not_uptodate(void)
5287{
5288 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
5289}
5290
Wanpeng Liab215882012-07-31 16:43:09 -07005291static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005292 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005293{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005294 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005295 struct mem_cgroup *mi;
5296 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005297
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005298 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005299 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005300 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005301 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
5302 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005303 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005304
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005305 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
5306 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
5307 mem_cgroup_read_events(memcg, i));
5308
5309 for (i = 0; i < NR_LRU_LISTS; i++)
5310 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
5311 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
5312
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005313 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005314 {
5315 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005316 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005317 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005318 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005319 seq_printf(m, "hierarchical_memsw_limit %llu\n",
5320 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08005321 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005322
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005323 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
5324 long long val = 0;
5325
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07005326 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005327 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07005328 for_each_mem_cgroup_tree(mi, memcg)
5329 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
5330 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
5331 }
5332
5333 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
5334 unsigned long long val = 0;
5335
5336 for_each_mem_cgroup_tree(mi, memcg)
5337 val += mem_cgroup_read_events(mi, i);
5338 seq_printf(m, "total_%s %llu\n",
5339 mem_cgroup_events_names[i], val);
5340 }
5341
5342 for (i = 0; i < NR_LRU_LISTS; i++) {
5343 unsigned long long val = 0;
5344
5345 for_each_mem_cgroup_tree(mi, memcg)
5346 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
5347 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07005348 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07005349
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005350#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005351 {
5352 int nid, zid;
5353 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07005354 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005355 unsigned long recent_rotated[2] = {0, 0};
5356 unsigned long recent_scanned[2] = {0, 0};
5357
5358 for_each_online_node(nid)
5359 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005360 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07005361 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005362
Hugh Dickins89abfab2012-05-29 15:06:53 -07005363 recent_rotated[0] += rstat->recent_rotated[0];
5364 recent_rotated[1] += rstat->recent_rotated[1];
5365 recent_scanned[0] += rstat->recent_scanned[0];
5366 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005367 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07005368 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
5369 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
5370 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
5371 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08005372 }
5373#endif
5374
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005375 return 0;
5376}
5377
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005378static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
5379{
5380 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5381
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07005382 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005383}
5384
5385static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
5386 u64 val)
5387{
5388 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5389 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08005390
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005391 if (val > 100)
5392 return -EINVAL;
5393
5394 if (cgrp->parent == NULL)
5395 return -EINVAL;
5396
5397 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08005398
5399 cgroup_lock();
5400
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005401 /* If under hierarchy, only empty-root can set this value */
Glauber Costab5f99b52013-02-22 16:34:53 -08005402 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
Li Zefan068b38c2009-01-15 13:51:26 -08005403 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005404 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08005405 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005406
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005407 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005408
Li Zefan068b38c2009-01-15 13:51:26 -08005409 cgroup_unlock();
5410
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005411 return 0;
5412}
5413
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005414static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
5415{
5416 struct mem_cgroup_threshold_ary *t;
5417 u64 usage;
5418 int i;
5419
5420 rcu_read_lock();
5421 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005422 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005423 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005424 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005425
5426 if (!t)
5427 goto unlock;
5428
5429 usage = mem_cgroup_usage(memcg, swap);
5430
5431 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07005432 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005433 * If it's not true, a threshold was crossed after last
5434 * call of __mem_cgroup_threshold().
5435 */
Phil Carmody5407a562010-05-26 14:42:42 -07005436 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005437
5438 /*
5439 * Iterate backward over array of thresholds starting from
5440 * current_threshold and check if a threshold is crossed.
5441 * If none of thresholds below usage is crossed, we read
5442 * only one element of the array here.
5443 */
5444 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
5445 eventfd_signal(t->entries[i].eventfd, 1);
5446
5447 /* i = current_threshold + 1 */
5448 i++;
5449
5450 /*
5451 * Iterate forward over array of thresholds starting from
5452 * current_threshold+1 and check if a threshold is crossed.
5453 * If none of thresholds above usage is crossed, we read
5454 * only one element of the array here.
5455 */
5456 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
5457 eventfd_signal(t->entries[i].eventfd, 1);
5458
5459 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07005460 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005461unlock:
5462 rcu_read_unlock();
5463}
5464
5465static void mem_cgroup_threshold(struct mem_cgroup *memcg)
5466{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07005467 while (memcg) {
5468 __mem_cgroup_threshold(memcg, false);
5469 if (do_swap_account)
5470 __mem_cgroup_threshold(memcg, true);
5471
5472 memcg = parent_mem_cgroup(memcg);
5473 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005474}
5475
5476static int compare_thresholds(const void *a, const void *b)
5477{
5478 const struct mem_cgroup_threshold *_a = a;
5479 const struct mem_cgroup_threshold *_b = b;
5480
5481 return _a->threshold - _b->threshold;
5482}
5483
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005484static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005485{
5486 struct mem_cgroup_eventfd_list *ev;
5487
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005488 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005489 eventfd_signal(ev->eventfd, 1);
5490 return 0;
5491}
5492
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005493static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005494{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005495 struct mem_cgroup *iter;
5496
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005497 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07005498 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005499}
5500
5501static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
5502 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005503{
5504 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005505 struct mem_cgroup_thresholds *thresholds;
5506 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005507 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005508 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005509 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005510
5511 ret = res_counter_memparse_write_strategy(args, &threshold);
5512 if (ret)
5513 return ret;
5514
5515 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005516
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005517 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005518 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005519 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005520 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005521 else
5522 BUG();
5523
5524 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5525
5526 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005527 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005528 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5529
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005530 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005531
5532 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005533 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005534 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005535 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005536 ret = -ENOMEM;
5537 goto unlock;
5538 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005539 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005540
5541 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005542 if (thresholds->primary) {
5543 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005544 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005545 }
5546
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005547 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005548 new->entries[size - 1].eventfd = eventfd;
5549 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005550
5551 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005552 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005553 compare_thresholds, NULL);
5554
5555 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005556 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005557 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07005558 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005559 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005560 * new->current_threshold will not be used until
5561 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005562 * it here.
5563 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005564 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07005565 } else
5566 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005567 }
5568
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005569 /* Free old spare buffer and save old primary buffer as spare */
5570 kfree(thresholds->spare);
5571 thresholds->spare = thresholds->primary;
5572
5573 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005574
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005575 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005576 synchronize_rcu();
5577
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005578unlock:
5579 mutex_unlock(&memcg->thresholds_lock);
5580
5581 return ret;
5582}
5583
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005584static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005585 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005586{
5587 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005588 struct mem_cgroup_thresholds *thresholds;
5589 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005590 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005591 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005592 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005593
5594 mutex_lock(&memcg->thresholds_lock);
5595 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005596 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005597 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005598 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005599 else
5600 BUG();
5601
Anton Vorontsov371528c2012-02-24 05:14:46 +04005602 if (!thresholds->primary)
5603 goto unlock;
5604
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005605 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
5606
5607 /* Check if a threshold crossed before removing */
5608 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
5609
5610 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005611 size = 0;
5612 for (i = 0; i < thresholds->primary->size; i++) {
5613 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005614 size++;
5615 }
5616
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005617 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005618
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005619 /* Set thresholds array to NULL if we don't have thresholds */
5620 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005621 kfree(new);
5622 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005623 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005624 }
5625
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005626 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005627
5628 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005629 new->current_threshold = -1;
5630 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
5631 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005632 continue;
5633
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005634 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07005635 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005636 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005637 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005638 * until rcu_assign_pointer(), so it's safe to increment
5639 * it here.
5640 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005641 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005642 }
5643 j++;
5644 }
5645
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005646swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005647 /* Swap primary and spare array */
5648 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07005649 /* If all events are unregistered, free the spare array */
5650 if (!new) {
5651 kfree(thresholds->spare);
5652 thresholds->spare = NULL;
5653 }
5654
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07005655 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005656
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005657 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005658 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04005659unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005660 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08005661}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005662
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005663static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
5664 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
5665{
5666 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
5667 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005668 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005669
5670 BUG_ON(type != _OOM_TYPE);
5671 event = kmalloc(sizeof(*event), GFP_KERNEL);
5672 if (!event)
5673 return -ENOMEM;
5674
Michal Hocko1af8efe2011-07-26 16:08:24 -07005675 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005676
5677 event->eventfd = eventfd;
5678 list_add(&event->list, &memcg->oom_notify);
5679
5680 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07005681 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005682 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07005683 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005684
5685 return 0;
5686}
5687
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07005688static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005689 struct cftype *cft, struct eventfd_ctx *eventfd)
5690{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005691 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005692 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08005693 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005694
5695 BUG_ON(type != _OOM_TYPE);
5696
Michal Hocko1af8efe2011-07-26 16:08:24 -07005697 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005698
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005699 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005700 if (ev->eventfd == eventfd) {
5701 list_del(&ev->list);
5702 kfree(ev);
5703 }
5704 }
5705
Michal Hocko1af8efe2011-07-26 16:08:24 -07005706 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005707}
5708
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005709static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
5710 struct cftype *cft, struct cgroup_map_cb *cb)
5711{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005712 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005713
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005714 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005715
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005716 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005717 cb->fill(cb, "under_oom", 1);
5718 else
5719 cb->fill(cb, "under_oom", 0);
5720 return 0;
5721}
5722
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005723static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
5724 struct cftype *cft, u64 val)
5725{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005726 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005727 struct mem_cgroup *parent;
5728
5729 /* cannot set to root cgroup and only 0 and 1 are allowed */
5730 if (!cgrp->parent || !((val == 0) || (val == 1)))
5731 return -EINVAL;
5732
5733 parent = mem_cgroup_from_cont(cgrp->parent);
5734
5735 cgroup_lock();
5736 /* oom-kill-disable is a flag for subhierarchy. */
Glauber Costab5f99b52013-02-22 16:34:53 -08005737 if ((parent->use_hierarchy) || memcg_has_children(memcg)) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005738 cgroup_unlock();
5739 return -EINVAL;
5740 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005741 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07005742 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005743 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005744 cgroup_unlock();
5745 return 0;
5746}
5747
Andrew Mortonc255a452012-07-31 16:43:02 -07005748#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03005749static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005750{
Glauber Costa55007d82012-12-18 14:22:38 -08005751 int ret;
5752
Glauber Costa2633d7a2012-12-18 14:22:34 -08005753 memcg->kmemcg_id = -1;
Glauber Costa55007d82012-12-18 14:22:38 -08005754 ret = memcg_propagate_kmem(memcg);
5755 if (ret)
5756 return ret;
Glauber Costa2633d7a2012-12-18 14:22:34 -08005757
Glauber Costa1d62e432012-04-09 19:36:33 -03005758 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00005759};
5760
Glauber Costa1d62e432012-04-09 19:36:33 -03005761static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005762{
Glauber Costa1d62e432012-04-09 19:36:33 -03005763 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08005764
5765 memcg_kmem_mark_dead(memcg);
5766
5767 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
5768 return;
5769
5770 /*
5771 * Charges already down to 0, undo mem_cgroup_get() done in the charge
5772 * path here, being careful not to race with memcg_uncharge_kmem: it is
5773 * possible that the charges went down to 0 between mark_dead and the
5774 * res_counter read, so in that case, we don't need the put
5775 */
5776 if (memcg_kmem_test_and_clear_dead(memcg))
5777 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005778}
Glauber Costae5671df2011-12-11 21:47:01 +00005779#else
Glauber Costacbe128e32012-04-09 19:36:34 -03005780static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00005781{
5782 return 0;
5783}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005784
Glauber Costa1d62e432012-04-09 19:36:33 -03005785static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005786{
5787}
Glauber Costae5671df2011-12-11 21:47:01 +00005788#endif
5789
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005790static struct cftype mem_cgroup_files[] = {
5791 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005792 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005793 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005794 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005795 .register_event = mem_cgroup_usage_register_event,
5796 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005797 },
5798 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005799 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005800 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005801 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005802 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07005803 },
5804 {
Balbir Singh0eea1032008-02-07 00:13:57 -08005805 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005806 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07005807 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005808 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005809 },
5810 {
Balbir Singh296c81d2009-09-23 15:56:36 -07005811 .name = "soft_limit_in_bytes",
5812 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5813 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005814 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005815 },
5816 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005817 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005818 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005819 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005820 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005821 },
Balbir Singh8697d332008-02-07 00:13:59 -08005822 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005823 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005824 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005825 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005826 {
5827 .name = "force_empty",
5828 .trigger = mem_cgroup_force_empty_write,
5829 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005830 {
5831 .name = "use_hierarchy",
5832 .write_u64 = mem_cgroup_hierarchy_write,
5833 .read_u64 = mem_cgroup_hierarchy_read,
5834 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005835 {
5836 .name = "swappiness",
5837 .read_u64 = mem_cgroup_swappiness_read,
5838 .write_u64 = mem_cgroup_swappiness_write,
5839 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005840 {
5841 .name = "move_charge_at_immigrate",
5842 .read_u64 = mem_cgroup_move_charge_read,
5843 .write_u64 = mem_cgroup_move_charge_write,
5844 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005845 {
5846 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005847 .read_map = mem_cgroup_oom_control_read,
5848 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005849 .register_event = mem_cgroup_oom_register_event,
5850 .unregister_event = mem_cgroup_oom_unregister_event,
5851 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5852 },
Ying Han406eb0c2011-05-26 16:25:37 -07005853#ifdef CONFIG_NUMA
5854 {
5855 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005856 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005857 },
5858#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005859#ifdef CONFIG_MEMCG_KMEM
5860 {
5861 .name = "kmem.limit_in_bytes",
5862 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5863 .write_string = mem_cgroup_write,
5864 .read = mem_cgroup_read,
5865 },
5866 {
5867 .name = "kmem.usage_in_bytes",
5868 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5869 .read = mem_cgroup_read,
5870 },
5871 {
5872 .name = "kmem.failcnt",
5873 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5874 .trigger = mem_cgroup_reset,
5875 .read = mem_cgroup_read,
5876 },
5877 {
5878 .name = "kmem.max_usage_in_bytes",
5879 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5880 .trigger = mem_cgroup_reset,
5881 .read = mem_cgroup_read,
5882 },
Glauber Costa749c5412012-12-18 14:23:01 -08005883#ifdef CONFIG_SLABINFO
5884 {
5885 .name = "kmem.slabinfo",
5886 .read_seq_string = mem_cgroup_slabinfo_read,
5887 },
5888#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005889#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005890 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005891};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005892
Michal Hocko2d110852013-02-22 16:34:43 -08005893#ifdef CONFIG_MEMCG_SWAP
5894static struct cftype memsw_cgroup_files[] = {
5895 {
5896 .name = "memsw.usage_in_bytes",
5897 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
5898 .read = mem_cgroup_read,
5899 .register_event = mem_cgroup_usage_register_event,
5900 .unregister_event = mem_cgroup_usage_unregister_event,
5901 },
5902 {
5903 .name = "memsw.max_usage_in_bytes",
5904 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5905 .trigger = mem_cgroup_reset,
5906 .read = mem_cgroup_read,
5907 },
5908 {
5909 .name = "memsw.limit_in_bytes",
5910 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5911 .write_string = mem_cgroup_write,
5912 .read = mem_cgroup_read,
5913 },
5914 {
5915 .name = "memsw.failcnt",
5916 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5917 .trigger = mem_cgroup_reset,
5918 .read = mem_cgroup_read,
5919 },
5920 { }, /* terminate */
5921};
5922#endif
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005923static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005924{
5925 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005926 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005927 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005928 /*
5929 * This routine is called against possible nodes.
5930 * But it's BUG to call kmalloc() against offline node.
5931 *
5932 * TODO: this routine can waste much memory for nodes which will
5933 * never be onlined. It's better to use memory hotplug callback
5934 * function.
5935 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005936 if (!node_state(node, N_NORMAL_MEMORY))
5937 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005938 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005939 if (!pn)
5940 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005941
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005942 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5943 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005944 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005945 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005946 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005947 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005948 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005949 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005950 return 0;
5951}
5952
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005953static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005954{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005955 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005956}
5957
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005958static struct mem_cgroup *mem_cgroup_alloc(void)
5959{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005960 struct mem_cgroup *memcg;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005961 size_t size = memcg_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005962
Glauber Costa45cf7eb2013-02-22 16:34:49 -08005963 /* Can be very big if nr_node_ids is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005964 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005965 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005966 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005967 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005968
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005969 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005970 return NULL;
5971
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005972 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5973 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005974 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005975 spin_lock_init(&memcg->pcp_counter_lock);
5976 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005977
5978out_free:
5979 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005980 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005981 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005982 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005983 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005984}
5985
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005986/*
Glauber Costac8b2a362012-12-18 14:22:13 -08005987 * At destroying mem_cgroup, references from swap_cgroup can remain.
5988 * (scanning all at force_empty is too costly...)
5989 *
5990 * Instead of clearing all references at force_empty, we remember
5991 * the number of reference from swap_cgroup and free mem_cgroup when
5992 * it goes down to 0.
5993 *
5994 * Removal of cgroup itself succeeds regardless of refs from swap.
Hugh Dickins59927fb2012-03-15 15:17:07 -07005995 */
Glauber Costac8b2a362012-12-18 14:22:13 -08005996
5997static void __mem_cgroup_free(struct mem_cgroup *memcg)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005998{
Glauber Costac8b2a362012-12-18 14:22:13 -08005999 int node;
Glauber Costa45cf7eb2013-02-22 16:34:49 -08006000 size_t size = memcg_size();
Hugh Dickins59927fb2012-03-15 15:17:07 -07006001
Glauber Costac8b2a362012-12-18 14:22:13 -08006002 mem_cgroup_remove_from_trees(memcg);
6003 free_css_id(&mem_cgroup_subsys, &memcg->css);
6004
6005 for_each_node(node)
6006 free_mem_cgroup_per_zone_info(memcg, node);
6007
6008 free_percpu(memcg->stat);
6009
Glauber Costa3f134612012-05-29 15:07:11 -07006010 /*
6011 * We need to make sure that (at least for now), the jump label
6012 * destruction code runs outside of the cgroup lock. This is because
6013 * get_online_cpus(), which is called from the static_branch update,
6014 * can't be called inside the cgroup_lock. cpusets are the ones
6015 * enforcing this dependency, so if they ever change, we might as well.
6016 *
6017 * schedule_work() will guarantee this happens. Be careful if you need
6018 * to move this code around, and make sure it is outside
6019 * the cgroup_lock.
6020 */
Glauber Costaa8964b92012-12-18 14:22:09 -08006021 disarm_static_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006022 if (size < PAGE_SIZE)
6023 kfree(memcg);
6024 else
6025 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006026}
Glauber Costa3afe36b2012-05-29 15:07:10 -07006027
Glauber Costac8b2a362012-12-18 14:22:13 -08006028
6029/*
6030 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
6031 * but in process context. The work_freeing structure is overlaid
6032 * on the rcu_freeing structure, which itself is overlaid on memsw.
6033 */
6034static void free_work(struct work_struct *work)
6035{
6036 struct mem_cgroup *memcg;
6037
6038 memcg = container_of(work, struct mem_cgroup, work_freeing);
6039 __mem_cgroup_free(memcg);
6040}
6041
Glauber Costa3afe36b2012-05-29 15:07:10 -07006042static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07006043{
6044 struct mem_cgroup *memcg;
6045
6046 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07006047 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07006048 schedule_work(&memcg->work_freeing);
6049}
6050
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006051static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006052{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006053 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006054}
6055
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006056static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006057{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006058 if (atomic_sub_and_test(count, &memcg->refcnt)) {
6059 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
Glauber Costac8b2a362012-12-18 14:22:13 -08006060 call_rcu(&memcg->rcu_freeing, free_rcu);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006061 if (parent)
6062 mem_cgroup_put(parent);
6063 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08006064}
6065
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006066static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006067{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006068 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006069}
6070
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006071/*
6072 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
6073 */
Glauber Costae1aab162011-12-11 21:47:03 +00006074struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006075{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006076 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006077 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006078 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006079}
Glauber Costae1aab162011-12-11 21:47:03 +00006080EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07006081
Balbir Singhf64c3f52009-09-23 15:56:37 -07006082static int mem_cgroup_soft_limit_tree_init(void)
6083{
6084 struct mem_cgroup_tree_per_node *rtpn;
6085 struct mem_cgroup_tree_per_zone *rtpz;
6086 int tmp, node, zone;
6087
Bob Liu3ed28fa2012-01-12 17:19:04 -08006088 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07006089 tmp = node;
6090 if (!node_state(node, N_NORMAL_MEMORY))
6091 tmp = -1;
6092 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
6093 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08006094 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006095
6096 soft_limit_tree.rb_tree_per_node[node] = rtpn;
6097
6098 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
6099 rtpz = &rtpn->rb_tree_per_zone[zone];
6100 rtpz->rb_root = RB_ROOT;
6101 spin_lock_init(&rtpz->lock);
6102 }
6103 }
6104 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08006105
6106err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08006107 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08006108 if (!soft_limit_tree.rb_tree_per_node[node])
6109 break;
6110 kfree(soft_limit_tree.rb_tree_per_node[node]);
6111 soft_limit_tree.rb_tree_per_node[node] = NULL;
6112 }
6113 return 1;
6114
Balbir Singhf64c3f52009-09-23 15:56:37 -07006115}
6116
Li Zefan0eb253e2009-01-15 13:51:25 -08006117static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08006118mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006119{
Glauber Costad142e3e2013-02-22 16:34:52 -08006120 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006121 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006122 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006123
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006124 memcg = mem_cgroup_alloc();
6125 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006126 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006127
Bob Liu3ed28fa2012-01-12 17:19:04 -08006128 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006129 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006130 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07006131
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006132 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006133 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006134 int cpu;
Glauber Costad142e3e2013-02-22 16:34:52 -08006135
Balbir Singhf64c3f52009-09-23 15:56:37 -07006136 if (mem_cgroup_soft_limit_tree_init())
6137 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08006138 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08006139 for_each_possible_cpu(cpu) {
6140 struct memcg_stock_pcp *stock =
6141 &per_cpu(memcg_stock, cpu);
6142 INIT_WORK(&stock->work, drain_local_stock);
6143 }
Glauber Costad142e3e2013-02-22 16:34:52 -08006144
6145 res_counter_init(&memcg->res, NULL);
6146 res_counter_init(&memcg->memsw, NULL);
6147 res_counter_init(&memcg->kmem, NULL);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006148 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08006149
Glauber Costad142e3e2013-02-22 16:34:52 -08006150 memcg->last_scanned_node = MAX_NUMNODES;
6151 INIT_LIST_HEAD(&memcg->oom_notify);
6152 atomic_set(&memcg->refcnt, 1);
6153 memcg->move_charge_at_immigrate = 0;
6154 mutex_init(&memcg->thresholds_lock);
6155 spin_lock_init(&memcg->move_lock);
6156
6157 return &memcg->css;
6158
6159free_out:
6160 __mem_cgroup_free(memcg);
6161 return ERR_PTR(error);
6162}
6163
6164static int
6165mem_cgroup_css_online(struct cgroup *cont)
6166{
6167 struct mem_cgroup *memcg, *parent;
6168 int error = 0;
6169
6170 if (!cont->parent)
6171 return 0;
6172
6173 memcg = mem_cgroup_from_cont(cont);
6174 parent = mem_cgroup_from_cont(cont->parent);
6175
6176 memcg->use_hierarchy = parent->use_hierarchy;
6177 memcg->oom_kill_disable = parent->oom_kill_disable;
6178 memcg->swappiness = mem_cgroup_swappiness(parent);
6179
6180 if (parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006181 res_counter_init(&memcg->res, &parent->res);
6182 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006183 res_counter_init(&memcg->kmem, &parent->kmem);
Glauber Costa55007d82012-12-18 14:22:38 -08006184
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08006185 /*
6186 * We increment refcnt of the parent to ensure that we can
6187 * safely access it on res_counter_charge/uncharge.
6188 * This refcnt will be decremented when freeing this
6189 * mem_cgroup(see mem_cgroup_put).
6190 */
6191 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08006192 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006193 res_counter_init(&memcg->res, NULL);
6194 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08006195 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006196 /*
6197 * Deeper hierachy with use_hierarchy == false doesn't make
6198 * much sense so let cgroup subsystem know about this
6199 * unfortunate state in our controller.
6200 */
Glauber Costad142e3e2013-02-22 16:34:52 -08006201 if (parent != root_mem_cgroup)
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07006202 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08006203 }
Glauber Costacbe128e32012-04-09 19:36:34 -03006204
6205 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
6206 if (error) {
6207 /*
6208 * We call put now because our (and parent's) refcnts
6209 * are already in place. mem_cgroup_put() will internally
6210 * call __mem_cgroup_free, so return directly
6211 */
6212 mem_cgroup_put(memcg);
Glauber Costacbe128e32012-04-09 19:36:34 -03006213 }
Glauber Costad142e3e2013-02-22 16:34:52 -08006214 return error;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006215}
6216
Tejun Heo92fb9742012-11-19 08:13:38 -08006217static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006218{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006219 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07006220
Michal Hockoab5196c2012-10-26 13:37:32 +02006221 mem_cgroup_reparent_charges(memcg);
Glauber Costa1f458cb2012-12-18 14:22:50 -08006222 mem_cgroup_destroy_all_caches(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08006223}
6224
Tejun Heo92fb9742012-11-19 08:13:38 -08006225static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006226{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006227 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08006228
Glauber Costa1d62e432012-04-09 19:36:33 -03006229 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00006230
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006231 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006232}
6233
Daisuke Nishimura02491442010-03-10 15:22:17 -08006234#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006235/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006236#define PRECHARGE_COUNT_AT_ONCE 256
6237static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006238{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006239 int ret = 0;
6240 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006241 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006242
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006243 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006244 mc.precharge += count;
6245 /* we don't need css_get for root */
6246 return ret;
6247 }
6248 /* try to charge at once */
6249 if (count > 1) {
6250 struct res_counter *dummy;
6251 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006252 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006253 * by cgroup_lock_live_cgroup() that it is not removed and we
6254 * are still under the same cgroup_mutex. So we can postpone
6255 * css_get().
6256 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006257 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006258 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006259 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006260 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006261 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006262 goto one_by_one;
6263 }
6264 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006265 return ret;
6266 }
6267one_by_one:
6268 /* fall back to one by one charge */
6269 while (count--) {
6270 if (signal_pending(current)) {
6271 ret = -EINTR;
6272 break;
6273 }
6274 if (!batch_count--) {
6275 batch_count = PRECHARGE_COUNT_AT_ONCE;
6276 cond_resched();
6277 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006278 ret = __mem_cgroup_try_charge(NULL,
6279 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006280 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006281 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08006282 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006283 mc.precharge++;
6284 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006285 return ret;
6286}
6287
6288/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006289 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006290 * @vma: the vma the pte to be checked belongs
6291 * @addr: the address corresponding to the pte to be checked
6292 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08006293 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006294 *
6295 * Returns
6296 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
6297 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
6298 * move charge. if @target is not NULL, the page is stored in target->page
6299 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08006300 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
6301 * target for charge migration. if @target is not NULL, the entry is stored
6302 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006303 *
6304 * Called with pte lock held.
6305 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006306union mc_target {
6307 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006308 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006309};
6310
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006311enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006312 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006313 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08006314 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006315};
6316
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006317static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
6318 unsigned long addr, pte_t ptent)
6319{
6320 struct page *page = vm_normal_page(vma, addr, ptent);
6321
6322 if (!page || !page_mapped(page))
6323 return NULL;
6324 if (PageAnon(page)) {
6325 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006326 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006327 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006328 } else if (!move_file())
6329 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006330 return NULL;
6331 if (!get_page_unless_zero(page))
6332 return NULL;
6333
6334 return page;
6335}
6336
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006337#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006338static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6339 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6340{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006341 struct page *page = NULL;
6342 swp_entry_t ent = pte_to_swp_entry(ptent);
6343
6344 if (!move_anon() || non_swap_entry(ent))
6345 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006346 /*
6347 * Because lookup_swap_cache() updates some statistics counter,
6348 * we call find_get_page() with swapper_space directly.
6349 */
Shaohua Li33806f02013-02-22 16:34:37 -08006350 page = find_get_page(swap_address_space(ent), ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006351 if (do_swap_account)
6352 entry->val = ent.val;
6353
6354 return page;
6355}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07006356#else
6357static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
6358 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6359{
6360 return NULL;
6361}
6362#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006363
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006364static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
6365 unsigned long addr, pte_t ptent, swp_entry_t *entry)
6366{
6367 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006368 struct address_space *mapping;
6369 pgoff_t pgoff;
6370
6371 if (!vma->vm_file) /* anonymous vma */
6372 return NULL;
6373 if (!move_file())
6374 return NULL;
6375
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006376 mapping = vma->vm_file->f_mapping;
6377 if (pte_none(ptent))
6378 pgoff = linear_page_index(vma, addr);
6379 else /* pte_file(ptent) is true */
6380 pgoff = pte_to_pgoff(ptent);
6381
6382 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006383 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006384
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006385#ifdef CONFIG_SWAP
6386 /* shmem/tmpfs may report page out on swap: account for that too. */
6387 if (radix_tree_exceptional_entry(page)) {
6388 swp_entry_t swap = radix_to_swp_entry(page);
6389 if (do_swap_account)
6390 *entry = swap;
Shaohua Li33806f02013-02-22 16:34:37 -08006391 page = find_get_page(swap_address_space(swap), swap.val);
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07006392 }
6393#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006394 return page;
6395}
6396
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006397static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006398 unsigned long addr, pte_t ptent, union mc_target *target)
6399{
Daisuke Nishimura02491442010-03-10 15:22:17 -08006400 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006401 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006402 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006403 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006404
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006405 if (pte_present(ptent))
6406 page = mc_handle_present_pte(vma, addr, ptent);
6407 else if (is_swap_pte(ptent))
6408 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07006409 else if (pte_none(ptent) || pte_file(ptent))
6410 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006411
6412 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006413 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006414 if (page) {
6415 pc = lookup_page_cgroup(page);
6416 /*
6417 * Do only loose check w/o page_cgroup lock.
6418 * mem_cgroup_move_account() checks the pc is valid or not under
6419 * the lock.
6420 */
6421 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6422 ret = MC_TARGET_PAGE;
6423 if (target)
6424 target->page = page;
6425 }
6426 if (!ret || !target)
6427 put_page(page);
6428 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07006429 /* There is a swap entry and a page doesn't exist or isn't charged */
6430 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08006431 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07006432 ret = MC_TARGET_SWAP;
6433 if (target)
6434 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006435 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006436 return ret;
6437}
6438
Naoya Horiguchi12724852012-03-21 16:34:28 -07006439#ifdef CONFIG_TRANSPARENT_HUGEPAGE
6440/*
6441 * We don't consider swapping or file mapped pages because THP does not
6442 * support them for now.
6443 * Caller should make sure that pmd_trans_huge(pmd) is true.
6444 */
6445static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6446 unsigned long addr, pmd_t pmd, union mc_target *target)
6447{
6448 struct page *page = NULL;
6449 struct page_cgroup *pc;
6450 enum mc_target_type ret = MC_TARGET_NONE;
6451
6452 page = pmd_page(pmd);
6453 VM_BUG_ON(!page || !PageHead(page));
6454 if (!move_anon())
6455 return ret;
6456 pc = lookup_page_cgroup(page);
6457 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
6458 ret = MC_TARGET_PAGE;
6459 if (target) {
6460 get_page(page);
6461 target->page = page;
6462 }
6463 }
6464 return ret;
6465}
6466#else
6467static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
6468 unsigned long addr, pmd_t pmd, union mc_target *target)
6469{
6470 return MC_TARGET_NONE;
6471}
6472#endif
6473
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006474static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
6475 unsigned long addr, unsigned long end,
6476 struct mm_walk *walk)
6477{
6478 struct vm_area_struct *vma = walk->private;
6479 pte_t *pte;
6480 spinlock_t *ptl;
6481
Naoya Horiguchi12724852012-03-21 16:34:28 -07006482 if (pmd_trans_huge_lock(pmd, vma) == 1) {
6483 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
6484 mc.precharge += HPAGE_PMD_NR;
6485 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006486 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006487 }
Dave Hansen03319322011-03-22 16:32:56 -07006488
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006489 if (pmd_trans_unstable(pmd))
6490 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006491 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6492 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006493 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006494 mc.precharge++; /* increment precharge temporarily */
6495 pte_unmap_unlock(pte - 1, ptl);
6496 cond_resched();
6497
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006498 return 0;
6499}
6500
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006501static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
6502{
6503 unsigned long precharge;
6504 struct vm_area_struct *vma;
6505
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006506 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006507 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6508 struct mm_walk mem_cgroup_count_precharge_walk = {
6509 .pmd_entry = mem_cgroup_count_precharge_pte_range,
6510 .mm = mm,
6511 .private = vma,
6512 };
6513 if (is_vm_hugetlb_page(vma))
6514 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006515 walk_page_range(vma->vm_start, vma->vm_end,
6516 &mem_cgroup_count_precharge_walk);
6517 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006518 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006519
6520 precharge = mc.precharge;
6521 mc.precharge = 0;
6522
6523 return precharge;
6524}
6525
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006526static int mem_cgroup_precharge_mc(struct mm_struct *mm)
6527{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006528 unsigned long precharge = mem_cgroup_count_precharge(mm);
6529
6530 VM_BUG_ON(mc.moving_task);
6531 mc.moving_task = current;
6532 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006533}
6534
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006535/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
6536static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006537{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006538 struct mem_cgroup *from = mc.from;
6539 struct mem_cgroup *to = mc.to;
6540
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006541 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006542 if (mc.precharge) {
6543 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
6544 mc.precharge = 0;
6545 }
6546 /*
6547 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
6548 * we must uncharge here.
6549 */
6550 if (mc.moved_charge) {
6551 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
6552 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006553 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006554 /* we must fixup refcnts and charges */
6555 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006556 /* uncharge swap account from the old cgroup */
6557 if (!mem_cgroup_is_root(mc.from))
6558 res_counter_uncharge(&mc.from->memsw,
6559 PAGE_SIZE * mc.moved_swap);
6560 __mem_cgroup_put(mc.from, mc.moved_swap);
6561
6562 if (!mem_cgroup_is_root(mc.to)) {
6563 /*
6564 * we charged both to->res and to->memsw, so we should
6565 * uncharge to->res.
6566 */
6567 res_counter_uncharge(&mc.to->res,
6568 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006569 }
6570 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006571 mc.moved_swap = 0;
6572 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006573 memcg_oom_recover(from);
6574 memcg_oom_recover(to);
6575 wake_up_all(&mc.waitq);
6576}
6577
6578static void mem_cgroup_clear_mc(void)
6579{
6580 struct mem_cgroup *from = mc.from;
6581
6582 /*
6583 * we must clear moving_task before waking up waiters at the end of
6584 * task migration.
6585 */
6586 mc.moving_task = NULL;
6587 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006588 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006589 mc.from = NULL;
6590 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006591 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006592 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006593}
6594
Li Zefan761b3ef2012-01-31 13:47:36 +08006595static int mem_cgroup_can_attach(struct cgroup *cgroup,
6596 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006597{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006598 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006599 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006600 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Glauber Costaee5e8472013-02-22 16:34:50 -08006601 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006602
Glauber Costaee5e8472013-02-22 16:34:50 -08006603 /*
6604 * We are now commited to this value whatever it is. Changes in this
6605 * tunable will only affect upcoming migrations, not the current one.
6606 * So we need to save it, and keep it going.
6607 */
6608 move_charge_at_immigrate = memcg->move_charge_at_immigrate;
6609 if (move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006610 struct mm_struct *mm;
6611 struct mem_cgroup *from = mem_cgroup_from_task(p);
6612
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006613 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006614
6615 mm = get_task_mm(p);
6616 if (!mm)
6617 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006618 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006619 if (mm->owner == p) {
6620 VM_BUG_ON(mc.from);
6621 VM_BUG_ON(mc.to);
6622 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006623 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006624 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07006625 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006626 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006627 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07006628 mc.to = memcg;
Glauber Costaee5e8472013-02-22 16:34:50 -08006629 mc.immigrate_flags = move_charge_at_immigrate;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07006630 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006631 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006632
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006633 ret = mem_cgroup_precharge_mc(mm);
6634 if (ret)
6635 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006636 }
6637 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006638 }
6639 return ret;
6640}
6641
Li Zefan761b3ef2012-01-31 13:47:36 +08006642static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6643 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006644{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006645 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006646}
6647
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006648static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
6649 unsigned long addr, unsigned long end,
6650 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006651{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006652 int ret = 0;
6653 struct vm_area_struct *vma = walk->private;
6654 pte_t *pte;
6655 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006656 enum mc_target_type target_type;
6657 union mc_target target;
6658 struct page *page;
6659 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006660
Naoya Horiguchi12724852012-03-21 16:34:28 -07006661 /*
6662 * We don't take compound_lock() here but no race with splitting thp
6663 * happens because:
6664 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
6665 * under splitting, which means there's no concurrent thp split,
6666 * - if another thread runs into split_huge_page() just after we
6667 * entered this if-block, the thread must wait for page table lock
6668 * to be unlocked in __split_huge_page_splitting(), where the main
6669 * part of thp split is not executed yet.
6670 */
6671 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07006672 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006673 spin_unlock(&vma->vm_mm->page_table_lock);
6674 return 0;
6675 }
6676 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
6677 if (target_type == MC_TARGET_PAGE) {
6678 page = target.page;
6679 if (!isolate_lru_page(page)) {
6680 pc = lookup_page_cgroup(page);
6681 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006682 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07006683 mc.precharge -= HPAGE_PMD_NR;
6684 mc.moved_charge += HPAGE_PMD_NR;
6685 }
6686 putback_lru_page(page);
6687 }
6688 put_page(page);
6689 }
6690 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07006691 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07006692 }
6693
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07006694 if (pmd_trans_unstable(pmd))
6695 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006696retry:
6697 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
6698 for (; addr != end; addr += PAGE_SIZE) {
6699 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08006700 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006701
6702 if (!mc.precharge)
6703 break;
6704
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006705 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006706 case MC_TARGET_PAGE:
6707 page = target.page;
6708 if (isolate_lru_page(page))
6709 goto put;
6710 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07006711 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07006712 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006713 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006714 /* we uncharge from mc.from later. */
6715 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006716 }
6717 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07006718put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006719 put_page(page);
6720 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08006721 case MC_TARGET_SWAP:
6722 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07006723 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08006724 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08006725 /* we fixup refcnts and charges later. */
6726 mc.moved_swap++;
6727 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08006728 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006729 default:
6730 break;
6731 }
6732 }
6733 pte_unmap_unlock(pte - 1, ptl);
6734 cond_resched();
6735
6736 if (addr != end) {
6737 /*
6738 * We have consumed all precharges we got in can_attach().
6739 * We try charge one by one, but don't do any additional
6740 * charges to mc.to if we have failed in charge once in attach()
6741 * phase.
6742 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08006743 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006744 if (!ret)
6745 goto retry;
6746 }
6747
6748 return ret;
6749}
6750
6751static void mem_cgroup_move_charge(struct mm_struct *mm)
6752{
6753 struct vm_area_struct *vma;
6754
6755 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006756retry:
6757 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
6758 /*
6759 * Someone who are holding the mmap_sem might be waiting in
6760 * waitq. So we cancel all extra charges, wake up all waiters,
6761 * and retry. Because we cancel precharges, we might not be able
6762 * to move enough charges, but moving charge is a best-effort
6763 * feature anyway, so it wouldn't be a big problem.
6764 */
6765 __mem_cgroup_clear_mc();
6766 cond_resched();
6767 goto retry;
6768 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006769 for (vma = mm->mmap; vma; vma = vma->vm_next) {
6770 int ret;
6771 struct mm_walk mem_cgroup_move_charge_walk = {
6772 .pmd_entry = mem_cgroup_move_charge_pte_range,
6773 .mm = mm,
6774 .private = vma,
6775 };
6776 if (is_vm_hugetlb_page(vma))
6777 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08006778 ret = walk_page_range(vma->vm_start, vma->vm_end,
6779 &mem_cgroup_move_charge_walk);
6780 if (ret)
6781 /*
6782 * means we have consumed all precharges and failed in
6783 * doing additional charge. Just abandon here.
6784 */
6785 break;
6786 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006787 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006788}
6789
Li Zefan761b3ef2012-01-31 13:47:36 +08006790static void mem_cgroup_move_task(struct cgroup *cont,
6791 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08006792{
Tejun Heo2f7ee562011-12-12 18:12:21 -08006793 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006794 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006795
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006796 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006797 if (mc.to)
6798 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08006799 mmput(mm);
6800 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07006801 if (mc.to)
6802 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08006803}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006804#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08006805static int mem_cgroup_can_attach(struct cgroup *cgroup,
6806 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006807{
6808 return 0;
6809}
Li Zefan761b3ef2012-01-31 13:47:36 +08006810static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
6811 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006812{
6813}
Li Zefan761b3ef2012-01-31 13:47:36 +08006814static void mem_cgroup_move_task(struct cgroup *cont,
6815 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07006816{
6817}
6818#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08006819
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006820struct cgroup_subsys mem_cgroup_subsys = {
6821 .name = "memory",
6822 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08006823 .css_alloc = mem_cgroup_css_alloc,
Glauber Costad142e3e2013-02-22 16:34:52 -08006824 .css_online = mem_cgroup_css_online,
Tejun Heo92fb9742012-11-19 08:13:38 -08006825 .css_offline = mem_cgroup_css_offline,
6826 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08006827 .can_attach = mem_cgroup_can_attach,
6828 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08006829 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07006830 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08006831 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07006832 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08006833};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006834
Andrew Mortonc255a452012-07-31 16:43:02 -07006835#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08006836static int __init enable_swap_account(char *s)
6837{
6838 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006839 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006840 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006841 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006842 really_do_swap_account = 0;
6843 return 1;
6844}
Michal Hockoa2c89902011-05-24 17:12:50 -07006845__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006846
Michal Hocko2d110852013-02-22 16:34:43 -08006847static void __init memsw_file_init(void)
6848{
Michal Hocko6acc8b02013-02-22 16:34:45 -08006849 WARN_ON(cgroup_add_cftypes(&mem_cgroup_subsys, memsw_cgroup_files));
Michal Hocko2d110852013-02-22 16:34:43 -08006850}
Michal Hocko6acc8b02013-02-22 16:34:45 -08006851
6852static void __init enable_swap_cgroup(void)
6853{
6854 if (!mem_cgroup_disabled() && really_do_swap_account) {
6855 do_swap_account = 1;
6856 memsw_file_init();
6857 }
6858}
6859
Michal Hocko2d110852013-02-22 16:34:43 -08006860#else
Michal Hocko6acc8b02013-02-22 16:34:45 -08006861static void __init enable_swap_cgroup(void)
Michal Hocko2d110852013-02-22 16:34:43 -08006862{
6863}
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006864#endif
Michal Hocko2d110852013-02-22 16:34:43 -08006865
6866/*
6867 * The rest of init is performed during ->css_alloc() for root css which
6868 * happens before initcalls. hotcpu_notifier() can't be done together as
6869 * it would introduce circular locking by adding cgroup_lock -> cpu hotplug
6870 * dependency. Do it from a subsys_initcall().
6871 */
6872static int __init mem_cgroup_init(void)
6873{
6874 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Michal Hocko6acc8b02013-02-22 16:34:45 -08006875 enable_swap_cgroup();
Michal Hocko2d110852013-02-22 16:34:43 -08006876 return 0;
6877}
6878subsys_initcall(mem_cgroup_init);