blob: 20e3619870ae588c960fa7e4785d11956359c594 [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 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -080013 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 */
23
24#include <linux/res_counter.h>
25#include <linux/memcontrol.h>
26#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080027#include <linux/mm.h>
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -080028#include <linux/hugetlb.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080029#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080030#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080031#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080033#include <linux/bit_spinlock.h>
34#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070035#include <linux/limits.h>
Paul Gortmakerb9e15ba2011-05-26 16:00:52 -040036#include <linux/export.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080037#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070038#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070039#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080040#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080041#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080042#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080043#include <linux/eventfd.h>
44#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080045#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080046#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070047#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070048#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070049#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080050#include <linux/cpu.h>
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -070051#include <linux/oom.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080052#include "internal.h"
Glauber Costad1a4c0b2011-12-11 21:47:04 +000053#include <net/sock.h>
Michal Hocko4bd2c1e2012-10-08 16:33:10 -070054#include <net/ip.h>
Glauber Costad1a4c0b2011-12-11 21:47:04 +000055#include <net/tcp_memcontrol.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080056
Balbir Singh8697d332008-02-07 00:13:59 -080057#include <asm/uaccess.h>
58
KOSAKI Motohirocc8e9702010-08-09 17:19:57 -070059#include <trace/events/vmscan.h>
60
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070061struct cgroup_subsys mem_cgroup_subsys __read_mostly;
David Rientjes68ae5642012-12-12 13:51:57 -080062EXPORT_SYMBOL(mem_cgroup_subsys);
63
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070064#define MEM_CGROUP_RECLAIM_RETRIES 5
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -070065static struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080066
Andrew Mortonc255a452012-07-31 16:43:02 -070067#ifdef CONFIG_MEMCG_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070068/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080069int do_swap_account __read_mostly;
Michal Hockoa42c3902010-11-24 12:57:08 -080070
71/* for remember boot option*/
Andrew Mortonc255a452012-07-31 16:43:02 -070072#ifdef CONFIG_MEMCG_SWAP_ENABLED
Michal Hockoa42c3902010-11-24 12:57:08 -080073static int really_do_swap_account __initdata = 1;
74#else
75static int really_do_swap_account __initdata = 0;
76#endif
77
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080078#else
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -070079#define do_swap_account 0
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080080#endif
81
82
Balbir Singh8cdea7c2008-02-07 00:13:50 -080083/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080084 * Statistics for memory cgroup.
85 */
86enum mem_cgroup_stat_index {
87 /*
88 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
89 */
90 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070091 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080092 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -070093 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080094 MEM_CGROUP_STAT_NSTATS,
95};
96
Johannes Weineraf7c4b02012-05-29 15:07:08 -070097static const char * const mem_cgroup_stat_names[] = {
98 "cache",
99 "rss",
100 "mapped_file",
101 "swap",
102};
103
Johannes Weinere9f89742011-03-23 16:42:37 -0700104enum mem_cgroup_events_index {
105 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
106 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
Ying Han456f9982011-05-26 16:25:38 -0700107 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
108 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
Johannes Weinere9f89742011-03-23 16:42:37 -0700109 MEM_CGROUP_EVENTS_NSTATS,
110};
Johannes Weineraf7c4b02012-05-29 15:07:08 -0700111
112static const char * const mem_cgroup_events_names[] = {
113 "pgpgin",
114 "pgpgout",
115 "pgfault",
116 "pgmajfault",
117};
118
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700119/*
120 * Per memcg event counter is incremented at every pagein/pageout. With THP,
121 * it will be incremated by the number of pages. This counter is used for
122 * for trigger some periodic events. This is straightforward and better
123 * than using jiffies etc. to handle periodic memcg event.
124 */
125enum mem_cgroup_events_target {
126 MEM_CGROUP_TARGET_THRESH,
127 MEM_CGROUP_TARGET_SOFTLIMIT,
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700128 MEM_CGROUP_TARGET_NUMAINFO,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700129 MEM_CGROUP_NTARGETS,
130};
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700131#define THRESHOLDS_EVENTS_TARGET 128
132#define SOFTLIMIT_EVENTS_TARGET 1024
133#define NUMAINFO_EVENTS_TARGET 1024
Johannes Weinere9f89742011-03-23 16:42:37 -0700134
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800135struct mem_cgroup_stat_cpu {
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700136 long count[MEM_CGROUP_STAT_NSTATS];
Johannes Weinere9f89742011-03-23 16:42:37 -0700137 unsigned long events[MEM_CGROUP_EVENTS_NSTATS];
Johannes Weiner13114712012-05-29 15:07:07 -0700138 unsigned long nr_page_events;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700139 unsigned long targets[MEM_CGROUP_NTARGETS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800140};
141
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800142struct mem_cgroup_reclaim_iter {
143 /* css_id of the last scanned hierarchy member */
144 int position;
145 /* scan generation, increased every round-trip */
146 unsigned int generation;
147};
148
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800149/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800150 * per-zone information in memory controller.
151 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800152struct mem_cgroup_per_zone {
Johannes Weiner6290df52012-01-12 17:18:10 -0800153 struct lruvec lruvec;
Hugh Dickins1eb49272012-03-21 16:34:19 -0700154 unsigned long lru_size[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800155
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800156 struct mem_cgroup_reclaim_iter reclaim_iter[DEF_PRIORITY + 1];
157
Balbir Singhf64c3f52009-09-23 15:56:37 -0700158 struct rb_node tree_node; /* RB tree node */
159 unsigned long long usage_in_excess;/* Set to the value by which */
160 /* the soft limit is exceeded*/
161 bool on_tree;
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700162 struct mem_cgroup *memcg; /* Back pointer, we cannot */
Balbir Singh4e416952009-09-23 15:56:39 -0700163 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800164};
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800165
166struct mem_cgroup_per_node {
167 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
168};
169
170struct mem_cgroup_lru_info {
171 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
172};
173
174/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700175 * Cgroups above their limits are maintained in a RB-Tree, independent of
176 * their hierarchy representation
177 */
178
179struct mem_cgroup_tree_per_zone {
180 struct rb_root rb_root;
181 spinlock_t lock;
182};
183
184struct mem_cgroup_tree_per_node {
185 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
186};
187
188struct mem_cgroup_tree {
189 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
190};
191
192static struct mem_cgroup_tree soft_limit_tree __read_mostly;
193
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800194struct mem_cgroup_threshold {
195 struct eventfd_ctx *eventfd;
196 u64 threshold;
197};
198
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700199/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800200struct mem_cgroup_threshold_ary {
Sha Zhengju748dad32012-05-29 15:06:57 -0700201 /* An array index points to threshold just below or equal to usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700202 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800203 /* Size of entries[] */
204 unsigned int size;
205 /* Array of thresholds */
206 struct mem_cgroup_threshold entries[0];
207};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700208
209struct mem_cgroup_thresholds {
210 /* Primary thresholds array */
211 struct mem_cgroup_threshold_ary *primary;
212 /*
213 * Spare threshold array.
214 * This is needed to make mem_cgroup_unregister_event() "never fail".
215 * It must be able to store at least primary->size - 1 entries.
216 */
217 struct mem_cgroup_threshold_ary *spare;
218};
219
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700220/* for OOM */
221struct mem_cgroup_eventfd_list {
222 struct list_head list;
223 struct eventfd_ctx *eventfd;
224};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800225
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700226static void mem_cgroup_threshold(struct mem_cgroup *memcg);
227static void mem_cgroup_oom_notify(struct mem_cgroup *memcg);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800228
Balbir Singhf64c3f52009-09-23 15:56:37 -0700229/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800230 * The memory controller data structure. The memory controller controls both
231 * page cache and RSS per cgroup. We would eventually like to provide
232 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
233 * to help the administrator determine what knobs to tune.
234 *
235 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800236 * we hit the water mark. May be even add a low water mark, such that
237 * no reclaim occurs from a cgroup at it's low water mark, this is
238 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800239 */
240struct mem_cgroup {
241 struct cgroup_subsys_state css;
242 /*
243 * the counter to account for memory usage
244 */
245 struct res_counter res;
Hugh Dickins59927fb2012-03-15 15:17:07 -0700246
247 union {
248 /*
249 * the counter to account for mem+swap usage.
250 */
251 struct res_counter memsw;
252
253 /*
254 * rcu_freeing is used only when freeing struct mem_cgroup,
255 * so put it into a union to avoid wasting more memory.
256 * It must be disjoint from the css field. It could be
257 * in a union with the res field, but res plays a much
258 * larger part in mem_cgroup life than memsw, and might
259 * be of interest, even at time of free, when debugging.
260 * So share rcu_head with the less interesting memsw.
261 */
262 struct rcu_head rcu_freeing;
263 /*
Glauber Costa3afe36b2012-05-29 15:07:10 -0700264 * We also need some space for a worker in deferred freeing.
265 * By the time we call it, rcu_freeing is no longer in use.
Hugh Dickins59927fb2012-03-15 15:17:07 -0700266 */
267 struct work_struct work_freeing;
268 };
269
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800270 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800271 * Per cgroup active and inactive list, similar to the
272 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800273 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800274 struct mem_cgroup_lru_info info;
Ying Han889976d2011-05-26 16:25:33 -0700275 int last_scanned_node;
276#if MAX_NUMNODES > 1
277 nodemask_t scan_nodes;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700278 atomic_t numainfo_events;
279 atomic_t numainfo_updating;
Ying Han889976d2011-05-26 16:25:33 -0700280#endif
Balbir Singh18f59ea2009-01-07 18:08:07 -0800281 /*
282 * Should the accounting and control be hierarchical, per subtree?
283 */
284 bool use_hierarchy;
Michal Hocko79dfdac2011-07-26 16:08:23 -0700285
286 bool oom_lock;
287 atomic_t under_oom;
288
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800289 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800290
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -0700291 int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700292 /* OOM-Killer disable */
293 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800294
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700295 /* set when res.limit == memsw.limit */
296 bool memsw_is_minimum;
297
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800298 /* protect arrays of thresholds */
299 struct mutex thresholds_lock;
300
301 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700302 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700303
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800304 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700305 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700306
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700307 /* For oom notifier event fd */
308 struct list_head oom_notify;
Johannes Weiner185efc02011-09-14 16:21:58 -0700309
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800310 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800311 * Should we move charges of a task when a task is moved into this
312 * mem_cgroup ? And what type of charges should we move ?
313 */
314 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800315 /*
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700316 * set > 0 if pages under this cgroup are moving to other cgroup.
317 */
318 atomic_t moving_account;
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -0700319 /* taken only while moving_account > 0 */
320 spinlock_t move_lock;
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -0700321 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800322 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800323 */
Kirill A. Shutemov3a7951b2012-05-29 15:06:56 -0700324 struct mem_cgroup_stat_cpu __percpu *stat;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700325 /*
326 * used when a cpu is offlined or other synchronizations
327 * See mem_cgroup_read_stat().
328 */
329 struct mem_cgroup_stat_cpu nocpu_base;
330 spinlock_t pcp_counter_lock;
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000331
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700332#if defined(CONFIG_MEMCG_KMEM) && defined(CONFIG_INET)
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000333 struct tcp_memcontrol tcp_mem;
334#endif
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800335};
336
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800337/* Stuffs for move charges at task migration. */
338/*
339 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
340 * left-shifted bitmap of these types.
341 */
342enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800343 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700344 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800345 NR_MOVE_TYPE,
346};
347
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800348/* "mc" and its members are protected by cgroup_mutex */
349static struct move_charge_struct {
Daisuke Nishimurab1dd6932010-11-24 12:57:06 -0800350 spinlock_t lock; /* for from, to */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800351 struct mem_cgroup *from;
352 struct mem_cgroup *to;
353 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800354 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800355 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800356 struct task_struct *moving_task; /* a task moving charges */
357 wait_queue_head_t waitq; /* a waitq for other context */
358} mc = {
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -0700359 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800360 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
361};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800362
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700363static bool move_anon(void)
364{
365 return test_bit(MOVE_CHARGE_TYPE_ANON,
366 &mc.to->move_charge_at_immigrate);
367}
368
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700369static bool move_file(void)
370{
371 return test_bit(MOVE_CHARGE_TYPE_FILE,
372 &mc.to->move_charge_at_immigrate);
373}
374
Balbir Singh4e416952009-09-23 15:56:39 -0700375/*
376 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
377 * limit reclaim to prevent infinite loops, if they ever occur.
378 */
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700379#define MEM_CGROUP_MAX_RECLAIM_LOOPS 100
380#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS 2
Balbir Singh4e416952009-09-23 15:56:39 -0700381
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800382enum charge_type {
383 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -0700384 MEM_CGROUP_CHARGE_TYPE_ANON,
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800385 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700386 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700387 NR_CHARGE_TYPE,
388};
389
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800390/* for encoding cft->private value on file */
Glauber Costa65c64ce2011-12-22 01:02:27 +0000391#define _MEM (0)
392#define _MEMSWAP (1)
393#define _OOM_TYPE (2)
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -0700394#define MEMFILE_PRIVATE(x, val) ((x) << 16 | (val))
395#define MEMFILE_TYPE(val) ((val) >> 16 & 0xffff)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800396#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700397/* Used for OOM nofiier */
398#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800399
Balbir Singh75822b42009-09-23 15:56:38 -0700400/*
401 * Reclaim flags for mem_cgroup_hierarchical_reclaim
402 */
403#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
404#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
405#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
406#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
407
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700408static void mem_cgroup_get(struct mem_cgroup *memcg);
409static void mem_cgroup_put(struct mem_cgroup *memcg);
Glauber Costae1aab162011-12-11 21:47:03 +0000410
Wanpeng Lib2145142012-07-31 16:46:01 -0700411static inline
412struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *s)
413{
414 return container_of(s, struct mem_cgroup, css);
415}
416
Michal Hocko7ffc0ed2012-10-08 16:33:13 -0700417static inline bool mem_cgroup_is_root(struct mem_cgroup *memcg)
418{
419 return (memcg == root_mem_cgroup);
420}
421
Glauber Costae1aab162011-12-11 21:47:03 +0000422/* Writing them here to avoid exposing memcg's inner layout */
Michal Hocko4bd2c1e2012-10-08 16:33:10 -0700423#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
Glauber Costae1aab162011-12-11 21:47:03 +0000424
Glauber Costae1aab162011-12-11 21:47:03 +0000425void sock_update_memcg(struct sock *sk)
426{
Glauber Costa376be5f2012-01-20 04:57:14 +0000427 if (mem_cgroup_sockets_enabled) {
Glauber Costae1aab162011-12-11 21:47:03 +0000428 struct mem_cgroup *memcg;
Glauber Costa3f134612012-05-29 15:07:11 -0700429 struct cg_proto *cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000430
431 BUG_ON(!sk->sk_prot->proto_cgroup);
432
Glauber Costaf3f511e2012-01-05 20:16:39 +0000433 /* Socket cloning can throw us here with sk_cgrp already
434 * filled. It won't however, necessarily happen from
435 * process context. So the test for root memcg given
436 * the current task's memcg won't help us in this case.
437 *
438 * Respecting the original socket's memcg is a better
439 * decision in this case.
440 */
441 if (sk->sk_cgrp) {
442 BUG_ON(mem_cgroup_is_root(sk->sk_cgrp->memcg));
443 mem_cgroup_get(sk->sk_cgrp->memcg);
444 return;
445 }
446
Glauber Costae1aab162011-12-11 21:47:03 +0000447 rcu_read_lock();
448 memcg = mem_cgroup_from_task(current);
Glauber Costa3f134612012-05-29 15:07:11 -0700449 cg_proto = sk->sk_prot->proto_cgroup(memcg);
450 if (!mem_cgroup_is_root(memcg) && memcg_proto_active(cg_proto)) {
Glauber Costae1aab162011-12-11 21:47:03 +0000451 mem_cgroup_get(memcg);
Glauber Costa3f134612012-05-29 15:07:11 -0700452 sk->sk_cgrp = cg_proto;
Glauber Costae1aab162011-12-11 21:47:03 +0000453 }
454 rcu_read_unlock();
455 }
456}
457EXPORT_SYMBOL(sock_update_memcg);
458
459void sock_release_memcg(struct sock *sk)
460{
Glauber Costa376be5f2012-01-20 04:57:14 +0000461 if (mem_cgroup_sockets_enabled && sk->sk_cgrp) {
Glauber Costae1aab162011-12-11 21:47:03 +0000462 struct mem_cgroup *memcg;
463 WARN_ON(!sk->sk_cgrp->memcg);
464 memcg = sk->sk_cgrp->memcg;
465 mem_cgroup_put(memcg);
466 }
467}
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000468
469struct cg_proto *tcp_proto_cgroup(struct mem_cgroup *memcg)
470{
471 if (!memcg || mem_cgroup_is_root(memcg))
472 return NULL;
473
474 return &memcg->tcp_mem.cg_proto;
475}
476EXPORT_SYMBOL(tcp_proto_cgroup);
Glauber Costae1aab162011-12-11 21:47:03 +0000477
Glauber Costa3f134612012-05-29 15:07:11 -0700478static void disarm_sock_keys(struct mem_cgroup *memcg)
479{
480 if (!memcg_proto_activated(&memcg->tcp_mem.cg_proto))
481 return;
482 static_key_slow_dec(&memcg_socket_limit_enabled);
483}
484#else
485static void disarm_sock_keys(struct mem_cgroup *memcg)
486{
487}
488#endif
489
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700490static void drain_all_stock_async(struct mem_cgroup *memcg);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800491
Balbir Singhf64c3f52009-09-23 15:56:37 -0700492static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700493mem_cgroup_zoneinfo(struct mem_cgroup *memcg, int nid, int zid)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700494{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700495 return &memcg->info.nodeinfo[nid]->zoneinfo[zid];
Balbir Singhf64c3f52009-09-23 15:56:37 -0700496}
497
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700498struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg)
Wu Fengguangd3242362009-12-16 12:19:59 +0100499{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700500 return &memcg->css;
Wu Fengguangd3242362009-12-16 12:19:59 +0100501}
502
Balbir Singhf64c3f52009-09-23 15:56:37 -0700503static struct mem_cgroup_per_zone *
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700504page_cgroup_zoneinfo(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700505{
Johannes Weiner97a6c372011-03-23 16:42:27 -0700506 int nid = page_to_nid(page);
507 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700508
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700509 return mem_cgroup_zoneinfo(memcg, nid, zid);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700510}
511
512static struct mem_cgroup_tree_per_zone *
513soft_limit_tree_node_zone(int nid, int zid)
514{
515 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
516}
517
518static struct mem_cgroup_tree_per_zone *
519soft_limit_tree_from_page(struct page *page)
520{
521 int nid = page_to_nid(page);
522 int zid = page_zonenum(page);
523
524 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
525}
526
527static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700528__mem_cgroup_insert_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700529 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700530 struct mem_cgroup_tree_per_zone *mctz,
531 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700532{
533 struct rb_node **p = &mctz->rb_root.rb_node;
534 struct rb_node *parent = NULL;
535 struct mem_cgroup_per_zone *mz_node;
536
537 if (mz->on_tree)
538 return;
539
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700540 mz->usage_in_excess = new_usage_in_excess;
541 if (!mz->usage_in_excess)
542 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700543 while (*p) {
544 parent = *p;
545 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
546 tree_node);
547 if (mz->usage_in_excess < mz_node->usage_in_excess)
548 p = &(*p)->rb_left;
549 /*
550 * We can't avoid mem cgroups that are over their soft
551 * limit by the same amount
552 */
553 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
554 p = &(*p)->rb_right;
555 }
556 rb_link_node(&mz->tree_node, parent, p);
557 rb_insert_color(&mz->tree_node, &mctz->rb_root);
558 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700559}
560
561static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700562__mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singh4e416952009-09-23 15:56:39 -0700563 struct mem_cgroup_per_zone *mz,
564 struct mem_cgroup_tree_per_zone *mctz)
565{
566 if (!mz->on_tree)
567 return;
568 rb_erase(&mz->tree_node, &mctz->rb_root);
569 mz->on_tree = false;
570}
571
572static void
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700573mem_cgroup_remove_exceeded(struct mem_cgroup *memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700574 struct mem_cgroup_per_zone *mz,
575 struct mem_cgroup_tree_per_zone *mctz)
576{
577 spin_lock(&mctz->lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700578 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700579 spin_unlock(&mctz->lock);
580}
581
Balbir Singhf64c3f52009-09-23 15:56:37 -0700582
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700583static void mem_cgroup_update_tree(struct mem_cgroup *memcg, struct page *page)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700584{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700585 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700586 struct mem_cgroup_per_zone *mz;
587 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700588 int nid = page_to_nid(page);
589 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700590 mctz = soft_limit_tree_from_page(page);
591
592 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700593 * Necessary to update all ancestors when hierarchy is used.
594 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700595 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700596 for (; memcg; memcg = parent_mem_cgroup(memcg)) {
597 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
598 excess = res_counter_soft_limit_excess(&memcg->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700599 /*
600 * We have to update the tree if mz is on RB-tree or
601 * mem is over its softlimit.
602 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700603 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700604 spin_lock(&mctz->lock);
605 /* if on-tree, remove it */
606 if (mz->on_tree)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700607 __mem_cgroup_remove_exceeded(memcg, mz, mctz);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700608 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700609 * Insert again. mz->usage_in_excess will be updated.
610 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700611 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700612 __mem_cgroup_insert_exceeded(memcg, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700613 spin_unlock(&mctz->lock);
614 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700615 }
616}
617
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700618static void mem_cgroup_remove_from_trees(struct mem_cgroup *memcg)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700619{
620 int node, zone;
621 struct mem_cgroup_per_zone *mz;
622 struct mem_cgroup_tree_per_zone *mctz;
623
Bob Liu3ed28fa2012-01-12 17:19:04 -0800624 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -0700625 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700626 mz = mem_cgroup_zoneinfo(memcg, node, zone);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700627 mctz = soft_limit_tree_node_zone(node, zone);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700628 mem_cgroup_remove_exceeded(memcg, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700629 }
630 }
631}
632
Balbir Singh4e416952009-09-23 15:56:39 -0700633static struct mem_cgroup_per_zone *
634__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
635{
636 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700637 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700638
639retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700640 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700641 rightmost = rb_last(&mctz->rb_root);
642 if (!rightmost)
643 goto done; /* Nothing to reclaim from */
644
645 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
646 /*
647 * Remove the node now but someone else can add it back,
648 * we will to add it back at the end of reclaim to its correct
649 * position in the tree.
650 */
Hugh Dickinsd79154b2012-03-21 16:34:18 -0700651 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
652 if (!res_counter_soft_limit_excess(&mz->memcg->res) ||
653 !css_tryget(&mz->memcg->css))
Balbir Singh4e416952009-09-23 15:56:39 -0700654 goto retry;
655done:
656 return mz;
657}
658
659static struct mem_cgroup_per_zone *
660mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
661{
662 struct mem_cgroup_per_zone *mz;
663
664 spin_lock(&mctz->lock);
665 mz = __mem_cgroup_largest_soft_limit_node(mctz);
666 spin_unlock(&mctz->lock);
667 return mz;
668}
669
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700670/*
671 * Implementation Note: reading percpu statistics for memcg.
672 *
673 * Both of vmstat[] and percpu_counter has threshold and do periodic
674 * synchronization to implement "quick" read. There are trade-off between
675 * reading cost and precision of value. Then, we may have a chance to implement
676 * a periodic synchronizion of counter in memcg's counter.
677 *
678 * But this _read() function is used for user interface now. The user accounts
679 * memory usage by memory cgroup and he _always_ requires exact value because
680 * he accounts memory. Even if we provide quick-and-fuzzy read, we always
681 * have to visit all online cpus and make sum. So, for now, unnecessary
682 * synchronization is not implemented. (just implemented for cpu hotplug)
683 *
684 * If there are kernel internal actions which can make use of some not-exact
685 * value, and reading all cpu value can be performance bottleneck in some
686 * common workload, threashold and synchonization as vmstat[] should be
687 * implemented.
688 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700689static long mem_cgroup_read_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700690 enum mem_cgroup_stat_index idx)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800691{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700692 long val = 0;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800693 int cpu;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800694
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700695 get_online_cpus();
696 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700697 val += per_cpu(memcg->stat->count[idx], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700698#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700699 spin_lock(&memcg->pcp_counter_lock);
700 val += memcg->nocpu_base.count[idx];
701 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700702#endif
703 put_online_cpus();
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800704 return val;
705}
706
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700707static void mem_cgroup_swap_statistics(struct mem_cgroup *memcg,
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700708 bool charge)
709{
710 int val = (charge) ? 1 : -1;
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -0700711 this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_SWAP], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700712}
713
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700714static unsigned long mem_cgroup_read_events(struct mem_cgroup *memcg,
Johannes Weinere9f89742011-03-23 16:42:37 -0700715 enum mem_cgroup_events_index idx)
716{
717 unsigned long val = 0;
718 int cpu;
719
720 for_each_online_cpu(cpu)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700721 val += per_cpu(memcg->stat->events[idx], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -0700722#ifdef CONFIG_HOTPLUG_CPU
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700723 spin_lock(&memcg->pcp_counter_lock);
724 val += memcg->nocpu_base.events[idx];
725 spin_unlock(&memcg->pcp_counter_lock);
Johannes Weinere9f89742011-03-23 16:42:37 -0700726#endif
727 return val;
728}
729
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700730static void mem_cgroup_charge_statistics(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700731 bool anon, int nr_pages)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800732{
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800733 preempt_disable();
734
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700735 /*
736 * Here, RSS means 'mapped anon' and anon's SwapCache. Shmem/tmpfs is
737 * counted as CACHE even if it's on ANON LRU.
738 */
739 if (anon)
740 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_RSS],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700741 nr_pages);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800742 else
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -0700743 __this_cpu_add(memcg->stat->count[MEM_CGROUP_STAT_CACHE],
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700744 nr_pages);
Balaji Rao55e462b2008-05-01 04:35:12 -0700745
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800746 /* pagein of a big page is an event. So, ignore page size */
747 if (nr_pages > 0)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700748 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGIN]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800749 else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700750 __this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGPGOUT]);
KAMEZAWA Hiroyuki3751d602011-02-01 15:52:45 -0800751 nr_pages = -nr_pages; /* for event */
752 }
KAMEZAWA Hiroyukie401f172011-01-20 14:44:23 -0800753
Johannes Weiner13114712012-05-29 15:07:07 -0700754 __this_cpu_add(memcg->stat->nr_page_events, nr_pages);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800755
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800756 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800757}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800758
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700759unsigned long
Hugh Dickins4d7dcca2012-05-29 15:07:08 -0700760mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
Konstantin Khlebnikov074291f2012-05-29 15:07:00 -0700761{
762 struct mem_cgroup_per_zone *mz;
763
764 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
765 return mz->lru_size[lru];
766}
767
768static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700769mem_cgroup_zone_nr_lru_pages(struct mem_cgroup *memcg, int nid, int zid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700770 unsigned int lru_mask)
Ying Han889976d2011-05-26 16:25:33 -0700771{
772 struct mem_cgroup_per_zone *mz;
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700773 enum lru_list lru;
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700774 unsigned long ret = 0;
775
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700776 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700777
Hugh Dickinsf156ab92012-03-21 16:34:19 -0700778 for_each_lru(lru) {
779 if (BIT(lru) & lru_mask)
780 ret += mz->lru_size[lru];
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700781 }
782 return ret;
783}
784
785static unsigned long
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700786mem_cgroup_node_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700787 int nid, unsigned int lru_mask)
788{
Ying Han889976d2011-05-26 16:25:33 -0700789 u64 total = 0;
790 int zid;
791
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700792 for (zid = 0; zid < MAX_NR_ZONES; zid++)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700793 total += mem_cgroup_zone_nr_lru_pages(memcg,
794 nid, zid, lru_mask);
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700795
Ying Han889976d2011-05-26 16:25:33 -0700796 return total;
797}
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700798
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700799static unsigned long mem_cgroup_nr_lru_pages(struct mem_cgroup *memcg,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -0700800 unsigned int lru_mask)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800801{
Ying Han889976d2011-05-26 16:25:33 -0700802 int nid;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800803 u64 total = 0;
804
Lai Jiangshan31aaea42012-12-12 13:51:27 -0800805 for_each_node_state(nid, N_MEMORY)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700806 total += mem_cgroup_node_nr_lru_pages(memcg, nid, lru_mask);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800807 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800808}
809
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800810static bool mem_cgroup_event_ratelimit(struct mem_cgroup *memcg,
811 enum mem_cgroup_events_target target)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800812{
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700813 unsigned long val, next;
814
Johannes Weiner13114712012-05-29 15:07:07 -0700815 val = __this_cpu_read(memcg->stat->nr_page_events);
Steven Rostedt47994012011-11-02 13:38:33 -0700816 next = __this_cpu_read(memcg->stat->targets[target]);
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700817 /* from time_after() in jiffies.h */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800818 if ((long)next - (long)val < 0) {
819 switch (target) {
820 case MEM_CGROUP_TARGET_THRESH:
821 next = val + THRESHOLDS_EVENTS_TARGET;
822 break;
823 case MEM_CGROUP_TARGET_SOFTLIMIT:
824 next = val + SOFTLIMIT_EVENTS_TARGET;
825 break;
826 case MEM_CGROUP_TARGET_NUMAINFO:
827 next = val + NUMAINFO_EVENTS_TARGET;
828 break;
829 default:
830 break;
831 }
832 __this_cpu_write(memcg->stat->targets[target], next);
833 return true;
Johannes Weiner7a159cc2011-03-23 16:42:38 -0700834 }
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800835 return false;
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800836}
837
838/*
839 * Check events in order.
840 *
841 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700842static void memcg_check_events(struct mem_cgroup *memcg, struct page *page)
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800843{
Steven Rostedt47994012011-11-02 13:38:33 -0700844 preempt_disable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800845 /* threshold event is triggered in finer grain than soft limit */
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800846 if (unlikely(mem_cgroup_event_ratelimit(memcg,
847 MEM_CGROUP_TARGET_THRESH))) {
Andrew Morton82b3f2a2012-02-03 15:37:14 -0800848 bool do_softlimit;
849 bool do_numainfo __maybe_unused;
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800850
851 do_softlimit = mem_cgroup_event_ratelimit(memcg,
852 MEM_CGROUP_TARGET_SOFTLIMIT);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700853#if MAX_NUMNODES > 1
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800854 do_numainfo = mem_cgroup_event_ratelimit(memcg,
855 MEM_CGROUP_TARGET_NUMAINFO);
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -0700856#endif
Johannes Weinerf53d7ce2012-01-12 17:18:23 -0800857 preempt_enable();
858
859 mem_cgroup_threshold(memcg);
860 if (unlikely(do_softlimit))
861 mem_cgroup_update_tree(memcg, page);
862#if MAX_NUMNODES > 1
863 if (unlikely(do_numainfo))
864 atomic_inc(&memcg->numainfo_events);
865#endif
866 } else
867 preempt_enable();
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800868}
869
Glauber Costad1a4c0b2011-12-11 21:47:04 +0000870struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800871{
Wanpeng Lib2145142012-07-31 16:46:01 -0700872 return mem_cgroup_from_css(
873 cgroup_subsys_state(cont, mem_cgroup_subsys_id));
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800874}
875
Balbir Singhcf475ad2008-04-29 01:00:16 -0700876struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800877{
Balbir Singh31a78f22008-09-28 23:09:31 +0100878 /*
879 * mm_update_next_owner() may clear mm->owner to NULL
880 * if it races with swapoff, page migration, etc.
881 * So this can be called with p == NULL.
882 */
883 if (unlikely(!p))
884 return NULL;
885
Wanpeng Lib2145142012-07-31 16:46:01 -0700886 return mem_cgroup_from_css(task_subsys_state(p, mem_cgroup_subsys_id));
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800887}
888
KOSAKI Motohiroa4336582011-06-15 15:08:13 -0700889struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800890{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700891 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700892
893 if (!mm)
894 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800895 /*
896 * Because we have no locks, mm->owner's may be being moved to other
897 * cgroup. We use css_tryget() here even if this looks
898 * pessimistic (rather than adding locks here).
899 */
900 rcu_read_lock();
901 do {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700902 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
903 if (unlikely(!memcg))
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800904 break;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700905 } while (!css_tryget(&memcg->css));
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800906 rcu_read_unlock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -0700907 return memcg;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800908}
909
Johannes Weiner56600482012-01-12 17:17:59 -0800910/**
911 * mem_cgroup_iter - iterate over memory cgroup hierarchy
912 * @root: hierarchy root
913 * @prev: previously returned memcg, NULL on first invocation
914 * @reclaim: cookie for shared reclaim walks, NULL for full walks
915 *
916 * Returns references to children of the hierarchy below @root, or
917 * @root itself, or %NULL after a full round-trip.
918 *
919 * Caller must pass the return value in @prev on subsequent
920 * invocations for reference counting, or use mem_cgroup_iter_break()
921 * to cancel a hierarchy walk before the round-trip is complete.
922 *
923 * Reclaimers can specify a zone and a priority level in @reclaim to
924 * divide up the memcgs in the hierarchy among all concurrent
925 * reclaimers operating on the same zone and priority.
926 */
927struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
928 struct mem_cgroup *prev,
929 struct mem_cgroup_reclaim_cookie *reclaim)
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700930{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800931 struct mem_cgroup *memcg = NULL;
932 int id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700933
Johannes Weiner56600482012-01-12 17:17:59 -0800934 if (mem_cgroup_disabled())
935 return NULL;
936
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -0700937 if (!root)
938 root = root_mem_cgroup;
939
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800940 if (prev && !reclaim)
941 id = css_id(&prev->css);
942
943 if (prev && prev != root)
944 css_put(&prev->css);
945
946 if (!root->use_hierarchy && root != root_mem_cgroup) {
947 if (prev)
948 return NULL;
949 return root;
950 }
951
952 while (!memcg) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800953 struct mem_cgroup_reclaim_iter *uninitialized_var(iter);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800954 struct cgroup_subsys_state *css;
955
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800956 if (reclaim) {
957 int nid = zone_to_nid(reclaim->zone);
958 int zid = zone_idx(reclaim->zone);
959 struct mem_cgroup_per_zone *mz;
960
961 mz = mem_cgroup_zoneinfo(root, nid, zid);
962 iter = &mz->reclaim_iter[reclaim->priority];
963 if (prev && reclaim->generation != iter->generation)
964 return NULL;
965 id = iter->position;
966 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800967
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700968 rcu_read_lock();
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800969 css = css_get_next(&mem_cgroup_subsys, id + 1, &root->css, &id);
970 if (css) {
971 if (css == &root->css || css_tryget(css))
Wanpeng Lib2145142012-07-31 16:46:01 -0700972 memcg = mem_cgroup_from_css(css);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800973 } else
974 id = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700975 rcu_read_unlock();
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700976
Johannes Weiner527a5ec2012-01-12 17:17:55 -0800977 if (reclaim) {
978 iter->position = id;
979 if (!css)
980 iter->generation++;
981 else if (!prev && memcg)
982 reclaim->generation = iter->generation;
983 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800984
985 if (prev && !css)
986 return NULL;
987 }
988 return memcg;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -0700989}
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800990
Johannes Weiner56600482012-01-12 17:17:59 -0800991/**
992 * mem_cgroup_iter_break - abort a hierarchy walk prematurely
993 * @root: hierarchy root
994 * @prev: last visited hierarchy member as returned by mem_cgroup_iter()
995 */
996void mem_cgroup_iter_break(struct mem_cgroup *root,
997 struct mem_cgroup *prev)
Johannes Weiner9f3a0d02012-01-12 17:17:48 -0800998{
999 if (!root)
1000 root = root_mem_cgroup;
1001 if (prev && prev != root)
1002 css_put(&prev->css);
1003}
1004
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001005/*
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001006 * Iteration constructs for visiting all cgroups (under a tree). If
1007 * loops are exited prematurely (break), mem_cgroup_iter_break() must
1008 * be used for reference counting.
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001009 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001010#define for_each_mem_cgroup_tree(iter, root) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001011 for (iter = mem_cgroup_iter(root, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001012 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001013 iter = mem_cgroup_iter(root, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001014
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001015#define for_each_mem_cgroup(iter) \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001016 for (iter = mem_cgroup_iter(NULL, NULL, NULL); \
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001017 iter != NULL; \
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001018 iter = mem_cgroup_iter(NULL, iter, NULL))
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001019
David Rientjes68ae5642012-12-12 13:51:57 -08001020void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
Ying Han456f9982011-05-26 16:25:38 -07001021{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001022 struct mem_cgroup *memcg;
Ying Han456f9982011-05-26 16:25:38 -07001023
Ying Han456f9982011-05-26 16:25:38 -07001024 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001025 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1026 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001027 goto out;
1028
1029 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001030 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001031 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1032 break;
1033 case PGMAJFAULT:
1034 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001035 break;
1036 default:
1037 BUG();
1038 }
1039out:
1040 rcu_read_unlock();
1041}
David Rientjes68ae5642012-12-12 13:51:57 -08001042EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001043
Johannes Weiner925b7672012-01-12 17:18:15 -08001044/**
1045 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1046 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001047 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001048 *
1049 * Returns the lru list vector holding pages for the given @zone and
1050 * @mem. This can be the global zone lruvec, if the memory controller
1051 * is disabled.
1052 */
1053struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1054 struct mem_cgroup *memcg)
1055{
1056 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001057 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001058
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001059 if (mem_cgroup_disabled()) {
1060 lruvec = &zone->lruvec;
1061 goto out;
1062 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001063
1064 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001065 lruvec = &mz->lruvec;
1066out:
1067 /*
1068 * Since a node can be onlined after the mem_cgroup was created,
1069 * we have to be prepared to initialize lruvec->zone here;
1070 * and if offlined then reonlined, we need to reinitialize it.
1071 */
1072 if (unlikely(lruvec->zone != zone))
1073 lruvec->zone = zone;
1074 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001075}
1076
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001077/*
1078 * Following LRU functions are allowed to be used without PCG_LOCK.
1079 * Operations are called by routine of global LRU independently from memcg.
1080 * What we have to take care of here is validness of pc->mem_cgroup.
1081 *
1082 * Changes to pc->mem_cgroup happens when
1083 * 1. charge
1084 * 2. moving account
1085 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1086 * It is added to LRU before charge.
1087 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1088 * When moving account, the page is not on LRU. It's isolated.
1089 */
1090
Johannes Weiner925b7672012-01-12 17:18:15 -08001091/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001092 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001093 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001094 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001095 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001096struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001097{
1098 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001099 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001100 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001101 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001102
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001103 if (mem_cgroup_disabled()) {
1104 lruvec = &zone->lruvec;
1105 goto out;
1106 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001107
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001108 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001109 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001110
1111 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001112 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001113 * an uncharged page off lru does nothing to secure
1114 * its former mem_cgroup from sudden removal.
1115 *
1116 * Our caller holds lru_lock, and PageCgroupUsed is updated
1117 * under page_cgroup lock: between them, they make all uses
1118 * of pc->mem_cgroup safe.
1119 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001120 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001121 pc->mem_cgroup = memcg = root_mem_cgroup;
1122
Johannes Weiner925b7672012-01-12 17:18:15 -08001123 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001124 lruvec = &mz->lruvec;
1125out:
1126 /*
1127 * Since a node can be onlined after the mem_cgroup was created,
1128 * we have to be prepared to initialize lruvec->zone here;
1129 * and if offlined then reonlined, we need to reinitialize it.
1130 */
1131 if (unlikely(lruvec->zone != zone))
1132 lruvec->zone = zone;
1133 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001134}
1135
1136/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001137 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1138 * @lruvec: mem_cgroup per zone lru vector
1139 * @lru: index of lru list the page is sitting on
1140 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001141 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001142 * This function must be called when a page is added to or removed from an
1143 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001144 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001145void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1146 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001147{
1148 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001149 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001150
1151 if (mem_cgroup_disabled())
1152 return;
1153
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001154 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1155 lru_size = mz->lru_size + lru;
1156 *lru_size += nr_pages;
1157 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001158}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001159
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001160/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001161 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001162 * hierarchy subtree
1163 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001164bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1165 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001166{
Johannes Weiner91c637342012-05-29 15:06:24 -07001167 if (root_memcg == memcg)
1168 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001169 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001170 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001171 return css_is_ancestor(&memcg->css, &root_memcg->css);
1172}
1173
1174static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1175 struct mem_cgroup *memcg)
1176{
1177 bool ret;
1178
Johannes Weiner91c637342012-05-29 15:06:24 -07001179 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001180 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001181 rcu_read_unlock();
1182 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001183}
1184
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001185int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001186{
1187 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001188 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001189 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001190
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001191 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001192 if (p) {
1193 curr = try_get_mem_cgroup_from_mm(p->mm);
1194 task_unlock(p);
1195 } else {
1196 /*
1197 * All threads may have already detached their mm's, but the oom
1198 * killer still needs to detect if they have already been oom
1199 * killed to prevent needlessly killing additional tasks.
1200 */
1201 task_lock(task);
1202 curr = mem_cgroup_from_task(task);
1203 if (curr)
1204 css_get(&curr->css);
1205 task_unlock(task);
1206 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001207 if (!curr)
1208 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001209 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001210 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001211 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001212 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1213 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001214 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001215 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001216 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001217 return ret;
1218}
1219
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001220int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001221{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001222 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001223 unsigned long inactive;
1224 unsigned long active;
1225 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001226
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001227 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1228 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001229
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001230 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1231 if (gb)
1232 inactive_ratio = int_sqrt(10 * gb);
1233 else
1234 inactive_ratio = 1;
1235
Johannes Weiner9b272972011-11-02 13:38:23 -07001236 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001237}
1238
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001239int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001240{
1241 unsigned long active;
1242 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001243
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001244 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1245 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001246
1247 return (active > inactive);
1248}
1249
Balbir Singh6d61ef42009-01-07 18:08:06 -08001250#define mem_cgroup_from_res_counter(counter, member) \
1251 container_of(counter, struct mem_cgroup, member)
1252
Johannes Weiner19942822011-02-01 15:52:43 -08001253/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001254 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001255 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001256 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001257 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001258 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001259 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001260static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001261{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001262 unsigned long long margin;
1263
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001264 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001265 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001266 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001267 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001268}
1269
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001270int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001271{
1272 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001273
1274 /* root ? */
1275 if (cgrp->parent == NULL)
1276 return vm_swappiness;
1277
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001278 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001279}
1280
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001281/*
1282 * memcg->moving_account is used for checking possibility that some thread is
1283 * calling move_account(). When a thread on CPU-A starts moving pages under
1284 * a memcg, other threads should check memcg->moving_account under
1285 * rcu_read_lock(), like this:
1286 *
1287 * CPU-A CPU-B
1288 * rcu_read_lock()
1289 * memcg->moving_account+1 if (memcg->mocing_account)
1290 * take heavy locks.
1291 * synchronize_rcu() update something.
1292 * rcu_read_unlock()
1293 * start move here.
1294 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001295
1296/* for quick checking without looking up memcg */
1297atomic_t memcg_moving __read_mostly;
1298
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001299static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001300{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001301 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001302 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001303 synchronize_rcu();
1304}
1305
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001306static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001307{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001308 /*
1309 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1310 * We check NULL in callee rather than caller.
1311 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001312 if (memcg) {
1313 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001314 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001315 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001316}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001317
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001318/*
1319 * 2 routines for checking "mem" is under move_account() or not.
1320 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001321 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1322 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001323 * pc->mem_cgroup may be overwritten.
1324 *
1325 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1326 * under hierarchy of moving cgroups. This is for
1327 * waiting at hith-memory prressure caused by "move".
1328 */
1329
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001330static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001331{
1332 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001333 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001334}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001335
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001336static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001337{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001338 struct mem_cgroup *from;
1339 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001340 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001341 /*
1342 * Unlike task_move routines, we access mc.to, mc.from not under
1343 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1344 */
1345 spin_lock(&mc.lock);
1346 from = mc.from;
1347 to = mc.to;
1348 if (!from)
1349 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001350
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001351 ret = mem_cgroup_same_or_subtree(memcg, from)
1352 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001353unlock:
1354 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001355 return ret;
1356}
1357
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001358static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001359{
1360 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001361 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001362 DEFINE_WAIT(wait);
1363 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1364 /* moving charge context might have finished. */
1365 if (mc.moving_task)
1366 schedule();
1367 finish_wait(&mc.waitq, &wait);
1368 return true;
1369 }
1370 }
1371 return false;
1372}
1373
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001374/*
1375 * Take this lock when
1376 * - a code tries to modify page's memcg while it's USED.
1377 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001378 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001379 */
1380static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1381 unsigned long *flags)
1382{
1383 spin_lock_irqsave(&memcg->move_lock, *flags);
1384}
1385
1386static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1387 unsigned long *flags)
1388{
1389 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1390}
1391
Balbir Singhe2224322009-04-02 16:57:39 -07001392/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001393 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001394 * @memcg: The memory cgroup that went over limit
1395 * @p: Task that is going to be killed
1396 *
1397 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1398 * enabled
1399 */
1400void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1401{
1402 struct cgroup *task_cgrp;
1403 struct cgroup *mem_cgrp;
1404 /*
1405 * Need a buffer in BSS, can't rely on allocations. The code relies
1406 * on the assumption that OOM is serialized for memory controller.
1407 * If this assumption is broken, revisit this code.
1408 */
1409 static char memcg_name[PATH_MAX];
1410 int ret;
1411
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001412 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001413 return;
1414
Balbir Singhe2224322009-04-02 16:57:39 -07001415 rcu_read_lock();
1416
1417 mem_cgrp = memcg->css.cgroup;
1418 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1419
1420 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1421 if (ret < 0) {
1422 /*
1423 * Unfortunately, we are unable to convert to a useful name
1424 * But we'll still print out the usage information
1425 */
1426 rcu_read_unlock();
1427 goto done;
1428 }
1429 rcu_read_unlock();
1430
1431 printk(KERN_INFO "Task in %s killed", memcg_name);
1432
1433 rcu_read_lock();
1434 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1435 if (ret < 0) {
1436 rcu_read_unlock();
1437 goto done;
1438 }
1439 rcu_read_unlock();
1440
1441 /*
1442 * Continues from above, so we don't need an KERN_ level
1443 */
1444 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1445done:
1446
1447 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1448 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1449 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1450 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1451 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1452 "failcnt %llu\n",
1453 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1454 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1455 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1456}
1457
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001458/*
1459 * This function returns the number of memcg under hierarchy tree. Returns
1460 * 1(self count) if no children.
1461 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001462static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001463{
1464 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001465 struct mem_cgroup *iter;
1466
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001467 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001468 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001469 return num;
1470}
1471
Balbir Singh6d61ef42009-01-07 18:08:06 -08001472/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001473 * Return the memory (and swap, if configured) limit for a memcg.
1474 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001475static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001476{
1477 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001478
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001479 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001480
David Rientjesa63d83f2010-08-09 17:19:46 -07001481 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001482 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001483 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001484 if (mem_cgroup_swappiness(memcg)) {
1485 u64 memsw;
1486
1487 limit += total_swap_pages << PAGE_SHIFT;
1488 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1489
1490 /*
1491 * If memsw is finite and limits the amount of swap space
1492 * available to this memcg, return that limit.
1493 */
1494 limit = min(limit, memsw);
1495 }
1496
1497 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001498}
1499
David Rientjes19965462012-12-11 16:00:26 -08001500static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1501 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001502{
1503 struct mem_cgroup *iter;
1504 unsigned long chosen_points = 0;
1505 unsigned long totalpages;
1506 unsigned int points = 0;
1507 struct task_struct *chosen = NULL;
1508
David Rientjes876aafb2012-07-31 16:43:48 -07001509 /*
1510 * If current has a pending SIGKILL, then automatically select it. The
1511 * goal is to allow it to allocate so that it may quickly exit and free
1512 * its memory.
1513 */
1514 if (fatal_signal_pending(current)) {
1515 set_thread_flag(TIF_MEMDIE);
1516 return;
1517 }
1518
1519 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001520 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1521 for_each_mem_cgroup_tree(iter, memcg) {
1522 struct cgroup *cgroup = iter->css.cgroup;
1523 struct cgroup_iter it;
1524 struct task_struct *task;
1525
1526 cgroup_iter_start(cgroup, &it);
1527 while ((task = cgroup_iter_next(cgroup, &it))) {
1528 switch (oom_scan_process_thread(task, totalpages, NULL,
1529 false)) {
1530 case OOM_SCAN_SELECT:
1531 if (chosen)
1532 put_task_struct(chosen);
1533 chosen = task;
1534 chosen_points = ULONG_MAX;
1535 get_task_struct(chosen);
1536 /* fall through */
1537 case OOM_SCAN_CONTINUE:
1538 continue;
1539 case OOM_SCAN_ABORT:
1540 cgroup_iter_end(cgroup, &it);
1541 mem_cgroup_iter_break(memcg, iter);
1542 if (chosen)
1543 put_task_struct(chosen);
1544 return;
1545 case OOM_SCAN_OK:
1546 break;
1547 };
1548 points = oom_badness(task, memcg, NULL, totalpages);
1549 if (points > chosen_points) {
1550 if (chosen)
1551 put_task_struct(chosen);
1552 chosen = task;
1553 chosen_points = points;
1554 get_task_struct(chosen);
1555 }
1556 }
1557 cgroup_iter_end(cgroup, &it);
1558 }
1559
1560 if (!chosen)
1561 return;
1562 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001563 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1564 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001565}
1566
Johannes Weiner56600482012-01-12 17:17:59 -08001567static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1568 gfp_t gfp_mask,
1569 unsigned long flags)
1570{
1571 unsigned long total = 0;
1572 bool noswap = false;
1573 int loop;
1574
1575 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1576 noswap = true;
1577 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1578 noswap = true;
1579
1580 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1581 if (loop)
1582 drain_all_stock_async(memcg);
1583 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1584 /*
1585 * Allow limit shrinkers, which are triggered directly
1586 * by userspace, to catch signals and stop reclaim
1587 * after minimal progress, regardless of the margin.
1588 */
1589 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1590 break;
1591 if (mem_cgroup_margin(memcg))
1592 break;
1593 /*
1594 * If nothing was reclaimed after two attempts, there
1595 * may be no reclaimable pages in this hierarchy.
1596 */
1597 if (loop && !total)
1598 break;
1599 }
1600 return total;
1601}
1602
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001603/**
1604 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001605 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001606 * @nid: the node ID to be checked.
1607 * @noswap : specify true here if the user wants flle only information.
1608 *
1609 * This function returns whether the specified memcg contains any
1610 * reclaimable pages on a node. Returns true if there are any reclaimable
1611 * pages in the node.
1612 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001613static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001614 int nid, bool noswap)
1615{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001616 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001617 return true;
1618 if (noswap || !total_swap_pages)
1619 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001620 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001621 return true;
1622 return false;
1623
1624}
Ying Han889976d2011-05-26 16:25:33 -07001625#if MAX_NUMNODES > 1
1626
1627/*
1628 * Always updating the nodemask is not very good - even if we have an empty
1629 * list or the wrong list here, we can start from some node and traverse all
1630 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1631 *
1632 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001633static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001634{
1635 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001636 /*
1637 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1638 * pagein/pageout changes since the last update.
1639 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001640 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001641 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001642 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001643 return;
1644
Ying Han889976d2011-05-26 16:25:33 -07001645 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001646 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001647
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001648 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001649
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001650 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1651 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001652 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001653
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001654 atomic_set(&memcg->numainfo_events, 0);
1655 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001656}
1657
1658/*
1659 * Selecting a node where we start reclaim from. Because what we need is just
1660 * reducing usage counter, start from anywhere is O,K. Considering
1661 * memory reclaim from current node, there are pros. and cons.
1662 *
1663 * Freeing memory from current node means freeing memory from a node which
1664 * we'll use or we've used. So, it may make LRU bad. And if several threads
1665 * hit limits, it will see a contention on a node. But freeing from remote
1666 * node means more costs for memory reclaim because of memory latency.
1667 *
1668 * Now, we use round-robin. Better algorithm is welcomed.
1669 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001670int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001671{
1672 int node;
1673
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001674 mem_cgroup_may_update_nodemask(memcg);
1675 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001676
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001677 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001678 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001679 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001680 /*
1681 * We call this when we hit limit, not when pages are added to LRU.
1682 * No LRU may hold pages because all pages are UNEVICTABLE or
1683 * memcg is too small and all pages are not on LRU. In that case,
1684 * we use curret node.
1685 */
1686 if (unlikely(node == MAX_NUMNODES))
1687 node = numa_node_id();
1688
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001689 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001690 return node;
1691}
1692
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001693/*
1694 * Check all nodes whether it contains reclaimable pages or not.
1695 * For quick scan, we make use of scan_nodes. This will allow us to skip
1696 * unused nodes. But scan_nodes is lazily updated and may not cotain
1697 * enough new information. We need to do double check.
1698 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001699static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001700{
1701 int nid;
1702
1703 /*
1704 * quick check...making use of scan_node.
1705 * We can skip unused nodes.
1706 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001707 if (!nodes_empty(memcg->scan_nodes)) {
1708 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001709 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001710 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001711
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001712 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001713 return true;
1714 }
1715 }
1716 /*
1717 * Check rest of nodes.
1718 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001719 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001720 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001721 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001722 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001723 return true;
1724 }
1725 return false;
1726}
1727
Ying Han889976d2011-05-26 16:25:33 -07001728#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001729int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001730{
1731 return 0;
1732}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001733
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001734static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001735{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001736 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001737}
Ying Han889976d2011-05-26 16:25:33 -07001738#endif
1739
Johannes Weiner56600482012-01-12 17:17:59 -08001740static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1741 struct zone *zone,
1742 gfp_t gfp_mask,
1743 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001744{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001745 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001746 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001747 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001748 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001749 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001750 struct mem_cgroup_reclaim_cookie reclaim = {
1751 .zone = zone,
1752 .priority = 0,
1753 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001754
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001755 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001756
Balbir Singh4e416952009-09-23 15:56:39 -07001757 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001758 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001759 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001760 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001761 if (loop >= 2) {
1762 /*
1763 * If we have not been able to reclaim
1764 * anything, it might because there are
1765 * no reclaimable pages under this hierarchy
1766 */
Johannes Weiner56600482012-01-12 17:17:59 -08001767 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001768 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001769 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001770 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001771 * excess >> 2 is not to excessive so as to
1772 * reclaim too much, nor too less that we keep
1773 * coming back to reclaim from this cgroup
1774 */
1775 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001776 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001777 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001778 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001779 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001780 }
Johannes Weiner56600482012-01-12 17:17:59 -08001781 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001782 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001783 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1784 zone, &nr_scanned);
1785 *total_scanned += nr_scanned;
1786 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001787 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001788 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001789 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001790 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001791}
1792
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001793/*
1794 * Check OOM-Killer is already running under our hierarchy.
1795 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001796 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001797 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001798static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001799{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001800 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001801
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001802 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001803 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001804 /*
1805 * this subtree of our hierarchy is already locked
1806 * so we cannot give a lock.
1807 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001808 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001809 mem_cgroup_iter_break(memcg, iter);
1810 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001811 } else
1812 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001813 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001814
Michal Hocko79dfdac2011-07-26 16:08:23 -07001815 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001816 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001817
1818 /*
1819 * OK, we failed to lock the whole subtree so we have to clean up
1820 * what we set up to the failing subtree
1821 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001822 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001823 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001824 mem_cgroup_iter_break(memcg, iter);
1825 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001826 }
1827 iter->oom_lock = false;
1828 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001829 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001830}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001831
Michal Hocko79dfdac2011-07-26 16:08:23 -07001832/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001833 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001834 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001835static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001836{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001837 struct mem_cgroup *iter;
1838
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001839 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001840 iter->oom_lock = false;
1841 return 0;
1842}
1843
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001844static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001845{
1846 struct mem_cgroup *iter;
1847
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001848 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001849 atomic_inc(&iter->under_oom);
1850}
1851
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001852static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001853{
1854 struct mem_cgroup *iter;
1855
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001856 /*
1857 * When a new child is created while the hierarchy is under oom,
1858 * mem_cgroup_oom_lock() may not be called. We have to use
1859 * atomic_add_unless() here.
1860 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001861 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001862 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001863}
1864
Michal Hocko1af8efe2011-07-26 16:08:24 -07001865static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001866static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1867
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001868struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001869 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001870 wait_queue_t wait;
1871};
1872
1873static int memcg_oom_wake_function(wait_queue_t *wait,
1874 unsigned mode, int sync, void *arg)
1875{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001876 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1877 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001878 struct oom_wait_info *oom_wait_info;
1879
1880 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001881 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001882
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001883 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001884 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001885 * Then we can use css_is_ancestor without taking care of RCU.
1886 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001887 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1888 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001889 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001890 return autoremove_wake_function(wait, mode, sync, arg);
1891}
1892
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001893static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001894{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001895 /* for filtering, pass "memcg" as argument. */
1896 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001897}
1898
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001899static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001900{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001901 if (memcg && atomic_read(&memcg->under_oom))
1902 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001903}
1904
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001905/*
1906 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1907 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001908static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
1909 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001910{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001911 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001912 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001913
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001914 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001915 owait.wait.flags = 0;
1916 owait.wait.func = memcg_oom_wake_function;
1917 owait.wait.private = current;
1918 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001919 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001920 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001921
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001922 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001923 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001924 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001925 /*
1926 * Even if signal_pending(), we can't quit charge() loop without
1927 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1928 * under OOM is always welcomed, use TASK_KILLABLE here.
1929 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001930 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001931 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001932 need_to_kill = false;
1933 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001934 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001935 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001936
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001937 if (need_to_kill) {
1938 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07001939 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001940 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001941 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001942 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001943 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001944 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001945 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001946 mem_cgroup_oom_unlock(memcg);
1947 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001948 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001949
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001950 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001951
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001952 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1953 return false;
1954 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001955 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001956 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001957}
1958
Balbir Singhd69b0422009-06-17 16:26:34 -07001959/*
1960 * Currently used to update mapped file statistics, but the routine can be
1961 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001962 *
1963 * Notes: Race condition
1964 *
1965 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1966 * it tends to be costly. But considering some conditions, we doesn't need
1967 * to do so _always_.
1968 *
1969 * Considering "charge", lock_page_cgroup() is not required because all
1970 * file-stat operations happen after a page is attached to radix-tree. There
1971 * are no race with "charge".
1972 *
1973 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1974 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1975 * if there are race with "uncharge". Statistics itself is properly handled
1976 * by flags.
1977 *
1978 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001979 * small, we check mm->moving_account and detect there are possibility of race
1980 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001981 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001982
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001983void __mem_cgroup_begin_update_page_stat(struct page *page,
1984 bool *locked, unsigned long *flags)
1985{
1986 struct mem_cgroup *memcg;
1987 struct page_cgroup *pc;
1988
1989 pc = lookup_page_cgroup(page);
1990again:
1991 memcg = pc->mem_cgroup;
1992 if (unlikely(!memcg || !PageCgroupUsed(pc)))
1993 return;
1994 /*
1995 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07001996 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001997 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001998 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001999 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002000 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002001 return;
2002
2003 move_lock_mem_cgroup(memcg, flags);
2004 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2005 move_unlock_mem_cgroup(memcg, flags);
2006 goto again;
2007 }
2008 *locked = true;
2009}
2010
2011void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2012{
2013 struct page_cgroup *pc = lookup_page_cgroup(page);
2014
2015 /*
2016 * It's guaranteed that pc->mem_cgroup never changes while
2017 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002018 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002019 */
2020 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2021}
2022
Greg Thelen2a7106f2011-01-13 15:47:37 -08002023void mem_cgroup_update_page_stat(struct page *page,
2024 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002025{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002026 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002027 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002028 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002029
Johannes Weinercfa44942012-01-12 17:18:38 -08002030 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002031 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002032
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002033 memcg = pc->mem_cgroup;
2034 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002035 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002036
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002037 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002038 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002039 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002040 break;
2041 default:
2042 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002043 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002044
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002045 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002046}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002047
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002048/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002049 * size of first charge trial. "32" comes from vmscan.c's magic value.
2050 * TODO: maybe necessary to use big numbers in big irons.
2051 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002052#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002053struct memcg_stock_pcp {
2054 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002055 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002056 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002057 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002058#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002059};
2060static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002061static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002062
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002063/**
2064 * consume_stock: Try to consume stocked charge on this cpu.
2065 * @memcg: memcg to consume from.
2066 * @nr_pages: how many pages to charge.
2067 *
2068 * The charges will only happen if @memcg matches the current cpu's memcg
2069 * stock, and at least @nr_pages are available in that stock. Failure to
2070 * service an allocation will refill the stock.
2071 *
2072 * returns true if successful, false otherwise.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002073 */
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002074static bool consume_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002075{
2076 struct memcg_stock_pcp *stock;
2077 bool ret = true;
2078
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002079 if (nr_pages > CHARGE_BATCH)
2080 return false;
2081
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002082 stock = &get_cpu_var(memcg_stock);
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002083 if (memcg == stock->cached && stock->nr_pages >= nr_pages)
2084 stock->nr_pages -= nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002085 else /* need to call res_counter_charge */
2086 ret = false;
2087 put_cpu_var(memcg_stock);
2088 return ret;
2089}
2090
2091/*
2092 * Returns stocks cached in percpu to res_counter and reset cached information.
2093 */
2094static void drain_stock(struct memcg_stock_pcp *stock)
2095{
2096 struct mem_cgroup *old = stock->cached;
2097
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002098 if (stock->nr_pages) {
2099 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2100
2101 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002102 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002103 res_counter_uncharge(&old->memsw, bytes);
2104 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002105 }
2106 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002107}
2108
2109/*
2110 * This must be called under preempt disabled or must be called by
2111 * a thread which is pinned to local cpu.
2112 */
2113static void drain_local_stock(struct work_struct *dummy)
2114{
2115 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2116 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002117 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002118}
2119
2120/*
2121 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002122 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002123 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002124static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002125{
2126 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2127
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002128 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002129 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002130 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002131 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002132 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002133 put_cpu_var(memcg_stock);
2134}
2135
2136/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002137 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002138 * of the hierarchy under it. sync flag says whether we should block
2139 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002140 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002141static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002142{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002143 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002144
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002145 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002146 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002147 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002148 for_each_online_cpu(cpu) {
2149 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002150 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002151
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002152 memcg = stock->cached;
2153 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002154 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002155 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002156 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002157 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2158 if (cpu == curcpu)
2159 drain_local_stock(&stock->work);
2160 else
2161 schedule_work_on(cpu, &stock->work);
2162 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002163 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002164 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002165
2166 if (!sync)
2167 goto out;
2168
2169 for_each_online_cpu(cpu) {
2170 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002171 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002172 flush_work(&stock->work);
2173 }
2174out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002175 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002176}
2177
2178/*
2179 * Tries to drain stocked charges in other cpus. This function is asynchronous
2180 * and just put a work per cpu for draining localy on each cpu. Caller can
2181 * expects some charges will be back to res_counter later but cannot wait for
2182 * it.
2183 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002184static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002185{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002186 /*
2187 * If someone calls draining, avoid adding more kworker runs.
2188 */
2189 if (!mutex_trylock(&percpu_charge_mutex))
2190 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002191 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002192 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002193}
2194
2195/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002196static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002197{
2198 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002199 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002200 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002201 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002202}
2203
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002204/*
2205 * This function drains percpu counter value from DEAD cpu and
2206 * move it to local cpu. Note that this function can be preempted.
2207 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002208static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002209{
2210 int i;
2211
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002212 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002213 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002214 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002215
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002216 per_cpu(memcg->stat->count[i], cpu) = 0;
2217 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002218 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002219 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002220 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002221
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002222 per_cpu(memcg->stat->events[i], cpu) = 0;
2223 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002224 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002225 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002226}
2227
2228static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002229 unsigned long action,
2230 void *hcpu)
2231{
2232 int cpu = (unsigned long)hcpu;
2233 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002234 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002235
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002236 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002237 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002238
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002239 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002240 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002241
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002242 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002243 mem_cgroup_drain_pcp_counter(iter, cpu);
2244
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002245 stock = &per_cpu(memcg_stock, cpu);
2246 drain_stock(stock);
2247 return NOTIFY_OK;
2248}
2249
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002250
2251/* See __mem_cgroup_try_charge() for details */
2252enum {
2253 CHARGE_OK, /* success */
2254 CHARGE_RETRY, /* need to retry but retry is not bad */
2255 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2256 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2257 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2258};
2259
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002260static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002261 unsigned int nr_pages, unsigned int min_pages,
2262 bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002263{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002264 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002265 struct mem_cgroup *mem_over_limit;
2266 struct res_counter *fail_res;
2267 unsigned long flags = 0;
2268 int ret;
2269
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002270 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002271
2272 if (likely(!ret)) {
2273 if (!do_swap_account)
2274 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002275 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002276 if (likely(!ret))
2277 return CHARGE_OK;
2278
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002279 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002280 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2281 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2282 } else
2283 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002284 /*
Johannes Weiner9221edb2011-02-01 15:52:42 -08002285 * Never reclaim on behalf of optional batching, retry with a
2286 * single page instead.
2287 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002288 if (nr_pages > min_pages)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002289 return CHARGE_RETRY;
2290
2291 if (!(gfp_mask & __GFP_WAIT))
2292 return CHARGE_WOULDBLOCK;
2293
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002294 if (gfp_mask & __GFP_NORETRY)
2295 return CHARGE_NOMEM;
2296
Johannes Weiner56600482012-01-12 17:17:59 -08002297 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002298 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002299 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002300 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002301 * Even though the limit is exceeded at this point, reclaim
2302 * may have been able to free some pages. Retry the charge
2303 * before killing the task.
2304 *
2305 * Only for regular pages, though: huge pages are rather
2306 * unlikely to succeed so close to the limit, and we fall back
2307 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002308 */
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002309 if (nr_pages <= (1 << PAGE_ALLOC_COSTLY_ORDER) && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002310 return CHARGE_RETRY;
2311
2312 /*
2313 * At task move, charge accounts can be doubly counted. So, it's
2314 * better to wait until the end of task_move if something is going on.
2315 */
2316 if (mem_cgroup_wait_acct_move(mem_over_limit))
2317 return CHARGE_RETRY;
2318
2319 /* If we don't need to call oom-killer at el, return immediately */
2320 if (!oom_check)
2321 return CHARGE_NOMEM;
2322 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002323 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002324 return CHARGE_OOM_DIE;
2325
2326 return CHARGE_RETRY;
2327}
2328
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002329/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002330 * __mem_cgroup_try_charge() does
2331 * 1. detect memcg to be charged against from passed *mm and *ptr,
2332 * 2. update res_counter
2333 * 3. call memory reclaim if necessary.
2334 *
2335 * In some special case, if the task is fatal, fatal_signal_pending() or
2336 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2337 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2338 * as possible without any hazards. 2: all pages should have a valid
2339 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2340 * pointer, that is treated as a charge to root_mem_cgroup.
2341 *
2342 * So __mem_cgroup_try_charge() will return
2343 * 0 ... on success, filling *ptr with a valid memcg pointer.
2344 * -ENOMEM ... charge failure because of resource limits.
2345 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2346 *
2347 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2348 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002349 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002350static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002351 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002352 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002353 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002354 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002355{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002356 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002357 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002358 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002359 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002360
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002361 /*
2362 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2363 * in system level. So, allow to go ahead dying process in addition to
2364 * MEMDIE process.
2365 */
2366 if (unlikely(test_thread_flag(TIF_MEMDIE)
2367 || fatal_signal_pending(current)))
2368 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002369
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002370 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002371 * We always charge the cgroup the mm_struct belongs to.
2372 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002373 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002374 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002375 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002376 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002377 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002378again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002379 if (*ptr) { /* css should be a valid one */
2380 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002381 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002382 goto done;
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002383 if (consume_stock(memcg, nr_pages))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002384 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002385 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002386 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002387 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002388
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002389 rcu_read_lock();
2390 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002391 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002392 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002393 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002394 * race with swapoff. Then, we have small risk of mis-accouning.
2395 * But such kind of mis-account by race always happens because
2396 * we don't have cgroup_mutex(). It's overkill and we allo that
2397 * small race, here.
2398 * (*) swapoff at el will charge against mm-struct not against
2399 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002400 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002401 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002402 if (!memcg)
2403 memcg = root_mem_cgroup;
2404 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002405 rcu_read_unlock();
2406 goto done;
2407 }
Suleiman Souhlala0956d52012-12-18 14:21:36 -08002408 if (consume_stock(memcg, nr_pages)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002409 /*
2410 * It seems dagerous to access memcg without css_get().
2411 * But considering how consume_stok works, it's not
2412 * necessary. If consume_stock success, some charges
2413 * from this memcg are cached on this cpu. So, we
2414 * don't need to call css_get()/css_tryget() before
2415 * calling consume_stock().
2416 */
2417 rcu_read_unlock();
2418 goto done;
2419 }
2420 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002421 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002422 rcu_read_unlock();
2423 goto again;
2424 }
2425 rcu_read_unlock();
2426 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002427
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002428 do {
2429 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002430
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002431 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002432 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002433 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002434 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002435 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002436
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002437 oom_check = false;
2438 if (oom && !nr_oom_retries) {
2439 oom_check = true;
2440 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2441 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002442
Suleiman Souhlal4c9c5352012-12-18 14:21:41 -08002443 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, nr_pages,
2444 oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002445 switch (ret) {
2446 case CHARGE_OK:
2447 break;
2448 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002449 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002450 css_put(&memcg->css);
2451 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002452 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002453 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002454 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002455 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002456 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002457 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002459 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002460 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002461 /* If oom, we never return -ENOMEM */
2462 nr_oom_retries--;
2463 break;
2464 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002465 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002466 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002467 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002468 } while (ret != CHARGE_OK);
2469
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002470 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002471 refill_stock(memcg, batch - nr_pages);
2472 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002473done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002474 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002475 return 0;
2476nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002477 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002478 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002479bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002480 *ptr = root_mem_cgroup;
2481 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002482}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002483
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002484/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002485 * Somemtimes we have to undo a charge we got by try_charge().
2486 * This function is for that and do uncharge, put css's refcnt.
2487 * gotten by try_charge().
2488 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002489static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002490 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002491{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002492 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002493 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002494
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002495 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002496 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002497 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002498 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002499}
2500
2501/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002502 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2503 * This is useful when moving usage to parent cgroup.
2504 */
2505static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2506 unsigned int nr_pages)
2507{
2508 unsigned long bytes = nr_pages * PAGE_SIZE;
2509
2510 if (mem_cgroup_is_root(memcg))
2511 return;
2512
2513 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2514 if (do_swap_account)
2515 res_counter_uncharge_until(&memcg->memsw,
2516 memcg->memsw.parent, bytes);
2517}
2518
2519/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002520 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002521 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2522 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2523 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002524 */
2525static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2526{
2527 struct cgroup_subsys_state *css;
2528
2529 /* ID 0 is unused ID */
2530 if (!id)
2531 return NULL;
2532 css = css_lookup(&mem_cgroup_subsys, id);
2533 if (!css)
2534 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002535 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002536}
2537
Wu Fengguange42d9d52009-12-16 12:19:59 +01002538struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002539{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002540 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002541 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002542 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002543 swp_entry_t ent;
2544
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002545 VM_BUG_ON(!PageLocked(page));
2546
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002547 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002548 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002549 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002550 memcg = pc->mem_cgroup;
2551 if (memcg && !css_tryget(&memcg->css))
2552 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002553 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002554 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002555 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002556 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002557 memcg = mem_cgroup_lookup(id);
2558 if (memcg && !css_tryget(&memcg->css))
2559 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002560 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002561 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002562 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002563 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002564}
2565
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002566static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002567 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002568 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002569 enum charge_type ctype,
2570 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002571{
Johannes Weinerce587e62012-04-24 20:22:33 +02002572 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002573 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002574 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002575 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002576 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002577
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002578 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002579 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002580 /*
2581 * we don't need page_cgroup_lock about tail pages, becase they are not
2582 * accessed by any other context at this point.
2583 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002584
2585 /*
2586 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2587 * may already be on some other mem_cgroup's LRU. Take care of it.
2588 */
2589 if (lrucare) {
2590 zone = page_zone(page);
2591 spin_lock_irq(&zone->lru_lock);
2592 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002593 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002594 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002595 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002596 was_on_lru = true;
2597 }
2598 }
2599
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002600 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002601 /*
2602 * We access a page_cgroup asynchronously without lock_page_cgroup().
2603 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2604 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2605 * before USED bit, we need memory barrier here.
2606 * See mem_cgroup_add_lru_list(), etc.
2607 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002608 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002609 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002610
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002611 if (lrucare) {
2612 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002613 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002614 VM_BUG_ON(PageLRU(page));
2615 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002616 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002617 }
2618 spin_unlock_irq(&zone->lru_lock);
2619 }
2620
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002621 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002622 anon = true;
2623 else
2624 anon = false;
2625
2626 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002627 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002628
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002629 /*
2630 * "charge_statistics" updated event counter. Then, check it.
2631 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2632 * if they exceeds softlimit.
2633 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002634 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002635}
2636
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002637#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2638
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002639#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002640/*
2641 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002642 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2643 * charge/uncharge will be never happen and move_account() is done under
2644 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002645 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002646void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002647{
2648 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002649 struct page_cgroup *pc;
2650 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002651
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002652 if (mem_cgroup_disabled())
2653 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002654 for (i = 1; i < HPAGE_PMD_NR; i++) {
2655 pc = head_pc + i;
2656 pc->mem_cgroup = head_pc->mem_cgroup;
2657 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002658 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2659 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002660}
Hugh Dickins12d27102012-01-12 17:19:52 -08002661#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002662
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002663/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002664 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002665 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002666 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002667 * @pc: page_cgroup of the page.
2668 * @from: mem_cgroup which the page is moved from.
2669 * @to: mem_cgroup which the page is moved to. @from != @to.
2670 *
2671 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002672 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002673 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002674 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002675 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2676 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002677 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002678static int mem_cgroup_move_account(struct page *page,
2679 unsigned int nr_pages,
2680 struct page_cgroup *pc,
2681 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002682 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002683{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002684 unsigned long flags;
2685 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002686 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002687
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002688 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002689 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002690 /*
2691 * The page is isolated from LRU. So, collapse function
2692 * will not handle this page. But page splitting can happen.
2693 * Do this check under compound_page_lock(). The caller should
2694 * hold it.
2695 */
2696 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002697 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002698 goto out;
2699
2700 lock_page_cgroup(pc);
2701
2702 ret = -EINVAL;
2703 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2704 goto unlock;
2705
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002706 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002707
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07002708 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002709 /* Update mapped_file data for mem_cgroup */
2710 preempt_disable();
2711 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2712 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2713 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002714 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002715 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002716
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002717 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002718 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002719 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002720 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002721 ret = 0;
2722unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002723 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002724 /*
2725 * check events
2726 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002727 memcg_check_events(to, page);
2728 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002729out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002730 return ret;
2731}
2732
Michal Hocko2ef37d32012-10-26 13:37:30 +02002733/**
2734 * mem_cgroup_move_parent - moves page to the parent group
2735 * @page: the page to move
2736 * @pc: page_cgroup of the page
2737 * @child: page's cgroup
2738 *
2739 * move charges to its parent or the root cgroup if the group has no
2740 * parent (aka use_hierarchy==0).
2741 * Although this might fail (get_page_unless_zero, isolate_lru_page or
2742 * mem_cgroup_move_account fails) the failure is always temporary and
2743 * it signals a race with a page removal/uncharge or migration. In the
2744 * first case the page is on the way out and it will vanish from the LRU
2745 * on the next attempt and the call should be retried later.
2746 * Isolation from the LRU fails only if page has been isolated from
2747 * the LRU since we looked at it and that usually means either global
2748 * reclaim or migration going on. The page will either get back to the
2749 * LRU or vanish.
2750 * Finaly mem_cgroup_move_account fails only if the page got uncharged
2751 * (!PageCgroupUsed) or moved to a different group. The page will
2752 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002753 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002754static int mem_cgroup_move_parent(struct page *page,
2755 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07002756 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002757{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002758 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002759 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002760 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002761 int ret;
2762
Michal Hockod8423012012-10-26 13:37:29 +02002763 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002764
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002765 ret = -EBUSY;
2766 if (!get_page_unless_zero(page))
2767 goto out;
2768 if (isolate_lru_page(page))
2769 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002770
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002771 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002772
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002773 parent = parent_mem_cgroup(child);
2774 /*
2775 * If no parent, move charges to root cgroup.
2776 */
2777 if (!parent)
2778 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002779
Michal Hocko2ef37d32012-10-26 13:37:30 +02002780 if (nr_pages > 1) {
2781 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002782 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02002783 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002784
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002785 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002786 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002787 if (!ret)
2788 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002789
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002790 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002791 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002792 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002793put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002794 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002795out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002796 return ret;
2797}
2798
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002799/*
2800 * Charge the memory controller for page usage.
2801 * Return
2802 * 0 if the charge was successful
2803 * < 0 if the cgroup is over its limit
2804 */
2805static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002806 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002807{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002808 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002809 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002810 bool oom = true;
2811 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002812
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002813 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002814 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002815 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002816 /*
2817 * Never OOM-kill a process for a huge page. The
2818 * fault handler will fall back to regular pages.
2819 */
2820 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002821 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002822
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002823 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002824 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002825 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02002826 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002827 return 0;
2828}
2829
2830int mem_cgroup_newpage_charge(struct page *page,
2831 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002832{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002833 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002834 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08002835 VM_BUG_ON(page_mapped(page));
2836 VM_BUG_ON(page->mapping && !PageAnon(page));
2837 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002838 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002839 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002840}
2841
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002842/*
2843 * While swap-in, try_charge -> commit or cancel, the page is locked.
2844 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002845 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002846 * "commit()" or removed by "cancel()"
2847 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002848static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2849 struct page *page,
2850 gfp_t mask,
2851 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002852{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002853 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07002854 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002855 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002856
Johannes Weiner90deb782012-07-31 16:45:47 -07002857 pc = lookup_page_cgroup(page);
2858 /*
2859 * Every swap fault against a single page tries to charge the
2860 * page, bail as early as possible. shmem_unuse() encounters
2861 * already charged pages, too. The USED bit is protected by
2862 * the page lock, which serializes swap cache removal, which
2863 * in turn serializes uncharging.
2864 */
2865 if (PageCgroupUsed(pc))
2866 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002867 if (!do_swap_account)
2868 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002869 memcg = try_get_mem_cgroup_from_page(page);
2870 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002871 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08002872 *memcgp = memcg;
2873 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002874 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002875 if (ret == -EINTR)
2876 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002877 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002878charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002879 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
2880 if (ret == -EINTR)
2881 ret = 0;
2882 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002883}
2884
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002885int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
2886 gfp_t gfp_mask, struct mem_cgroup **memcgp)
2887{
2888 *memcgp = NULL;
2889 if (mem_cgroup_disabled())
2890 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07002891 /*
2892 * A racing thread's fault, or swapoff, may have already
2893 * updated the pte, and even removed page from swap cache: in
2894 * those cases unuse_pte()'s pte_same() test will fail; but
2895 * there's also a KSM case which does need to charge the page.
2896 */
2897 if (!PageSwapCache(page)) {
2898 int ret;
2899
2900 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
2901 if (ret == -EINTR)
2902 ret = 0;
2903 return ret;
2904 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002905 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
2906}
2907
Johannes Weiner827a03d2012-07-31 16:45:36 -07002908void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
2909{
2910 if (mem_cgroup_disabled())
2911 return;
2912 if (!memcg)
2913 return;
2914 __mem_cgroup_cancel_charge(memcg, 1);
2915}
2916
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002917static void
Johannes Weiner72835c82012-01-12 17:18:32 -08002918__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002919 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002920{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002921 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002922 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08002923 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002924 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002925
Johannes Weinerce587e62012-04-24 20:22:33 +02002926 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002927 /*
2928 * Now swap is on-memory. This means this page may be
2929 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002930 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2931 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2932 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002933 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002934 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002935 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07002936 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002937 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002938}
2939
Johannes Weiner72835c82012-01-12 17:18:32 -08002940void mem_cgroup_commit_charge_swapin(struct page *page,
2941 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002942{
Johannes Weiner72835c82012-01-12 17:18:32 -08002943 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002944 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002945}
2946
Johannes Weiner827a03d2012-07-31 16:45:36 -07002947int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2948 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002949{
Johannes Weiner827a03d2012-07-31 16:45:36 -07002950 struct mem_cgroup *memcg = NULL;
2951 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
2952 int ret;
2953
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002954 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07002955 return 0;
2956 if (PageCompound(page))
2957 return 0;
2958
Johannes Weiner827a03d2012-07-31 16:45:36 -07002959 if (!PageSwapCache(page))
2960 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
2961 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002962 ret = __mem_cgroup_try_charge_swapin(mm, page,
2963 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07002964 if (!ret)
2965 __mem_cgroup_commit_charge_swapin(page, memcg, type);
2966 }
2967 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002968}
2969
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002970static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002971 unsigned int nr_pages,
2972 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002973{
2974 struct memcg_batch_info *batch = NULL;
2975 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002976
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002977 /* If swapout, usage of swap doesn't decrease */
2978 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2979 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002980
2981 batch = &current->memcg_batch;
2982 /*
2983 * In usual, we do css_get() when we remember memcg pointer.
2984 * But in this case, we keep res->usage until end of a series of
2985 * uncharges. Then, it's ok to ignore memcg's refcnt.
2986 */
2987 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002988 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002989 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002990 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002991 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002992 * the same cgroup and we have chance to coalesce uncharges.
2993 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2994 * because we want to do uncharge as soon as possible.
2995 */
2996
2997 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2998 goto direct_uncharge;
2999
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003000 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08003001 goto direct_uncharge;
3002
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003003 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003004 * In typical case, batch->memcg == mem. This means we can
3005 * merge a series of uncharges to an uncharge of res_counter.
3006 * If not, we uncharge res_counter ony by one.
3007 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003008 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003009 goto direct_uncharge;
3010 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003011 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003012 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003013 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003014 return;
3015direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003016 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003017 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003018 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3019 if (unlikely(batch->memcg != memcg))
3020 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003021}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003022
Balbir Singh8697d332008-02-07 00:13:59 -08003023/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003024 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003025 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003026static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003027__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3028 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003029{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003030 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003031 unsigned int nr_pages = 1;
3032 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003033 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003034
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003035 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003036 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003037
Johannes Weiner0c59b892012-07-31 16:45:31 -07003038 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003039
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003040 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003041 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003042 VM_BUG_ON(!PageTransHuge(page));
3043 }
Balbir Singh8697d332008-02-07 00:13:59 -08003044 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003045 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003046 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003047 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003048 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003049 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003050
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003051 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003052
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003053 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003054
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003055 if (!PageCgroupUsed(pc))
3056 goto unlock_out;
3057
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003058 anon = PageAnon(page);
3059
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003060 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003061 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003062 /*
3063 * Generally PageAnon tells if it's the anon statistics to be
3064 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3065 * used before page reached the stage of being marked PageAnon.
3066 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003067 anon = true;
3068 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003069 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003070 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07003071 if (page_mapped(page))
3072 goto unlock_out;
3073 /*
3074 * Pages under migration may not be uncharged. But
3075 * end_migration() /must/ be the one uncharging the
3076 * unused post-migration page and so it has to call
3077 * here with the migration bit still set. See the
3078 * res_counter handling below.
3079 */
3080 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003081 goto unlock_out;
3082 break;
3083 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3084 if (!PageAnon(page)) { /* Shared memory */
3085 if (page->mapping && !page_is_file_cache(page))
3086 goto unlock_out;
3087 } else if (page_mapped(page)) /* Anon */
3088 goto unlock_out;
3089 break;
3090 default:
3091 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003092 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003093
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003094 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003095
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003096 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003097 /*
3098 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3099 * freed from LRU. This is safe because uncharged page is expected not
3100 * to be reused (freed soon). Exception is SwapCache, it's handled by
3101 * special functions.
3102 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003103
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003104 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003105 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003106 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003107 * will never be freed.
3108 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003109 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003110 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003111 mem_cgroup_swap_statistics(memcg, true);
3112 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003113 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003114 /*
3115 * Migration does not charge the res_counter for the
3116 * replacement page, so leave it alone when phasing out the
3117 * page that is unused after the migration.
3118 */
3119 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003120 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003121
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003122 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003123
3124unlock_out:
3125 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003126 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003127}
3128
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003129void mem_cgroup_uncharge_page(struct page *page)
3130{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003131 /* early check. */
3132 if (page_mapped(page))
3133 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08003134 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07003135 if (PageSwapCache(page))
3136 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07003137 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003138}
3139
3140void mem_cgroup_uncharge_cache_page(struct page *page)
3141{
3142 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003143 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07003144 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003145}
3146
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003147/*
3148 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3149 * In that cases, pages are freed continuously and we can expect pages
3150 * are in the same memcg. All these calls itself limits the number of
3151 * pages freed at once, then uncharge_start/end() is called properly.
3152 * This may be called prural(2) times in a context,
3153 */
3154
3155void mem_cgroup_uncharge_start(void)
3156{
3157 current->memcg_batch.do_batch++;
3158 /* We can do nest. */
3159 if (current->memcg_batch.do_batch == 1) {
3160 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003161 current->memcg_batch.nr_pages = 0;
3162 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003163 }
3164}
3165
3166void mem_cgroup_uncharge_end(void)
3167{
3168 struct memcg_batch_info *batch = &current->memcg_batch;
3169
3170 if (!batch->do_batch)
3171 return;
3172
3173 batch->do_batch--;
3174 if (batch->do_batch) /* If stacked, do nothing. */
3175 return;
3176
3177 if (!batch->memcg)
3178 return;
3179 /*
3180 * This "batch->memcg" is valid without any css_get/put etc...
3181 * bacause we hide charges behind us.
3182 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003183 if (batch->nr_pages)
3184 res_counter_uncharge(&batch->memcg->res,
3185 batch->nr_pages * PAGE_SIZE);
3186 if (batch->memsw_nr_pages)
3187 res_counter_uncharge(&batch->memcg->memsw,
3188 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003189 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003190 /* forget this pointer (for sanity check) */
3191 batch->memcg = NULL;
3192}
3193
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003194#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003195/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003196 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003197 * memcg information is recorded to swap_cgroup of "ent"
3198 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003199void
3200mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003201{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003202 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003203 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003204
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003205 if (!swapout) /* this was a swap cache but the swap is unused ! */
3206 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3207
Johannes Weiner0030f532012-07-31 16:45:25 -07003208 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003209
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003210 /*
3211 * record memcg information, if swapout && memcg != NULL,
3212 * mem_cgroup_get() was called in uncharge().
3213 */
3214 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003215 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003216}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003217#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003218
Andrew Mortonc255a452012-07-31 16:43:02 -07003219#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003220/*
3221 * called from swap_entry_free(). remove record in swap_cgroup and
3222 * uncharge "memsw" account.
3223 */
3224void mem_cgroup_uncharge_swap(swp_entry_t ent)
3225{
3226 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003227 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003228
3229 if (!do_swap_account)
3230 return;
3231
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003232 id = swap_cgroup_record(ent, 0);
3233 rcu_read_lock();
3234 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003235 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003236 /*
3237 * We uncharge this because swap is freed.
3238 * This memcg can be obsolete one. We avoid calling css_tryget
3239 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003240 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003241 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003242 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003243 mem_cgroup_put(memcg);
3244 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003245 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003246}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003247
3248/**
3249 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3250 * @entry: swap entry to be moved
3251 * @from: mem_cgroup which the entry is moved from
3252 * @to: mem_cgroup which the entry is moved to
3253 *
3254 * It succeeds only when the swap_cgroup's record for this entry is the same
3255 * as the mem_cgroup's id of @from.
3256 *
3257 * Returns 0 on success, -EINVAL on failure.
3258 *
3259 * The caller must have charged to @to, IOW, called res_counter_charge() about
3260 * both res and memsw, and called css_get().
3261 */
3262static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003263 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003264{
3265 unsigned short old_id, new_id;
3266
3267 old_id = css_id(&from->css);
3268 new_id = css_id(&to->css);
3269
3270 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003271 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003272 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003273 /*
3274 * This function is only called from task migration context now.
3275 * It postpones res_counter and refcount handling till the end
3276 * of task migration(mem_cgroup_clear_mc()) for performance
3277 * improvement. But we cannot postpone mem_cgroup_get(to)
3278 * because if the process that has been moved to @to does
3279 * swap-in, the refcount of @to might be decreased to 0.
3280 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003281 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003282 return 0;
3283 }
3284 return -EINVAL;
3285}
3286#else
3287static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003288 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003289{
3290 return -EINVAL;
3291}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003292#endif
3293
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003294/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003295 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3296 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003297 */
Johannes Weiner0030f532012-07-31 16:45:25 -07003298void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
3299 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003300{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003301 struct mem_cgroup *memcg = NULL;
Mel Gormanb32967f2012-11-19 12:35:47 +00003302 unsigned int nr_pages = 1;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003303 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003304 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003305
Johannes Weiner72835c82012-01-12 17:18:32 -08003306 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003307
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003308 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07003309 return;
Balbir Singh40779602008-04-04 14:29:59 -07003310
Mel Gormanb32967f2012-11-19 12:35:47 +00003311 if (PageTransHuge(page))
3312 nr_pages <<= compound_order(page);
3313
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003314 pc = lookup_page_cgroup(page);
3315 lock_page_cgroup(pc);
3316 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003317 memcg = pc->mem_cgroup;
3318 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003319 /*
3320 * At migrating an anonymous page, its mapcount goes down
3321 * to 0 and uncharge() will be called. But, even if it's fully
3322 * unmapped, migration may fail and this page has to be
3323 * charged again. We set MIGRATION flag here and delay uncharge
3324 * until end_migration() is called
3325 *
3326 * Corner Case Thinking
3327 * A)
3328 * When the old page was mapped as Anon and it's unmap-and-freed
3329 * while migration was ongoing.
3330 * If unmap finds the old page, uncharge() of it will be delayed
3331 * until end_migration(). If unmap finds a new page, it's
3332 * uncharged when it make mapcount to be 1->0. If unmap code
3333 * finds swap_migration_entry, the new page will not be mapped
3334 * and end_migration() will find it(mapcount==0).
3335 *
3336 * B)
3337 * When the old page was mapped but migraion fails, the kernel
3338 * remaps it. A charge for it is kept by MIGRATION flag even
3339 * if mapcount goes down to 0. We can do remap successfully
3340 * without charging it again.
3341 *
3342 * C)
3343 * The "old" page is under lock_page() until the end of
3344 * migration, so, the old page itself will not be swapped-out.
3345 * If the new page is swapped out before end_migraton, our
3346 * hook to usual swap-out path will catch the event.
3347 */
3348 if (PageAnon(page))
3349 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003350 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003351 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003352 /*
3353 * If the page is not charged at this point,
3354 * we return here.
3355 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003356 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07003357 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003358
Johannes Weiner72835c82012-01-12 17:18:32 -08003359 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003360 /*
3361 * We charge new page before it's used/mapped. So, even if unlock_page()
3362 * is called before end_migration, we can catch all events on this new
3363 * page. In the case new page is migrated but not remapped, new page's
3364 * mapcount will be finally 0 and we call uncharge in end_migration().
3365 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003366 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003367 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003368 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07003369 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07003370 /*
3371 * The page is committed to the memcg, but it's not actually
3372 * charged to the res_counter since we plan on replacing the
3373 * old one and only one page is going to be left afterwards.
3374 */
Mel Gormanb32967f2012-11-19 12:35:47 +00003375 __mem_cgroup_commit_charge(memcg, newpage, nr_pages, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003376}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003377
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003378/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003379void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003380 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003381{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003382 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003383 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003384 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003385
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003386 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003387 return;
Tejun Heob25ed602012-11-05 09:16:59 -08003388
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003389 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003390 used = oldpage;
3391 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003392 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003393 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003394 unused = oldpage;
3395 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003396 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07003397 __mem_cgroup_uncharge_common(unused,
3398 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
3399 : MEM_CGROUP_CHARGE_TYPE_CACHE,
3400 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07003401 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003402 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003403 * We disallowed uncharge of pages under migration because mapcount
3404 * of the page goes down to zero, temporarly.
3405 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003406 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003407 pc = lookup_page_cgroup(oldpage);
3408 lock_page_cgroup(pc);
3409 ClearPageCgroupMigration(pc);
3410 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003411
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003412 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003413 * If a page is a file cache, radix-tree replacement is very atomic
3414 * and we can skip this check. When it was an Anon page, its mapcount
3415 * goes down to 0. But because we added MIGRATION flage, it's not
3416 * uncharged yet. There are several case but page->mapcount check
3417 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3418 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003419 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003420 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003421 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003422}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003423
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003424/*
3425 * At replace page cache, newpage is not under any memcg but it's on
3426 * LRU. So, this function doesn't touch res_counter but handles LRU
3427 * in correct way. Both pages are locked so we cannot race with uncharge.
3428 */
3429void mem_cgroup_replace_page_cache(struct page *oldpage,
3430 struct page *newpage)
3431{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003432 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003433 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003434 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003435
3436 if (mem_cgroup_disabled())
3437 return;
3438
3439 pc = lookup_page_cgroup(oldpage);
3440 /* fix accounting on old pages */
3441 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003442 if (PageCgroupUsed(pc)) {
3443 memcg = pc->mem_cgroup;
3444 mem_cgroup_charge_statistics(memcg, false, -1);
3445 ClearPageCgroupUsed(pc);
3446 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003447 unlock_page_cgroup(pc);
3448
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003449 /*
3450 * When called from shmem_replace_page(), in some cases the
3451 * oldpage has already been charged, and in some cases not.
3452 */
3453 if (!memcg)
3454 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003455 /*
3456 * Even if newpage->mapping was NULL before starting replacement,
3457 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3458 * LRU while we overwrite pc->mem_cgroup.
3459 */
Johannes Weinerce587e62012-04-24 20:22:33 +02003460 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003461}
3462
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003463#ifdef CONFIG_DEBUG_VM
3464static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3465{
3466 struct page_cgroup *pc;
3467
3468 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003469 /*
3470 * Can be NULL while feeding pages into the page allocator for
3471 * the first time, i.e. during boot or memory hotplug;
3472 * or when mem_cgroup_disabled().
3473 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003474 if (likely(pc) && PageCgroupUsed(pc))
3475 return pc;
3476 return NULL;
3477}
3478
3479bool mem_cgroup_bad_page_check(struct page *page)
3480{
3481 if (mem_cgroup_disabled())
3482 return false;
3483
3484 return lookup_page_cgroup_used(page) != NULL;
3485}
3486
3487void mem_cgroup_print_bad_page(struct page *page)
3488{
3489 struct page_cgroup *pc;
3490
3491 pc = lookup_page_cgroup_used(page);
3492 if (pc) {
Hugh Dickins90b3fea2012-01-12 17:19:54 -08003493 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003494 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003495 }
3496}
3497#endif
3498
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003499static DEFINE_MUTEX(set_limit_mutex);
3500
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003501static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003502 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003503{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003504 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003505 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003506 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003507 int children = mem_cgroup_count_children(memcg);
3508 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003509 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003510
3511 /*
3512 * For keeping hierarchical_reclaim simple, how long we should retry
3513 * is depends on callers. We set our retry-count to be function
3514 * of # of children which we should visit in this loop.
3515 */
3516 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3517
3518 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003519
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003520 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003521 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003522 if (signal_pending(current)) {
3523 ret = -EINTR;
3524 break;
3525 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003526 /*
3527 * Rather than hide all in some function, I do this in
3528 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003529 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003530 */
3531 mutex_lock(&set_limit_mutex);
3532 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3533 if (memswlimit < val) {
3534 ret = -EINVAL;
3535 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003536 break;
3537 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003538
3539 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3540 if (memlimit < val)
3541 enlarge = 1;
3542
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003543 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003544 if (!ret) {
3545 if (memswlimit == val)
3546 memcg->memsw_is_minimum = true;
3547 else
3548 memcg->memsw_is_minimum = false;
3549 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003550 mutex_unlock(&set_limit_mutex);
3551
3552 if (!ret)
3553 break;
3554
Johannes Weiner56600482012-01-12 17:17:59 -08003555 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3556 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003557 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3558 /* Usage is reduced ? */
3559 if (curusage >= oldusage)
3560 retry_count--;
3561 else
3562 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003563 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003564 if (!ret && enlarge)
3565 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003566
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003567 return ret;
3568}
3569
Li Zefan338c8432009-06-17 16:27:15 -07003570static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3571 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003572{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003573 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003574 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003575 int children = mem_cgroup_count_children(memcg);
3576 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003577 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003578
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003579 /* see mem_cgroup_resize_res_limit */
3580 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3581 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003582 while (retry_count) {
3583 if (signal_pending(current)) {
3584 ret = -EINTR;
3585 break;
3586 }
3587 /*
3588 * Rather than hide all in some function, I do this in
3589 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003590 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003591 */
3592 mutex_lock(&set_limit_mutex);
3593 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3594 if (memlimit > val) {
3595 ret = -EINVAL;
3596 mutex_unlock(&set_limit_mutex);
3597 break;
3598 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003599 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3600 if (memswlimit < val)
3601 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003602 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003603 if (!ret) {
3604 if (memlimit == val)
3605 memcg->memsw_is_minimum = true;
3606 else
3607 memcg->memsw_is_minimum = false;
3608 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003609 mutex_unlock(&set_limit_mutex);
3610
3611 if (!ret)
3612 break;
3613
Johannes Weiner56600482012-01-12 17:17:59 -08003614 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3615 MEM_CGROUP_RECLAIM_NOSWAP |
3616 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003617 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003618 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003619 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003620 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003621 else
3622 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003623 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003624 if (!ret && enlarge)
3625 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003626 return ret;
3627}
3628
Balbir Singh4e416952009-09-23 15:56:39 -07003629unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003630 gfp_t gfp_mask,
3631 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003632{
3633 unsigned long nr_reclaimed = 0;
3634 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3635 unsigned long reclaimed;
3636 int loop = 0;
3637 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003638 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003639 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003640
3641 if (order > 0)
3642 return 0;
3643
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003644 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003645 /*
3646 * This loop can run a while, specially if mem_cgroup's continuously
3647 * keep exceeding their soft limit and putting the system under
3648 * pressure
3649 */
3650 do {
3651 if (next_mz)
3652 mz = next_mz;
3653 else
3654 mz = mem_cgroup_largest_soft_limit_node(mctz);
3655 if (!mz)
3656 break;
3657
Ying Han0ae5e892011-05-26 16:25:25 -07003658 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003659 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08003660 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003661 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003662 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003663 spin_lock(&mctz->lock);
3664
3665 /*
3666 * If we failed to reclaim anything from this memory cgroup
3667 * it is time to move on to the next cgroup
3668 */
3669 next_mz = NULL;
3670 if (!reclaimed) {
3671 do {
3672 /*
3673 * Loop until we find yet another one.
3674 *
3675 * By the time we get the soft_limit lock
3676 * again, someone might have aded the
3677 * group back on the RB tree. Iterate to
3678 * make sure we get a different mem.
3679 * mem_cgroup_largest_soft_limit_node returns
3680 * NULL if no other cgroup is present on
3681 * the tree
3682 */
3683 next_mz =
3684 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003685 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003686 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003687 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003688 break;
3689 } while (1);
3690 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003691 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
3692 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003693 /*
3694 * One school of thought says that we should not add
3695 * back the node to the tree if reclaim returns 0.
3696 * But our reclaim could return 0, simply because due
3697 * to priority we are exposing a smaller subset of
3698 * memory to reclaim from. Consider this as a longer
3699 * term TODO.
3700 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003701 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003702 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003703 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003704 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003705 loop++;
3706 /*
3707 * Could not reclaim anything and there are no more
3708 * mem cgroups to try or we seem to be looping without
3709 * reclaiming anything.
3710 */
3711 if (!nr_reclaimed &&
3712 (next_mz == NULL ||
3713 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3714 break;
3715 } while (!nr_reclaimed);
3716 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003717 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003718 return nr_reclaimed;
3719}
3720
Michal Hocko2ef37d32012-10-26 13:37:30 +02003721/**
3722 * mem_cgroup_force_empty_list - clears LRU of a group
3723 * @memcg: group to clear
3724 * @node: NUMA node
3725 * @zid: zone id
3726 * @lru: lru to to clear
3727 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003728 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02003729 * reclaim the pages page themselves - pages are moved to the parent (or root)
3730 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003731 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02003732static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003733 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003734{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003735 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003736 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003737 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003738 struct page *busy;
3739 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003740
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003741 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003742 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3743 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003744
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003745 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003746 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08003747 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003748 struct page *page;
3749
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003750 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003751 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003752 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003753 break;
3754 }
Johannes Weiner925b7672012-01-12 17:18:15 -08003755 page = list_entry(list->prev, struct page, lru);
3756 if (busy == page) {
3757 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003758 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003759 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003760 continue;
3761 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003762 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003763
Johannes Weiner925b7672012-01-12 17:18:15 -08003764 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07003765
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003766 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003767 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08003768 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003769 cond_resched();
3770 } else
3771 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003772 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003773}
3774
3775/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003776 * make mem_cgroup's charge to be 0 if there is no task by moving
3777 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003778 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02003779 *
3780 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003781 */
Michal Hockoab5196c2012-10-26 13:37:32 +02003782static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003783{
Michal Hockoc26251f2012-10-26 13:37:28 +02003784 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003785
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003786 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003787 /* This is for making all *used* pages to be on LRU. */
3788 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003789 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003790 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08003791 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003792 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07003793 enum lru_list lru;
3794 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003795 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07003796 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003797 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003798 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003799 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003800 mem_cgroup_end_move(memcg);
3801 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003802 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003803
Michal Hocko2ef37d32012-10-26 13:37:30 +02003804 /*
3805 * This is a safety check because mem_cgroup_force_empty_list
3806 * could have raced with mem_cgroup_replace_page_cache callers
3807 * so the lru seemed empty but the page could have been added
3808 * right after the check. RES_USAGE should be safe as we always
3809 * charge before adding to the LRU.
3810 */
3811 } while (res_counter_read_u64(&memcg->res, RES_USAGE) > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02003812}
3813
3814/*
3815 * Reclaims as many pages from the given memcg as possible and moves
3816 * the rest to the parent.
3817 *
3818 * Caller is responsible for holding css reference for memcg.
3819 */
3820static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3821{
3822 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
3823 struct cgroup *cgrp = memcg->css.cgroup;
3824
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003825 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02003826 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
3827 return -EBUSY;
3828
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003829 /* we call try-to-free pages for make this cgroup empty */
3830 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003831 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07003832 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003833 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003834
Michal Hockoc26251f2012-10-26 13:37:28 +02003835 if (signal_pending(current))
3836 return -EINTR;
3837
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003838 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003839 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003840 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003841 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003842 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003843 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003844 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003845
3846 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003847 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02003848 mem_cgroup_reparent_charges(memcg);
3849
3850 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003851}
3852
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07003853static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003854{
Michal Hockoc26251f2012-10-26 13:37:28 +02003855 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
3856 int ret;
3857
Michal Hockod8423012012-10-26 13:37:29 +02003858 if (mem_cgroup_is_root(memcg))
3859 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02003860 css_get(&memcg->css);
3861 ret = mem_cgroup_force_empty(memcg);
3862 css_put(&memcg->css);
3863
3864 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003865}
3866
3867
Balbir Singh18f59ea2009-01-07 18:08:07 -08003868static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3869{
3870 return mem_cgroup_from_cont(cont)->use_hierarchy;
3871}
3872
3873static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3874 u64 val)
3875{
3876 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003877 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003878 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003879 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003880
3881 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003882 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003883
3884 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07003885
3886 if (memcg->use_hierarchy == val)
3887 goto out;
3888
Balbir Singh18f59ea2009-01-07 18:08:07 -08003889 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003890 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003891 * in the child subtrees. If it is unset, then the change can
3892 * occur, provided the current cgroup has no children.
3893 *
3894 * For the root cgroup, parent_mem is NULL, we allow value to be
3895 * set if there are no children.
3896 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003897 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003898 (val == 1 || val == 0)) {
3899 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003900 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003901 else
3902 retval = -EBUSY;
3903 } else
3904 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003905
3906out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08003907 cgroup_unlock();
3908
3909 return retval;
3910}
3911
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003912
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003913static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003914 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003915{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003916 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003917 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003918
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003919 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003920 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003921 val += mem_cgroup_read_stat(iter, idx);
3922
3923 if (val < 0) /* race ? */
3924 val = 0;
3925 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003926}
3927
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003928static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003929{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003930 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003931
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003932 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003933 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00003934 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003935 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00003936 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003937 }
3938
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003939 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3940 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003941
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003942 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003943 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003944
3945 return val << PAGE_SHIFT;
3946}
3947
Tejun Heoaf36f902012-04-01 12:09:55 -07003948static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
3949 struct file *file, char __user *buf,
3950 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003951{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003952 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07003953 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003954 u64 val;
Tejun Heoaf36f902012-04-01 12:09:55 -07003955 int type, name, len;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003956
3957 type = MEMFILE_TYPE(cft->private);
3958 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07003959
3960 if (!do_swap_account && type == _MEMSWAP)
3961 return -EOPNOTSUPP;
3962
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003963 switch (type) {
3964 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003965 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003966 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003967 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003968 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003969 break;
3970 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003971 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003972 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003973 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003974 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003975 break;
3976 default:
3977 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003978 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003979
3980 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
3981 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003982}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003983/*
3984 * The user of this function is...
3985 * RES_LIMIT.
3986 */
Paul Menage856c13a2008-07-25 01:47:04 -07003987static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3988 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003989{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003990 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003991 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003992 unsigned long long val;
3993 int ret;
3994
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003995 type = MEMFILE_TYPE(cft->private);
3996 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07003997
3998 if (!do_swap_account && type == _MEMSWAP)
3999 return -EOPNOTSUPP;
4000
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004001 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004002 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07004003 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
4004 ret = -EINVAL;
4005 break;
4006 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004007 /* This function does all necessary parse...reuse it */
4008 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004009 if (ret)
4010 break;
4011 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004012 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004013 else
4014 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004015 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004016 case RES_SOFT_LIMIT:
4017 ret = res_counter_memparse_write_strategy(buffer, &val);
4018 if (ret)
4019 break;
4020 /*
4021 * For memsw, soft limits are hard to implement in terms
4022 * of semantics, for now, we support soft limits for
4023 * control without swap
4024 */
4025 if (type == _MEM)
4026 ret = res_counter_set_soft_limit(&memcg->res, val);
4027 else
4028 ret = -EINVAL;
4029 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004030 default:
4031 ret = -EINVAL; /* should be BUG() ? */
4032 break;
4033 }
4034 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004035}
4036
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004037static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4038 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4039{
4040 struct cgroup *cgroup;
4041 unsigned long long min_limit, min_memsw_limit, tmp;
4042
4043 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4044 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4045 cgroup = memcg->css.cgroup;
4046 if (!memcg->use_hierarchy)
4047 goto out;
4048
4049 while (cgroup->parent) {
4050 cgroup = cgroup->parent;
4051 memcg = mem_cgroup_from_cont(cgroup);
4052 if (!memcg->use_hierarchy)
4053 break;
4054 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4055 min_limit = min(min_limit, tmp);
4056 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4057 min_memsw_limit = min(min_memsw_limit, tmp);
4058 }
4059out:
4060 *mem_limit = min_limit;
4061 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004062}
4063
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004064static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004065{
Tejun Heoaf36f902012-04-01 12:09:55 -07004066 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004067 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004068
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004069 type = MEMFILE_TYPE(event);
4070 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07004071
4072 if (!do_swap_account && type == _MEMSWAP)
4073 return -EOPNOTSUPP;
4074
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004075 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004076 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004077 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004078 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004079 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004080 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004081 break;
4082 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004083 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004084 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004085 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004086 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004087 break;
4088 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004089
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004090 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004091}
4092
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004093static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4094 struct cftype *cft)
4095{
4096 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4097}
4098
Daisuke Nishimura02491442010-03-10 15:22:17 -08004099#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004100static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4101 struct cftype *cft, u64 val)
4102{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004103 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004104
4105 if (val >= (1 << NR_MOVE_TYPE))
4106 return -EINVAL;
4107 /*
4108 * We check this value several times in both in can_attach() and
4109 * attach(), so we need cgroup lock to prevent this value from being
4110 * inconsistent.
4111 */
4112 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004113 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004114 cgroup_unlock();
4115
4116 return 0;
4117}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004118#else
4119static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4120 struct cftype *cft, u64 val)
4121{
4122 return -ENOSYS;
4123}
4124#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004125
Ying Han406eb0c2011-05-26 16:25:37 -07004126#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07004127static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07004128 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07004129{
4130 int nid;
4131 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4132 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004133 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07004134
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004135 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004136 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004137 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004138 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004139 seq_printf(m, " N%d=%lu", nid, node_nr);
4140 }
4141 seq_putc(m, '\n');
4142
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004143 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004144 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004145 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004146 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004147 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004148 seq_printf(m, " N%d=%lu", nid, node_nr);
4149 }
4150 seq_putc(m, '\n');
4151
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004152 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004153 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004154 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004155 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004156 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004157 seq_printf(m, " N%d=%lu", nid, node_nr);
4158 }
4159 seq_putc(m, '\n');
4160
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004161 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004162 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004163 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004164 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004165 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004166 seq_printf(m, " N%d=%lu", nid, node_nr);
4167 }
4168 seq_putc(m, '\n');
4169 return 0;
4170}
4171#endif /* CONFIG_NUMA */
4172
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004173static const char * const mem_cgroup_lru_names[] = {
4174 "inactive_anon",
4175 "active_anon",
4176 "inactive_file",
4177 "active_file",
4178 "unevictable",
4179};
4180
4181static inline void mem_cgroup_lru_names_not_uptodate(void)
4182{
4183 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4184}
4185
Wanpeng Liab215882012-07-31 16:43:09 -07004186static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004187 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004188{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004189 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004190 struct mem_cgroup *mi;
4191 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004192
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004193 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004194 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004195 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004196 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4197 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004198 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004199
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004200 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4201 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4202 mem_cgroup_read_events(memcg, i));
4203
4204 for (i = 0; i < NR_LRU_LISTS; i++)
4205 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4206 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4207
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004208 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004209 {
4210 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004211 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004212 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004213 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004214 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4215 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004216 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004217
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004218 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4219 long long val = 0;
4220
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004221 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004222 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004223 for_each_mem_cgroup_tree(mi, memcg)
4224 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4225 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4226 }
4227
4228 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4229 unsigned long long val = 0;
4230
4231 for_each_mem_cgroup_tree(mi, memcg)
4232 val += mem_cgroup_read_events(mi, i);
4233 seq_printf(m, "total_%s %llu\n",
4234 mem_cgroup_events_names[i], val);
4235 }
4236
4237 for (i = 0; i < NR_LRU_LISTS; i++) {
4238 unsigned long long val = 0;
4239
4240 for_each_mem_cgroup_tree(mi, memcg)
4241 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4242 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004243 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004244
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004245#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004246 {
4247 int nid, zid;
4248 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004249 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004250 unsigned long recent_rotated[2] = {0, 0};
4251 unsigned long recent_scanned[2] = {0, 0};
4252
4253 for_each_online_node(nid)
4254 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004255 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07004256 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004257
Hugh Dickins89abfab2012-05-29 15:06:53 -07004258 recent_rotated[0] += rstat->recent_rotated[0];
4259 recent_rotated[1] += rstat->recent_rotated[1];
4260 recent_scanned[0] += rstat->recent_scanned[0];
4261 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004262 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004263 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4264 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4265 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4266 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004267 }
4268#endif
4269
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004270 return 0;
4271}
4272
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004273static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4274{
4275 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4276
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004277 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004278}
4279
4280static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4281 u64 val)
4282{
4283 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4284 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004285
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004286 if (val > 100)
4287 return -EINVAL;
4288
4289 if (cgrp->parent == NULL)
4290 return -EINVAL;
4291
4292 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004293
4294 cgroup_lock();
4295
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004296 /* If under hierarchy, only empty-root can set this value */
4297 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004298 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4299 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004300 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004301 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004302
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004303 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004304
Li Zefan068b38c2009-01-15 13:51:26 -08004305 cgroup_unlock();
4306
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004307 return 0;
4308}
4309
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004310static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4311{
4312 struct mem_cgroup_threshold_ary *t;
4313 u64 usage;
4314 int i;
4315
4316 rcu_read_lock();
4317 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004318 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004319 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004320 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004321
4322 if (!t)
4323 goto unlock;
4324
4325 usage = mem_cgroup_usage(memcg, swap);
4326
4327 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004328 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004329 * If it's not true, a threshold was crossed after last
4330 * call of __mem_cgroup_threshold().
4331 */
Phil Carmody5407a562010-05-26 14:42:42 -07004332 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004333
4334 /*
4335 * Iterate backward over array of thresholds starting from
4336 * current_threshold and check if a threshold is crossed.
4337 * If none of thresholds below usage is crossed, we read
4338 * only one element of the array here.
4339 */
4340 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4341 eventfd_signal(t->entries[i].eventfd, 1);
4342
4343 /* i = current_threshold + 1 */
4344 i++;
4345
4346 /*
4347 * Iterate forward over array of thresholds starting from
4348 * current_threshold+1 and check if a threshold is crossed.
4349 * If none of thresholds above usage is crossed, we read
4350 * only one element of the array here.
4351 */
4352 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4353 eventfd_signal(t->entries[i].eventfd, 1);
4354
4355 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004356 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004357unlock:
4358 rcu_read_unlock();
4359}
4360
4361static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4362{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004363 while (memcg) {
4364 __mem_cgroup_threshold(memcg, false);
4365 if (do_swap_account)
4366 __mem_cgroup_threshold(memcg, true);
4367
4368 memcg = parent_mem_cgroup(memcg);
4369 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004370}
4371
4372static int compare_thresholds(const void *a, const void *b)
4373{
4374 const struct mem_cgroup_threshold *_a = a;
4375 const struct mem_cgroup_threshold *_b = b;
4376
4377 return _a->threshold - _b->threshold;
4378}
4379
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004380static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004381{
4382 struct mem_cgroup_eventfd_list *ev;
4383
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004384 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004385 eventfd_signal(ev->eventfd, 1);
4386 return 0;
4387}
4388
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004389static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004390{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004391 struct mem_cgroup *iter;
4392
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004393 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004394 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004395}
4396
4397static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4398 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004399{
4400 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004401 struct mem_cgroup_thresholds *thresholds;
4402 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004403 int type = MEMFILE_TYPE(cft->private);
4404 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004405 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004406
4407 ret = res_counter_memparse_write_strategy(args, &threshold);
4408 if (ret)
4409 return ret;
4410
4411 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004412
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004413 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004414 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004415 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004416 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004417 else
4418 BUG();
4419
4420 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4421
4422 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004423 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004424 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4425
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004426 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004427
4428 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004429 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004430 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004431 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004432 ret = -ENOMEM;
4433 goto unlock;
4434 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004435 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004436
4437 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004438 if (thresholds->primary) {
4439 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004440 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004441 }
4442
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004443 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004444 new->entries[size - 1].eventfd = eventfd;
4445 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004446
4447 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004448 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004449 compare_thresholds, NULL);
4450
4451 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004452 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004453 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004454 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004455 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004456 * new->current_threshold will not be used until
4457 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004458 * it here.
4459 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004460 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004461 } else
4462 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004463 }
4464
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004465 /* Free old spare buffer and save old primary buffer as spare */
4466 kfree(thresholds->spare);
4467 thresholds->spare = thresholds->primary;
4468
4469 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004470
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004471 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004472 synchronize_rcu();
4473
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004474unlock:
4475 mutex_unlock(&memcg->thresholds_lock);
4476
4477 return ret;
4478}
4479
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004480static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004481 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004482{
4483 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004484 struct mem_cgroup_thresholds *thresholds;
4485 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004486 int type = MEMFILE_TYPE(cft->private);
4487 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004488 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004489
4490 mutex_lock(&memcg->thresholds_lock);
4491 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004492 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004493 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004494 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004495 else
4496 BUG();
4497
Anton Vorontsov371528c2012-02-24 05:14:46 +04004498 if (!thresholds->primary)
4499 goto unlock;
4500
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004501 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4502
4503 /* Check if a threshold crossed before removing */
4504 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4505
4506 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004507 size = 0;
4508 for (i = 0; i < thresholds->primary->size; i++) {
4509 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004510 size++;
4511 }
4512
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004513 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004514
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004515 /* Set thresholds array to NULL if we don't have thresholds */
4516 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004517 kfree(new);
4518 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004519 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004520 }
4521
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004522 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004523
4524 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004525 new->current_threshold = -1;
4526 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4527 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004528 continue;
4529
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004530 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004531 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004532 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004533 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004534 * until rcu_assign_pointer(), so it's safe to increment
4535 * it here.
4536 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004537 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004538 }
4539 j++;
4540 }
4541
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004542swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004543 /* Swap primary and spare array */
4544 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004545 /* If all events are unregistered, free the spare array */
4546 if (!new) {
4547 kfree(thresholds->spare);
4548 thresholds->spare = NULL;
4549 }
4550
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004551 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004552
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004553 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004554 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004555unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004556 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004557}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004558
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004559static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4560 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4561{
4562 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4563 struct mem_cgroup_eventfd_list *event;
4564 int type = MEMFILE_TYPE(cft->private);
4565
4566 BUG_ON(type != _OOM_TYPE);
4567 event = kmalloc(sizeof(*event), GFP_KERNEL);
4568 if (!event)
4569 return -ENOMEM;
4570
Michal Hocko1af8efe2011-07-26 16:08:24 -07004571 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004572
4573 event->eventfd = eventfd;
4574 list_add(&event->list, &memcg->oom_notify);
4575
4576 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004577 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004578 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004579 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004580
4581 return 0;
4582}
4583
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004584static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004585 struct cftype *cft, struct eventfd_ctx *eventfd)
4586{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004587 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004588 struct mem_cgroup_eventfd_list *ev, *tmp;
4589 int type = MEMFILE_TYPE(cft->private);
4590
4591 BUG_ON(type != _OOM_TYPE);
4592
Michal Hocko1af8efe2011-07-26 16:08:24 -07004593 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004594
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004595 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004596 if (ev->eventfd == eventfd) {
4597 list_del(&ev->list);
4598 kfree(ev);
4599 }
4600 }
4601
Michal Hocko1af8efe2011-07-26 16:08:24 -07004602 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004603}
4604
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004605static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4606 struct cftype *cft, struct cgroup_map_cb *cb)
4607{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004608 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004609
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004610 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004611
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004612 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004613 cb->fill(cb, "under_oom", 1);
4614 else
4615 cb->fill(cb, "under_oom", 0);
4616 return 0;
4617}
4618
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004619static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4620 struct cftype *cft, u64 val)
4621{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004622 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004623 struct mem_cgroup *parent;
4624
4625 /* cannot set to root cgroup and only 0 and 1 are allowed */
4626 if (!cgrp->parent || !((val == 0) || (val == 1)))
4627 return -EINVAL;
4628
4629 parent = mem_cgroup_from_cont(cgrp->parent);
4630
4631 cgroup_lock();
4632 /* oom-kill-disable is a flag for subhierarchy. */
4633 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004634 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004635 cgroup_unlock();
4636 return -EINVAL;
4637 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004638 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004639 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004640 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004641 cgroup_unlock();
4642 return 0;
4643}
4644
Andrew Mortonc255a452012-07-31 16:43:02 -07004645#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03004646static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004647{
Glauber Costa1d62e432012-04-09 19:36:33 -03004648 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00004649};
4650
Glauber Costa1d62e432012-04-09 19:36:33 -03004651static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004652{
Glauber Costa1d62e432012-04-09 19:36:33 -03004653 mem_cgroup_sockets_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004654}
Glauber Costae5671df2011-12-11 21:47:01 +00004655#else
Glauber Costacbe128e32012-04-09 19:36:34 -03004656static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004657{
4658 return 0;
4659}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004660
Glauber Costa1d62e432012-04-09 19:36:33 -03004661static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004662{
4663}
Glauber Costae5671df2011-12-11 21:47:01 +00004664#endif
4665
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004666static struct cftype mem_cgroup_files[] = {
4667 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004668 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004669 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07004670 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004671 .register_event = mem_cgroup_usage_register_event,
4672 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004673 },
4674 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004675 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004676 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004677 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004678 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004679 },
4680 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004681 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004682 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004683 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004684 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004685 },
4686 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004687 .name = "soft_limit_in_bytes",
4688 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4689 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004690 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07004691 },
4692 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004693 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004694 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004695 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004696 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004697 },
Balbir Singh8697d332008-02-07 00:13:59 -08004698 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004699 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07004700 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004701 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004702 {
4703 .name = "force_empty",
4704 .trigger = mem_cgroup_force_empty_write,
4705 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004706 {
4707 .name = "use_hierarchy",
4708 .write_u64 = mem_cgroup_hierarchy_write,
4709 .read_u64 = mem_cgroup_hierarchy_read,
4710 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004711 {
4712 .name = "swappiness",
4713 .read_u64 = mem_cgroup_swappiness_read,
4714 .write_u64 = mem_cgroup_swappiness_write,
4715 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004716 {
4717 .name = "move_charge_at_immigrate",
4718 .read_u64 = mem_cgroup_move_charge_read,
4719 .write_u64 = mem_cgroup_move_charge_write,
4720 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004721 {
4722 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004723 .read_map = mem_cgroup_oom_control_read,
4724 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004725 .register_event = mem_cgroup_oom_register_event,
4726 .unregister_event = mem_cgroup_oom_unregister_event,
4727 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4728 },
Ying Han406eb0c2011-05-26 16:25:37 -07004729#ifdef CONFIG_NUMA
4730 {
4731 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07004732 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004733 },
4734#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07004735#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004736 {
4737 .name = "memsw.usage_in_bytes",
4738 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07004739 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004740 .register_event = mem_cgroup_usage_register_event,
4741 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004742 },
4743 {
4744 .name = "memsw.max_usage_in_bytes",
4745 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4746 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004747 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004748 },
4749 {
4750 .name = "memsw.limit_in_bytes",
4751 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4752 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004753 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004754 },
4755 {
4756 .name = "memsw.failcnt",
4757 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4758 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004759 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004760 },
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004761#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07004762 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004763};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004764
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004765static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004766{
4767 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004768 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004769 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004770 /*
4771 * This routine is called against possible nodes.
4772 * But it's BUG to call kmalloc() against offline node.
4773 *
4774 * TODO: this routine can waste much memory for nodes which will
4775 * never be onlined. It's better to use memory hotplug callback
4776 * function.
4777 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004778 if (!node_state(node, N_NORMAL_MEMORY))
4779 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004780 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004781 if (!pn)
4782 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004783
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004784 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4785 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004786 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004787 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004788 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004789 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004790 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004791 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004792 return 0;
4793}
4794
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004795static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004796{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004797 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004798}
4799
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004800static struct mem_cgroup *mem_cgroup_alloc(void)
4801{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004802 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004803 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004804
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004805 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004806 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004807 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004808 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004809 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004810
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004811 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004812 return NULL;
4813
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004814 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4815 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004816 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004817 spin_lock_init(&memcg->pcp_counter_lock);
4818 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004819
4820out_free:
4821 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004822 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004823 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004824 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004825 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004826}
4827
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004828/*
Glauber Costa3afe36b2012-05-29 15:07:10 -07004829 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
Hugh Dickins59927fb2012-03-15 15:17:07 -07004830 * but in process context. The work_freeing structure is overlaid
4831 * on the rcu_freeing structure, which itself is overlaid on memsw.
4832 */
Glauber Costa3afe36b2012-05-29 15:07:10 -07004833static void free_work(struct work_struct *work)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004834{
4835 struct mem_cgroup *memcg;
Glauber Costa3afe36b2012-05-29 15:07:10 -07004836 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004837
4838 memcg = container_of(work, struct mem_cgroup, work_freeing);
Glauber Costa3f134612012-05-29 15:07:11 -07004839 /*
4840 * We need to make sure that (at least for now), the jump label
4841 * destruction code runs outside of the cgroup lock. This is because
4842 * get_online_cpus(), which is called from the static_branch update,
4843 * can't be called inside the cgroup_lock. cpusets are the ones
4844 * enforcing this dependency, so if they ever change, we might as well.
4845 *
4846 * schedule_work() will guarantee this happens. Be careful if you need
4847 * to move this code around, and make sure it is outside
4848 * the cgroup_lock.
4849 */
4850 disarm_sock_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07004851 if (size < PAGE_SIZE)
4852 kfree(memcg);
4853 else
4854 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004855}
Glauber Costa3afe36b2012-05-29 15:07:10 -07004856
4857static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004858{
4859 struct mem_cgroup *memcg;
4860
4861 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07004862 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004863 schedule_work(&memcg->work_freeing);
4864}
4865
4866/*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004867 * At destroying mem_cgroup, references from swap_cgroup can remain.
4868 * (scanning all at force_empty is too costly...)
4869 *
4870 * Instead of clearing all references at force_empty, we remember
4871 * the number of reference from swap_cgroup and free mem_cgroup when
4872 * it goes down to 0.
4873 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004874 * Removal of cgroup itself succeeds regardless of refs from swap.
4875 */
4876
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004877static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004878{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004879 int node;
4880
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004881 mem_cgroup_remove_from_trees(memcg);
4882 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004883
Bob Liu3ed28fa2012-01-12 17:19:04 -08004884 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004885 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004886
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004887 free_percpu(memcg->stat);
Glauber Costa3afe36b2012-05-29 15:07:10 -07004888 call_rcu(&memcg->rcu_freeing, free_rcu);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004889}
4890
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004891static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004892{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004893 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004894}
4895
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004896static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004897{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004898 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4899 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4900 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004901 if (parent)
4902 mem_cgroup_put(parent);
4903 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004904}
4905
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004906static void mem_cgroup_put(struct mem_cgroup *memcg)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004907{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004908 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004909}
4910
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004911/*
4912 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4913 */
Glauber Costae1aab162011-12-11 21:47:03 +00004914struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004915{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004916 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004917 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004918 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004919}
Glauber Costae1aab162011-12-11 21:47:03 +00004920EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004921
Andrew Mortonc255a452012-07-31 16:43:02 -07004922#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004923static void __init enable_swap_cgroup(void)
4924{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004925 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004926 do_swap_account = 1;
4927}
4928#else
4929static void __init enable_swap_cgroup(void)
4930{
4931}
4932#endif
4933
Balbir Singhf64c3f52009-09-23 15:56:37 -07004934static int mem_cgroup_soft_limit_tree_init(void)
4935{
4936 struct mem_cgroup_tree_per_node *rtpn;
4937 struct mem_cgroup_tree_per_zone *rtpz;
4938 int tmp, node, zone;
4939
Bob Liu3ed28fa2012-01-12 17:19:04 -08004940 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07004941 tmp = node;
4942 if (!node_state(node, N_NORMAL_MEMORY))
4943 tmp = -1;
4944 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4945 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08004946 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004947
4948 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4949
4950 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4951 rtpz = &rtpn->rb_tree_per_zone[zone];
4952 rtpz->rb_root = RB_ROOT;
4953 spin_lock_init(&rtpz->lock);
4954 }
4955 }
4956 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08004957
4958err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08004959 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08004960 if (!soft_limit_tree.rb_tree_per_node[node])
4961 break;
4962 kfree(soft_limit_tree.rb_tree_per_node[node]);
4963 soft_limit_tree.rb_tree_per_node[node] = NULL;
4964 }
4965 return 1;
4966
Balbir Singhf64c3f52009-09-23 15:56:37 -07004967}
4968
Li Zefan0eb253e2009-01-15 13:51:25 -08004969static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08004970mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004971{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004972 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004973 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004974 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004975
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004976 memcg = mem_cgroup_alloc();
4977 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004978 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004979
Bob Liu3ed28fa2012-01-12 17:19:04 -08004980 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004981 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004982 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004983
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004984 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004985 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004986 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004987 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004988 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004989 if (mem_cgroup_soft_limit_tree_init())
4990 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08004991 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004992 for_each_possible_cpu(cpu) {
4993 struct memcg_stock_pcp *stock =
4994 &per_cpu(memcg_stock, cpu);
4995 INIT_WORK(&stock->work, drain_local_stock);
4996 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004997 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004998 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004999 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005000 memcg->use_hierarchy = parent->use_hierarchy;
5001 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005002 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08005003
Balbir Singh18f59ea2009-01-07 18:08:07 -08005004 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005005 res_counter_init(&memcg->res, &parent->res);
5006 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08005007 /*
5008 * We increment refcnt of the parent to ensure that we can
5009 * safely access it on res_counter_charge/uncharge.
5010 * This refcnt will be decremented when freeing this
5011 * mem_cgroup(see mem_cgroup_put).
5012 */
5013 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005014 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005015 res_counter_init(&memcg->res, NULL);
5016 res_counter_init(&memcg->memsw, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005017 /*
5018 * Deeper hierachy with use_hierarchy == false doesn't make
5019 * much sense so let cgroup subsystem know about this
5020 * unfortunate state in our controller.
5021 */
5022 if (parent && parent != root_mem_cgroup)
5023 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005024 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005025 memcg->last_scanned_node = MAX_NUMNODES;
5026 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005027
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005028 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005029 memcg->swappiness = mem_cgroup_swappiness(parent);
5030 atomic_set(&memcg->refcnt, 1);
5031 memcg->move_charge_at_immigrate = 0;
5032 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07005033 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03005034
5035 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
5036 if (error) {
5037 /*
5038 * We call put now because our (and parent's) refcnts
5039 * are already in place. mem_cgroup_put() will internally
5040 * call __mem_cgroup_free, so return directly
5041 */
5042 mem_cgroup_put(memcg);
5043 return ERR_PTR(error);
5044 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005045 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005046free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005047 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005048 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005049}
5050
Tejun Heo92fb9742012-11-19 08:13:38 -08005051static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005052{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005053 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005054
Michal Hockoab5196c2012-10-26 13:37:32 +02005055 mem_cgroup_reparent_charges(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005056}
5057
Tejun Heo92fb9742012-11-19 08:13:38 -08005058static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005059{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005060 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005061
Glauber Costa1d62e432012-04-09 19:36:33 -03005062 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005063
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005064 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005065}
5066
Daisuke Nishimura02491442010-03-10 15:22:17 -08005067#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005068/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005069#define PRECHARGE_COUNT_AT_ONCE 256
5070static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005071{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005072 int ret = 0;
5073 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005074 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005075
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005076 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005077 mc.precharge += count;
5078 /* we don't need css_get for root */
5079 return ret;
5080 }
5081 /* try to charge at once */
5082 if (count > 1) {
5083 struct res_counter *dummy;
5084 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005085 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005086 * by cgroup_lock_live_cgroup() that it is not removed and we
5087 * are still under the same cgroup_mutex. So we can postpone
5088 * css_get().
5089 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005090 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005091 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005092 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005093 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005094 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005095 goto one_by_one;
5096 }
5097 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005098 return ret;
5099 }
5100one_by_one:
5101 /* fall back to one by one charge */
5102 while (count--) {
5103 if (signal_pending(current)) {
5104 ret = -EINTR;
5105 break;
5106 }
5107 if (!batch_count--) {
5108 batch_count = PRECHARGE_COUNT_AT_ONCE;
5109 cond_resched();
5110 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005111 ret = __mem_cgroup_try_charge(NULL,
5112 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005113 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005114 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005115 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005116 mc.precharge++;
5117 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005118 return ret;
5119}
5120
5121/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005122 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005123 * @vma: the vma the pte to be checked belongs
5124 * @addr: the address corresponding to the pte to be checked
5125 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005126 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005127 *
5128 * Returns
5129 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5130 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5131 * move charge. if @target is not NULL, the page is stored in target->page
5132 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005133 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5134 * target for charge migration. if @target is not NULL, the entry is stored
5135 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005136 *
5137 * Called with pte lock held.
5138 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005139union mc_target {
5140 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005141 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005142};
5143
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005144enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005145 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005146 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005147 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005148};
5149
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005150static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5151 unsigned long addr, pte_t ptent)
5152{
5153 struct page *page = vm_normal_page(vma, addr, ptent);
5154
5155 if (!page || !page_mapped(page))
5156 return NULL;
5157 if (PageAnon(page)) {
5158 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005159 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005160 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005161 } else if (!move_file())
5162 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005163 return NULL;
5164 if (!get_page_unless_zero(page))
5165 return NULL;
5166
5167 return page;
5168}
5169
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005170#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005171static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5172 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5173{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005174 struct page *page = NULL;
5175 swp_entry_t ent = pte_to_swp_entry(ptent);
5176
5177 if (!move_anon() || non_swap_entry(ent))
5178 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005179 /*
5180 * Because lookup_swap_cache() updates some statistics counter,
5181 * we call find_get_page() with swapper_space directly.
5182 */
5183 page = find_get_page(&swapper_space, ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005184 if (do_swap_account)
5185 entry->val = ent.val;
5186
5187 return page;
5188}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005189#else
5190static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5191 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5192{
5193 return NULL;
5194}
5195#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005196
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005197static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5198 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5199{
5200 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005201 struct address_space *mapping;
5202 pgoff_t pgoff;
5203
5204 if (!vma->vm_file) /* anonymous vma */
5205 return NULL;
5206 if (!move_file())
5207 return NULL;
5208
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005209 mapping = vma->vm_file->f_mapping;
5210 if (pte_none(ptent))
5211 pgoff = linear_page_index(vma, addr);
5212 else /* pte_file(ptent) is true */
5213 pgoff = pte_to_pgoff(ptent);
5214
5215 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005216 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005217
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005218#ifdef CONFIG_SWAP
5219 /* shmem/tmpfs may report page out on swap: account for that too. */
5220 if (radix_tree_exceptional_entry(page)) {
5221 swp_entry_t swap = radix_to_swp_entry(page);
5222 if (do_swap_account)
5223 *entry = swap;
5224 page = find_get_page(&swapper_space, swap.val);
5225 }
5226#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005227 return page;
5228}
5229
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005230static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005231 unsigned long addr, pte_t ptent, union mc_target *target)
5232{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005233 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005234 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005235 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005236 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005237
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005238 if (pte_present(ptent))
5239 page = mc_handle_present_pte(vma, addr, ptent);
5240 else if (is_swap_pte(ptent))
5241 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005242 else if (pte_none(ptent) || pte_file(ptent))
5243 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005244
5245 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005246 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005247 if (page) {
5248 pc = lookup_page_cgroup(page);
5249 /*
5250 * Do only loose check w/o page_cgroup lock.
5251 * mem_cgroup_move_account() checks the pc is valid or not under
5252 * the lock.
5253 */
5254 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5255 ret = MC_TARGET_PAGE;
5256 if (target)
5257 target->page = page;
5258 }
5259 if (!ret || !target)
5260 put_page(page);
5261 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005262 /* There is a swap entry and a page doesn't exist or isn't charged */
5263 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08005264 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005265 ret = MC_TARGET_SWAP;
5266 if (target)
5267 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005268 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005269 return ret;
5270}
5271
Naoya Horiguchi12724852012-03-21 16:34:28 -07005272#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5273/*
5274 * We don't consider swapping or file mapped pages because THP does not
5275 * support them for now.
5276 * Caller should make sure that pmd_trans_huge(pmd) is true.
5277 */
5278static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5279 unsigned long addr, pmd_t pmd, union mc_target *target)
5280{
5281 struct page *page = NULL;
5282 struct page_cgroup *pc;
5283 enum mc_target_type ret = MC_TARGET_NONE;
5284
5285 page = pmd_page(pmd);
5286 VM_BUG_ON(!page || !PageHead(page));
5287 if (!move_anon())
5288 return ret;
5289 pc = lookup_page_cgroup(page);
5290 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5291 ret = MC_TARGET_PAGE;
5292 if (target) {
5293 get_page(page);
5294 target->page = page;
5295 }
5296 }
5297 return ret;
5298}
5299#else
5300static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5301 unsigned long addr, pmd_t pmd, union mc_target *target)
5302{
5303 return MC_TARGET_NONE;
5304}
5305#endif
5306
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005307static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5308 unsigned long addr, unsigned long end,
5309 struct mm_walk *walk)
5310{
5311 struct vm_area_struct *vma = walk->private;
5312 pte_t *pte;
5313 spinlock_t *ptl;
5314
Naoya Horiguchi12724852012-03-21 16:34:28 -07005315 if (pmd_trans_huge_lock(pmd, vma) == 1) {
5316 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5317 mc.precharge += HPAGE_PMD_NR;
5318 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005319 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005320 }
Dave Hansen03319322011-03-22 16:32:56 -07005321
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005322 if (pmd_trans_unstable(pmd))
5323 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005324 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5325 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005326 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005327 mc.precharge++; /* increment precharge temporarily */
5328 pte_unmap_unlock(pte - 1, ptl);
5329 cond_resched();
5330
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005331 return 0;
5332}
5333
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005334static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5335{
5336 unsigned long precharge;
5337 struct vm_area_struct *vma;
5338
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005339 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005340 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5341 struct mm_walk mem_cgroup_count_precharge_walk = {
5342 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5343 .mm = mm,
5344 .private = vma,
5345 };
5346 if (is_vm_hugetlb_page(vma))
5347 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005348 walk_page_range(vma->vm_start, vma->vm_end,
5349 &mem_cgroup_count_precharge_walk);
5350 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005351 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005352
5353 precharge = mc.precharge;
5354 mc.precharge = 0;
5355
5356 return precharge;
5357}
5358
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005359static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5360{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005361 unsigned long precharge = mem_cgroup_count_precharge(mm);
5362
5363 VM_BUG_ON(mc.moving_task);
5364 mc.moving_task = current;
5365 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005366}
5367
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005368/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5369static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005370{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005371 struct mem_cgroup *from = mc.from;
5372 struct mem_cgroup *to = mc.to;
5373
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005374 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005375 if (mc.precharge) {
5376 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5377 mc.precharge = 0;
5378 }
5379 /*
5380 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5381 * we must uncharge here.
5382 */
5383 if (mc.moved_charge) {
5384 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5385 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005386 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005387 /* we must fixup refcnts and charges */
5388 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005389 /* uncharge swap account from the old cgroup */
5390 if (!mem_cgroup_is_root(mc.from))
5391 res_counter_uncharge(&mc.from->memsw,
5392 PAGE_SIZE * mc.moved_swap);
5393 __mem_cgroup_put(mc.from, mc.moved_swap);
5394
5395 if (!mem_cgroup_is_root(mc.to)) {
5396 /*
5397 * we charged both to->res and to->memsw, so we should
5398 * uncharge to->res.
5399 */
5400 res_counter_uncharge(&mc.to->res,
5401 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005402 }
5403 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005404 mc.moved_swap = 0;
5405 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005406 memcg_oom_recover(from);
5407 memcg_oom_recover(to);
5408 wake_up_all(&mc.waitq);
5409}
5410
5411static void mem_cgroup_clear_mc(void)
5412{
5413 struct mem_cgroup *from = mc.from;
5414
5415 /*
5416 * we must clear moving_task before waking up waiters at the end of
5417 * task migration.
5418 */
5419 mc.moving_task = NULL;
5420 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005421 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005422 mc.from = NULL;
5423 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005424 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005425 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005426}
5427
Li Zefan761b3ef2012-01-31 13:47:36 +08005428static int mem_cgroup_can_attach(struct cgroup *cgroup,
5429 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005430{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005431 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005432 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005433 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005434
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005435 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005436 struct mm_struct *mm;
5437 struct mem_cgroup *from = mem_cgroup_from_task(p);
5438
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005439 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005440
5441 mm = get_task_mm(p);
5442 if (!mm)
5443 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005444 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005445 if (mm->owner == p) {
5446 VM_BUG_ON(mc.from);
5447 VM_BUG_ON(mc.to);
5448 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005449 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005450 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005451 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005452 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005453 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005454 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005455 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005456 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005457
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005458 ret = mem_cgroup_precharge_mc(mm);
5459 if (ret)
5460 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005461 }
5462 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005463 }
5464 return ret;
5465}
5466
Li Zefan761b3ef2012-01-31 13:47:36 +08005467static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5468 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005469{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005470 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005471}
5472
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005473static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5474 unsigned long addr, unsigned long end,
5475 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005476{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005477 int ret = 0;
5478 struct vm_area_struct *vma = walk->private;
5479 pte_t *pte;
5480 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005481 enum mc_target_type target_type;
5482 union mc_target target;
5483 struct page *page;
5484 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005485
Naoya Horiguchi12724852012-03-21 16:34:28 -07005486 /*
5487 * We don't take compound_lock() here but no race with splitting thp
5488 * happens because:
5489 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5490 * under splitting, which means there's no concurrent thp split,
5491 * - if another thread runs into split_huge_page() just after we
5492 * entered this if-block, the thread must wait for page table lock
5493 * to be unlocked in __split_huge_page_splitting(), where the main
5494 * part of thp split is not executed yet.
5495 */
5496 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005497 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005498 spin_unlock(&vma->vm_mm->page_table_lock);
5499 return 0;
5500 }
5501 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5502 if (target_type == MC_TARGET_PAGE) {
5503 page = target.page;
5504 if (!isolate_lru_page(page)) {
5505 pc = lookup_page_cgroup(page);
5506 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005507 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005508 mc.precharge -= HPAGE_PMD_NR;
5509 mc.moved_charge += HPAGE_PMD_NR;
5510 }
5511 putback_lru_page(page);
5512 }
5513 put_page(page);
5514 }
5515 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005516 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005517 }
5518
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005519 if (pmd_trans_unstable(pmd))
5520 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005521retry:
5522 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5523 for (; addr != end; addr += PAGE_SIZE) {
5524 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005525 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005526
5527 if (!mc.precharge)
5528 break;
5529
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005530 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005531 case MC_TARGET_PAGE:
5532 page = target.page;
5533 if (isolate_lru_page(page))
5534 goto put;
5535 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005536 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005537 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005538 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005539 /* we uncharge from mc.from later. */
5540 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005541 }
5542 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005543put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005544 put_page(page);
5545 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005546 case MC_TARGET_SWAP:
5547 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005548 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005549 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005550 /* we fixup refcnts and charges later. */
5551 mc.moved_swap++;
5552 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005553 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005554 default:
5555 break;
5556 }
5557 }
5558 pte_unmap_unlock(pte - 1, ptl);
5559 cond_resched();
5560
5561 if (addr != end) {
5562 /*
5563 * We have consumed all precharges we got in can_attach().
5564 * We try charge one by one, but don't do any additional
5565 * charges to mc.to if we have failed in charge once in attach()
5566 * phase.
5567 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005568 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005569 if (!ret)
5570 goto retry;
5571 }
5572
5573 return ret;
5574}
5575
5576static void mem_cgroup_move_charge(struct mm_struct *mm)
5577{
5578 struct vm_area_struct *vma;
5579
5580 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005581retry:
5582 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5583 /*
5584 * Someone who are holding the mmap_sem might be waiting in
5585 * waitq. So we cancel all extra charges, wake up all waiters,
5586 * and retry. Because we cancel precharges, we might not be able
5587 * to move enough charges, but moving charge is a best-effort
5588 * feature anyway, so it wouldn't be a big problem.
5589 */
5590 __mem_cgroup_clear_mc();
5591 cond_resched();
5592 goto retry;
5593 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005594 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5595 int ret;
5596 struct mm_walk mem_cgroup_move_charge_walk = {
5597 .pmd_entry = mem_cgroup_move_charge_pte_range,
5598 .mm = mm,
5599 .private = vma,
5600 };
5601 if (is_vm_hugetlb_page(vma))
5602 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005603 ret = walk_page_range(vma->vm_start, vma->vm_end,
5604 &mem_cgroup_move_charge_walk);
5605 if (ret)
5606 /*
5607 * means we have consumed all precharges and failed in
5608 * doing additional charge. Just abandon here.
5609 */
5610 break;
5611 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005612 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005613}
5614
Li Zefan761b3ef2012-01-31 13:47:36 +08005615static void mem_cgroup_move_task(struct cgroup *cont,
5616 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005617{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005618 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005619 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005620
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005621 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005622 if (mc.to)
5623 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005624 mmput(mm);
5625 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005626 if (mc.to)
5627 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005628}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005629#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08005630static int mem_cgroup_can_attach(struct cgroup *cgroup,
5631 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005632{
5633 return 0;
5634}
Li Zefan761b3ef2012-01-31 13:47:36 +08005635static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5636 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005637{
5638}
Li Zefan761b3ef2012-01-31 13:47:36 +08005639static void mem_cgroup_move_task(struct cgroup *cont,
5640 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005641{
5642}
5643#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005644
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005645struct cgroup_subsys mem_cgroup_subsys = {
5646 .name = "memory",
5647 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08005648 .css_alloc = mem_cgroup_css_alloc,
5649 .css_offline = mem_cgroup_css_offline,
5650 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005651 .can_attach = mem_cgroup_can_attach,
5652 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005653 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07005654 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005655 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005656 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005657};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005658
Andrew Mortonc255a452012-07-31 16:43:02 -07005659#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005660static int __init enable_swap_account(char *s)
5661{
5662 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005663 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005664 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005665 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005666 really_do_swap_account = 0;
5667 return 1;
5668}
Michal Hockoa2c89902011-05-24 17:12:50 -07005669__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005670
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005671#endif