blob: 9a62ac3ea8818d5dbfc7ee8ea8d0a75e43d8b425 [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
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700123/*
124 * Per memcg event counter is incremented at every pagein/pageout. With THP,
125 * it will be incremated by the number of pages. This counter is used for
126 * for trigger some periodic events. This is straightforward and better
127 * than using jiffies etc. to handle periodic memcg event.
128 */
129enum mem_cgroup_events_target {
130 MEM_CGROUP_TARGET_THRESH,
131 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700132 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700133 MEM_CGROUP_NTARGETS,
134};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700135#define THRESHOLDS_EVENTS_TARGET 128
136#define SOFTLIMIT_EVENTS_TARGET 1024
137#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700138
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800139struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700140 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700141 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700142 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700143 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800144};
145
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800146struct mem_cgroup_reclaim_iter {
147 /* css_id of the last scanned hierarchy member */
148 int position;
149 /* scan generation, increased every round-trip */
150 unsigned int generation;
151};
152
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800153/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800154 * per-zone information in memory controller.
155 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800156struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800157 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700158 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800159
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800160 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
161
Balbir Singhf64c3f52009-09-23 15:56:37 -0700162 struct rb_node tree_node; /* RB tree node */
163 unsigned long long usage_in_excess;/* Set to the value by which */
164 /* the soft limit is exceeded*/
165 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700166 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700167 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800168};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800169
170struct mem_cgroup_per_node {
171 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
172};
173
174struct mem_cgroup_lru_info {
175 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
176};
177
178/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700179 * Cgroups above their limits are maintained in a RB-Tree, independent of
180 * their hierarchy representation
181 */
182
183struct mem_cgroup_tree_per_zone {
184 struct rb_root rb_root;
185 spinlock_t lock;
186};
187
188struct mem_cgroup_tree_per_node {
189 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
190};
191
192struct mem_cgroup_tree {
193 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
194};
195
196static struct mem_cgroup_tree soft_limit_tree __read_mostly;
197
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800198struct mem_cgroup_threshold {
199 struct eventfd_ctx *eventfd;
200 u64 threshold;
201};
202
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700203/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800204struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700205 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700206 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800207 /* Size of entries[] */
208 unsigned int size;
209 /* Array of thresholds */
210 struct mem_cgroup_threshold entries[0];
211};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700212
213struct mem_cgroup_thresholds {
214 /* Primary thresholds array */
215 struct mem_cgroup_threshold_ary *primary;
216 /*
217 * Spare threshold array.
218 * This is needed to make mem_cgroup_unregister_event() "never fail".
219 * It must be able to store at least primary->size - 1 entries.
220 */
221 struct mem_cgroup_threshold_ary *spare;
222};
223
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700224/* for OOM */
225struct mem_cgroup_eventfd_list {
226 struct list_head list;
227 struct eventfd_ctx *eventfd;
228};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800229
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700230static void mem_cgroup_threshold(struct mem_cgroup *memcg);
231static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800232
Balbir Singhf64c3f52009-09-23 15:56:37 -0700233/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800234 * The memory controller data structure. The memory controller controls both
235 * page cache and RSS per cgroup. We would eventually like to provide
236 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
237 * to help the administrator determine what knobs to tune.
238 *
239 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800240 * we hit the water mark. May be even add a low water mark, such that
241 * no reclaim occurs from a cgroup at it's low water mark, this is
242 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800243 */
244struct mem_cgroup {
245 struct cgroup_subsys_state css;
246 /*
247 * the counter to account for memory usage
248 */
249 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700250
251 union {
252 /*
253 * the counter to account for mem+swap usage.
254 */
255 struct res_counter memsw;
256
257 /*
258 * rcu_freeing is used only when freeing struct mem_cgroup,
259 * so put it into a union to avoid wasting more memory.
260 * It must be disjoint from the css field. It could be
261 * in a union with the res field, but res plays a much
262 * larger part in mem_cgroup life than memsw, and might
263 * be of interest, even at time of free, when debugging.
264 * So share rcu_head with the less interesting memsw.
265 */
266 struct rcu_head rcu_freeing;
267 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700268 * We also need some space for a worker in deferred freeing.
269 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700270 */
271 struct work_struct work_freeing;
272 };
273
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800274 /*
Glauber Costa510fc4e2012-12-18 14:21:47 -0800275 * the counter to account for kernel memory usage.
276 */
277 struct res_counter kmem;
278 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800279 * Per cgroup active and inactive list, similar to the
280 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800281 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800282 struct mem_cgroup_lru_info info;
Ying Han889976d2011-05-26 16:25:33 -0700283 int last_scanned_node;
284#if MAX_NUMNODES > 1
285 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700286 atomic_t numainfo_events;
287 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700288#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800289 /*
290 * Should the accounting and control be hierarchical, per subtree?
291 */
292 bool use_hierarchy;
Glauber Costa510fc4e2012-12-18 14:21:47 -0800293 unsigned long kmem_account_flags; /* See KMEM_ACCOUNTED_*, below */
Michal Hocko79dfdac2011-07-26 16:08:23 -0700294
295 bool oom_lock;
296 atomic_t under_oom;
297
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800298 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800299
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700300 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700301 /* OOM-Killer disable */
302 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800303
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700304 /* set when res.limit == memsw.limit */
305 bool memsw_is_minimum;
306
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800307 /* protect arrays of thresholds */
308 struct mutex thresholds_lock;
309
310 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700311 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700312
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800313 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700314 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700315
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700316 /* For oom notifier event fd */
317 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700318
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800319 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800320 * Should we move charges of a task when a task is moved into this
321 * mem_cgroup ? And what type of charges should we move ?
322 */
323 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800324 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700325 * set > 0 if pages under this cgroup are moving to other cgroup.
326 */
327 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700328 /* taken only while moving_account > 0 */
329 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700330 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800331 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800332 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700333 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700334 /*
335 * used when a cpu is offlined or other synchronizations
336 * See mem_cgroup_read_stat().
337 */
338 struct mem_cgroup_stat_cpu nocpu_base;
339 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000340
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700341#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000342 struct tcp_memcontrol tcp_mem;
343#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800344};
345
Glauber Costa510fc4e2012-12-18 14:21:47 -0800346/* internal only representation about the status of kmem accounting. */
347enum {
348 KMEM_ACCOUNTED_ACTIVE = 0, /* accounted by this cgroup itself */
Glauber Costa7de37682012-12-18 14:22:07 -0800349 KMEM_ACCOUNTED_DEAD, /* dead memcg with pending kmem charges */
Glauber Costa510fc4e2012-12-18 14:21:47 -0800350};
351
352#define KMEM_ACCOUNTED_MASK (1 << KMEM_ACCOUNTED_ACTIVE)
353
354#ifdef CONFIG_MEMCG_KMEM
355static inline void memcg_kmem_set_active(struct mem_cgroup *memcg)
356{
357 set_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
358}
Glauber Costa7de37682012-12-18 14:22:07 -0800359
360static bool memcg_kmem_is_active(struct mem_cgroup *memcg)
361{
362 return test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags);
363}
364
365static void memcg_kmem_mark_dead(struct mem_cgroup *memcg)
366{
367 if (test_bit(KMEM_ACCOUNTED_ACTIVE, &memcg->kmem_account_flags))
368 set_bit(KMEM_ACCOUNTED_DEAD, &memcg->kmem_account_flags);
369}
370
371static bool memcg_kmem_test_and_clear_dead(struct mem_cgroup *memcg)
372{
373 return test_and_clear_bit(KMEM_ACCOUNTED_DEAD,
374 &memcg->kmem_account_flags);
375}
Glauber Costa510fc4e2012-12-18 14:21:47 -0800376#endif
377
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800378/* Stuffs for move charges at task migration. */
379/*
380 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
381 * left-shifted bitmap of these types.
382 */
383enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800384 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700385 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800386 NR_MOVE_TYPE,
387};
388
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800389/* "mc" and its members are protected by cgroup_mutex */
390static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800391 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800392 struct mem_cgroup *from;
393 struct mem_cgroup *to;
394 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800395 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800396 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800397 struct task_struct *moving_task; /* a task moving charges */
398 wait_queue_head_t waitq; /* a waitq for other context */
399} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700400 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800401 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
402};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800403
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700404static bool move_anon(void)
405{
406 return test_bit(MOVE_CHARGE_TYPE_ANON,
407 &mc.to->move_charge_at_immigrate);
408}
409
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700410static bool move_file(void)
411{
412 return test_bit(MOVE_CHARGE_TYPE_FILE,
413 &mc.to->move_charge_at_immigrate);
414}
415
Balbir Singh4e416952009-09-23 15:56:39 -0700416/*
417 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
418 * limit reclaim to prevent infinite loops, if they ever occur.
419 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700420#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
421#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700422
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800423enum charge_type {
424 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700425 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800426 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700427 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700428 NR_CHARGE_TYPE,
429};
430
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800431/* for encoding cft->private value on file */
Glauber Costa86ae53e2012-12-18 14:21:45 -0800432enum res_type {
433 _MEM,
434 _MEMSWAP,
435 _OOM_TYPE,
Glauber Costa510fc4e2012-12-18 14:21:47 -0800436 _KMEM,
Glauber Costa86ae53e2012-12-18 14:21:45 -0800437};
438
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700439#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
440#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800441#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700442/* Used for OOM nofiier */
443#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800444
Balbir Singh75822b42009-09-23 15:56:38 -0700445/*
446 * Reclaim flags for mem_cgroup_hierarchical_reclaim
447 */
448#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
449#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
450#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
451#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
452
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700453static void mem_cgroup_get(struct mem_cgroup *memcg);
454static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000455
Wanpeng Lib2145142012-07-31 16:46:01 -0700456static inline
457struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
458{
459 return container_of(s, struct mem_cgroup, css);
460}
461
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700462static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
463{
464 return (memcg == root_mem_cgroup);
465}
466
Glauber Costae1aab162011-12-11 21:47:03 +0000467/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700468#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000469
Glauber Costae1aab162011-12-11 21:47:03 +0000470void sock_update_memcg(struct sock *sk)
471{
Glauber Costa376be5f2012-01-20 04:57:14 +0000472 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000473 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700474 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000475
476 BUG_ON(!sk->sk_prot->proto_cgroup);
477
Glauber Costaf3f511e2012-01-05 20:16:39 +0000478 /* Socket cloning can throw us here with sk_cgrp already
479 * filled. It won't however, necessarily happen from
480 * process context. So the test for root memcg given
481 * the current task's memcg won't help us in this case.
482 *
483 * Respecting the original socket's memcg is a better
484 * decision in this case.
485 */
486 if (sk->sk_cgrp) {
487 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
488 mem_cgroup_get(sk->sk_cgrp->memcg);
489 return;
490 }
491
Glauber Costae1aab162011-12-11 21:47:03 +0000492 rcu_read_lock();
493 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700494 cg_proto = sk->sk_prot->proto_cgroup(memcg);
495 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000496 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700497 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000498 }
499 rcu_read_unlock();
500 }
501}
502EXPORT_SYMBOL(sock_update_memcg);
503
504void sock_release_memcg(struct sock *sk)
505{
Glauber Costa376be5f2012-01-20 04:57:14 +0000506 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000507 struct mem_cgroup *memcg;
508 WARN_ON(!sk->sk_cgrp->memcg);
509 memcg = sk->sk_cgrp->memcg;
510 mem_cgroup_put(memcg);
511 }
512}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000513
514struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
515{
516 if (!memcg || mem_cgroup_is_root(memcg))
517 return NULL;
518
519 return &memcg->tcp_mem.cg_proto;
520}
521EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000522
Glauber Costa3f134612012-05-29 15:07:11 -0700523static void disarm_sock_keys(struct mem_cgroup *memcg)
524{
525 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
526 return;
527 static_key_slow_dec(&memcg_socket_limit_enabled);
528}
529#else
530static void disarm_sock_keys(struct mem_cgroup *memcg)
531{
532}
533#endif
534
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700535static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800536
Balbir Singhf64c3f52009-09-23 15:56:37 -0700537static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700538mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700539{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700540 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700541}
542
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700543struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100544{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700545 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100546}
547
Balbir Singhf64c3f52009-09-23 15:56:37 -0700548static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700549page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700550{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700551 int nid = page_to_nid(page);
552 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700553
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700554 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700555}
556
557static struct mem_cgroup_tree_per_zone *
558soft_limit_tree_node_zone(int nid, int zid)
559{
560 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
561}
562
563static struct mem_cgroup_tree_per_zone *
564soft_limit_tree_from_page(struct page *page)
565{
566 int nid = page_to_nid(page);
567 int zid = page_zonenum(page);
568
569 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
570}
571
572static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700573__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700574 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700575 struct mem_cgroup_tree_per_zone *mctz,
576 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700577{
578 struct rb_node **p = &mctz->rb_root.rb_node;
579 struct rb_node *parent = NULL;
580 struct mem_cgroup_per_zone *mz_node;
581
582 if (mz->on_tree)
583 return;
584
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700585 mz->usage_in_excess = new_usage_in_excess;
586 if (!mz->usage_in_excess)
587 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700588 while (*p) {
589 parent = *p;
590 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
591 tree_node);
592 if (mz->usage_in_excess < mz_node->usage_in_excess)
593 p = &(*p)->rb_left;
594 /*
595 * We can't avoid mem cgroups that are over their soft
596 * limit by the same amount
597 */
598 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
599 p = &(*p)->rb_right;
600 }
601 rb_link_node(&mz->tree_node, parent, p);
602 rb_insert_color(&mz->tree_node, &mctz->rb_root);
603 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700604}
605
606static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700607__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700608 struct mem_cgroup_per_zone *mz,
609 struct mem_cgroup_tree_per_zone *mctz)
610{
611 if (!mz->on_tree)
612 return;
613 rb_erase(&mz->tree_node, &mctz->rb_root);
614 mz->on_tree = false;
615}
616
617static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700618mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700619 struct mem_cgroup_per_zone *mz,
620 struct mem_cgroup_tree_per_zone *mctz)
621{
622 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700623 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700624 spin_unlock(&mctz->lock);
625}
626
Balbir Singhf64c3f52009-09-23 15:56:37 -0700627
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700628static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700629{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700630 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700631 struct mem_cgroup_per_zone *mz;
632 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700633 int nid = page_to_nid(page);
634 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700635 mctz = soft_limit_tree_from_page(page);
636
637 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700638 * Necessary to update all ancestors when hierarchy is used.
639 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700640 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700641 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
642 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
643 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700644 /*
645 * We have to update the tree if mz is on RB-tree or
646 * mem is over its softlimit.
647 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700648 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700649 spin_lock(&mctz->lock);
650 /* if on-tree, remove it */
651 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700652 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700653 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700654 * Insert again. mz->usage_in_excess will be updated.
655 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700656 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700657 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700658 spin_unlock(&mctz->lock);
659 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700660 }
661}
662
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700663static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700664{
665 int node, zone;
666 struct mem_cgroup_per_zone *mz;
667 struct mem_cgroup_tree_per_zone *mctz;
668
Bob Liu3ed28fa2012-01-12 17:19:04 -0800669 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700670 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700671 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700672 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700673 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700674 }
675 }
676}
677
Balbir Singh4e416952009-09-23 15:56:39 -0700678static struct mem_cgroup_per_zone *
679__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
680{
681 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700682 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700683
684retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700685 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700686 rightmost = rb_last(&mctz->rb_root);
687 if (!rightmost)
688 goto done; /* Nothing to reclaim from */
689
690 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
691 /*
692 * Remove the node now but someone else can add it back,
693 * we will to add it back at the end of reclaim to its correct
694 * position in the tree.
695 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700696 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
697 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
698 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700699 goto retry;
700done:
701 return mz;
702}
703
704static struct mem_cgroup_per_zone *
705mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
706{
707 struct mem_cgroup_per_zone *mz;
708
709 spin_lock(&mctz->lock);
710 mz = __mem_cgroup_largest_soft_limit_node(mctz);
711 spin_unlock(&mctz->lock);
712 return mz;
713}
714
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700715/*
716 * Implementation Note: reading percpu statistics for memcg.
717 *
718 * Both of vmstat[] and percpu_counter has threshold and do periodic
719 * synchronization to implement "quick" read. There are trade-off between
720 * reading cost and precision of value. Then, we may have a chance to implement
721 * a periodic synchronizion of counter in memcg's counter.
722 *
723 * But this _read() function is used for user interface now. The user accounts
724 * memory usage by memory cgroup and he _always_ requires exact value because
725 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
726 * have to visit all online cpus and make sum. So, for now, unnecessary
727 * synchronization is not implemented. (just implemented for cpu hotplug)
728 *
729 * If there are kernel internal actions which can make use of some not-exact
730 * value, and reading all cpu value can be performance bottleneck in some
731 * common workload, threashold and synchonization as vmstat[] should be
732 * implemented.
733 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700734static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700735 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800736{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700737 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800738 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800739
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700740 get_online_cpus();
741 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700742 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700743#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700744 spin_lock(&memcg->pcp_counter_lock);
745 val += memcg->nocpu_base.count[idx];
746 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700747#endif
748 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800749 return val;
750}
751
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700752static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700753 bool charge)
754{
755 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700756 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700757}
758
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700759static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700760 enum mem_cgroup_events_index idx)
761{
762 unsigned long val = 0;
763 int cpu;
764
765 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700766 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700767#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700768 spin_lock(&memcg->pcp_counter_lock);
769 val += memcg->nocpu_base.events[idx];
770 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700771#endif
772 return val;
773}
774
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700775static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700776 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800777{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800778 preempt_disable();
779
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700780 /*
781 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
782 * counted as CACHE even if it's on ANON LRU.
783 */
784 if (anon)
785 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700786 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800787 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700788 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700789 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700790
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800791 /* pagein of a big page is an event. So, ignore page size */
792 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700793 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800794 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700795 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800796 nr_pages = -nr_pages; /* for event */
797 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800798
Johannes Weiner13114712012-05-29 15:07:07 -0700799 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800800
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800801 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800802}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800803
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700804unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700805mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700806{
807 struct mem_cgroup_per_zone *mz;
808
809 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
810 return mz->lru_size[lru];
811}
812
813static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700814mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700815 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700816{
817 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700818 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700819 unsigned long ret = 0;
820
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700821 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700822
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700823 for_each_lru(lru) {
824 if (BIT(lru) & lru_mask)
825 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700826 }
827 return ret;
828}
829
830static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700831mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700832 int nid, unsigned int lru_mask)
833{
Ying Han889976d2011-05-26 16:25:33 -0700834 u64 total = 0;
835 int zid;
836
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700837 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700838 total += mem_cgroup_zone_nr_lru_pages(memcg,
839 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700840
Ying Han889976d2011-05-26 16:25:33 -0700841 return total;
842}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700843
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700844static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700845 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800846{
Ying Han889976d2011-05-26 16:25:33 -0700847 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800848 u64 total = 0;
849
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800850 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700851 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800852 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800853}
854
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800855static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
856 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800857{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700858 unsigned long val, next;
859
Johannes Weiner13114712012-05-29 15:07:07 -0700860 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700861 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700862 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800863 if ((long)next - (long)val < 0) {
864 switch (target) {
865 case MEM_CGROUP_TARGET_THRESH:
866 next = val + THRESHOLDS_EVENTS_TARGET;
867 break;
868 case MEM_CGROUP_TARGET_SOFTLIMIT:
869 next = val + SOFTLIMIT_EVENTS_TARGET;
870 break;
871 case MEM_CGROUP_TARGET_NUMAINFO:
872 next = val + NUMAINFO_EVENTS_TARGET;
873 break;
874 default:
875 break;
876 }
877 __this_cpu_write(memcg->stat->targets[target], next);
878 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700879 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800880 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800881}
882
883/*
884 * Check events in order.
885 *
886 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700887static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800888{
Steven Rostedt47994012011-11-02 13:38:33 -0700889 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800890 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800891 if (unlikely(mem_cgroup_event_ratelimit(memcg,
892 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800893 bool do_softlimit;
894 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800895
896 do_softlimit = mem_cgroup_event_ratelimit(memcg,
897 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700898#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800899 do_numainfo = mem_cgroup_event_ratelimit(memcg,
900 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700901#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800902 preempt_enable();
903
904 mem_cgroup_threshold(memcg);
905 if (unlikely(do_softlimit))
906 mem_cgroup_update_tree(memcg, page);
907#if MAX_NUMNODES > 1
908 if (unlikely(do_numainfo))
909 atomic_inc(&memcg->numainfo_events);
910#endif
911 } else
912 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800913}
914
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000915struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800916{
Wanpeng Lib2145142012-07-31 16:46:01 -0700917 return mem_cgroup_from_css(
918 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800919}
920
Balbir Singhcf475ad2008-04-29 01:00:16 -0700921struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800922{
Balbir Singh31a78f22008-09-28 23:09:31 +0100923 /*
924 * mm_update_next_owner() may clear mm->owner to NULL
925 * if it races with swapoff, page migration, etc.
926 * So this can be called with p == NULL.
927 */
928 if (unlikely(!p))
929 return NULL;
930
Wanpeng Lib2145142012-07-31 16:46:01 -0700931 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800932}
933
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700934struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800935{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700936 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700937
938 if (!mm)
939 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800940 /*
941 * Because we have no locks, mm->owner's may be being moved to other
942 * cgroup. We use css_tryget() here even if this looks
943 * pessimistic (rather than adding locks here).
944 */
945 rcu_read_lock();
946 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700947 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
948 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800949 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700950 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800951 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700952 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800953}
954
Johannes Weiner56600482012-01-12 17:17:59 -0800955/**
956 * mem_cgroup_iter - iterate over memory cgroup hierarchy
957 * @root: hierarchy root
958 * @prev: previously returned memcg, NULL on first invocation
959 * @reclaim: cookie for shared reclaim walks, NULL for full walks
960 *
961 * Returns references to children of the hierarchy below @root, or
962 * @root itself, or %NULL after a full round-trip.
963 *
964 * Caller must pass the return value in @prev on subsequent
965 * invocations for reference counting, or use mem_cgroup_iter_break()
966 * to cancel a hierarchy walk before the round-trip is complete.
967 *
968 * Reclaimers can specify a zone and a priority level in @reclaim to
969 * divide up the memcgs in the hierarchy among all concurrent
970 * reclaimers operating on the same zone and priority.
971 */
972struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
973 struct mem_cgroup *prev,
974 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700975{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800976 struct mem_cgroup *memcg = NULL;
977 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700978
Johannes Weiner56600482012-01-12 17:17:59 -0800979 if (mem_cgroup_disabled())
980 return NULL;
981
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700982 if (!root)
983 root = root_mem_cgroup;
984
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800985 if (prev && !reclaim)
986 id = css_id(&prev->css);
987
988 if (prev && prev != root)
989 css_put(&prev->css);
990
991 if (!root->use_hierarchy && root != root_mem_cgroup) {
992 if (prev)
993 return NULL;
994 return root;
995 }
996
997 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800998 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800999 struct cgroup_subsys_state *css;
1000
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001001 if (reclaim) {
1002 int nid = zone_to_nid(reclaim->zone);
1003 int zid = zone_idx(reclaim->zone);
1004 struct mem_cgroup_per_zone *mz;
1005
1006 mz = mem_cgroup_zoneinfo(root, nid, zid);
1007 iter = &mz->reclaim_iter[reclaim->priority];
1008 if (prev && reclaim->generation != iter->generation)
1009 return NULL;
1010 id = iter->position;
1011 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001012
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001013 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001014 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
1015 if (css) {
1016 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -07001017 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001018 } else
1019 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001020 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001021
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001022 if (reclaim) {
1023 iter->position = id;
1024 if (!css)
1025 iter->generation++;
1026 else if (!prev && memcg)
1027 reclaim->generation = iter->generation;
1028 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001029
1030 if (prev && !css)
1031 return NULL;
1032 }
1033 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001034}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001035
Johannes Weiner56600482012-01-12 17:17:59 -08001036/**
1037 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
1038 * @root: hierarchy root
1039 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
1040 */
1041void mem_cgroup_iter_break(struct mem_cgroup *root,
1042 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001043{
1044 if (!root)
1045 root = root_mem_cgroup;
1046 if (prev && prev != root)
1047 css_put(&prev->css);
1048}
1049
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001050/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001051 * Iteration constructs for visiting all cgroups (under a tree). If
1052 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1053 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001054 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001055#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001056 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001057 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001058 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001059
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001060#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001061 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001062 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001063 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001064
David Rientjes68ae5642012-12-12 13:51:57 -08001065void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001066{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001067 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001068
Ying Han456f9982011-05-26 16:25:38 -07001069 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001070 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1071 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001072 goto out;
1073
1074 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001075 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001076 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1077 break;
1078 case PGMAJFAULT:
1079 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001080 break;
1081 default:
1082 BUG();
1083 }
1084out:
1085 rcu_read_unlock();
1086}
David Rientjes68ae5642012-12-12 13:51:57 -08001087EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001088
Johannes Weiner925b7672012-01-12 17:18:15 -08001089/**
1090 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1091 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001092 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001093 *
1094 * Returns the lru list vector holding pages for the given @zone and
1095 * @mem. This can be the global zone lruvec, if the memory controller
1096 * is disabled.
1097 */
1098struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1099 struct mem_cgroup *memcg)
1100{
1101 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001102 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001103
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001104 if (mem_cgroup_disabled()) {
1105 lruvec = &zone->lruvec;
1106 goto out;
1107 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001108
1109 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001110 lruvec = &mz->lruvec;
1111out:
1112 /*
1113 * Since a node can be onlined after the mem_cgroup was created,
1114 * we have to be prepared to initialize lruvec->zone here;
1115 * and if offlined then reonlined, we need to reinitialize it.
1116 */
1117 if (unlikely(lruvec->zone != zone))
1118 lruvec->zone = zone;
1119 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001120}
1121
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001122/*
1123 * Following LRU functions are allowed to be used without PCG_LOCK.
1124 * Operations are called by routine of global LRU independently from memcg.
1125 * What we have to take care of here is validness of pc->mem_cgroup.
1126 *
1127 * Changes to pc->mem_cgroup happens when
1128 * 1. charge
1129 * 2. moving account
1130 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1131 * It is added to LRU before charge.
1132 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1133 * When moving account, the page is not on LRU. It's isolated.
1134 */
1135
Johannes Weiner925b7672012-01-12 17:18:15 -08001136/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001137 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001138 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001139 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001140 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001141struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001142{
1143 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001144 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001145 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001146 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001147
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001148 if (mem_cgroup_disabled()) {
1149 lruvec = &zone->lruvec;
1150 goto out;
1151 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001152
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001153 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001154 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001155
1156 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001157 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001158 * an uncharged page off lru does nothing to secure
1159 * its former mem_cgroup from sudden removal.
1160 *
1161 * Our caller holds lru_lock, and PageCgroupUsed is updated
1162 * under page_cgroup lock: between them, they make all uses
1163 * of pc->mem_cgroup safe.
1164 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001165 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001166 pc->mem_cgroup = memcg = root_mem_cgroup;
1167
Johannes Weiner925b7672012-01-12 17:18:15 -08001168 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001169 lruvec = &mz->lruvec;
1170out:
1171 /*
1172 * Since a node can be onlined after the mem_cgroup was created,
1173 * we have to be prepared to initialize lruvec->zone here;
1174 * and if offlined then reonlined, we need to reinitialize it.
1175 */
1176 if (unlikely(lruvec->zone != zone))
1177 lruvec->zone = zone;
1178 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001179}
1180
1181/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001182 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1183 * @lruvec: mem_cgroup per zone lru vector
1184 * @lru: index of lru list the page is sitting on
1185 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001186 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001187 * This function must be called when a page is added to or removed from an
1188 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001189 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001190void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1191 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001192{
1193 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001194 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001195
1196 if (mem_cgroup_disabled())
1197 return;
1198
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001199 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1200 lru_size = mz->lru_size + lru;
1201 *lru_size += nr_pages;
1202 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001203}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001204
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001205/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001206 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001207 * hierarchy subtree
1208 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001209bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1210 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001211{
Johannes Weiner91c637342012-05-29 15:06:24 -07001212 if (root_memcg == memcg)
1213 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001214 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001215 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001216 return css_is_ancestor(&memcg->css, &root_memcg->css);
1217}
1218
1219static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1220 struct mem_cgroup *memcg)
1221{
1222 bool ret;
1223
Johannes Weiner91c637342012-05-29 15:06:24 -07001224 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001225 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001226 rcu_read_unlock();
1227 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001228}
1229
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001230int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001231{
1232 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001233 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001234 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001235
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001236 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001237 if (p) {
1238 curr = try_get_mem_cgroup_from_mm(p->mm);
1239 task_unlock(p);
1240 } else {
1241 /*
1242 * All threads may have already detached their mm's, but the oom
1243 * killer still needs to detect if they have already been oom
1244 * killed to prevent needlessly killing additional tasks.
1245 */
1246 task_lock(task);
1247 curr = mem_cgroup_from_task(task);
1248 if (curr)
1249 css_get(&curr->css);
1250 task_unlock(task);
1251 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001252 if (!curr)
1253 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001254 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001255 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001256 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001257 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1258 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001259 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001260 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001261 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001262 return ret;
1263}
1264
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001265int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001266{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001267 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001268 unsigned long inactive;
1269 unsigned long active;
1270 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001271
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001272 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1273 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001274
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001275 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1276 if (gb)
1277 inactive_ratio = int_sqrt(10 * gb);
1278 else
1279 inactive_ratio = 1;
1280
Johannes Weiner9b272972011-11-02 13:38:23 -07001281 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001282}
1283
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001284int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001285{
1286 unsigned long active;
1287 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001288
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001289 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1290 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001291
1292 return (active > inactive);
1293}
1294
Balbir Singh6d61ef42009-01-07 18:08:06 -08001295#define mem_cgroup_from_res_counter(counter, member) \
1296 container_of(counter, struct mem_cgroup, member)
1297
Johannes Weiner19942822011-02-01 15:52:43 -08001298/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001299 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001300 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001301 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001302 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001303 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001304 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001305static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001306{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001307 unsigned long long margin;
1308
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001309 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001310 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001311 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001312 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001313}
1314
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001315int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001316{
1317 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001318
1319 /* root ? */
1320 if (cgrp->parent == NULL)
1321 return vm_swappiness;
1322
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001323 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001324}
1325
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001326/*
1327 * memcg->moving_account is used for checking possibility that some thread is
1328 * calling move_account(). When a thread on CPU-A starts moving pages under
1329 * a memcg, other threads should check memcg->moving_account under
1330 * rcu_read_lock(), like this:
1331 *
1332 * CPU-A CPU-B
1333 * rcu_read_lock()
1334 * memcg->moving_account+1 if (memcg->mocing_account)
1335 * take heavy locks.
1336 * synchronize_rcu() update something.
1337 * rcu_read_unlock()
1338 * start move here.
1339 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001340
1341/* for quick checking without looking up memcg */
1342atomic_t memcg_moving __read_mostly;
1343
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001344static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001345{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001346 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001347 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001348 synchronize_rcu();
1349}
1350
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001351static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001352{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001353 /*
1354 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1355 * We check NULL in callee rather than caller.
1356 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001357 if (memcg) {
1358 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001359 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001360 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001361}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001362
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001363/*
1364 * 2 routines for checking "mem" is under move_account() or not.
1365 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001366 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1367 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001368 * pc->mem_cgroup may be overwritten.
1369 *
1370 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1371 * under hierarchy of moving cgroups. This is for
1372 * waiting at hith-memory prressure caused by "move".
1373 */
1374
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001375static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001376{
1377 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001378 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001379}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001380
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001381static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001382{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001383 struct mem_cgroup *from;
1384 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001385 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001386 /*
1387 * Unlike task_move routines, we access mc.to, mc.from not under
1388 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1389 */
1390 spin_lock(&mc.lock);
1391 from = mc.from;
1392 to = mc.to;
1393 if (!from)
1394 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001395
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001396 ret = mem_cgroup_same_or_subtree(memcg, from)
1397 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001398unlock:
1399 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001400 return ret;
1401}
1402
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001403static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001404{
1405 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001406 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001407 DEFINE_WAIT(wait);
1408 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1409 /* moving charge context might have finished. */
1410 if (mc.moving_task)
1411 schedule();
1412 finish_wait(&mc.waitq, &wait);
1413 return true;
1414 }
1415 }
1416 return false;
1417}
1418
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001419/*
1420 * Take this lock when
1421 * - a code tries to modify page's memcg while it's USED.
1422 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001423 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001424 */
1425static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1426 unsigned long *flags)
1427{
1428 spin_lock_irqsave(&memcg->move_lock, *flags);
1429}
1430
1431static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1432 unsigned long *flags)
1433{
1434 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1435}
1436
Balbir Singhe2224322009-04-02 16:57:39 -07001437/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001438 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001439 * @memcg: The memory cgroup that went over limit
1440 * @p: Task that is going to be killed
1441 *
1442 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1443 * enabled
1444 */
1445void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1446{
1447 struct cgroup *task_cgrp;
1448 struct cgroup *mem_cgrp;
1449 /*
1450 * Need a buffer in BSS, can't rely on allocations. The code relies
1451 * on the assumption that OOM is serialized for memory controller.
1452 * If this assumption is broken, revisit this code.
1453 */
1454 static char memcg_name[PATH_MAX];
1455 int ret;
1456
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001457 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001458 return;
1459
Balbir Singhe2224322009-04-02 16:57:39 -07001460 rcu_read_lock();
1461
1462 mem_cgrp = memcg->css.cgroup;
1463 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1464
1465 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1466 if (ret < 0) {
1467 /*
1468 * Unfortunately, we are unable to convert to a useful name
1469 * But we'll still print out the usage information
1470 */
1471 rcu_read_unlock();
1472 goto done;
1473 }
1474 rcu_read_unlock();
1475
1476 printk(KERN_INFO "Task in %s killed", memcg_name);
1477
1478 rcu_read_lock();
1479 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1480 if (ret < 0) {
1481 rcu_read_unlock();
1482 goto done;
1483 }
1484 rcu_read_unlock();
1485
1486 /*
1487 * Continues from above, so we don't need an KERN_ level
1488 */
1489 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1490done:
1491
1492 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1493 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1494 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1495 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1496 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1497 "failcnt %llu\n",
1498 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1499 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1500 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
Glauber Costa510fc4e2012-12-18 14:21:47 -08001501 printk(KERN_INFO "kmem: usage %llukB, limit %llukB, failcnt %llu\n",
1502 res_counter_read_u64(&memcg->kmem, RES_USAGE) >> 10,
1503 res_counter_read_u64(&memcg->kmem, RES_LIMIT) >> 10,
1504 res_counter_read_u64(&memcg->kmem, RES_FAILCNT));
Balbir Singhe2224322009-04-02 16:57:39 -07001505}
1506
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001507/*
1508 * This function returns the number of memcg under hierarchy tree. Returns
1509 * 1(self count) if no children.
1510 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001511static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001512{
1513 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001514 struct mem_cgroup *iter;
1515
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001516 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001517 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001518 return num;
1519}
1520
Balbir Singh6d61ef42009-01-07 18:08:06 -08001521/*
David Rientjesa63d83f42010-08-09 17:19:46 -07001522 * Return the memory (and swap, if configured) limit for a memcg.
1523 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001524static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f42010-08-09 17:19:46 -07001525{
1526 u64 limit;
David Rientjesa63d83f42010-08-09 17:19:46 -07001527
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001528 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001529
David Rientjesa63d83f42010-08-09 17:19:46 -07001530 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001531 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f42010-08-09 17:19:46 -07001532 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001533 if (mem_cgroup_swappiness(memcg)) {
1534 u64 memsw;
1535
1536 limit += total_swap_pages << PAGE_SHIFT;
1537 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1538
1539 /*
1540 * If memsw is finite and limits the amount of swap space
1541 * available to this memcg, return that limit.
1542 */
1543 limit = min(limit, memsw);
1544 }
1545
1546 return limit;
David Rientjesa63d83f42010-08-09 17:19:46 -07001547}
1548
David Rientjes19965462012-12-11 16:00:26 -08001549static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1550 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001551{
1552 struct mem_cgroup *iter;
1553 unsigned long chosen_points = 0;
1554 unsigned long totalpages;
1555 unsigned int points = 0;
1556 struct task_struct *chosen = NULL;
1557
David Rientjes876aafb2012-07-31 16:43:48 -07001558 /*
1559 * If current has a pending SIGKILL, then automatically select it. The
1560 * goal is to allow it to allocate so that it may quickly exit and free
1561 * its memory.
1562 */
1563 if (fatal_signal_pending(current)) {
1564 set_thread_flag(TIF_MEMDIE);
1565 return;
1566 }
1567
1568 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001569 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1570 for_each_mem_cgroup_tree(iter, memcg) {
1571 struct cgroup *cgroup = iter->css.cgroup;
1572 struct cgroup_iter it;
1573 struct task_struct *task;
1574
1575 cgroup_iter_start(cgroup, &it);
1576 while ((task = cgroup_iter_next(cgroup, &it))) {
1577 switch (oom_scan_process_thread(task, totalpages, NULL,
1578 false)) {
1579 case OOM_SCAN_SELECT:
1580 if (chosen)
1581 put_task_struct(chosen);
1582 chosen = task;
1583 chosen_points = ULONG_MAX;
1584 get_task_struct(chosen);
1585 /* fall through */
1586 case OOM_SCAN_CONTINUE:
1587 continue;
1588 case OOM_SCAN_ABORT:
1589 cgroup_iter_end(cgroup, &it);
1590 mem_cgroup_iter_break(memcg, iter);
1591 if (chosen)
1592 put_task_struct(chosen);
1593 return;
1594 case OOM_SCAN_OK:
1595 break;
1596 };
1597 points = oom_badness(task, memcg, NULL, totalpages);
1598 if (points > chosen_points) {
1599 if (chosen)
1600 put_task_struct(chosen);
1601 chosen = task;
1602 chosen_points = points;
1603 get_task_struct(chosen);
1604 }
1605 }
1606 cgroup_iter_end(cgroup, &it);
1607 }
1608
1609 if (!chosen)
1610 return;
1611 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001612 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1613 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001614}
1615
Johannes Weiner56600482012-01-12 17:17:59 -08001616static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1617 gfp_t gfp_mask,
1618 unsigned long flags)
1619{
1620 unsigned long total = 0;
1621 bool noswap = false;
1622 int loop;
1623
1624 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1625 noswap = true;
1626 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1627 noswap = true;
1628
1629 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1630 if (loop)
1631 drain_all_stock_async(memcg);
1632 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1633 /*
1634 * Allow limit shrinkers, which are triggered directly
1635 * by userspace, to catch signals and stop reclaim
1636 * after minimal progress, regardless of the margin.
1637 */
1638 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1639 break;
1640 if (mem_cgroup_margin(memcg))
1641 break;
1642 /*
1643 * If nothing was reclaimed after two attempts, there
1644 * may be no reclaimable pages in this hierarchy.
1645 */
1646 if (loop && !total)
1647 break;
1648 }
1649 return total;
1650}
1651
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001652/**
1653 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001654 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001655 * @nid: the node ID to be checked.
1656 * @noswap : specify true here if the user wants flle only information.
1657 *
1658 * This function returns whether the specified memcg contains any
1659 * reclaimable pages on a node. Returns true if there are any reclaimable
1660 * pages in the node.
1661 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001662static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001663 int nid, bool noswap)
1664{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001665 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001666 return true;
1667 if (noswap || !total_swap_pages)
1668 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001669 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001670 return true;
1671 return false;
1672
1673}
Ying Han889976d2011-05-26 16:25:33 -07001674#if MAX_NUMNODES > 1
1675
1676/*
1677 * Always updating the nodemask is not very good - even if we have an empty
1678 * list or the wrong list here, we can start from some node and traverse all
1679 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1680 *
1681 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001682static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001683{
1684 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001685 /*
1686 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1687 * pagein/pageout changes since the last update.
1688 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001689 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001690 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001691 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001692 return;
1693
Ying Han889976d2011-05-26 16:25:33 -07001694 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001695 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001696
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001697 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001698
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001699 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1700 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001701 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001702
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001703 atomic_set(&memcg->numainfo_events, 0);
1704 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001705}
1706
1707/*
1708 * Selecting a node where we start reclaim from. Because what we need is just
1709 * reducing usage counter, start from anywhere is O,K. Considering
1710 * memory reclaim from current node, there are pros. and cons.
1711 *
1712 * Freeing memory from current node means freeing memory from a node which
1713 * we'll use or we've used. So, it may make LRU bad. And if several threads
1714 * hit limits, it will see a contention on a node. But freeing from remote
1715 * node means more costs for memory reclaim because of memory latency.
1716 *
1717 * Now, we use round-robin. Better algorithm is welcomed.
1718 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001719int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001720{
1721 int node;
1722
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001723 mem_cgroup_may_update_nodemask(memcg);
1724 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001725
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001726 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001727 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001728 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001729 /*
1730 * We call this when we hit limit, not when pages are added to LRU.
1731 * No LRU may hold pages because all pages are UNEVICTABLE or
1732 * memcg is too small and all pages are not on LRU. In that case,
1733 * we use curret node.
1734 */
1735 if (unlikely(node == MAX_NUMNODES))
1736 node = numa_node_id();
1737
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001738 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001739 return node;
1740}
1741
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001742/*
1743 * Check all nodes whether it contains reclaimable pages or not.
1744 * For quick scan, we make use of scan_nodes. This will allow us to skip
1745 * unused nodes. But scan_nodes is lazily updated and may not cotain
1746 * enough new information. We need to do double check.
1747 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001748static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001749{
1750 int nid;
1751
1752 /*
1753 * quick check...making use of scan_node.
1754 * We can skip unused nodes.
1755 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001756 if (!nodes_empty(memcg->scan_nodes)) {
1757 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001758 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001759 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001760
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001761 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001762 return true;
1763 }
1764 }
1765 /*
1766 * Check rest of nodes.
1767 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001768 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001769 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001770 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001771 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001772 return true;
1773 }
1774 return false;
1775}
1776
Ying Han889976d2011-05-26 16:25:33 -07001777#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001778int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001779{
1780 return 0;
1781}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001782
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001783static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001784{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001785 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001786}
Ying Han889976d2011-05-26 16:25:33 -07001787#endif
1788
Johannes Weiner56600482012-01-12 17:17:59 -08001789static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1790 struct zone *zone,
1791 gfp_t gfp_mask,
1792 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001793{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001794 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001795 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001796 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001797 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001798 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001799 struct mem_cgroup_reclaim_cookie reclaim = {
1800 .zone = zone,
1801 .priority = 0,
1802 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001803
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001804 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001805
Balbir Singh4e416952009-09-23 15:56:39 -07001806 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001807 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001808 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001809 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001810 if (loop >= 2) {
1811 /*
1812 * If we have not been able to reclaim
1813 * anything, it might because there are
1814 * no reclaimable pages under this hierarchy
1815 */
Johannes Weiner56600482012-01-12 17:17:59 -08001816 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001817 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001818 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001819 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001820 * excess >> 2 is not to excessive so as to
1821 * reclaim too much, nor too less that we keep
1822 * coming back to reclaim from this cgroup
1823 */
1824 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001825 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001826 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001827 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001828 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001829 }
Johannes Weiner56600482012-01-12 17:17:59 -08001830 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001831 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001832 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1833 zone, &nr_scanned);
1834 *total_scanned += nr_scanned;
1835 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001836 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001837 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001838 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001839 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001840}
1841
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001842/*
1843 * Check OOM-Killer is already running under our hierarchy.
1844 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001845 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001846 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001847static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001848{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001849 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001850
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001851 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001852 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001853 /*
1854 * this subtree of our hierarchy is already locked
1855 * so we cannot give a lock.
1856 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001857 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001858 mem_cgroup_iter_break(memcg, iter);
1859 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001860 } else
1861 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001862 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001863
Michal Hocko79dfdac2011-07-26 16:08:23 -07001864 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001865 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001866
1867 /*
1868 * OK, we failed to lock the whole subtree so we have to clean up
1869 * what we set up to the failing subtree
1870 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001871 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001872 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001873 mem_cgroup_iter_break(memcg, iter);
1874 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001875 }
1876 iter->oom_lock = false;
1877 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001878 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001879}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001880
Michal Hocko79dfdac2011-07-26 16:08:23 -07001881/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001882 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001883 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001884static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001885{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001886 struct mem_cgroup *iter;
1887
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001888 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001889 iter->oom_lock = false;
1890 return 0;
1891}
1892
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001893static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001894{
1895 struct mem_cgroup *iter;
1896
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001897 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001898 atomic_inc(&iter->under_oom);
1899}
1900
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001901static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001902{
1903 struct mem_cgroup *iter;
1904
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001905 /*
1906 * When a new child is created while the hierarchy is under oom,
1907 * mem_cgroup_oom_lock() may not be called. We have to use
1908 * atomic_add_unless() here.
1909 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001910 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001911 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001912}
1913
Michal Hocko1af8efe2011-07-26 16:08:24 -07001914static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001915static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1916
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001917struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001918 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001919 wait_queue_t wait;
1920};
1921
1922static int memcg_oom_wake_function(wait_queue_t *wait,
1923 unsigned mode, int sync, void *arg)
1924{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001925 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1926 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001927 struct oom_wait_info *oom_wait_info;
1928
1929 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001930 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001931
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001932 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001933 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001934 * Then we can use css_is_ancestor without taking care of RCU.
1935 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001936 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1937 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001938 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001939 return autoremove_wake_function(wait, mode, sync, arg);
1940}
1941
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001942static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001943{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001944 /* for filtering, pass "memcg" as argument. */
1945 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001946}
1947
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001948static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001949{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001950 if (memcg && atomic_read(&memcg->under_oom))
1951 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001952}
1953
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001954/*
1955 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1956 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001957static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
1958 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001959{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001960 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001961 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001962
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001963 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001964 owait.wait.flags = 0;
1965 owait.wait.func = memcg_oom_wake_function;
1966 owait.wait.private = current;
1967 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001968 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001969 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001970
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001971 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001972 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001973 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001974 /*
1975 * Even if signal_pending(), we can't quit charge() loop without
1976 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1977 * under OOM is always welcomed, use TASK_KILLABLE here.
1978 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001979 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001980 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001981 need_to_kill = false;
1982 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001983 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001984 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001985
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001986 if (need_to_kill) {
1987 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07001988 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001989 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001990 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001991 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001992 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001993 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001994 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001995 mem_cgroup_oom_unlock(memcg);
1996 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001997 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001998
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001999 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07002000
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002001 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
2002 return false;
2003 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07002004 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002005 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07002006}
2007
Balbir Singhd69b0422009-06-17 16:26:34 -07002008/*
2009 * Currently used to update mapped file statistics, but the routine can be
2010 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002011 *
2012 * Notes: Race condition
2013 *
2014 * We usually use page_cgroup_lock() for accessing page_cgroup member but
2015 * it tends to be costly. But considering some conditions, we doesn't need
2016 * to do so _always_.
2017 *
2018 * Considering "charge", lock_page_cgroup() is not required because all
2019 * file-stat operations happen after a page is attached to radix-tree. There
2020 * are no race with "charge".
2021 *
2022 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
2023 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
2024 * if there are race with "uncharge". Statistics itself is properly handled
2025 * by flags.
2026 *
2027 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002028 * small, we check mm->moving_account and detect there are possibility of race
2029 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07002030 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002031
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002032void __mem_cgroup_begin_update_page_stat(struct page *page,
2033 bool *locked, unsigned long *flags)
2034{
2035 struct mem_cgroup *memcg;
2036 struct page_cgroup *pc;
2037
2038 pc = lookup_page_cgroup(page);
2039again:
2040 memcg = pc->mem_cgroup;
2041 if (unlikely(!memcg || !PageCgroupUsed(pc)))
2042 return;
2043 /*
2044 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07002045 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002046 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002047 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002048 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002049 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002050 return;
2051
2052 move_lock_mem_cgroup(memcg, flags);
2053 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2054 move_unlock_mem_cgroup(memcg, flags);
2055 goto again;
2056 }
2057 *locked = true;
2058}
2059
2060void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2061{
2062 struct page_cgroup *pc = lookup_page_cgroup(page);
2063
2064 /*
2065 * It's guaranteed that pc->mem_cgroup never changes while
2066 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002067 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002068 */
2069 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2070}
2071
Greg Thelen2a7106f2011-01-13 15:47:37 -08002072void mem_cgroup_update_page_stat(struct page *page,
2073 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002074{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002075 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002076 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002077 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002078
Johannes Weinercfa44942012-01-12 17:18:38 -08002079 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002080 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002081
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002082 memcg = pc->mem_cgroup;
2083 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002084 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002085
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002086 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002087 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002088 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002089 break;
2090 default:
2091 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002092 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002093
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002094 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002095}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002096
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002097/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002098 * size of first charge trial. "32" comes from vmscan.c's magic value.
2099 * TODO: maybe necessary to use big numbers in big irons.
2100 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002101#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002102struct memcg_stock_pcp {
2103 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002104 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002105 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002106 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002107#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002108};
2109static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002110static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002111
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002112/**
2113 * consume_stock: Try to consume stocked charge on this cpu.
2114 * @memcg: memcg to consume from.
2115 * @nr_pages: how many pages to charge.
2116 *
2117 * The charges will only happen if @memcg matches the current cpu's memcg
2118 * stock, and at least @nr_pages are available in that stock. Failure to
2119 * service an allocation will refill the stock.
2120 *
2121 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002122 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002123static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002124{
2125 struct memcg_stock_pcp *stock;
2126 bool ret = true;
2127
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002128 if (nr_pages > CHARGE_BATCH)
2129 return false;
2130
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002131 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002132 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2133 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002134 else /* need to call res_counter_charge */
2135 ret = false;
2136 put_cpu_var(memcg_stock);
2137 return ret;
2138}
2139
2140/*
2141 * Returns stocks cached in percpu to res_counter and reset cached information.
2142 */
2143static void drain_stock(struct memcg_stock_pcp *stock)
2144{
2145 struct mem_cgroup *old = stock->cached;
2146
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002147 if (stock->nr_pages) {
2148 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2149
2150 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002151 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002152 res_counter_uncharge(&old->memsw, bytes);
2153 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002154 }
2155 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002156}
2157
2158/*
2159 * This must be called under preempt disabled or must be called by
2160 * a thread which is pinned to local cpu.
2161 */
2162static void drain_local_stock(struct work_struct *dummy)
2163{
2164 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2165 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002166 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002167}
2168
2169/*
2170 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002171 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002172 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002173static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002174{
2175 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2176
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002177 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002178 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002179 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002180 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002181 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002182 put_cpu_var(memcg_stock);
2183}
2184
2185/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002186 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002187 * of the hierarchy under it. sync flag says whether we should block
2188 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002189 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002190static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002191{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002192 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002193
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002194 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002195 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002196 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002197 for_each_online_cpu(cpu) {
2198 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002199 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002200
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002201 memcg = stock->cached;
2202 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002203 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002204 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002205 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002206 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2207 if (cpu == curcpu)
2208 drain_local_stock(&stock->work);
2209 else
2210 schedule_work_on(cpu, &stock->work);
2211 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002212 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002213 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002214
2215 if (!sync)
2216 goto out;
2217
2218 for_each_online_cpu(cpu) {
2219 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002220 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002221 flush_work(&stock->work);
2222 }
2223out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002224 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002225}
2226
2227/*
2228 * Tries to drain stocked charges in other cpus. This function is asynchronous
2229 * and just put a work per cpu for draining localy on each cpu. Caller can
2230 * expects some charges will be back to res_counter later but cannot wait for
2231 * it.
2232 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002233static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002234{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002235 /*
2236 * If someone calls draining, avoid adding more kworker runs.
2237 */
2238 if (!mutex_trylock(&percpu_charge_mutex))
2239 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002240 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002241 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002242}
2243
2244/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002245static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002246{
2247 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002248 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002249 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002250 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002251}
2252
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002253/*
2254 * This function drains percpu counter value from DEAD cpu and
2255 * move it to local cpu. Note that this function can be preempted.
2256 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002257static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002258{
2259 int i;
2260
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002261 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002262 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002263 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002264
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002265 per_cpu(memcg->stat->count[i], cpu) = 0;
2266 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002267 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002268 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002269 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002270
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002271 per_cpu(memcg->stat->events[i], cpu) = 0;
2272 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002273 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002274 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002275}
2276
2277static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002278 unsigned long action,
2279 void *hcpu)
2280{
2281 int cpu = (unsigned long)hcpu;
2282 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002283 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002284
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002285 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002286 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002287
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002288 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002289 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002290
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002291 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002292 mem_cgroup_drain_pcp_counter(iter, cpu);
2293
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002294 stock = &per_cpu(memcg_stock, cpu);
2295 drain_stock(stock);
2296 return NOTIFY_OK;
2297}
2298
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002299
2300/* See __mem_cgroup_try_charge() for details */
2301enum {
2302 CHARGE_OK, /* success */
2303 CHARGE_RETRY, /* need to retry but retry is not bad */
2304 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2305 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2306 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2307};
2308
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002309static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002310 unsigned int nr_pages, unsigned int min_pages,
2311 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002312{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002313 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002314 struct mem_cgroup *mem_over_limit;
2315 struct res_counter *fail_res;
2316 unsigned long flags = 0;
2317 int ret;
2318
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002319 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002320
2321 if (likely(!ret)) {
2322 if (!do_swap_account)
2323 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002324 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002325 if (likely(!ret))
2326 return CHARGE_OK;
2327
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002328 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002329 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2330 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2331 } else
2332 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002333 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002334 * Never reclaim on behalf of optional batching, retry with a
2335 * single page instead.
2336 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002337 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002338 return CHARGE_RETRY;
2339
2340 if (!(gfp_mask & __GFP_WAIT))
2341 return CHARGE_WOULDBLOCK;
2342
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002343 if (gfp_mask & __GFP_NORETRY)
2344 return CHARGE_NOMEM;
2345
Johannes Weiner56600482012-01-12 17:17:59 -08002346 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002347 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002348 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002349 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002350 * Even though the limit is exceeded at this point, reclaim
2351 * may have been able to free some pages. Retry the charge
2352 * before killing the task.
2353 *
2354 * Only for regular pages, though: huge pages are rather
2355 * unlikely to succeed so close to the limit, and we fall back
2356 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002357 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002358 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002359 return CHARGE_RETRY;
2360
2361 /*
2362 * At task move, charge accounts can be doubly counted. So, it's
2363 * better to wait until the end of task_move if something is going on.
2364 */
2365 if (mem_cgroup_wait_acct_move(mem_over_limit))
2366 return CHARGE_RETRY;
2367
2368 /* If we don't need to call oom-killer at el, return immediately */
2369 if (!oom_check)
2370 return CHARGE_NOMEM;
2371 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002372 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002373 return CHARGE_OOM_DIE;
2374
2375 return CHARGE_RETRY;
2376}
2377
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002378/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002379 * __mem_cgroup_try_charge() does
2380 * 1. detect memcg to be charged against from passed *mm and *ptr,
2381 * 2. update res_counter
2382 * 3. call memory reclaim if necessary.
2383 *
2384 * In some special case, if the task is fatal, fatal_signal_pending() or
2385 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2386 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2387 * as possible without any hazards. 2: all pages should have a valid
2388 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2389 * pointer, that is treated as a charge to root_mem_cgroup.
2390 *
2391 * So __mem_cgroup_try_charge() will return
2392 * 0 ... on success, filling *ptr with a valid memcg pointer.
2393 * -ENOMEM ... charge failure because of resource limits.
2394 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2395 *
2396 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2397 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002398 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002399static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002400 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002401 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002402 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002403 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002404{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002405 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002406 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002407 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002408 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002409
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002410 /*
2411 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2412 * in system level. So, allow to go ahead dying process in addition to
2413 * MEMDIE process.
2414 */
2415 if (unlikely(test_thread_flag(TIF_MEMDIE)
2416 || fatal_signal_pending(current)))
2417 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002418
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002419 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002420 * We always charge the cgroup the mm_struct belongs to.
2421 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002422 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002423 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002424 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002425 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002426 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002427again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002428 if (*ptr) { /* css should be a valid one */
2429 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002430 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002431 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002432 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002433 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002434 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002435 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002436 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002437
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002438 rcu_read_lock();
2439 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002440 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002441 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002442 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002443 * race with swapoff. Then, we have small risk of mis-accouning.
2444 * But such kind of mis-account by race always happens because
2445 * we don't have cgroup_mutex(). It's overkill and we allo that
2446 * small race, here.
2447 * (*) swapoff at el will charge against mm-struct not against
2448 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002449 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002450 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002451 if (!memcg)
2452 memcg = root_mem_cgroup;
2453 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002454 rcu_read_unlock();
2455 goto done;
2456 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002457 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002458 /*
2459 * It seems dagerous to access memcg without css_get().
2460 * But considering how consume_stok works, it's not
2461 * necessary. If consume_stock success, some charges
2462 * from this memcg are cached on this cpu. So, we
2463 * don't need to call css_get()/css_tryget() before
2464 * calling consume_stock().
2465 */
2466 rcu_read_unlock();
2467 goto done;
2468 }
2469 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002470 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002471 rcu_read_unlock();
2472 goto again;
2473 }
2474 rcu_read_unlock();
2475 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002476
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002477 do {
2478 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002479
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002480 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002481 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002482 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002483 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002484 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002485
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002486 oom_check = false;
2487 if (oom && !nr_oom_retries) {
2488 oom_check = true;
2489 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2490 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002491
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002492 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2493 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002494 switch (ret) {
2495 case CHARGE_OK:
2496 break;
2497 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002498 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002499 css_put(&memcg->css);
2500 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002501 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002502 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002503 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002504 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002505 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002506 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002507 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002508 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002509 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002510 /* If oom, we never return -ENOMEM */
2511 nr_oom_retries--;
2512 break;
2513 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002514 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002515 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002516 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002517 } while (ret != CHARGE_OK);
2518
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002519 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002520 refill_stock(memcg, batch - nr_pages);
2521 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002522done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002523 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002524 return 0;
2525nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002526 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002527 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002528bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002529 *ptr = root_mem_cgroup;
2530 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002531}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002532
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002533/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002534 * Somemtimes we have to undo a charge we got by try_charge().
2535 * This function is for that and do uncharge, put css's refcnt.
2536 * gotten by try_charge().
2537 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002538static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002539 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002540{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002541 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002542 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002543
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002544 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002545 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002546 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002547 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002548}
2549
2550/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002551 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2552 * This is useful when moving usage to parent cgroup.
2553 */
2554static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2555 unsigned int nr_pages)
2556{
2557 unsigned long bytes = nr_pages * PAGE_SIZE;
2558
2559 if (mem_cgroup_is_root(memcg))
2560 return;
2561
2562 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2563 if (do_swap_account)
2564 res_counter_uncharge_until(&memcg->memsw,
2565 memcg->memsw.parent, bytes);
2566}
2567
2568/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002569 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002570 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2571 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2572 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002573 */
2574static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2575{
2576 struct cgroup_subsys_state *css;
2577
2578 /* ID 0 is unused ID */
2579 if (!id)
2580 return NULL;
2581 css = css_lookup(&mem_cgroup_subsys, id);
2582 if (!css)
2583 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002584 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002585}
2586
Wu Fengguange42d9d52009-12-16 12:19:59 +01002587struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002588{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002589 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002590 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002591 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002592 swp_entry_t ent;
2593
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002594 VM_BUG_ON(!PageLocked(page));
2595
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002596 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002597 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002598 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002599 memcg = pc->mem_cgroup;
2600 if (memcg && !css_tryget(&memcg->css))
2601 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002602 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002603 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002604 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002605 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002606 memcg = mem_cgroup_lookup(id);
2607 if (memcg && !css_tryget(&memcg->css))
2608 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002609 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002610 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002611 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002612 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002613}
2614
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002615static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002616 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002617 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002618 enum charge_type ctype,
2619 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002620{
Johannes Weinerce587e62012-04-24 20:22:33 +02002621 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002622 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002623 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002624 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002625 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002626
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002627 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002628 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002629 /*
2630 * we don't need page_cgroup_lock about tail pages, becase they are not
2631 * accessed by any other context at this point.
2632 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002633
2634 /*
2635 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2636 * may already be on some other mem_cgroup's LRU. Take care of it.
2637 */
2638 if (lrucare) {
2639 zone = page_zone(page);
2640 spin_lock_irq(&zone->lru_lock);
2641 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002642 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002643 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002644 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002645 was_on_lru = true;
2646 }
2647 }
2648
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002649 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002650 /*
2651 * We access a page_cgroup asynchronously without lock_page_cgroup().
2652 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2653 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2654 * before USED bit, we need memory barrier here.
2655 * See mem_cgroup_add_lru_list(), etc.
2656 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002657 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002658 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002659
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002660 if (lrucare) {
2661 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002662 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002663 VM_BUG_ON(PageLRU(page));
2664 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002665 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002666 }
2667 spin_unlock_irq(&zone->lru_lock);
2668 }
2669
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002670 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002671 anon = true;
2672 else
2673 anon = false;
2674
2675 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002676 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002677
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002678 /*
2679 * "charge_statistics" updated event counter. Then, check it.
2680 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2681 * if they exceeds softlimit.
2682 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002683 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002684}
2685
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002686#ifdef CONFIG_MEMCG_KMEM
2687static inline bool memcg_can_account_kmem(struct mem_cgroup *memcg)
2688{
2689 return !mem_cgroup_disabled() && !mem_cgroup_is_root(memcg) &&
2690 (memcg->kmem_account_flags & KMEM_ACCOUNTED_MASK);
2691}
2692
2693static int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp, u64 size)
2694{
2695 struct res_counter *fail_res;
2696 struct mem_cgroup *_memcg;
2697 int ret = 0;
2698 bool may_oom;
2699
2700 ret = res_counter_charge(&memcg->kmem, size, &fail_res);
2701 if (ret)
2702 return ret;
2703
2704 /*
2705 * Conditions under which we can wait for the oom_killer. Those are
2706 * the same conditions tested by the core page allocator
2707 */
2708 may_oom = (gfp & __GFP_FS) && !(gfp & __GFP_NORETRY);
2709
2710 _memcg = memcg;
2711 ret = __mem_cgroup_try_charge(NULL, gfp, size >> PAGE_SHIFT,
2712 &_memcg, may_oom);
2713
2714 if (ret == -EINTR) {
2715 /*
2716 * __mem_cgroup_try_charge() chosed to bypass to root due to
2717 * OOM kill or fatal signal. Since our only options are to
2718 * either fail the allocation or charge it to this cgroup, do
2719 * it as a temporary condition. But we can't fail. From a
2720 * kmem/slab perspective, the cache has already been selected,
2721 * by mem_cgroup_kmem_get_cache(), so it is too late to change
2722 * our minds.
2723 *
2724 * This condition will only trigger if the task entered
2725 * memcg_charge_kmem in a sane state, but was OOM-killed during
2726 * __mem_cgroup_try_charge() above. Tasks that were already
2727 * dying when the allocation triggers should have been already
2728 * directed to the root cgroup in memcontrol.h
2729 */
2730 res_counter_charge_nofail(&memcg->res, size, &fail_res);
2731 if (do_swap_account)
2732 res_counter_charge_nofail(&memcg->memsw, size,
2733 &fail_res);
2734 ret = 0;
2735 } else if (ret)
2736 res_counter_uncharge(&memcg->kmem, size);
2737
2738 return ret;
2739}
2740
2741static void memcg_uncharge_kmem(struct mem_cgroup *memcg, u64 size)
2742{
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002743 res_counter_uncharge(&memcg->res, size);
2744 if (do_swap_account)
2745 res_counter_uncharge(&memcg->memsw, size);
Glauber Costa7de37682012-12-18 14:22:07 -08002746
2747 /* Not down to 0 */
2748 if (res_counter_uncharge(&memcg->kmem, size))
2749 return;
2750
2751 if (memcg_kmem_test_and_clear_dead(memcg))
2752 mem_cgroup_put(memcg);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002753}
2754
2755/*
2756 * We need to verify if the allocation against current->mm->owner's memcg is
2757 * possible for the given order. But the page is not allocated yet, so we'll
2758 * need a further commit step to do the final arrangements.
2759 *
2760 * It is possible for the task to switch cgroups in this mean time, so at
2761 * commit time, we can't rely on task conversion any longer. We'll then use
2762 * the handle argument to return to the caller which cgroup we should commit
2763 * against. We could also return the memcg directly and avoid the pointer
2764 * passing, but a boolean return value gives better semantics considering
2765 * the compiled-out case as well.
2766 *
2767 * Returning true means the allocation is possible.
2768 */
2769bool
2770__memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **_memcg, int order)
2771{
2772 struct mem_cgroup *memcg;
2773 int ret;
2774
2775 *_memcg = NULL;
2776 memcg = try_get_mem_cgroup_from_mm(current->mm);
2777
2778 /*
2779 * very rare case described in mem_cgroup_from_task. Unfortunately there
2780 * isn't much we can do without complicating this too much, and it would
2781 * be gfp-dependent anyway. Just let it go
2782 */
2783 if (unlikely(!memcg))
2784 return true;
2785
2786 if (!memcg_can_account_kmem(memcg)) {
2787 css_put(&memcg->css);
2788 return true;
2789 }
2790
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002791 ret = memcg_charge_kmem(memcg, gfp, PAGE_SIZE << order);
2792 if (!ret)
2793 *_memcg = memcg;
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002794
2795 css_put(&memcg->css);
2796 return (ret == 0);
2797}
2798
2799void __memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg,
2800 int order)
2801{
2802 struct page_cgroup *pc;
2803
2804 VM_BUG_ON(mem_cgroup_is_root(memcg));
2805
2806 /* The page allocation failed. Revert */
2807 if (!page) {
2808 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002809 return;
2810 }
2811
2812 pc = lookup_page_cgroup(page);
2813 lock_page_cgroup(pc);
2814 pc->mem_cgroup = memcg;
2815 SetPageCgroupUsed(pc);
2816 unlock_page_cgroup(pc);
2817}
2818
2819void __memcg_kmem_uncharge_pages(struct page *page, int order)
2820{
2821 struct mem_cgroup *memcg = NULL;
2822 struct page_cgroup *pc;
2823
2824
2825 pc = lookup_page_cgroup(page);
2826 /*
2827 * Fast unlocked return. Theoretically might have changed, have to
2828 * check again after locking.
2829 */
2830 if (!PageCgroupUsed(pc))
2831 return;
2832
2833 lock_page_cgroup(pc);
2834 if (PageCgroupUsed(pc)) {
2835 memcg = pc->mem_cgroup;
2836 ClearPageCgroupUsed(pc);
2837 }
2838 unlock_page_cgroup(pc);
2839
2840 /*
2841 * We trust that only if there is a memcg associated with the page, it
2842 * is a valid allocation
2843 */
2844 if (!memcg)
2845 return;
2846
2847 VM_BUG_ON(mem_cgroup_is_root(memcg));
2848 memcg_uncharge_kmem(memcg, PAGE_SIZE << order);
Glauber Costa7ae1e1d2012-12-18 14:21:56 -08002849}
2850#endif /* CONFIG_MEMCG_KMEM */
2851
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002852#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2853
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002854#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002855/*
2856 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002857 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2858 * charge/uncharge will be never happen and move_account() is done under
2859 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002860 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002861void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002862{
2863 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002864 struct page_cgroup *pc;
2865 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002866
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002867 if (mem_cgroup_disabled())
2868 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002869 for (i = 1; i < HPAGE_PMD_NR; i++) {
2870 pc = head_pc + i;
2871 pc->mem_cgroup = head_pc->mem_cgroup;
2872 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002873 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2874 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002875}
Hugh Dickins12d27102012-01-12 17:19:52 -08002876#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002877
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002878/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002879 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002880 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002881 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002882 * @pc: page_cgroup of the page.
2883 * @from: mem_cgroup which the page is moved from.
2884 * @to: mem_cgroup which the page is moved to. @from != @to.
2885 *
2886 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002887 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002888 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002889 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002890 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2891 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002892 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002893static int mem_cgroup_move_account(struct page *page,
2894 unsigned int nr_pages,
2895 struct page_cgroup *pc,
2896 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002897 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002898{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002899 unsigned long flags;
2900 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002901 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002902
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002903 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002904 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002905 /*
2906 * The page is isolated from LRU. So, collapse function
2907 * will not handle this page. But page splitting can happen.
2908 * Do this check under compound_page_lock(). The caller should
2909 * hold it.
2910 */
2911 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002912 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002913 goto out;
2914
2915 lock_page_cgroup(pc);
2916
2917 ret = -EINVAL;
2918 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2919 goto unlock;
2920
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002921 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002922
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07002923 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002924 /* Update mapped_file data for mem_cgroup */
2925 preempt_disable();
2926 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2927 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2928 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002929 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002930 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002931
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002932 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002933 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002934 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002935 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002936 ret = 0;
2937unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002938 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002939 /*
2940 * check events
2941 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002942 memcg_check_events(to, page);
2943 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002944out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002945 return ret;
2946}
2947
Michal Hocko2ef37d32012-10-26 13:37:30 +02002948/**
2949 * mem_cgroup_move_parent - moves page to the parent group
2950 * @page: the page to move
2951 * @pc: page_cgroup of the page
2952 * @child: page's cgroup
2953 *
2954 * move charges to its parent or the root cgroup if the group has no
2955 * parent (aka use_hierarchy==0).
2956 * Although this might fail (get_page_unless_zero, isolate_lru_page or
2957 * mem_cgroup_move_account fails) the failure is always temporary and
2958 * it signals a race with a page removal/uncharge or migration. In the
2959 * first case the page is on the way out and it will vanish from the LRU
2960 * on the next attempt and the call should be retried later.
2961 * Isolation from the LRU fails only if page has been isolated from
2962 * the LRU since we looked at it and that usually means either global
2963 * reclaim or migration going on. The page will either get back to the
2964 * LRU or vanish.
2965 * Finaly mem_cgroup_move_account fails only if the page got uncharged
2966 * (!PageCgroupUsed) or moved to a different group. The page will
2967 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002968 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002969static int mem_cgroup_move_parent(struct page *page,
2970 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07002971 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002972{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002973 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002974 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002975 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002976 int ret;
2977
Michal Hockod8423012012-10-26 13:37:29 +02002978 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002979
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002980 ret = -EBUSY;
2981 if (!get_page_unless_zero(page))
2982 goto out;
2983 if (isolate_lru_page(page))
2984 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002985
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002986 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002987
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002988 parent = parent_mem_cgroup(child);
2989 /*
2990 * If no parent, move charges to root cgroup.
2991 */
2992 if (!parent)
2993 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002994
Michal Hocko2ef37d32012-10-26 13:37:30 +02002995 if (nr_pages > 1) {
2996 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002997 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02002998 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002999
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003000 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07003001 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07003002 if (!ret)
3003 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08003004
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003005 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08003006 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003007 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003008put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08003009 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08003010out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003011 return ret;
3012}
3013
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003014/*
3015 * Charge the memory controller for page usage.
3016 * Return
3017 * 0 if the charge was successful
3018 * < 0 if the cgroup is over its limit
3019 */
3020static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07003021 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003022{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003023 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003024 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08003025 bool oom = true;
3026 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08003027
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003028 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003029 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003030 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08003031 /*
3032 * Never OOM-kill a process for a huge page. The
3033 * fault handler will fall back to regular pages.
3034 */
3035 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003036 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003037
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003038 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003039 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003040 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02003041 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003042 return 0;
3043}
3044
3045int mem_cgroup_newpage_charge(struct page *page,
3046 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003047{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003048 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07003049 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08003050 VM_BUG_ON(page_mapped(page));
3051 VM_BUG_ON(page->mapping && !PageAnon(page));
3052 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003053 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003054 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08003055}
3056
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003057/*
3058 * While swap-in, try_charge -> commit or cancel, the page is locked.
3059 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02003060 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003061 * "commit()" or removed by "cancel()"
3062 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003063static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
3064 struct page *page,
3065 gfp_t mask,
3066 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003067{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003068 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07003069 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003070 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003071
Johannes Weiner90deb782012-07-31 16:45:47 -07003072 pc = lookup_page_cgroup(page);
3073 /*
3074 * Every swap fault against a single page tries to charge the
3075 * page, bail as early as possible. shmem_unuse() encounters
3076 * already charged pages, too. The USED bit is protected by
3077 * the page lock, which serializes swap cache removal, which
3078 * in turn serializes uncharging.
3079 */
3080 if (PageCgroupUsed(pc))
3081 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003082 if (!do_swap_account)
3083 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003084 memcg = try_get_mem_cgroup_from_page(page);
3085 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003086 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08003087 *memcgp = memcg;
3088 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003089 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003090 if (ret == -EINTR)
3091 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08003092 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003093charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08003094 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
3095 if (ret == -EINTR)
3096 ret = 0;
3097 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003098}
3099
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003100int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
3101 gfp_t gfp_mask, struct mem_cgroup **memcgp)
3102{
3103 *memcgp = NULL;
3104 if (mem_cgroup_disabled())
3105 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07003106 /*
3107 * A racing thread's fault, or swapoff, may have already
3108 * updated the pte, and even removed page from swap cache: in
3109 * those cases unuse_pte()'s pte_same() test will fail; but
3110 * there's also a KSM case which does need to charge the page.
3111 */
3112 if (!PageSwapCache(page)) {
3113 int ret;
3114
3115 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
3116 if (ret == -EINTR)
3117 ret = 0;
3118 return ret;
3119 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003120 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
3121}
3122
Johannes Weiner827a03d2012-07-31 16:45:36 -07003123void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
3124{
3125 if (mem_cgroup_disabled())
3126 return;
3127 if (!memcg)
3128 return;
3129 __mem_cgroup_cancel_charge(memcg, 1);
3130}
3131
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003132static void
Johannes Weiner72835c82012-01-12 17:18:32 -08003133__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003134 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003135{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003136 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003137 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08003138 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003139 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07003140
Johannes Weinerce587e62012-04-24 20:22:33 +02003141 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003142 /*
3143 * Now swap is on-memory. This means this page may be
3144 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003145 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
3146 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
3147 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003148 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08003149 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003150 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07003151 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003152 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003153}
3154
Johannes Weiner72835c82012-01-12 17:18:32 -08003155void mem_cgroup_commit_charge_swapin(struct page *page,
3156 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003157{
Johannes Weiner72835c82012-01-12 17:18:32 -08003158 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003159 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07003160}
3161
Johannes Weiner827a03d2012-07-31 16:45:36 -07003162int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
3163 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003164{
Johannes Weiner827a03d2012-07-31 16:45:36 -07003165 struct mem_cgroup *memcg = NULL;
3166 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
3167 int ret;
3168
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003169 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07003170 return 0;
3171 if (PageCompound(page))
3172 return 0;
3173
Johannes Weiner827a03d2012-07-31 16:45:36 -07003174 if (!PageSwapCache(page))
3175 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
3176 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07003177 ret = __mem_cgroup_try_charge_swapin(mm, page,
3178 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07003179 if (!ret)
3180 __mem_cgroup_commit_charge_swapin(page, memcg, type);
3181 }
3182 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003183}
3184
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003185static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003186 unsigned int nr_pages,
3187 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003188{
3189 struct memcg_batch_info *batch = NULL;
3190 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003191
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003192 /* If swapout, usage of swap doesn't decrease */
3193 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
3194 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003195
3196 batch = &current->memcg_batch;
3197 /*
3198 * In usual, we do css_get() when we remember memcg pointer.
3199 * But in this case, we keep res->usage until end of a series of
3200 * uncharges. Then, it's ok to ignore memcg's refcnt.
3201 */
3202 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003203 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003204 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003205 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003206 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003207 * the same cgroup and we have chance to coalesce uncharges.
3208 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
3209 * because we want to do uncharge as soon as possible.
3210 */
3211
3212 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
3213 goto direct_uncharge;
3214
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003215 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003216 goto direct_uncharge;
3217
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003218 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003219 * In typical case, batch->memcg == mem. This means we can
3220 * merge a series of uncharges to an uncharge of res_counter.
3221 * If not, we uncharge res_counter ony by one.
3222 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003223 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003224 goto direct_uncharge;
3225 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003226 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003227 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003228 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003229 return;
3230direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003231 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003232 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003233 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3234 if (unlikely(batch->memcg != memcg))
3235 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003236}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003237
Balbir Singh8697d332008-02-07 00:13:59 -08003238/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003239 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003240 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003241static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003242__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3243 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003244{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003245 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003246 unsigned int nr_pages = 1;
3247 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003248 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003249
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003250 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003251 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003252
Johannes Weiner0c59b892012-07-31 16:45:31 -07003253 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003254
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003255 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003256 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003257 VM_BUG_ON(!PageTransHuge(page));
3258 }
Balbir Singh8697d332008-02-07 00:13:59 -08003259 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003260 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003261 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003262 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003263 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003264 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003265
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003266 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003267
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003268 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003269
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003270 if (!PageCgroupUsed(pc))
3271 goto unlock_out;
3272
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003273 anon = PageAnon(page);
3274
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003275 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003276 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003277 /*
3278 * Generally PageAnon tells if it's the anon statistics to be
3279 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3280 * used before page reached the stage of being marked PageAnon.
3281 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003282 anon = true;
3283 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003284 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003285 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07003286 if (page_mapped(page))
3287 goto unlock_out;
3288 /*
3289 * Pages under migration may not be uncharged. But
3290 * end_migration() /must/ be the one uncharging the
3291 * unused post-migration page and so it has to call
3292 * here with the migration bit still set. See the
3293 * res_counter handling below.
3294 */
3295 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003296 goto unlock_out;
3297 break;
3298 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3299 if (!PageAnon(page)) { /* Shared memory */
3300 if (page->mapping && !page_is_file_cache(page))
3301 goto unlock_out;
3302 } else if (page_mapped(page)) /* Anon */
3303 goto unlock_out;
3304 break;
3305 default:
3306 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003307 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003308
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003309 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003310
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003311 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003312 /*
3313 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3314 * freed from LRU. This is safe because uncharged page is expected not
3315 * to be reused (freed soon). Exception is SwapCache, it's handled by
3316 * special functions.
3317 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003318
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003319 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003320 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003321 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003322 * will never be freed.
3323 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003324 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003325 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003326 mem_cgroup_swap_statistics(memcg, true);
3327 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003328 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003329 /*
3330 * Migration does not charge the res_counter for the
3331 * replacement page, so leave it alone when phasing out the
3332 * page that is unused after the migration.
3333 */
3334 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003335 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003336
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003337 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003338
3339unlock_out:
3340 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003341 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003342}
3343
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003344void mem_cgroup_uncharge_page(struct page *page)
3345{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003346 /* early check. */
3347 if (page_mapped(page))
3348 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08003349 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07003350 if (PageSwapCache(page))
3351 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07003352 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003353}
3354
3355void mem_cgroup_uncharge_cache_page(struct page *page)
3356{
3357 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003358 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07003359 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003360}
3361
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003362/*
3363 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3364 * In that cases, pages are freed continuously and we can expect pages
3365 * are in the same memcg. All these calls itself limits the number of
3366 * pages freed at once, then uncharge_start/end() is called properly.
3367 * This may be called prural(2) times in a context,
3368 */
3369
3370void mem_cgroup_uncharge_start(void)
3371{
3372 current->memcg_batch.do_batch++;
3373 /* We can do nest. */
3374 if (current->memcg_batch.do_batch == 1) {
3375 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003376 current->memcg_batch.nr_pages = 0;
3377 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003378 }
3379}
3380
3381void mem_cgroup_uncharge_end(void)
3382{
3383 struct memcg_batch_info *batch = &current->memcg_batch;
3384
3385 if (!batch->do_batch)
3386 return;
3387
3388 batch->do_batch--;
3389 if (batch->do_batch) /* If stacked, do nothing. */
3390 return;
3391
3392 if (!batch->memcg)
3393 return;
3394 /*
3395 * This "batch->memcg" is valid without any css_get/put etc...
3396 * bacause we hide charges behind us.
3397 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003398 if (batch->nr_pages)
3399 res_counter_uncharge(&batch->memcg->res,
3400 batch->nr_pages * PAGE_SIZE);
3401 if (batch->memsw_nr_pages)
3402 res_counter_uncharge(&batch->memcg->memsw,
3403 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003404 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003405 /* forget this pointer (for sanity check) */
3406 batch->memcg = NULL;
3407}
3408
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003409#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003410/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003411 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003412 * memcg information is recorded to swap_cgroup of "ent"
3413 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003414void
3415mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003416{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003417 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003418 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003419
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003420 if (!swapout) /* this was a swap cache but the swap is unused ! */
3421 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3422
Johannes Weiner0030f532012-07-31 16:45:25 -07003423 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003424
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003425 /*
3426 * record memcg information, if swapout && memcg != NULL,
3427 * mem_cgroup_get() was called in uncharge().
3428 */
3429 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003430 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003431}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003432#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003433
Andrew Mortonc255a452012-07-31 16:43:02 -07003434#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003435/*
3436 * called from swap_entry_free(). remove record in swap_cgroup and
3437 * uncharge "memsw" account.
3438 */
3439void mem_cgroup_uncharge_swap(swp_entry_t ent)
3440{
3441 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003442 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003443
3444 if (!do_swap_account)
3445 return;
3446
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003447 id = swap_cgroup_record(ent, 0);
3448 rcu_read_lock();
3449 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003450 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003451 /*
3452 * We uncharge this because swap is freed.
3453 * This memcg can be obsolete one. We avoid calling css_tryget
3454 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003455 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003456 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003457 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003458 mem_cgroup_put(memcg);
3459 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003460 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003461}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003462
3463/**
3464 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3465 * @entry: swap entry to be moved
3466 * @from: mem_cgroup which the entry is moved from
3467 * @to: mem_cgroup which the entry is moved to
3468 *
3469 * It succeeds only when the swap_cgroup's record for this entry is the same
3470 * as the mem_cgroup's id of @from.
3471 *
3472 * Returns 0 on success, -EINVAL on failure.
3473 *
3474 * The caller must have charged to @to, IOW, called res_counter_charge() about
3475 * both res and memsw, and called css_get().
3476 */
3477static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003478 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003479{
3480 unsigned short old_id, new_id;
3481
3482 old_id = css_id(&from->css);
3483 new_id = css_id(&to->css);
3484
3485 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003486 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003487 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003488 /*
3489 * This function is only called from task migration context now.
3490 * It postpones res_counter and refcount handling till the end
3491 * of task migration(mem_cgroup_clear_mc()) for performance
3492 * improvement. But we cannot postpone mem_cgroup_get(to)
3493 * because if the process that has been moved to @to does
3494 * swap-in, the refcount of @to might be decreased to 0.
3495 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003496 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003497 return 0;
3498 }
3499 return -EINVAL;
3500}
3501#else
3502static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003503 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003504{
3505 return -EINVAL;
3506}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003507#endif
3508
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003509/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003510 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3511 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003512 */
Johannes Weiner0030f532012-07-31 16:45:25 -07003513void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
3514 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003515{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003516 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00003517 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003518 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003519 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003520
Johannes Weiner72835c82012-01-12 17:18:32 -08003521 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003522
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003523 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07003524 return;
Balbir Singh40779602008-04-04 14:29:59 -07003525
Mel Gormanb32967f2012-11-19 12:35:47 +00003526 if (PageTransHuge(page))
3527 nr_pages <<= compound_order(page);
3528
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003529 pc = lookup_page_cgroup(page);
3530 lock_page_cgroup(pc);
3531 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003532 memcg = pc->mem_cgroup;
3533 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003534 /*
3535 * At migrating an anonymous page, its mapcount goes down
3536 * to 0 and uncharge() will be called. But, even if it's fully
3537 * unmapped, migration may fail and this page has to be
3538 * charged again. We set MIGRATION flag here and delay uncharge
3539 * until end_migration() is called
3540 *
3541 * Corner Case Thinking
3542 * A)
3543 * When the old page was mapped as Anon and it's unmap-and-freed
3544 * while migration was ongoing.
3545 * If unmap finds the old page, uncharge() of it will be delayed
3546 * until end_migration(). If unmap finds a new page, it's
3547 * uncharged when it make mapcount to be 1->0. If unmap code
3548 * finds swap_migration_entry, the new page will not be mapped
3549 * and end_migration() will find it(mapcount==0).
3550 *
3551 * B)
3552 * When the old page was mapped but migraion fails, the kernel
3553 * remaps it. A charge for it is kept by MIGRATION flag even
3554 * if mapcount goes down to 0. We can do remap successfully
3555 * without charging it again.
3556 *
3557 * C)
3558 * The "old" page is under lock_page() until the end of
3559 * migration, so, the old page itself will not be swapped-out.
3560 * If the new page is swapped out before end_migraton, our
3561 * hook to usual swap-out path will catch the event.
3562 */
3563 if (PageAnon(page))
3564 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003565 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003566 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003567 /*
3568 * If the page is not charged at this point,
3569 * we return here.
3570 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003571 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07003572 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003573
Johannes Weiner72835c82012-01-12 17:18:32 -08003574 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003575 /*
3576 * We charge new page before it's used/mapped. So, even if unlock_page()
3577 * is called before end_migration, we can catch all events on this new
3578 * page. In the case new page is migrated but not remapped, new page's
3579 * mapcount will be finally 0 and we call uncharge in end_migration().
3580 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003581 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003582 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003583 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07003584 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07003585 /*
3586 * The page is committed to the memcg, but it's not actually
3587 * charged to the res_counter since we plan on replacing the
3588 * old one and only one page is going to be left afterwards.
3589 */
Mel Gormanb32967f2012-11-19 12:35:47 +00003590 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003591}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003592
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003593/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003594void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003595 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003596{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003597 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003598 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003599 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003600
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003601 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003602 return;
Tejun Heob25ed602012-11-05 09:16:59 -08003603
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003604 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003605 used = oldpage;
3606 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003607 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003608 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003609 unused = oldpage;
3610 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003611 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07003612 __mem_cgroup_uncharge_common(unused,
3613 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
3614 : MEM_CGROUP_CHARGE_TYPE_CACHE,
3615 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07003616 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003617 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003618 * We disallowed uncharge of pages under migration because mapcount
3619 * of the page goes down to zero, temporarly.
3620 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003621 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003622 pc = lookup_page_cgroup(oldpage);
3623 lock_page_cgroup(pc);
3624 ClearPageCgroupMigration(pc);
3625 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003626
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003627 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003628 * If a page is a file cache, radix-tree replacement is very atomic
3629 * and we can skip this check. When it was an Anon page, its mapcount
3630 * goes down to 0. But because we added MIGRATION flage, it's not
3631 * uncharged yet. There are several case but page->mapcount check
3632 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3633 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003634 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003635 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003636 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003637}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003638
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003639/*
3640 * At replace page cache, newpage is not under any memcg but it's on
3641 * LRU. So, this function doesn't touch res_counter but handles LRU
3642 * in correct way. Both pages are locked so we cannot race with uncharge.
3643 */
3644void mem_cgroup_replace_page_cache(struct page *oldpage,
3645 struct page *newpage)
3646{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003647 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003648 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003649 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003650
3651 if (mem_cgroup_disabled())
3652 return;
3653
3654 pc = lookup_page_cgroup(oldpage);
3655 /* fix accounting on old pages */
3656 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003657 if (PageCgroupUsed(pc)) {
3658 memcg = pc->mem_cgroup;
3659 mem_cgroup_charge_statistics(memcg, false, -1);
3660 ClearPageCgroupUsed(pc);
3661 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003662 unlock_page_cgroup(pc);
3663
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003664 /*
3665 * When called from shmem_replace_page(), in some cases the
3666 * oldpage has already been charged, and in some cases not.
3667 */
3668 if (!memcg)
3669 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003670 /*
3671 * Even if newpage->mapping was NULL before starting replacement,
3672 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3673 * LRU while we overwrite pc->mem_cgroup.
3674 */
Johannes Weinerce587e62012-04-24 20:22:33 +02003675 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003676}
3677
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003678#ifdef CONFIG_DEBUG_VM
3679static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3680{
3681 struct page_cgroup *pc;
3682
3683 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003684 /*
3685 * Can be NULL while feeding pages into the page allocator for
3686 * the first time, i.e. during boot or memory hotplug;
3687 * or when mem_cgroup_disabled().
3688 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003689 if (likely(pc) && PageCgroupUsed(pc))
3690 return pc;
3691 return NULL;
3692}
3693
3694bool mem_cgroup_bad_page_check(struct page *page)
3695{
3696 if (mem_cgroup_disabled())
3697 return false;
3698
3699 return lookup_page_cgroup_used(page) != NULL;
3700}
3701
3702void mem_cgroup_print_bad_page(struct page *page)
3703{
3704 struct page_cgroup *pc;
3705
3706 pc = lookup_page_cgroup_used(page);
3707 if (pc) {
Hugh Dickins90b3fea2012-01-12 17:19:54 -08003708 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003709 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003710 }
3711}
3712#endif
3713
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003714static DEFINE_MUTEX(set_limit_mutex);
3715
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003716static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003717 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003718{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003719 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003720 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003721 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003722 int children = mem_cgroup_count_children(memcg);
3723 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003724 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003725
3726 /*
3727 * For keeping hierarchical_reclaim simple, how long we should retry
3728 * is depends on callers. We set our retry-count to be function
3729 * of # of children which we should visit in this loop.
3730 */
3731 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3732
3733 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003734
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003735 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003736 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003737 if (signal_pending(current)) {
3738 ret = -EINTR;
3739 break;
3740 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003741 /*
3742 * Rather than hide all in some function, I do this in
3743 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003744 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003745 */
3746 mutex_lock(&set_limit_mutex);
3747 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3748 if (memswlimit < val) {
3749 ret = -EINVAL;
3750 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003751 break;
3752 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003753
3754 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3755 if (memlimit < val)
3756 enlarge = 1;
3757
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003758 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003759 if (!ret) {
3760 if (memswlimit == val)
3761 memcg->memsw_is_minimum = true;
3762 else
3763 memcg->memsw_is_minimum = false;
3764 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003765 mutex_unlock(&set_limit_mutex);
3766
3767 if (!ret)
3768 break;
3769
Johannes Weiner56600482012-01-12 17:17:59 -08003770 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3771 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003772 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3773 /* Usage is reduced ? */
3774 if (curusage >= oldusage)
3775 retry_count--;
3776 else
3777 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003778 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003779 if (!ret && enlarge)
3780 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003781
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003782 return ret;
3783}
3784
Li Zefan338c8432009-06-17 16:27:15 -07003785static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3786 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003787{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003788 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003789 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003790 int children = mem_cgroup_count_children(memcg);
3791 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003792 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003793
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003794 /* see mem_cgroup_resize_res_limit */
3795 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3796 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003797 while (retry_count) {
3798 if (signal_pending(current)) {
3799 ret = -EINTR;
3800 break;
3801 }
3802 /*
3803 * Rather than hide all in some function, I do this in
3804 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003805 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003806 */
3807 mutex_lock(&set_limit_mutex);
3808 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3809 if (memlimit > val) {
3810 ret = -EINVAL;
3811 mutex_unlock(&set_limit_mutex);
3812 break;
3813 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003814 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3815 if (memswlimit < val)
3816 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003817 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003818 if (!ret) {
3819 if (memlimit == val)
3820 memcg->memsw_is_minimum = true;
3821 else
3822 memcg->memsw_is_minimum = false;
3823 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003824 mutex_unlock(&set_limit_mutex);
3825
3826 if (!ret)
3827 break;
3828
Johannes Weiner56600482012-01-12 17:17:59 -08003829 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3830 MEM_CGROUP_RECLAIM_NOSWAP |
3831 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003832 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003833 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003834 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003835 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003836 else
3837 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003838 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003839 if (!ret && enlarge)
3840 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003841 return ret;
3842}
3843
Balbir Singh4e416952009-09-23 15:56:39 -07003844unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003845 gfp_t gfp_mask,
3846 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003847{
3848 unsigned long nr_reclaimed = 0;
3849 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3850 unsigned long reclaimed;
3851 int loop = 0;
3852 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003853 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003854 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003855
3856 if (order > 0)
3857 return 0;
3858
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003859 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003860 /*
3861 * This loop can run a while, specially if mem_cgroup's continuously
3862 * keep exceeding their soft limit and putting the system under
3863 * pressure
3864 */
3865 do {
3866 if (next_mz)
3867 mz = next_mz;
3868 else
3869 mz = mem_cgroup_largest_soft_limit_node(mctz);
3870 if (!mz)
3871 break;
3872
Ying Han0ae5e892011-05-26 16:25:25 -07003873 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003874 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08003875 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003876 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003877 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003878 spin_lock(&mctz->lock);
3879
3880 /*
3881 * If we failed to reclaim anything from this memory cgroup
3882 * it is time to move on to the next cgroup
3883 */
3884 next_mz = NULL;
3885 if (!reclaimed) {
3886 do {
3887 /*
3888 * Loop until we find yet another one.
3889 *
3890 * By the time we get the soft_limit lock
3891 * again, someone might have aded the
3892 * group back on the RB tree. Iterate to
3893 * make sure we get a different mem.
3894 * mem_cgroup_largest_soft_limit_node returns
3895 * NULL if no other cgroup is present on
3896 * the tree
3897 */
3898 next_mz =
3899 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003900 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003901 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003902 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003903 break;
3904 } while (1);
3905 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003906 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
3907 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003908 /*
3909 * One school of thought says that we should not add
3910 * back the node to the tree if reclaim returns 0.
3911 * But our reclaim could return 0, simply because due
3912 * to priority we are exposing a smaller subset of
3913 * memory to reclaim from. Consider this as a longer
3914 * term TODO.
3915 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003916 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003917 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003918 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003919 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003920 loop++;
3921 /*
3922 * Could not reclaim anything and there are no more
3923 * mem cgroups to try or we seem to be looping without
3924 * reclaiming anything.
3925 */
3926 if (!nr_reclaimed &&
3927 (next_mz == NULL ||
3928 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3929 break;
3930 } while (!nr_reclaimed);
3931 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003932 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003933 return nr_reclaimed;
3934}
3935
Michal Hocko2ef37d32012-10-26 13:37:30 +02003936/**
3937 * mem_cgroup_force_empty_list - clears LRU of a group
3938 * @memcg: group to clear
3939 * @node: NUMA node
3940 * @zid: zone id
3941 * @lru: lru to to clear
3942 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003943 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02003944 * reclaim the pages page themselves - pages are moved to the parent (or root)
3945 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003946 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02003947static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003948 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003949{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003950 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003951 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003952 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003953 struct page *busy;
3954 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003955
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003956 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003957 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3958 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003959
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003960 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003961 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08003962 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003963 struct page *page;
3964
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003965 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003966 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003967 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003968 break;
3969 }
Johannes Weiner925b7672012-01-12 17:18:15 -08003970 page = list_entry(list->prev, struct page, lru);
3971 if (busy == page) {
3972 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003973 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003974 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003975 continue;
3976 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003977 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003978
Johannes Weiner925b7672012-01-12 17:18:15 -08003979 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07003980
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003981 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003982 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08003983 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003984 cond_resched();
3985 } else
3986 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003987 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003988}
3989
3990/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003991 * make mem_cgroup's charge to be 0 if there is no task by moving
3992 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003993 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02003994 *
3995 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003996 */
Michal Hockoab5196c2012-10-26 13:37:32 +02003997static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003998{
Michal Hockoc26251f2012-10-26 13:37:28 +02003999 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08004000
Daisuke Nishimurafce66472010-01-15 17:01:30 -08004001 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004002 /* This is for making all *used* pages to be on LRU. */
4003 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004004 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004005 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004006 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004007 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004008 enum lru_list lru;
4009 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02004010 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07004011 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004012 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004013 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004014 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004015 mem_cgroup_end_move(memcg);
4016 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07004017 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004018
Michal Hocko2ef37d32012-10-26 13:37:30 +02004019 /*
4020 * This is a safety check because mem_cgroup_force_empty_list
4021 * could have raced with mem_cgroup_replace_page_cache callers
4022 * so the lru seemed empty but the page could have been added
4023 * right after the check. RES_USAGE should be safe as we always
4024 * charge before adding to the LRU.
4025 */
4026 } while (res_counter_read_u64(&memcg->res, RES_USAGE) > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02004027}
4028
4029/*
4030 * Reclaims as many pages from the given memcg as possible and moves
4031 * the rest to the parent.
4032 *
4033 * Caller is responsible for holding css reference for memcg.
4034 */
4035static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
4036{
4037 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
4038 struct cgroup *cgrp = memcg->css.cgroup;
4039
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004040 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02004041 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
4042 return -EBUSY;
4043
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004044 /* we call try-to-free pages for make this cgroup empty */
4045 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004046 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07004047 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004048 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004049
Michal Hockoc26251f2012-10-26 13:37:28 +02004050 if (signal_pending(current))
4051 return -EINTR;
4052
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004053 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07004054 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004055 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004056 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004057 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02004058 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004059 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08004060
4061 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004062 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02004063 mem_cgroup_reparent_charges(memcg);
4064
4065 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08004066}
4067
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07004068static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004069{
Michal Hockoc26251f2012-10-26 13:37:28 +02004070 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4071 int ret;
4072
Michal Hockod8423012012-10-26 13:37:29 +02004073 if (mem_cgroup_is_root(memcg))
4074 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02004075 css_get(&memcg->css);
4076 ret = mem_cgroup_force_empty(memcg);
4077 css_put(&memcg->css);
4078
4079 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004080}
4081
4082
Balbir Singh18f59ea2009-01-07 18:08:07 -08004083static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
4084{
4085 return mem_cgroup_from_cont(cont)->use_hierarchy;
4086}
4087
4088static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
4089 u64 val)
4090{
4091 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004092 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004093 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004094 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004095
4096 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004097 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004098
4099 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07004100
4101 if (memcg->use_hierarchy == val)
4102 goto out;
4103
Balbir Singh18f59ea2009-01-07 18:08:07 -08004104 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02004105 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08004106 * in the child subtrees. If it is unset, then the change can
4107 * occur, provided the current cgroup has no children.
4108 *
4109 * For the root cgroup, parent_mem is NULL, we allow value to be
4110 * set if there are no children.
4111 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004112 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08004113 (val == 1 || val == 0)) {
4114 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004115 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004116 else
4117 retval = -EBUSY;
4118 } else
4119 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07004120
4121out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08004122 cgroup_unlock();
4123
4124 return retval;
4125}
4126
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004127
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004128static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004129 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004130{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004131 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004132 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004133
Johannes Weiner7a159cc2011-03-23 16:42:38 -07004134 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004135 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004136 val += mem_cgroup_read_stat(iter, idx);
4137
4138 if (val < 0) /* race ? */
4139 val = 0;
4140 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07004141}
4142
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004143static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004144{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004145 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004146
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004147 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004148 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00004149 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004150 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00004151 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004152 }
4153
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004154 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
4155 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004156
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004157 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004158 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004159
4160 return val << PAGE_SHIFT;
4161}
4162
Tejun Heoaf36f902012-04-01 12:09:55 -07004163static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
4164 struct file *file, char __user *buf,
4165 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004166{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004167 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07004168 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004169 u64 val;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004170 int name, len;
4171 enum res_type type;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004172
4173 type = MEMFILE_TYPE(cft->private);
4174 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004175
4176 if (!do_swap_account && type == _MEMSWAP)
4177 return -EOPNOTSUPP;
4178
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004179 switch (type) {
4180 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004181 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004182 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004183 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004184 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004185 break;
4186 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004187 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004188 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08004189 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004190 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004191 break;
Glauber Costa510fc4e2012-12-18 14:21:47 -08004192 case _KMEM:
4193 val = res_counter_read_u64(&memcg->kmem, name);
4194 break;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004195 default:
4196 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004197 }
Tejun Heoaf36f902012-04-01 12:09:55 -07004198
4199 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
4200 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004201}
Glauber Costa510fc4e2012-12-18 14:21:47 -08004202
4203static int memcg_update_kmem_limit(struct cgroup *cont, u64 val)
4204{
4205 int ret = -EINVAL;
4206#ifdef CONFIG_MEMCG_KMEM
4207 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
4208 /*
4209 * For simplicity, we won't allow this to be disabled. It also can't
4210 * be changed if the cgroup has children already, or if tasks had
4211 * already joined.
4212 *
4213 * If tasks join before we set the limit, a person looking at
4214 * kmem.usage_in_bytes will have no way to determine when it took
4215 * place, which makes the value quite meaningless.
4216 *
4217 * After it first became limited, changes in the value of the limit are
4218 * of course permitted.
4219 *
4220 * Taking the cgroup_lock is really offensive, but it is so far the only
4221 * way to guarantee that no children will appear. There are plenty of
4222 * other offenders, and they should all go away. Fine grained locking
4223 * is probably the way to go here. When we are fully hierarchical, we
4224 * can also get rid of the use_hierarchy check.
4225 */
4226 cgroup_lock();
4227 mutex_lock(&set_limit_mutex);
4228 if (!memcg->kmem_account_flags && val != RESOURCE_MAX) {
4229 if (cgroup_task_count(cont) || (memcg->use_hierarchy &&
4230 !list_empty(&cont->children))) {
4231 ret = -EBUSY;
4232 goto out;
4233 }
4234 ret = res_counter_set_limit(&memcg->kmem, val);
4235 VM_BUG_ON(ret);
4236
4237 memcg_kmem_set_active(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08004238 /*
4239 * kmem charges can outlive the cgroup. In the case of slab
4240 * pages, for instance, a page contain objects from various
4241 * processes, so it is unfeasible to migrate them away. We
4242 * need to reference count the memcg because of that.
4243 */
4244 mem_cgroup_get(memcg);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004245 } else
4246 ret = res_counter_set_limit(&memcg->kmem, val);
4247out:
4248 mutex_unlock(&set_limit_mutex);
4249 cgroup_unlock();
4250#endif
4251 return ret;
4252}
4253
4254static void memcg_propagate_kmem(struct mem_cgroup *memcg)
4255{
4256 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4257 if (!parent)
4258 return;
4259 memcg->kmem_account_flags = parent->kmem_account_flags;
Glauber Costa7de37682012-12-18 14:22:07 -08004260#ifdef CONFIG_MEMCG_KMEM
4261 if (memcg_kmem_is_active(memcg))
4262 mem_cgroup_get(memcg);
4263#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08004264}
4265
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004266/*
4267 * The user of this function is...
4268 * RES_LIMIT.
4269 */
Paul Menage856c13a2008-07-25 01:47:04 -07004270static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
4271 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004272{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004273 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004274 enum res_type type;
4275 int name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004276 unsigned long long val;
4277 int ret;
4278
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004279 type = MEMFILE_TYPE(cft->private);
4280 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07004281
4282 if (!do_swap_account && type == _MEMSWAP)
4283 return -EOPNOTSUPP;
4284
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004285 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004286 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004287 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4288 ret = -EINVAL;
4289 break;
4290 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004291 /* This function does all necessary parse...reuse it */
4292 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004293 if (ret)
4294 break;
4295 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004296 ret = mem_cgroup_resize_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004297 else if (type == _MEMSWAP)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004298 ret = mem_cgroup_resize_memsw_limit(memcg, val);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004299 else if (type == _KMEM)
4300 ret = memcg_update_kmem_limit(cont, val);
4301 else
4302 return -EINVAL;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004303 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004304 case RES_SOFT_LIMIT:
4305 ret = res_counter_memparse_write_strategy(buffer, &val);
4306 if (ret)
4307 break;
4308 /*
4309 * For memsw, soft limits are hard to implement in terms
4310 * of semantics, for now, we support soft limits for
4311 * control without swap
4312 */
4313 if (type == _MEM)
4314 ret = res_counter_set_soft_limit(&memcg->res, val);
4315 else
4316 ret = -EINVAL;
4317 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004318 default:
4319 ret = -EINVAL; /* should be BUG() ? */
4320 break;
4321 }
4322 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004323}
4324
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004325static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4326 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4327{
4328 struct cgroup *cgroup;
4329 unsigned long long min_limit, min_memsw_limit, tmp;
4330
4331 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4332 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4333 cgroup = memcg->css.cgroup;
4334 if (!memcg->use_hierarchy)
4335 goto out;
4336
4337 while (cgroup->parent) {
4338 cgroup = cgroup->parent;
4339 memcg = mem_cgroup_from_cont(cgroup);
4340 if (!memcg->use_hierarchy)
4341 break;
4342 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4343 min_limit = min(min_limit, tmp);
4344 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4345 min_memsw_limit = min(min_memsw_limit, tmp);
4346 }
4347out:
4348 *mem_limit = min_limit;
4349 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004350}
4351
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004352static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004353{
Tejun Heoaf36f902012-04-01 12:09:55 -07004354 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Glauber Costa86ae53e2012-12-18 14:21:45 -08004355 int name;
4356 enum res_type type;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004357
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004358 type = MEMFILE_TYPE(event);
4359 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07004360
4361 if (!do_swap_account && type == _MEMSWAP)
4362 return -EOPNOTSUPP;
4363
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004364 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004365 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004366 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004367 res_counter_reset_max(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004368 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004369 res_counter_reset_max(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004370 else if (type == _KMEM)
4371 res_counter_reset_max(&memcg->kmem);
4372 else
4373 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004374 break;
4375 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004376 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004377 res_counter_reset_failcnt(&memcg->res);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004378 else if (type == _MEMSWAP)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004379 res_counter_reset_failcnt(&memcg->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08004380 else if (type == _KMEM)
4381 res_counter_reset_failcnt(&memcg->kmem);
4382 else
4383 return -EINVAL;
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004384 break;
4385 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004386
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004387 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004388}
4389
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004390static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4391 struct cftype *cft)
4392{
4393 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4394}
4395
Daisuke Nishimura02491442010-03-10 15:22:17 -08004396#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004397static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4398 struct cftype *cft, u64 val)
4399{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004400 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004401
4402 if (val >= (1 << NR_MOVE_TYPE))
4403 return -EINVAL;
4404 /*
4405 * We check this value several times in both in can_attach() and
4406 * attach(), so we need cgroup lock to prevent this value from being
4407 * inconsistent.
4408 */
4409 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004410 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004411 cgroup_unlock();
4412
4413 return 0;
4414}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004415#else
4416static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4417 struct cftype *cft, u64 val)
4418{
4419 return -ENOSYS;
4420}
4421#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004422
Ying Han406eb0c2011-05-26 16:25:37 -07004423#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07004424static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07004425 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07004426{
4427 int nid;
4428 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4429 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004430 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07004431
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004432 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004433 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004434 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004435 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004436 seq_printf(m, " N%d=%lu", nid, node_nr);
4437 }
4438 seq_putc(m, '\n');
4439
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004440 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004441 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004442 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004443 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004444 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004445 seq_printf(m, " N%d=%lu", nid, node_nr);
4446 }
4447 seq_putc(m, '\n');
4448
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004449 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004450 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004451 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004452 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004453 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004454 seq_printf(m, " N%d=%lu", nid, node_nr);
4455 }
4456 seq_putc(m, '\n');
4457
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004458 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004459 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004460 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004461 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004462 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004463 seq_printf(m, " N%d=%lu", nid, node_nr);
4464 }
4465 seq_putc(m, '\n');
4466 return 0;
4467}
4468#endif /* CONFIG_NUMA */
4469
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004470static const char * const mem_cgroup_lru_names[] = {
4471 "inactive_anon",
4472 "active_anon",
4473 "inactive_file",
4474 "active_file",
4475 "unevictable",
4476};
4477
4478static inline void mem_cgroup_lru_names_not_uptodate(void)
4479{
4480 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4481}
4482
Wanpeng Liab215882012-07-31 16:43:09 -07004483static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004484 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004485{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004486 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004487 struct mem_cgroup *mi;
4488 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004489
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004490 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004491 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004492 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004493 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4494 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004495 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004496
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004497 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4498 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4499 mem_cgroup_read_events(memcg, i));
4500
4501 for (i = 0; i < NR_LRU_LISTS; i++)
4502 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4503 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4504
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004505 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004506 {
4507 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004508 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004509 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004510 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004511 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4512 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004513 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004514
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004515 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4516 long long val = 0;
4517
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004518 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004519 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004520 for_each_mem_cgroup_tree(mi, memcg)
4521 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4522 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4523 }
4524
4525 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4526 unsigned long long val = 0;
4527
4528 for_each_mem_cgroup_tree(mi, memcg)
4529 val += mem_cgroup_read_events(mi, i);
4530 seq_printf(m, "total_%s %llu\n",
4531 mem_cgroup_events_names[i], val);
4532 }
4533
4534 for (i = 0; i < NR_LRU_LISTS; i++) {
4535 unsigned long long val = 0;
4536
4537 for_each_mem_cgroup_tree(mi, memcg)
4538 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4539 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004540 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004541
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004542#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004543 {
4544 int nid, zid;
4545 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004546 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004547 unsigned long recent_rotated[2] = {0, 0};
4548 unsigned long recent_scanned[2] = {0, 0};
4549
4550 for_each_online_node(nid)
4551 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004552 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07004553 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004554
Hugh Dickins89abfab2012-05-29 15:06:53 -07004555 recent_rotated[0] += rstat->recent_rotated[0];
4556 recent_rotated[1] += rstat->recent_rotated[1];
4557 recent_scanned[0] += rstat->recent_scanned[0];
4558 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004559 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004560 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4561 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4562 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4563 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004564 }
4565#endif
4566
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004567 return 0;
4568}
4569
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004570static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4571{
4572 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4573
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004574 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004575}
4576
4577static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4578 u64 val)
4579{
4580 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4581 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004582
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004583 if (val > 100)
4584 return -EINVAL;
4585
4586 if (cgrp->parent == NULL)
4587 return -EINVAL;
4588
4589 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004590
4591 cgroup_lock();
4592
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004593 /* If under hierarchy, only empty-root can set this value */
4594 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004595 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4596 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004597 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004598 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004599
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004600 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004601
Li Zefan068b38c2009-01-15 13:51:26 -08004602 cgroup_unlock();
4603
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004604 return 0;
4605}
4606
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004607static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4608{
4609 struct mem_cgroup_threshold_ary *t;
4610 u64 usage;
4611 int i;
4612
4613 rcu_read_lock();
4614 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004615 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004616 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004617 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004618
4619 if (!t)
4620 goto unlock;
4621
4622 usage = mem_cgroup_usage(memcg, swap);
4623
4624 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004625 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004626 * If it's not true, a threshold was crossed after last
4627 * call of __mem_cgroup_threshold().
4628 */
Phil Carmody5407a562010-05-26 14:42:42 -07004629 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004630
4631 /*
4632 * Iterate backward over array of thresholds starting from
4633 * current_threshold and check if a threshold is crossed.
4634 * If none of thresholds below usage is crossed, we read
4635 * only one element of the array here.
4636 */
4637 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4638 eventfd_signal(t->entries[i].eventfd, 1);
4639
4640 /* i = current_threshold + 1 */
4641 i++;
4642
4643 /*
4644 * Iterate forward over array of thresholds starting from
4645 * current_threshold+1 and check if a threshold is crossed.
4646 * If none of thresholds above usage is crossed, we read
4647 * only one element of the array here.
4648 */
4649 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4650 eventfd_signal(t->entries[i].eventfd, 1);
4651
4652 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004653 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004654unlock:
4655 rcu_read_unlock();
4656}
4657
4658static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4659{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004660 while (memcg) {
4661 __mem_cgroup_threshold(memcg, false);
4662 if (do_swap_account)
4663 __mem_cgroup_threshold(memcg, true);
4664
4665 memcg = parent_mem_cgroup(memcg);
4666 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004667}
4668
4669static int compare_thresholds(const void *a, const void *b)
4670{
4671 const struct mem_cgroup_threshold *_a = a;
4672 const struct mem_cgroup_threshold *_b = b;
4673
4674 return _a->threshold - _b->threshold;
4675}
4676
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004677static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004678{
4679 struct mem_cgroup_eventfd_list *ev;
4680
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004681 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004682 eventfd_signal(ev->eventfd, 1);
4683 return 0;
4684}
4685
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004686static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004687{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004688 struct mem_cgroup *iter;
4689
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004690 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004691 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004692}
4693
4694static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4695 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004696{
4697 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004698 struct mem_cgroup_thresholds *thresholds;
4699 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004700 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004701 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004702 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004703
4704 ret = res_counter_memparse_write_strategy(args, &threshold);
4705 if (ret)
4706 return ret;
4707
4708 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004709
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004710 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004711 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004712 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004713 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004714 else
4715 BUG();
4716
4717 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4718
4719 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004720 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004721 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4722
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004723 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004724
4725 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004726 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004727 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004728 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004729 ret = -ENOMEM;
4730 goto unlock;
4731 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004732 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004733
4734 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004735 if (thresholds->primary) {
4736 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004737 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004738 }
4739
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004740 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004741 new->entries[size - 1].eventfd = eventfd;
4742 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004743
4744 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004745 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004746 compare_thresholds, NULL);
4747
4748 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004749 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004750 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004751 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004752 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004753 * new->current_threshold will not be used until
4754 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004755 * it here.
4756 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004757 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004758 } else
4759 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004760 }
4761
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004762 /* Free old spare buffer and save old primary buffer as spare */
4763 kfree(thresholds->spare);
4764 thresholds->spare = thresholds->primary;
4765
4766 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004767
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004768 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004769 synchronize_rcu();
4770
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004771unlock:
4772 mutex_unlock(&memcg->thresholds_lock);
4773
4774 return ret;
4775}
4776
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004777static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004778 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004779{
4780 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004781 struct mem_cgroup_thresholds *thresholds;
4782 struct mem_cgroup_threshold_ary *new;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004783 enum res_type type = MEMFILE_TYPE(cft->private);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004784 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004785 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004786
4787 mutex_lock(&memcg->thresholds_lock);
4788 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004789 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004790 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004791 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004792 else
4793 BUG();
4794
Anton Vorontsov371528c2012-02-24 05:14:46 +04004795 if (!thresholds->primary)
4796 goto unlock;
4797
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004798 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4799
4800 /* Check if a threshold crossed before removing */
4801 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4802
4803 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004804 size = 0;
4805 for (i = 0; i < thresholds->primary->size; i++) {
4806 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004807 size++;
4808 }
4809
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004810 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004811
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004812 /* Set thresholds array to NULL if we don't have thresholds */
4813 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004814 kfree(new);
4815 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004816 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004817 }
4818
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004819 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004820
4821 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004822 new->current_threshold = -1;
4823 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4824 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004825 continue;
4826
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004827 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004828 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004829 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004830 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004831 * until rcu_assign_pointer(), so it's safe to increment
4832 * it here.
4833 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004834 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004835 }
4836 j++;
4837 }
4838
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004839swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004840 /* Swap primary and spare array */
4841 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004842 /* If all events are unregistered, free the spare array */
4843 if (!new) {
4844 kfree(thresholds->spare);
4845 thresholds->spare = NULL;
4846 }
4847
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004848 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004849
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004850 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004851 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004852unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004853 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004854}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004855
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004856static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4857 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4858{
4859 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4860 struct mem_cgroup_eventfd_list *event;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004861 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004862
4863 BUG_ON(type != _OOM_TYPE);
4864 event = kmalloc(sizeof(*event), GFP_KERNEL);
4865 if (!event)
4866 return -ENOMEM;
4867
Michal Hocko1af8efe2011-07-26 16:08:24 -07004868 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004869
4870 event->eventfd = eventfd;
4871 list_add(&event->list, &memcg->oom_notify);
4872
4873 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004874 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004875 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004876 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004877
4878 return 0;
4879}
4880
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004881static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004882 struct cftype *cft, struct eventfd_ctx *eventfd)
4883{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004884 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004885 struct mem_cgroup_eventfd_list *ev, *tmp;
Glauber Costa86ae53e2012-12-18 14:21:45 -08004886 enum res_type type = MEMFILE_TYPE(cft->private);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004887
4888 BUG_ON(type != _OOM_TYPE);
4889
Michal Hocko1af8efe2011-07-26 16:08:24 -07004890 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004891
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004892 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004893 if (ev->eventfd == eventfd) {
4894 list_del(&ev->list);
4895 kfree(ev);
4896 }
4897 }
4898
Michal Hocko1af8efe2011-07-26 16:08:24 -07004899 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004900}
4901
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004902static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4903 struct cftype *cft, struct cgroup_map_cb *cb)
4904{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004905 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004906
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004907 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004908
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004909 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004910 cb->fill(cb, "under_oom", 1);
4911 else
4912 cb->fill(cb, "under_oom", 0);
4913 return 0;
4914}
4915
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004916static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4917 struct cftype *cft, u64 val)
4918{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004919 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004920 struct mem_cgroup *parent;
4921
4922 /* cannot set to root cgroup and only 0 and 1 are allowed */
4923 if (!cgrp->parent || !((val == 0) || (val == 1)))
4924 return -EINVAL;
4925
4926 parent = mem_cgroup_from_cont(cgrp->parent);
4927
4928 cgroup_lock();
4929 /* oom-kill-disable is a flag for subhierarchy. */
4930 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004931 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004932 cgroup_unlock();
4933 return -EINVAL;
4934 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004935 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004936 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004937 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004938 cgroup_unlock();
4939 return 0;
4940}
4941
Andrew Mortonc255a452012-07-31 16:43:02 -07004942#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03004943static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004944{
Glauber Costa510fc4e2012-12-18 14:21:47 -08004945 memcg_propagate_kmem(memcg);
Glauber Costa1d62e432012-04-09 19:36:33 -03004946 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00004947};
4948
Glauber Costa1d62e432012-04-09 19:36:33 -03004949static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004950{
Glauber Costa1d62e432012-04-09 19:36:33 -03004951 mem_cgroup_sockets_destroy(memcg);
Glauber Costa7de37682012-12-18 14:22:07 -08004952
4953 memcg_kmem_mark_dead(memcg);
4954
4955 if (res_counter_read_u64(&memcg->kmem, RES_USAGE) != 0)
4956 return;
4957
4958 /*
4959 * Charges already down to 0, undo mem_cgroup_get() done in the charge
4960 * path here, being careful not to race with memcg_uncharge_kmem: it is
4961 * possible that the charges went down to 0 between mark_dead and the
4962 * res_counter read, so in that case, we don't need the put
4963 */
4964 if (memcg_kmem_test_and_clear_dead(memcg))
4965 mem_cgroup_put(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004966}
Glauber Costae5671df2011-12-11 21:47:01 +00004967#else
Glauber Costacbe128e32012-04-09 19:36:34 -03004968static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004969{
4970 return 0;
4971}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004972
Glauber Costa1d62e432012-04-09 19:36:33 -03004973static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004974{
4975}
Glauber Costae5671df2011-12-11 21:47:01 +00004976#endif
4977
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004978static struct cftype mem_cgroup_files[] = {
4979 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004980 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004981 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07004982 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004983 .register_event = mem_cgroup_usage_register_event,
4984 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004985 },
4986 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004987 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004988 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004989 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004990 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004991 },
4992 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004993 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004994 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004995 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004996 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004997 },
4998 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004999 .name = "soft_limit_in_bytes",
5000 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
5001 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005002 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07005003 },
5004 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005005 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005006 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07005007 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005008 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005009 },
Balbir Singh8697d332008-02-07 00:13:59 -08005010 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005011 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005012 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08005013 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08005014 {
5015 .name = "force_empty",
5016 .trigger = mem_cgroup_force_empty_write,
5017 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08005018 {
5019 .name = "use_hierarchy",
5020 .write_u64 = mem_cgroup_hierarchy_write,
5021 .read_u64 = mem_cgroup_hierarchy_read,
5022 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005023 {
5024 .name = "swappiness",
5025 .read_u64 = mem_cgroup_swappiness_read,
5026 .write_u64 = mem_cgroup_swappiness_write,
5027 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005028 {
5029 .name = "move_charge_at_immigrate",
5030 .read_u64 = mem_cgroup_move_charge_read,
5031 .write_u64 = mem_cgroup_move_charge_write,
5032 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005033 {
5034 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07005035 .read_map = mem_cgroup_oom_control_read,
5036 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005037 .register_event = mem_cgroup_oom_register_event,
5038 .unregister_event = mem_cgroup_oom_unregister_event,
5039 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
5040 },
Ying Han406eb0c2011-05-26 16:25:37 -07005041#ifdef CONFIG_NUMA
5042 {
5043 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07005044 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07005045 },
5046#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07005047#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005048 {
5049 .name = "memsw.usage_in_bytes",
5050 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07005051 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07005052 .register_event = mem_cgroup_usage_register_event,
5053 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005054 },
5055 {
5056 .name = "memsw.max_usage_in_bytes",
5057 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
5058 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005059 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005060 },
5061 {
5062 .name = "memsw.limit_in_bytes",
5063 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
5064 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07005065 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005066 },
5067 {
5068 .name = "memsw.failcnt",
5069 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
5070 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07005071 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005072 },
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005073#endif
Glauber Costa510fc4e2012-12-18 14:21:47 -08005074#ifdef CONFIG_MEMCG_KMEM
5075 {
5076 .name = "kmem.limit_in_bytes",
5077 .private = MEMFILE_PRIVATE(_KMEM, RES_LIMIT),
5078 .write_string = mem_cgroup_write,
5079 .read = mem_cgroup_read,
5080 },
5081 {
5082 .name = "kmem.usage_in_bytes",
5083 .private = MEMFILE_PRIVATE(_KMEM, RES_USAGE),
5084 .read = mem_cgroup_read,
5085 },
5086 {
5087 .name = "kmem.failcnt",
5088 .private = MEMFILE_PRIVATE(_KMEM, RES_FAILCNT),
5089 .trigger = mem_cgroup_reset,
5090 .read = mem_cgroup_read,
5091 },
5092 {
5093 .name = "kmem.max_usage_in_bytes",
5094 .private = MEMFILE_PRIVATE(_KMEM, RES_MAX_USAGE),
5095 .trigger = mem_cgroup_reset,
5096 .read = mem_cgroup_read,
5097 },
5098#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07005099 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07005100};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005101
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005102static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005103{
5104 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005105 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005106 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005107 /*
5108 * This routine is called against possible nodes.
5109 * But it's BUG to call kmalloc() against offline node.
5110 *
5111 * TODO: this routine can waste much memory for nodes which will
5112 * never be onlined. It's better to use memory hotplug callback
5113 * function.
5114 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07005115 if (!node_state(node, N_NORMAL_MEMORY))
5116 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08005117 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005118 if (!pn)
5119 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005120
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005121 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5122 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08005123 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07005124 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07005125 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005126 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005127 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07005128 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005129 return 0;
5130}
5131
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005132static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005133{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005134 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08005135}
5136
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005137static struct mem_cgroup *mem_cgroup_alloc(void)
5138{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005139 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005140 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005141
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08005142 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08005143 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005144 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005145 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005146 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005147
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005148 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07005149 return NULL;
5150
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005151 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
5152 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005153 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005154 spin_lock_init(&memcg->pcp_counter_lock);
5155 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005156
5157out_free:
5158 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005159 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005160 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07005161 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08005162 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005163}
5164
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005165/*
Glauber Costa3afe36b2012-05-29 15:07:10 -07005166 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
Hugh Dickins59927fb2012-03-15 15:17:07 -07005167 * but in process context. The work_freeing structure is overlaid
5168 * on the rcu_freeing structure, which itself is overlaid on memsw.
5169 */
Glauber Costa3afe36b2012-05-29 15:07:10 -07005170static void free_work(struct work_struct *work)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005171{
5172 struct mem_cgroup *memcg;
Glauber Costa3afe36b2012-05-29 15:07:10 -07005173 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005174
5175 memcg = container_of(work, struct mem_cgroup, work_freeing);
Glauber Costa3f134612012-05-29 15:07:11 -07005176 /*
5177 * We need to make sure that (at least for now), the jump label
5178 * destruction code runs outside of the cgroup lock. This is because
5179 * get_online_cpus(), which is called from the static_branch update,
5180 * can't be called inside the cgroup_lock. cpusets are the ones
5181 * enforcing this dependency, so if they ever change, we might as well.
5182 *
5183 * schedule_work() will guarantee this happens. Be careful if you need
5184 * to move this code around, and make sure it is outside
5185 * the cgroup_lock.
5186 */
5187 disarm_sock_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005188 if (size < PAGE_SIZE)
5189 kfree(memcg);
5190 else
5191 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005192}
Glauber Costa3afe36b2012-05-29 15:07:10 -07005193
5194static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07005195{
5196 struct mem_cgroup *memcg;
5197
5198 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005199 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07005200 schedule_work(&memcg->work_freeing);
5201}
5202
5203/*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005204 * At destroying mem_cgroup, references from swap_cgroup can remain.
5205 * (scanning all at force_empty is too costly...)
5206 *
5207 * Instead of clearing all references at force_empty, we remember
5208 * the number of reference from swap_cgroup and free mem_cgroup when
5209 * it goes down to 0.
5210 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005211 * Removal of cgroup itself succeeds regardless of refs from swap.
5212 */
5213
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005214static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005215{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005216 int node;
5217
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005218 mem_cgroup_remove_from_trees(memcg);
5219 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005220
Bob Liu3ed28fa2012-01-12 17:19:04 -08005221 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005222 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08005223
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005224 free_percpu(memcg->stat);
Glauber Costa3afe36b2012-05-29 15:07:10 -07005225 call_rcu(&memcg->rcu_freeing, free_rcu);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005226}
5227
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005228static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005229{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005230 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005231}
5232
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005233static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005234{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005235 if (atomic_sub_and_test(count, &memcg->refcnt)) {
5236 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
5237 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005238 if (parent)
5239 mem_cgroup_put(parent);
5240 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08005241}
5242
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005243static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005244{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005245 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005246}
5247
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005248/*
5249 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
5250 */
Glauber Costae1aab162011-12-11 21:47:03 +00005251struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005252{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005253 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005254 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005255 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005256}
Glauber Costae1aab162011-12-11 21:47:03 +00005257EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07005258
Andrew Mortonc255a452012-07-31 16:43:02 -07005259#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005260static void __init enable_swap_cgroup(void)
5261{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08005262 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005263 do_swap_account = 1;
5264}
5265#else
5266static void __init enable_swap_cgroup(void)
5267{
5268}
5269#endif
5270
Balbir Singhf64c3f52009-09-23 15:56:37 -07005271static int mem_cgroup_soft_limit_tree_init(void)
5272{
5273 struct mem_cgroup_tree_per_node *rtpn;
5274 struct mem_cgroup_tree_per_zone *rtpz;
5275 int tmp, node, zone;
5276
Bob Liu3ed28fa2012-01-12 17:19:04 -08005277 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07005278 tmp = node;
5279 if (!node_state(node, N_NORMAL_MEMORY))
5280 tmp = -1;
5281 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
5282 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08005283 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005284
5285 soft_limit_tree.rb_tree_per_node[node] = rtpn;
5286
5287 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
5288 rtpz = &rtpn->rb_tree_per_zone[zone];
5289 rtpz->rb_root = RB_ROOT;
5290 spin_lock_init(&rtpz->lock);
5291 }
5292 }
5293 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08005294
5295err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08005296 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08005297 if (!soft_limit_tree.rb_tree_per_node[node])
5298 break;
5299 kfree(soft_limit_tree.rb_tree_per_node[node]);
5300 soft_limit_tree.rb_tree_per_node[node] = NULL;
5301 }
5302 return 1;
5303
Balbir Singhf64c3f52009-09-23 15:56:37 -07005304}
5305
Li Zefan0eb253e2009-01-15 13:51:25 -08005306static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08005307mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005308{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005309 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005310 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005311 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005312
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005313 memcg = mem_cgroup_alloc();
5314 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005315 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08005316
Bob Liu3ed28fa2012-01-12 17:19:04 -08005317 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005318 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005319 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005320
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005321 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005322 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005323 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005324 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005325 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07005326 if (mem_cgroup_soft_limit_tree_init())
5327 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08005328 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08005329 for_each_possible_cpu(cpu) {
5330 struct memcg_stock_pcp *stock =
5331 &per_cpu(memcg_stock, cpu);
5332 INIT_WORK(&stock->work, drain_local_stock);
5333 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07005334 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005335 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005336 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005337 memcg->use_hierarchy = parent->use_hierarchy;
5338 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005339 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005340
Balbir Singh18f59ea2009-01-07 18:08:07 -08005341 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005342 res_counter_init(&memcg->res, &parent->res);
5343 res_counter_init(&memcg->memsw, &parent->memsw);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005344 res_counter_init(&memcg->kmem, &parent->kmem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005345 /*
5346 * We increment refcnt of the parent to ensure that we can
5347 * safely access it on res_counter_charge/uncharge.
5348 * This refcnt will be decremented when freeing this
5349 * mem_cgroup(see mem_cgroup_put).
5350 */
5351 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005352 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005353 res_counter_init(&memcg->res, NULL);
5354 res_counter_init(&memcg->memsw, NULL);
Glauber Costa510fc4e2012-12-18 14:21:47 -08005355 res_counter_init(&memcg->kmem, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005356 /*
5357 * Deeper hierachy with use_hierarchy == false doesn't make
5358 * much sense so let cgroup subsystem know about this
5359 * unfortunate state in our controller.
5360 */
5361 if (parent && parent != root_mem_cgroup)
5362 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005363 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005364 memcg->last_scanned_node = MAX_NUMNODES;
5365 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005366
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005367 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005368 memcg->swappiness = mem_cgroup_swappiness(parent);
5369 atomic_set(&memcg->refcnt, 1);
5370 memcg->move_charge_at_immigrate = 0;
5371 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07005372 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03005373
5374 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
5375 if (error) {
5376 /*
5377 * We call put now because our (and parent's) refcnts
5378 * are already in place. mem_cgroup_put() will internally
5379 * call __mem_cgroup_free, so return directly
5380 */
5381 mem_cgroup_put(memcg);
5382 return ERR_PTR(error);
5383 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005384 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005385free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005386 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005387 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005388}
5389
Tejun Heo92fb9742012-11-19 08:13:38 -08005390static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005391{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005392 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005393
Michal Hockoab5196c2012-10-26 13:37:32 +02005394 mem_cgroup_reparent_charges(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005395}
5396
Tejun Heo92fb9742012-11-19 08:13:38 -08005397static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005398{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005399 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005400
Glauber Costa1d62e432012-04-09 19:36:33 -03005401 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005402
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005403 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005404}
5405
Daisuke Nishimura02491442010-03-10 15:22:17 -08005406#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005407/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005408#define PRECHARGE_COUNT_AT_ONCE 256
5409static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005410{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005411 int ret = 0;
5412 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005413 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005414
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005415 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005416 mc.precharge += count;
5417 /* we don't need css_get for root */
5418 return ret;
5419 }
5420 /* try to charge at once */
5421 if (count > 1) {
5422 struct res_counter *dummy;
5423 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005424 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005425 * by cgroup_lock_live_cgroup() that it is not removed and we
5426 * are still under the same cgroup_mutex. So we can postpone
5427 * css_get().
5428 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005429 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005430 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005431 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005432 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005433 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005434 goto one_by_one;
5435 }
5436 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005437 return ret;
5438 }
5439one_by_one:
5440 /* fall back to one by one charge */
5441 while (count--) {
5442 if (signal_pending(current)) {
5443 ret = -EINTR;
5444 break;
5445 }
5446 if (!batch_count--) {
5447 batch_count = PRECHARGE_COUNT_AT_ONCE;
5448 cond_resched();
5449 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005450 ret = __mem_cgroup_try_charge(NULL,
5451 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005452 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005453 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005454 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005455 mc.precharge++;
5456 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005457 return ret;
5458}
5459
5460/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005461 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005462 * @vma: the vma the pte to be checked belongs
5463 * @addr: the address corresponding to the pte to be checked
5464 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005465 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005466 *
5467 * Returns
5468 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5469 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5470 * move charge. if @target is not NULL, the page is stored in target->page
5471 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005472 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5473 * target for charge migration. if @target is not NULL, the entry is stored
5474 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005475 *
5476 * Called with pte lock held.
5477 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005478union mc_target {
5479 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005480 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005481};
5482
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005483enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005484 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005485 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005486 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005487};
5488
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005489static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5490 unsigned long addr, pte_t ptent)
5491{
5492 struct page *page = vm_normal_page(vma, addr, ptent);
5493
5494 if (!page || !page_mapped(page))
5495 return NULL;
5496 if (PageAnon(page)) {
5497 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005498 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005499 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005500 } else if (!move_file())
5501 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005502 return NULL;
5503 if (!get_page_unless_zero(page))
5504 return NULL;
5505
5506 return page;
5507}
5508
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005509#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005510static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5511 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5512{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005513 struct page *page = NULL;
5514 swp_entry_t ent = pte_to_swp_entry(ptent);
5515
5516 if (!move_anon() || non_swap_entry(ent))
5517 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005518 /*
5519 * Because lookup_swap_cache() updates some statistics counter,
5520 * we call find_get_page() with swapper_space directly.
5521 */
5522 page = find_get_page(&swapper_space, ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005523 if (do_swap_account)
5524 entry->val = ent.val;
5525
5526 return page;
5527}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005528#else
5529static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5530 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5531{
5532 return NULL;
5533}
5534#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005535
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005536static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5537 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5538{
5539 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005540 struct address_space *mapping;
5541 pgoff_t pgoff;
5542
5543 if (!vma->vm_file) /* anonymous vma */
5544 return NULL;
5545 if (!move_file())
5546 return NULL;
5547
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005548 mapping = vma->vm_file->f_mapping;
5549 if (pte_none(ptent))
5550 pgoff = linear_page_index(vma, addr);
5551 else /* pte_file(ptent) is true */
5552 pgoff = pte_to_pgoff(ptent);
5553
5554 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005555 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005556
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005557#ifdef CONFIG_SWAP
5558 /* shmem/tmpfs may report page out on swap: account for that too. */
5559 if (radix_tree_exceptional_entry(page)) {
5560 swp_entry_t swap = radix_to_swp_entry(page);
5561 if (do_swap_account)
5562 *entry = swap;
5563 page = find_get_page(&swapper_space, swap.val);
5564 }
5565#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005566 return page;
5567}
5568
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005569static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005570 unsigned long addr, pte_t ptent, union mc_target *target)
5571{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005572 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005573 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005574 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005575 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005576
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005577 if (pte_present(ptent))
5578 page = mc_handle_present_pte(vma, addr, ptent);
5579 else if (is_swap_pte(ptent))
5580 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005581 else if (pte_none(ptent) || pte_file(ptent))
5582 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005583
5584 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005585 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005586 if (page) {
5587 pc = lookup_page_cgroup(page);
5588 /*
5589 * Do only loose check w/o page_cgroup lock.
5590 * mem_cgroup_move_account() checks the pc is valid or not under
5591 * the lock.
5592 */
5593 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5594 ret = MC_TARGET_PAGE;
5595 if (target)
5596 target->page = page;
5597 }
5598 if (!ret || !target)
5599 put_page(page);
5600 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005601 /* There is a swap entry and a page doesn't exist or isn't charged */
5602 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08005603 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005604 ret = MC_TARGET_SWAP;
5605 if (target)
5606 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005607 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005608 return ret;
5609}
5610
Naoya Horiguchi12724852012-03-21 16:34:28 -07005611#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5612/*
5613 * We don't consider swapping or file mapped pages because THP does not
5614 * support them for now.
5615 * Caller should make sure that pmd_trans_huge(pmd) is true.
5616 */
5617static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5618 unsigned long addr, pmd_t pmd, union mc_target *target)
5619{
5620 struct page *page = NULL;
5621 struct page_cgroup *pc;
5622 enum mc_target_type ret = MC_TARGET_NONE;
5623
5624 page = pmd_page(pmd);
5625 VM_BUG_ON(!page || !PageHead(page));
5626 if (!move_anon())
5627 return ret;
5628 pc = lookup_page_cgroup(page);
5629 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5630 ret = MC_TARGET_PAGE;
5631 if (target) {
5632 get_page(page);
5633 target->page = page;
5634 }
5635 }
5636 return ret;
5637}
5638#else
5639static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5640 unsigned long addr, pmd_t pmd, union mc_target *target)
5641{
5642 return MC_TARGET_NONE;
5643}
5644#endif
5645
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005646static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5647 unsigned long addr, unsigned long end,
5648 struct mm_walk *walk)
5649{
5650 struct vm_area_struct *vma = walk->private;
5651 pte_t *pte;
5652 spinlock_t *ptl;
5653
Naoya Horiguchi12724852012-03-21 16:34:28 -07005654 if (pmd_trans_huge_lock(pmd, vma) == 1) {
5655 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5656 mc.precharge += HPAGE_PMD_NR;
5657 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005658 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005659 }
Dave Hansen03319322011-03-22 16:32:56 -07005660
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005661 if (pmd_trans_unstable(pmd))
5662 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005663 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5664 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005665 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005666 mc.precharge++; /* increment precharge temporarily */
5667 pte_unmap_unlock(pte - 1, ptl);
5668 cond_resched();
5669
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005670 return 0;
5671}
5672
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005673static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5674{
5675 unsigned long precharge;
5676 struct vm_area_struct *vma;
5677
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005678 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005679 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5680 struct mm_walk mem_cgroup_count_precharge_walk = {
5681 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5682 .mm = mm,
5683 .private = vma,
5684 };
5685 if (is_vm_hugetlb_page(vma))
5686 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005687 walk_page_range(vma->vm_start, vma->vm_end,
5688 &mem_cgroup_count_precharge_walk);
5689 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005690 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005691
5692 precharge = mc.precharge;
5693 mc.precharge = 0;
5694
5695 return precharge;
5696}
5697
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005698static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5699{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005700 unsigned long precharge = mem_cgroup_count_precharge(mm);
5701
5702 VM_BUG_ON(mc.moving_task);
5703 mc.moving_task = current;
5704 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005705}
5706
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005707/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5708static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005709{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005710 struct mem_cgroup *from = mc.from;
5711 struct mem_cgroup *to = mc.to;
5712
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005713 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005714 if (mc.precharge) {
5715 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5716 mc.precharge = 0;
5717 }
5718 /*
5719 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5720 * we must uncharge here.
5721 */
5722 if (mc.moved_charge) {
5723 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5724 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005725 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005726 /* we must fixup refcnts and charges */
5727 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005728 /* uncharge swap account from the old cgroup */
5729 if (!mem_cgroup_is_root(mc.from))
5730 res_counter_uncharge(&mc.from->memsw,
5731 PAGE_SIZE * mc.moved_swap);
5732 __mem_cgroup_put(mc.from, mc.moved_swap);
5733
5734 if (!mem_cgroup_is_root(mc.to)) {
5735 /*
5736 * we charged both to->res and to->memsw, so we should
5737 * uncharge to->res.
5738 */
5739 res_counter_uncharge(&mc.to->res,
5740 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005741 }
5742 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005743 mc.moved_swap = 0;
5744 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005745 memcg_oom_recover(from);
5746 memcg_oom_recover(to);
5747 wake_up_all(&mc.waitq);
5748}
5749
5750static void mem_cgroup_clear_mc(void)
5751{
5752 struct mem_cgroup *from = mc.from;
5753
5754 /*
5755 * we must clear moving_task before waking up waiters at the end of
5756 * task migration.
5757 */
5758 mc.moving_task = NULL;
5759 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005760 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005761 mc.from = NULL;
5762 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005763 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005764 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005765}
5766
Li Zefan761b3ef2012-01-31 13:47:36 +08005767static int mem_cgroup_can_attach(struct cgroup *cgroup,
5768 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005769{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005770 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005771 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005772 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005773
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005774 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005775 struct mm_struct *mm;
5776 struct mem_cgroup *from = mem_cgroup_from_task(p);
5777
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005778 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005779
5780 mm = get_task_mm(p);
5781 if (!mm)
5782 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005783 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005784 if (mm->owner == p) {
5785 VM_BUG_ON(mc.from);
5786 VM_BUG_ON(mc.to);
5787 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005788 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005789 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005790 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005791 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005792 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005793 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005794 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005795 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005796
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005797 ret = mem_cgroup_precharge_mc(mm);
5798 if (ret)
5799 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005800 }
5801 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005802 }
5803 return ret;
5804}
5805
Li Zefan761b3ef2012-01-31 13:47:36 +08005806static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5807 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005808{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005809 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005810}
5811
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005812static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5813 unsigned long addr, unsigned long end,
5814 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005815{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005816 int ret = 0;
5817 struct vm_area_struct *vma = walk->private;
5818 pte_t *pte;
5819 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005820 enum mc_target_type target_type;
5821 union mc_target target;
5822 struct page *page;
5823 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005824
Naoya Horiguchi12724852012-03-21 16:34:28 -07005825 /*
5826 * We don't take compound_lock() here but no race with splitting thp
5827 * happens because:
5828 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5829 * under splitting, which means there's no concurrent thp split,
5830 * - if another thread runs into split_huge_page() just after we
5831 * entered this if-block, the thread must wait for page table lock
5832 * to be unlocked in __split_huge_page_splitting(), where the main
5833 * part of thp split is not executed yet.
5834 */
5835 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005836 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005837 spin_unlock(&vma->vm_mm->page_table_lock);
5838 return 0;
5839 }
5840 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5841 if (target_type == MC_TARGET_PAGE) {
5842 page = target.page;
5843 if (!isolate_lru_page(page)) {
5844 pc = lookup_page_cgroup(page);
5845 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005846 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005847 mc.precharge -= HPAGE_PMD_NR;
5848 mc.moved_charge += HPAGE_PMD_NR;
5849 }
5850 putback_lru_page(page);
5851 }
5852 put_page(page);
5853 }
5854 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005855 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005856 }
5857
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005858 if (pmd_trans_unstable(pmd))
5859 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005860retry:
5861 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5862 for (; addr != end; addr += PAGE_SIZE) {
5863 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005864 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005865
5866 if (!mc.precharge)
5867 break;
5868
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005869 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005870 case MC_TARGET_PAGE:
5871 page = target.page;
5872 if (isolate_lru_page(page))
5873 goto put;
5874 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005875 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005876 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005877 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005878 /* we uncharge from mc.from later. */
5879 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005880 }
5881 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005882put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005883 put_page(page);
5884 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005885 case MC_TARGET_SWAP:
5886 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005887 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005888 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005889 /* we fixup refcnts and charges later. */
5890 mc.moved_swap++;
5891 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005892 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005893 default:
5894 break;
5895 }
5896 }
5897 pte_unmap_unlock(pte - 1, ptl);
5898 cond_resched();
5899
5900 if (addr != end) {
5901 /*
5902 * We have consumed all precharges we got in can_attach().
5903 * We try charge one by one, but don't do any additional
5904 * charges to mc.to if we have failed in charge once in attach()
5905 * phase.
5906 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005907 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005908 if (!ret)
5909 goto retry;
5910 }
5911
5912 return ret;
5913}
5914
5915static void mem_cgroup_move_charge(struct mm_struct *mm)
5916{
5917 struct vm_area_struct *vma;
5918
5919 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005920retry:
5921 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5922 /*
5923 * Someone who are holding the mmap_sem might be waiting in
5924 * waitq. So we cancel all extra charges, wake up all waiters,
5925 * and retry. Because we cancel precharges, we might not be able
5926 * to move enough charges, but moving charge is a best-effort
5927 * feature anyway, so it wouldn't be a big problem.
5928 */
5929 __mem_cgroup_clear_mc();
5930 cond_resched();
5931 goto retry;
5932 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005933 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5934 int ret;
5935 struct mm_walk mem_cgroup_move_charge_walk = {
5936 .pmd_entry = mem_cgroup_move_charge_pte_range,
5937 .mm = mm,
5938 .private = vma,
5939 };
5940 if (is_vm_hugetlb_page(vma))
5941 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005942 ret = walk_page_range(vma->vm_start, vma->vm_end,
5943 &mem_cgroup_move_charge_walk);
5944 if (ret)
5945 /*
5946 * means we have consumed all precharges and failed in
5947 * doing additional charge. Just abandon here.
5948 */
5949 break;
5950 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005951 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005952}
5953
Li Zefan761b3ef2012-01-31 13:47:36 +08005954static void mem_cgroup_move_task(struct cgroup *cont,
5955 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005956{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005957 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005958 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005959
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005960 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005961 if (mc.to)
5962 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005963 mmput(mm);
5964 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005965 if (mc.to)
5966 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005967}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005968#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08005969static int mem_cgroup_can_attach(struct cgroup *cgroup,
5970 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005971{
5972 return 0;
5973}
Li Zefan761b3ef2012-01-31 13:47:36 +08005974static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5975 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005976{
5977}
Li Zefan761b3ef2012-01-31 13:47:36 +08005978static void mem_cgroup_move_task(struct cgroup *cont,
5979 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005980{
5981}
5982#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005983
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005984struct cgroup_subsys mem_cgroup_subsys = {
5985 .name = "memory",
5986 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08005987 .css_alloc = mem_cgroup_css_alloc,
5988 .css_offline = mem_cgroup_css_offline,
5989 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005990 .can_attach = mem_cgroup_can_attach,
5991 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005992 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07005993 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005994 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005995 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005996};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005997
Andrew Mortonc255a452012-07-31 16:43:02 -07005998#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005999static int __init enable_swap_account(char *s)
6000{
6001 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07006002 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006003 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07006004 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08006005 really_do_swap_account = 0;
6006 return 1;
6007}
Michal Hockoa2c89902011-05-24 17:12:50 -07006008__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006009
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08006010#endif