blob: 7f0e3571df7eef2ee6f132ab3ca62a6a1c1ba6a9 [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
1024 if (!mm)
1025 return;
1026
1027 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001028 memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
1029 if (unlikely(!memcg))
Ying Han456f9982011-05-26 16:25:38 -07001030 goto out;
1031
1032 switch (idx) {
Ying Han456f9982011-05-26 16:25:38 -07001033 case PGFAULT:
Johannes Weiner0e574a92012-01-12 17:18:35 -08001034 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGFAULT]);
1035 break;
1036 case PGMAJFAULT:
1037 this_cpu_inc(memcg->stat->events[MEM_CGROUP_EVENTS_PGMAJFAULT]);
Ying Han456f9982011-05-26 16:25:38 -07001038 break;
1039 default:
1040 BUG();
1041 }
1042out:
1043 rcu_read_unlock();
1044}
David Rientjes68ae5642012-12-12 13:51:57 -08001045EXPORT_SYMBOL(__mem_cgroup_count_vm_event);
Ying Han456f9982011-05-26 16:25:38 -07001046
Johannes Weiner925b7672012-01-12 17:18:15 -08001047/**
1048 * mem_cgroup_zone_lruvec - get the lru list vector for a zone and memcg
1049 * @zone: zone of the wanted lruvec
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001050 * @memcg: memcg of the wanted lruvec
Johannes Weiner925b7672012-01-12 17:18:15 -08001051 *
1052 * Returns the lru list vector holding pages for the given @zone and
1053 * @mem. This can be the global zone lruvec, if the memory controller
1054 * is disabled.
1055 */
1056struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
1057 struct mem_cgroup *memcg)
1058{
1059 struct mem_cgroup_per_zone *mz;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001060 struct lruvec *lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001061
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001062 if (mem_cgroup_disabled()) {
1063 lruvec = &zone->lruvec;
1064 goto out;
1065 }
Johannes Weiner925b7672012-01-12 17:18:15 -08001066
1067 mz = mem_cgroup_zoneinfo(memcg, zone_to_nid(zone), zone_idx(zone));
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001068 lruvec = &mz->lruvec;
1069out:
1070 /*
1071 * Since a node can be onlined after the mem_cgroup was created,
1072 * we have to be prepared to initialize lruvec->zone here;
1073 * and if offlined then reonlined, we need to reinitialize it.
1074 */
1075 if (unlikely(lruvec->zone != zone))
1076 lruvec->zone = zone;
1077 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001078}
1079
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001080/*
1081 * Following LRU functions are allowed to be used without PCG_LOCK.
1082 * Operations are called by routine of global LRU independently from memcg.
1083 * What we have to take care of here is validness of pc->mem_cgroup.
1084 *
1085 * Changes to pc->mem_cgroup happens when
1086 * 1. charge
1087 * 2. moving account
1088 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
1089 * It is added to LRU before charge.
1090 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
1091 * When moving account, the page is not on LRU. It's isolated.
1092 */
1093
Johannes Weiner925b7672012-01-12 17:18:15 -08001094/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001095 * mem_cgroup_page_lruvec - return lruvec for adding an lru page
Johannes Weiner925b7672012-01-12 17:18:15 -08001096 * @page: the page
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001097 * @zone: zone of the page
Minchan Kim3f58a822011-03-22 16:32:53 -07001098 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001099struct lruvec *mem_cgroup_page_lruvec(struct page *page, struct zone *zone)
Minchan Kim3f58a822011-03-22 16:32:53 -07001100{
1101 struct mem_cgroup_per_zone *mz;
Johannes Weiner925b7672012-01-12 17:18:15 -08001102 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001103 struct page_cgroup *pc;
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001104 struct lruvec *lruvec;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001105
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001106 if (mem_cgroup_disabled()) {
1107 lruvec = &zone->lruvec;
1108 goto out;
1109 }
Christoph Lameterb69408e2008-10-18 20:26:14 -07001110
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001111 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08001112 memcg = pc->mem_cgroup;
Hugh Dickins75121022012-03-05 14:59:18 -08001113
1114 /*
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001115 * Surreptitiously switch any uncharged offlist page to root:
Hugh Dickins75121022012-03-05 14:59:18 -08001116 * an uncharged page off lru does nothing to secure
1117 * its former mem_cgroup from sudden removal.
1118 *
1119 * Our caller holds lru_lock, and PageCgroupUsed is updated
1120 * under page_cgroup lock: between them, they make all uses
1121 * of pc->mem_cgroup safe.
1122 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001123 if (!PageLRU(page) && !PageCgroupUsed(pc) && memcg != root_mem_cgroup)
Hugh Dickins75121022012-03-05 14:59:18 -08001124 pc->mem_cgroup = memcg = root_mem_cgroup;
1125
Johannes Weiner925b7672012-01-12 17:18:15 -08001126 mz = page_cgroup_zoneinfo(memcg, page);
Hugh Dickinsbea8c152012-11-16 14:14:54 -08001127 lruvec = &mz->lruvec;
1128out:
1129 /*
1130 * Since a node can be onlined after the mem_cgroup was created,
1131 * we have to be prepared to initialize lruvec->zone here;
1132 * and if offlined then reonlined, we need to reinitialize it.
1133 */
1134 if (unlikely(lruvec->zone != zone))
1135 lruvec->zone = zone;
1136 return lruvec;
Johannes Weiner925b7672012-01-12 17:18:15 -08001137}
1138
1139/**
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001140 * mem_cgroup_update_lru_size - account for adding or removing an lru page
1141 * @lruvec: mem_cgroup per zone lru vector
1142 * @lru: index of lru list the page is sitting on
1143 * @nr_pages: positive when adding or negative when removing
Johannes Weiner925b7672012-01-12 17:18:15 -08001144 *
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001145 * This function must be called when a page is added to or removed from an
1146 * lru list.
Johannes Weiner925b7672012-01-12 17:18:15 -08001147 */
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001148void mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
1149 int nr_pages)
Johannes Weiner925b7672012-01-12 17:18:15 -08001150{
1151 struct mem_cgroup_per_zone *mz;
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001152 unsigned long *lru_size;
Johannes Weiner925b7672012-01-12 17:18:15 -08001153
1154 if (mem_cgroup_disabled())
1155 return;
1156
Hugh Dickinsfa9add62012-05-29 15:07:09 -07001157 mz = container_of(lruvec, struct mem_cgroup_per_zone, lruvec);
1158 lru_size = mz->lru_size + lru;
1159 *lru_size += nr_pages;
1160 VM_BUG_ON((long)(*lru_size) < 0);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001161}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001162
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001163/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001164 * Checks whether given mem is same or in the root_mem_cgroup's
Michal Hocko3e920412011-07-26 16:08:29 -07001165 * hierarchy subtree
1166 */
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001167bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1168 struct mem_cgroup *memcg)
Michal Hocko3e920412011-07-26 16:08:29 -07001169{
Johannes Weiner91c637342012-05-29 15:06:24 -07001170 if (root_memcg == memcg)
1171 return true;
Hugh Dickins3a981f42012-06-20 12:52:58 -07001172 if (!root_memcg->use_hierarchy || !memcg)
Johannes Weiner91c637342012-05-29 15:06:24 -07001173 return false;
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001174 return css_is_ancestor(&memcg->css, &root_memcg->css);
1175}
1176
1177static bool mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
1178 struct mem_cgroup *memcg)
1179{
1180 bool ret;
1181
Johannes Weiner91c637342012-05-29 15:06:24 -07001182 rcu_read_lock();
Johannes Weinerc3ac9a82012-05-29 15:06:25 -07001183 ret = __mem_cgroup_same_or_subtree(root_memcg, memcg);
Johannes Weiner91c637342012-05-29 15:06:24 -07001184 rcu_read_unlock();
1185 return ret;
Michal Hocko3e920412011-07-26 16:08:29 -07001186}
1187
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001188int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg)
David Rientjes4c4a2212008-02-07 00:14:06 -08001189{
1190 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001191 struct mem_cgroup *curr = NULL;
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001192 struct task_struct *p;
David Rientjes4c4a2212008-02-07 00:14:06 -08001193
KAMEZAWA Hiroyuki158e0a22010-08-10 18:03:00 -07001194 p = find_lock_task_mm(task);
David Rientjesde077d22012-01-12 17:18:52 -08001195 if (p) {
1196 curr = try_get_mem_cgroup_from_mm(p->mm);
1197 task_unlock(p);
1198 } else {
1199 /*
1200 * All threads may have already detached their mm's, but the oom
1201 * killer still needs to detect if they have already been oom
1202 * killed to prevent needlessly killing additional tasks.
1203 */
1204 task_lock(task);
1205 curr = mem_cgroup_from_task(task);
1206 if (curr)
1207 css_get(&curr->css);
1208 task_unlock(task);
1209 }
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001210 if (!curr)
1211 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001212 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001213 * We should check use_hierarchy of "memcg" not "curr". Because checking
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001214 * use_hierarchy of "curr" here make this function true if hierarchy is
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001215 * enabled in "curr" and "curr" is a child of "memcg" in *cgroup*
1216 * hierarchy(even if use_hierarchy is disabled in "memcg").
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001217 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001218 ret = mem_cgroup_same_or_subtree(memcg, curr);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001219 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -08001220 return ret;
1221}
1222
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001223int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001224{
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001225 unsigned long inactive_ratio;
Johannes Weiner9b272972011-11-02 13:38:23 -07001226 unsigned long inactive;
1227 unsigned long active;
1228 unsigned long gb;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001229
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001230 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_ANON);
1231 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08001232
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001233 gb = (inactive + active) >> (30 - PAGE_SHIFT);
1234 if (gb)
1235 inactive_ratio = int_sqrt(10 * gb);
1236 else
1237 inactive_ratio = 1;
1238
Johannes Weiner9b272972011-11-02 13:38:23 -07001239 return inactive * inactive_ratio < active;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001240}
1241
Konstantin Khlebnikovc56d5c72012-05-29 15:07:00 -07001242int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001243{
1244 unsigned long active;
1245 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08001246
Hugh Dickins4d7dcca2012-05-29 15:07:08 -07001247 inactive = mem_cgroup_get_lru_size(lruvec, LRU_INACTIVE_FILE);
1248 active = mem_cgroup_get_lru_size(lruvec, LRU_ACTIVE_FILE);
Rik van Riel56e49d22009-06-16 15:32:28 -07001249
1250 return (active > inactive);
1251}
1252
Balbir Singh6d61ef42009-01-07 18:08:06 -08001253#define mem_cgroup_from_res_counter(counter, member) \
1254 container_of(counter, struct mem_cgroup, member)
1255
Johannes Weiner19942822011-02-01 15:52:43 -08001256/**
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001257 * mem_cgroup_margin - calculate chargeable space of a memory cgroup
Wanpeng Lidad75572012-06-20 12:53:01 -07001258 * @memcg: the memory cgroup
Johannes Weiner19942822011-02-01 15:52:43 -08001259 *
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001260 * Returns the maximum amount of memory @mem can be charged with, in
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001261 * pages.
Johannes Weiner19942822011-02-01 15:52:43 -08001262 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001263static unsigned long mem_cgroup_margin(struct mem_cgroup *memcg)
Johannes Weiner19942822011-02-01 15:52:43 -08001264{
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001265 unsigned long long margin;
1266
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001267 margin = res_counter_margin(&memcg->res);
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001268 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001269 margin = min(margin, res_counter_margin(&memcg->memsw));
Johannes Weiner7ec99d62011-03-23 16:42:36 -07001270 return margin >> PAGE_SHIFT;
Johannes Weiner19942822011-02-01 15:52:43 -08001271}
1272
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07001273int mem_cgroup_swappiness(struct mem_cgroup *memcg)
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001274{
1275 struct cgroup *cgrp = memcg->css.cgroup;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001276
1277 /* root ? */
1278 if (cgrp->parent == NULL)
1279 return vm_swappiness;
1280
Johannes Weinerbf1ff262011-03-23 16:42:32 -07001281 return memcg->swappiness;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001282}
1283
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001284/*
1285 * memcg->moving_account is used for checking possibility that some thread is
1286 * calling move_account(). When a thread on CPU-A starts moving pages under
1287 * a memcg, other threads should check memcg->moving_account under
1288 * rcu_read_lock(), like this:
1289 *
1290 * CPU-A CPU-B
1291 * rcu_read_lock()
1292 * memcg->moving_account+1 if (memcg->mocing_account)
1293 * take heavy locks.
1294 * synchronize_rcu() update something.
1295 * rcu_read_unlock()
1296 * start move here.
1297 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001298
1299/* for quick checking without looking up memcg */
1300atomic_t memcg_moving __read_mostly;
1301
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001302static void mem_cgroup_start_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001303{
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001304 atomic_inc(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001305 atomic_inc(&memcg->moving_account);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001306 synchronize_rcu();
1307}
1308
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001309static void mem_cgroup_end_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001310{
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001311 /*
1312 * Now, mem_cgroup_clear_mc() may call this function with NULL.
1313 * We check NULL in callee rather than caller.
1314 */
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001315 if (memcg) {
1316 atomic_dec(&memcg_moving);
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001317 atomic_dec(&memcg->moving_account);
KAMEZAWA Hiroyuki4331f7d2012-03-21 16:34:26 -07001318 }
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001319}
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001320
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001321/*
1322 * 2 routines for checking "mem" is under move_account() or not.
1323 *
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001324 * mem_cgroup_stolen() - checking whether a cgroup is mc.from or not. This
1325 * is used for avoiding races in accounting. If true,
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001326 * pc->mem_cgroup may be overwritten.
1327 *
1328 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1329 * under hierarchy of moving cgroups. This is for
1330 * waiting at hith-memory prressure caused by "move".
1331 */
1332
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001333static bool mem_cgroup_stolen(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001334{
1335 VM_BUG_ON(!rcu_read_lock_held());
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001336 return atomic_read(&memcg->moving_account) > 0;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001337}
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001338
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001339static bool mem_cgroup_under_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001340{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001341 struct mem_cgroup *from;
1342 struct mem_cgroup *to;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001343 bool ret = false;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001344 /*
1345 * Unlike task_move routines, we access mc.to, mc.from not under
1346 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1347 */
1348 spin_lock(&mc.lock);
1349 from = mc.from;
1350 to = mc.to;
1351 if (!from)
1352 goto unlock;
Michal Hocko3e920412011-07-26 16:08:29 -07001353
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001354 ret = mem_cgroup_same_or_subtree(memcg, from)
1355 || mem_cgroup_same_or_subtree(memcg, to);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07001356unlock:
1357 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001358 return ret;
1359}
1360
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001361static bool mem_cgroup_wait_acct_move(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001362{
1363 if (mc.moving_task && current != mc.moving_task) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001364 if (mem_cgroup_under_move(memcg)) {
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07001365 DEFINE_WAIT(wait);
1366 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1367 /* moving charge context might have finished. */
1368 if (mc.moving_task)
1369 schedule();
1370 finish_wait(&mc.waitq, &wait);
1371 return true;
1372 }
1373 }
1374 return false;
1375}
1376
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001377/*
1378 * Take this lock when
1379 * - a code tries to modify page's memcg while it's USED.
1380 * - a code tries to modify page state accounting in a memcg.
Andrew Morton13fd1dd92012-03-21 16:34:26 -07001381 * see mem_cgroup_stolen(), too.
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07001382 */
1383static void move_lock_mem_cgroup(struct mem_cgroup *memcg,
1384 unsigned long *flags)
1385{
1386 spin_lock_irqsave(&memcg->move_lock, *flags);
1387}
1388
1389static void move_unlock_mem_cgroup(struct mem_cgroup *memcg,
1390 unsigned long *flags)
1391{
1392 spin_unlock_irqrestore(&memcg->move_lock, *flags);
1393}
1394
Balbir Singhe2224322009-04-02 16:57:39 -07001395/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001396 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001397 * @memcg: The memory cgroup that went over limit
1398 * @p: Task that is going to be killed
1399 *
1400 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1401 * enabled
1402 */
1403void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1404{
1405 struct cgroup *task_cgrp;
1406 struct cgroup *mem_cgrp;
1407 /*
1408 * Need a buffer in BSS, can't rely on allocations. The code relies
1409 * on the assumption that OOM is serialized for memory controller.
1410 * If this assumption is broken, revisit this code.
1411 */
1412 static char memcg_name[PATH_MAX];
1413 int ret;
1414
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001415 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001416 return;
1417
Balbir Singhe2224322009-04-02 16:57:39 -07001418 rcu_read_lock();
1419
1420 mem_cgrp = memcg->css.cgroup;
1421 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1422
1423 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1424 if (ret < 0) {
1425 /*
1426 * Unfortunately, we are unable to convert to a useful name
1427 * But we'll still print out the usage information
1428 */
1429 rcu_read_unlock();
1430 goto done;
1431 }
1432 rcu_read_unlock();
1433
1434 printk(KERN_INFO "Task in %s killed", memcg_name);
1435
1436 rcu_read_lock();
1437 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1438 if (ret < 0) {
1439 rcu_read_unlock();
1440 goto done;
1441 }
1442 rcu_read_unlock();
1443
1444 /*
1445 * Continues from above, so we don't need an KERN_ level
1446 */
1447 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1448done:
1449
1450 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1451 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1452 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1453 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1454 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1455 "failcnt %llu\n",
1456 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1457 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1458 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1459}
1460
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001461/*
1462 * This function returns the number of memcg under hierarchy tree. Returns
1463 * 1(self count) if no children.
1464 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001465static int mem_cgroup_count_children(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001466{
1467 int num = 0;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001468 struct mem_cgroup *iter;
1469
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001470 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001471 num++;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001472 return num;
1473}
1474
Balbir Singh6d61ef42009-01-07 18:08:06 -08001475/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001476 * Return the memory (and swap, if configured) limit for a memcg.
1477 */
David Rientjes9cbb78b2012-07-31 16:43:44 -07001478static u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
David Rientjesa63d83f2010-08-09 17:19:46 -07001479{
1480 u64 limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001481
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001482 limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
Johannes Weinerf3e8eb72011-01-13 15:47:39 -08001483
David Rientjesa63d83f2010-08-09 17:19:46 -07001484 /*
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001485 * Do not consider swap space if we cannot swap due to swappiness
David Rientjesa63d83f2010-08-09 17:19:46 -07001486 */
Michal Hocko9a5a8f12012-11-16 14:14:49 -08001487 if (mem_cgroup_swappiness(memcg)) {
1488 u64 memsw;
1489
1490 limit += total_swap_pages << PAGE_SHIFT;
1491 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1492
1493 /*
1494 * If memsw is finite and limits the amount of swap space
1495 * available to this memcg, return that limit.
1496 */
1497 limit = min(limit, memsw);
1498 }
1499
1500 return limit;
David Rientjesa63d83f2010-08-09 17:19:46 -07001501}
1502
David Rientjes19965462012-12-11 16:00:26 -08001503static void mem_cgroup_out_of_memory(struct mem_cgroup *memcg, gfp_t gfp_mask,
1504 int order)
David Rientjes9cbb78b2012-07-31 16:43:44 -07001505{
1506 struct mem_cgroup *iter;
1507 unsigned long chosen_points = 0;
1508 unsigned long totalpages;
1509 unsigned int points = 0;
1510 struct task_struct *chosen = NULL;
1511
David Rientjes876aafb2012-07-31 16:43:48 -07001512 /*
1513 * If current has a pending SIGKILL, then automatically select it. The
1514 * goal is to allow it to allocate so that it may quickly exit and free
1515 * its memory.
1516 */
1517 if (fatal_signal_pending(current)) {
1518 set_thread_flag(TIF_MEMDIE);
1519 return;
1520 }
1521
1522 check_panic_on_oom(CONSTRAINT_MEMCG, gfp_mask, order, NULL);
David Rientjes9cbb78b2012-07-31 16:43:44 -07001523 totalpages = mem_cgroup_get_limit(memcg) >> PAGE_SHIFT ? : 1;
1524 for_each_mem_cgroup_tree(iter, memcg) {
1525 struct cgroup *cgroup = iter->css.cgroup;
1526 struct cgroup_iter it;
1527 struct task_struct *task;
1528
1529 cgroup_iter_start(cgroup, &it);
1530 while ((task = cgroup_iter_next(cgroup, &it))) {
1531 switch (oom_scan_process_thread(task, totalpages, NULL,
1532 false)) {
1533 case OOM_SCAN_SELECT:
1534 if (chosen)
1535 put_task_struct(chosen);
1536 chosen = task;
1537 chosen_points = ULONG_MAX;
1538 get_task_struct(chosen);
1539 /* fall through */
1540 case OOM_SCAN_CONTINUE:
1541 continue;
1542 case OOM_SCAN_ABORT:
1543 cgroup_iter_end(cgroup, &it);
1544 mem_cgroup_iter_break(memcg, iter);
1545 if (chosen)
1546 put_task_struct(chosen);
1547 return;
1548 case OOM_SCAN_OK:
1549 break;
1550 };
1551 points = oom_badness(task, memcg, NULL, totalpages);
1552 if (points > chosen_points) {
1553 if (chosen)
1554 put_task_struct(chosen);
1555 chosen = task;
1556 chosen_points = points;
1557 get_task_struct(chosen);
1558 }
1559 }
1560 cgroup_iter_end(cgroup, &it);
1561 }
1562
1563 if (!chosen)
1564 return;
1565 points = chosen_points * 1000 / totalpages;
David Rientjes9cbb78b2012-07-31 16:43:44 -07001566 oom_kill_process(chosen, gfp_mask, order, points, totalpages, memcg,
1567 NULL, "Memory cgroup out of memory");
David Rientjes9cbb78b2012-07-31 16:43:44 -07001568}
1569
Johannes Weiner56600482012-01-12 17:17:59 -08001570static unsigned long mem_cgroup_reclaim(struct mem_cgroup *memcg,
1571 gfp_t gfp_mask,
1572 unsigned long flags)
1573{
1574 unsigned long total = 0;
1575 bool noswap = false;
1576 int loop;
1577
1578 if (flags & MEM_CGROUP_RECLAIM_NOSWAP)
1579 noswap = true;
1580 if (!(flags & MEM_CGROUP_RECLAIM_SHRINK) && memcg->memsw_is_minimum)
1581 noswap = true;
1582
1583 for (loop = 0; loop < MEM_CGROUP_MAX_RECLAIM_LOOPS; loop++) {
1584 if (loop)
1585 drain_all_stock_async(memcg);
1586 total += try_to_free_mem_cgroup_pages(memcg, gfp_mask, noswap);
1587 /*
1588 * Allow limit shrinkers, which are triggered directly
1589 * by userspace, to catch signals and stop reclaim
1590 * after minimal progress, regardless of the margin.
1591 */
1592 if (total && (flags & MEM_CGROUP_RECLAIM_SHRINK))
1593 break;
1594 if (mem_cgroup_margin(memcg))
1595 break;
1596 /*
1597 * If nothing was reclaimed after two attempts, there
1598 * may be no reclaimable pages in this hierarchy.
1599 */
1600 if (loop && !total)
1601 break;
1602 }
1603 return total;
1604}
1605
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001606/**
1607 * test_mem_cgroup_node_reclaimable
Wanpeng Lidad75572012-06-20 12:53:01 -07001608 * @memcg: the target memcg
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001609 * @nid: the node ID to be checked.
1610 * @noswap : specify true here if the user wants flle only information.
1611 *
1612 * This function returns whether the specified memcg contains any
1613 * reclaimable pages on a node. Returns true if there are any reclaimable
1614 * pages in the node.
1615 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001616static bool test_mem_cgroup_node_reclaimable(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001617 int nid, bool noswap)
1618{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001619 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_FILE))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001620 return true;
1621 if (noswap || !total_swap_pages)
1622 return false;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001623 if (mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL_ANON))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001624 return true;
1625 return false;
1626
1627}
Ying Han889976d2011-05-26 16:25:33 -07001628#if MAX_NUMNODES > 1
1629
1630/*
1631 * Always updating the nodemask is not very good - even if we have an empty
1632 * list or the wrong list here, we can start from some node and traverse all
1633 * nodes based on the zonelist. So update the list loosely once per 10 secs.
1634 *
1635 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001636static void mem_cgroup_may_update_nodemask(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001637{
1638 int nid;
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001639 /*
1640 * numainfo_events > 0 means there was at least NUMAINFO_EVENTS_TARGET
1641 * pagein/pageout changes since the last update.
1642 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001643 if (!atomic_read(&memcg->numainfo_events))
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001644 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001645 if (atomic_inc_return(&memcg->numainfo_updating) > 1)
Ying Han889976d2011-05-26 16:25:33 -07001646 return;
1647
Ying Han889976d2011-05-26 16:25:33 -07001648 /* make a nodemask where this memcg uses memory from */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001649 memcg->scan_nodes = node_states[N_MEMORY];
Ying Han889976d2011-05-26 16:25:33 -07001650
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001651 for_each_node_mask(nid, node_states[N_MEMORY]) {
Ying Han889976d2011-05-26 16:25:33 -07001652
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001653 if (!test_mem_cgroup_node_reclaimable(memcg, nid, false))
1654 node_clear(nid, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001655 }
KAMEZAWA Hiroyuki453a9bf2011-07-08 15:39:43 -07001656
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001657 atomic_set(&memcg->numainfo_events, 0);
1658 atomic_set(&memcg->numainfo_updating, 0);
Ying Han889976d2011-05-26 16:25:33 -07001659}
1660
1661/*
1662 * Selecting a node where we start reclaim from. Because what we need is just
1663 * reducing usage counter, start from anywhere is O,K. Considering
1664 * memory reclaim from current node, there are pros. and cons.
1665 *
1666 * Freeing memory from current node means freeing memory from a node which
1667 * we'll use or we've used. So, it may make LRU bad. And if several threads
1668 * hit limits, it will see a contention on a node. But freeing from remote
1669 * node means more costs for memory reclaim because of memory latency.
1670 *
1671 * Now, we use round-robin. Better algorithm is welcomed.
1672 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001673int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001674{
1675 int node;
1676
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001677 mem_cgroup_may_update_nodemask(memcg);
1678 node = memcg->last_scanned_node;
Ying Han889976d2011-05-26 16:25:33 -07001679
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001680 node = next_node(node, memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001681 if (node == MAX_NUMNODES)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001682 node = first_node(memcg->scan_nodes);
Ying Han889976d2011-05-26 16:25:33 -07001683 /*
1684 * We call this when we hit limit, not when pages are added to LRU.
1685 * No LRU may hold pages because all pages are UNEVICTABLE or
1686 * memcg is too small and all pages are not on LRU. In that case,
1687 * we use curret node.
1688 */
1689 if (unlikely(node == MAX_NUMNODES))
1690 node = numa_node_id();
1691
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001692 memcg->last_scanned_node = node;
Ying Han889976d2011-05-26 16:25:33 -07001693 return node;
1694}
1695
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001696/*
1697 * Check all nodes whether it contains reclaimable pages or not.
1698 * For quick scan, we make use of scan_nodes. This will allow us to skip
1699 * unused nodes. But scan_nodes is lazily updated and may not cotain
1700 * enough new information. We need to do double check.
1701 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001702static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001703{
1704 int nid;
1705
1706 /*
1707 * quick check...making use of scan_node.
1708 * We can skip unused nodes.
1709 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001710 if (!nodes_empty(memcg->scan_nodes)) {
1711 for (nid = first_node(memcg->scan_nodes);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001712 nid < MAX_NUMNODES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001713 nid = next_node(nid, memcg->scan_nodes)) {
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001714
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001715 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001716 return true;
1717 }
1718 }
1719 /*
1720 * Check rest of nodes.
1721 */
Lai Jiangshan31aaea42012-12-12 13:51:27 -08001722 for_each_node_state(nid, N_MEMORY) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001723 if (node_isset(nid, memcg->scan_nodes))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001724 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001725 if (test_mem_cgroup_node_reclaimable(memcg, nid, noswap))
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001726 return true;
1727 }
1728 return false;
1729}
1730
Ying Han889976d2011-05-26 16:25:33 -07001731#else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001732int mem_cgroup_select_victim_node(struct mem_cgroup *memcg)
Ying Han889976d2011-05-26 16:25:33 -07001733{
1734 return 0;
1735}
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001736
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001737static bool mem_cgroup_reclaimable(struct mem_cgroup *memcg, bool noswap)
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001738{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001739 return test_mem_cgroup_node_reclaimable(memcg, 0, noswap);
KAMEZAWA Hiroyuki4d0c0662011-07-08 15:39:42 -07001740}
Ying Han889976d2011-05-26 16:25:33 -07001741#endif
1742
Johannes Weiner56600482012-01-12 17:17:59 -08001743static int mem_cgroup_soft_reclaim(struct mem_cgroup *root_memcg,
1744 struct zone *zone,
1745 gfp_t gfp_mask,
1746 unsigned long *total_scanned)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001747{
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001748 struct mem_cgroup *victim = NULL;
Johannes Weiner56600482012-01-12 17:17:59 -08001749 int total = 0;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001750 int loop = 0;
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001751 unsigned long excess;
Johannes Weiner185efc02011-09-14 16:21:58 -07001752 unsigned long nr_scanned;
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001753 struct mem_cgroup_reclaim_cookie reclaim = {
1754 .zone = zone,
1755 .priority = 0,
1756 };
Johannes Weiner9d11ea92011-03-23 16:42:21 -07001757
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001758 excess = res_counter_soft_limit_excess(&root_memcg->res) >> PAGE_SHIFT;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001759
Balbir Singh4e416952009-09-23 15:56:39 -07001760 while (1) {
Johannes Weiner527a5ec2012-01-12 17:17:55 -08001761 victim = mem_cgroup_iter(root_memcg, victim, &reclaim);
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001762 if (!victim) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001763 loop++;
Balbir Singh4e416952009-09-23 15:56:39 -07001764 if (loop >= 2) {
1765 /*
1766 * If we have not been able to reclaim
1767 * anything, it might because there are
1768 * no reclaimable pages under this hierarchy
1769 */
Johannes Weiner56600482012-01-12 17:17:59 -08001770 if (!total)
Balbir Singh4e416952009-09-23 15:56:39 -07001771 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001772 /*
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001773 * We want to do more targeted reclaim.
Balbir Singh4e416952009-09-23 15:56:39 -07001774 * excess >> 2 is not to excessive so as to
1775 * reclaim too much, nor too less that we keep
1776 * coming back to reclaim from this cgroup
1777 */
1778 if (total >= (excess >> 2) ||
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001779 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS))
Balbir Singh4e416952009-09-23 15:56:39 -07001780 break;
Balbir Singh4e416952009-09-23 15:56:39 -07001781 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001782 continue;
Balbir Singh4e416952009-09-23 15:56:39 -07001783 }
Johannes Weiner56600482012-01-12 17:17:59 -08001784 if (!mem_cgroup_reclaimable(victim, false))
Balbir Singh6d61ef42009-01-07 18:08:06 -08001785 continue;
Johannes Weiner56600482012-01-12 17:17:59 -08001786 total += mem_cgroup_shrink_node_zone(victim, gfp_mask, false,
1787 zone, &nr_scanned);
1788 *total_scanned += nr_scanned;
1789 if (!res_counter_soft_limit_excess(&root_memcg->res))
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001790 break;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001791 }
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001792 mem_cgroup_iter_break(root_memcg, victim);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001793 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001794}
1795
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001796/*
1797 * Check OOM-Killer is already running under our hierarchy.
1798 * If someone is running, return false.
Michal Hocko1af8efe2011-07-26 16:08:24 -07001799 * Has to be called with memcg_oom_lock
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001800 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001801static bool mem_cgroup_oom_lock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001802{
Michal Hocko79dfdac2011-07-26 16:08:23 -07001803 struct mem_cgroup *iter, *failed = NULL;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001804
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001805 for_each_mem_cgroup_tree(iter, memcg) {
Johannes Weiner23751be2011-08-25 15:59:16 -07001806 if (iter->oom_lock) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001807 /*
1808 * this subtree of our hierarchy is already locked
1809 * so we cannot give a lock.
1810 */
Michal Hocko79dfdac2011-07-26 16:08:23 -07001811 failed = iter;
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001812 mem_cgroup_iter_break(memcg, iter);
1813 break;
Johannes Weiner23751be2011-08-25 15:59:16 -07001814 } else
1815 iter->oom_lock = true;
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001816 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001817
Michal Hocko79dfdac2011-07-26 16:08:23 -07001818 if (!failed)
Johannes Weiner23751be2011-08-25 15:59:16 -07001819 return true;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001820
1821 /*
1822 * OK, we failed to lock the whole subtree so we have to clean up
1823 * what we set up to the failing subtree
1824 */
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001825 for_each_mem_cgroup_tree(iter, memcg) {
Michal Hocko79dfdac2011-07-26 16:08:23 -07001826 if (iter == failed) {
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08001827 mem_cgroup_iter_break(memcg, iter);
1828 break;
Michal Hocko79dfdac2011-07-26 16:08:23 -07001829 }
1830 iter->oom_lock = false;
1831 }
Johannes Weiner23751be2011-08-25 15:59:16 -07001832 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001833}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001834
Michal Hocko79dfdac2011-07-26 16:08:23 -07001835/*
Michal Hocko1af8efe2011-07-26 16:08:24 -07001836 * Has to be called with memcg_oom_lock
Michal Hocko79dfdac2011-07-26 16:08:23 -07001837 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001838static int mem_cgroup_oom_unlock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001839{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07001840 struct mem_cgroup *iter;
1841
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001842 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001843 iter->oom_lock = false;
1844 return 0;
1845}
1846
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001847static void mem_cgroup_mark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001848{
1849 struct mem_cgroup *iter;
1850
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001851 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001852 atomic_inc(&iter->under_oom);
1853}
1854
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001855static void mem_cgroup_unmark_under_oom(struct mem_cgroup *memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001856{
1857 struct mem_cgroup *iter;
1858
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001859 /*
1860 * When a new child is created while the hierarchy is under oom,
1861 * mem_cgroup_oom_lock() may not be called. We have to use
1862 * atomic_add_unless() here.
1863 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001864 for_each_mem_cgroup_tree(iter, memcg)
Michal Hocko79dfdac2011-07-26 16:08:23 -07001865 atomic_add_unless(&iter->under_oom, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001866}
1867
Michal Hocko1af8efe2011-07-26 16:08:24 -07001868static DEFINE_SPINLOCK(memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001869static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1870
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001871struct oom_wait_info {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001872 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001873 wait_queue_t wait;
1874};
1875
1876static int memcg_oom_wake_function(wait_queue_t *wait,
1877 unsigned mode, int sync, void *arg)
1878{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001879 struct mem_cgroup *wake_memcg = (struct mem_cgroup *)arg;
1880 struct mem_cgroup *oom_wait_memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001881 struct oom_wait_info *oom_wait_info;
1882
1883 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001884 oom_wait_memcg = oom_wait_info->memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001885
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001886 /*
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001887 * Both of oom_wait_info->memcg and wake_memcg are stable under us.
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001888 * Then we can use css_is_ancestor without taking care of RCU.
1889 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001890 if (!mem_cgroup_same_or_subtree(oom_wait_memcg, wake_memcg)
1891 && !mem_cgroup_same_or_subtree(wake_memcg, oom_wait_memcg))
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001892 return 0;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001893 return autoremove_wake_function(wait, mode, sync, arg);
1894}
1895
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001896static void memcg_wakeup_oom(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001897{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001898 /* for filtering, pass "memcg" as argument. */
1899 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, memcg);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001900}
1901
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001902static void memcg_oom_recover(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001903{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001904 if (memcg && atomic_read(&memcg->under_oom))
1905 memcg_wakeup_oom(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001906}
1907
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001908/*
1909 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1910 */
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07001911static bool mem_cgroup_handle_oom(struct mem_cgroup *memcg, gfp_t mask,
1912 int order)
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001913{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001914 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001915 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001916
Hugh Dickinsd79154b2012-03-21 16:34:18 -07001917 owait.memcg = memcg;
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001918 owait.wait.flags = 0;
1919 owait.wait.func = memcg_oom_wake_function;
1920 owait.wait.private = current;
1921 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001922 need_to_kill = true;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001923 mem_cgroup_mark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001924
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001925 /* At first, try to OOM lock hierarchy under memcg.*/
Michal Hocko1af8efe2011-07-26 16:08:24 -07001926 spin_lock(&memcg_oom_lock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001927 locked = mem_cgroup_oom_lock(memcg);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001928 /*
1929 * Even if signal_pending(), we can't quit charge() loop without
1930 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1931 * under OOM is always welcomed, use TASK_KILLABLE here.
1932 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001933 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001934 if (!locked || memcg->oom_kill_disable)
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001935 need_to_kill = false;
1936 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001937 mem_cgroup_oom_notify(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001938 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001939
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001940 if (need_to_kill) {
1941 finish_wait(&memcg_oom_waitq, &owait.wait);
David Rientjese845e192012-03-21 16:34:10 -07001942 mem_cgroup_out_of_memory(memcg, mask, order);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001943 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001944 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001945 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001946 }
Michal Hocko1af8efe2011-07-26 16:08:24 -07001947 spin_lock(&memcg_oom_lock);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001948 if (locked)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001949 mem_cgroup_oom_unlock(memcg);
1950 memcg_wakeup_oom(memcg);
Michal Hocko1af8efe2011-07-26 16:08:24 -07001951 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001952
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07001953 mem_cgroup_unmark_under_oom(memcg);
Michal Hocko79dfdac2011-07-26 16:08:23 -07001954
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001955 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1956 return false;
1957 /* Give chance to dying process */
KAMEZAWA Hiroyuki715a5ee2011-11-02 13:38:18 -07001958 schedule_timeout_uninterruptible(1);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001959 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001960}
1961
Balbir Singhd69b0422009-06-17 16:26:34 -07001962/*
1963 * Currently used to update mapped file statistics, but the routine can be
1964 * generalized to update other statistics as well.
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07001965 *
1966 * Notes: Race condition
1967 *
1968 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1969 * it tends to be costly. But considering some conditions, we doesn't need
1970 * to do so _always_.
1971 *
1972 * Considering "charge", lock_page_cgroup() is not required because all
1973 * file-stat operations happen after a page is attached to radix-tree. There
1974 * are no race with "charge".
1975 *
1976 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1977 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1978 * if there are race with "uncharge". Statistics itself is properly handled
1979 * by flags.
1980 *
1981 * Considering "move", this is an only case we see a race. To make the race
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07001982 * small, we check mm->moving_account and detect there are possibility of race
1983 * If there is, we take a lock.
Balbir Singhd69b0422009-06-17 16:26:34 -07001984 */
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07001985
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07001986void __mem_cgroup_begin_update_page_stat(struct page *page,
1987 bool *locked, unsigned long *flags)
1988{
1989 struct mem_cgroup *memcg;
1990 struct page_cgroup *pc;
1991
1992 pc = lookup_page_cgroup(page);
1993again:
1994 memcg = pc->mem_cgroup;
1995 if (unlikely(!memcg || !PageCgroupUsed(pc)))
1996 return;
1997 /*
1998 * If this memory cgroup is not under account moving, we don't
Wanpeng Lida92c472012-07-31 16:43:26 -07001999 * need to take move_lock_mem_cgroup(). Because we already hold
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002000 * rcu_read_lock(), any calls to move_account will be delayed until
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002001 * rcu_read_unlock() if mem_cgroup_stolen() == true.
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002002 */
Andrew Morton13fd1dd92012-03-21 16:34:26 -07002003 if (!mem_cgroup_stolen(memcg))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002004 return;
2005
2006 move_lock_mem_cgroup(memcg, flags);
2007 if (memcg != pc->mem_cgroup || !PageCgroupUsed(pc)) {
2008 move_unlock_mem_cgroup(memcg, flags);
2009 goto again;
2010 }
2011 *locked = true;
2012}
2013
2014void __mem_cgroup_end_update_page_stat(struct page *page, unsigned long *flags)
2015{
2016 struct page_cgroup *pc = lookup_page_cgroup(page);
2017
2018 /*
2019 * It's guaranteed that pc->mem_cgroup never changes while
2020 * lock is held because a routine modifies pc->mem_cgroup
Wanpeng Lida92c472012-07-31 16:43:26 -07002021 * should take move_lock_mem_cgroup().
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002022 */
2023 move_unlock_mem_cgroup(pc->mem_cgroup, flags);
2024}
2025
Greg Thelen2a7106f2011-01-13 15:47:37 -08002026void mem_cgroup_update_page_stat(struct page *page,
2027 enum mem_cgroup_page_stat_item idx, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07002028{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002029 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07002030 struct page_cgroup *pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyukidbd4ea72011-01-13 15:47:38 -08002031 unsigned long uninitialized_var(flags);
Balbir Singhd69b0422009-06-17 16:26:34 -07002032
Johannes Weinercfa44942012-01-12 17:18:38 -08002033 if (mem_cgroup_disabled())
Balbir Singhd69b0422009-06-17 16:26:34 -07002034 return;
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002035
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002036 memcg = pc->mem_cgroup;
2037 if (unlikely(!memcg || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki89c06bd2012-03-21 16:34:25 -07002038 return;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002039
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002040 switch (idx) {
Greg Thelen2a7106f2011-01-13 15:47:37 -08002041 case MEMCG_NR_FILE_MAPPED:
Greg Thelen2a7106f2011-01-13 15:47:37 -08002042 idx = MEM_CGROUP_STAT_FILE_MAPPED;
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002043 break;
2044 default:
2045 BUG();
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07002046 }
Balbir Singhd69b0422009-06-17 16:26:34 -07002047
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002048 this_cpu_add(memcg->stat->count[idx], val);
Balbir Singhd69b0422009-06-17 16:26:34 -07002049}
KAMEZAWA Hiroyuki26174ef2010-10-27 15:33:43 -07002050
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002051/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002052 * size of first charge trial. "32" comes from vmscan.c's magic value.
2053 * TODO: maybe necessary to use big numbers in big irons.
2054 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002055#define CHARGE_BATCH 32U
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002056struct memcg_stock_pcp {
2057 struct mem_cgroup *cached; /* this never be root cgroup */
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002058 unsigned int nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002059 struct work_struct work;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002060 unsigned long flags;
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002061#define FLUSHING_CACHED_CHARGE 0
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002062};
2063static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002064static DEFINE_MUTEX(percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002065
2066/*
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002067 * Try to consume stocked charge on this cpu. If success, one page is consumed
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002068 * from local stock and true is returned. If the stock is 0 or charges from a
2069 * cgroup which is not current target, returns false. This stock will be
2070 * refilled.
2071 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002072static bool consume_stock(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002073{
2074 struct memcg_stock_pcp *stock;
2075 bool ret = true;
2076
2077 stock = &get_cpu_var(memcg_stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002078 if (memcg == stock->cached && stock->nr_pages)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002079 stock->nr_pages--;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002080 else /* need to call res_counter_charge */
2081 ret = false;
2082 put_cpu_var(memcg_stock);
2083 return ret;
2084}
2085
2086/*
2087 * Returns stocks cached in percpu to res_counter and reset cached information.
2088 */
2089static void drain_stock(struct memcg_stock_pcp *stock)
2090{
2091 struct mem_cgroup *old = stock->cached;
2092
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002093 if (stock->nr_pages) {
2094 unsigned long bytes = stock->nr_pages * PAGE_SIZE;
2095
2096 res_counter_uncharge(&old->res, bytes);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002097 if (do_swap_account)
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002098 res_counter_uncharge(&old->memsw, bytes);
2099 stock->nr_pages = 0;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002100 }
2101 stock->cached = NULL;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002102}
2103
2104/*
2105 * This must be called under preempt disabled or must be called by
2106 * a thread which is pinned to local cpu.
2107 */
2108static void drain_local_stock(struct work_struct *dummy)
2109{
2110 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
2111 drain_stock(stock);
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002112 clear_bit(FLUSHING_CACHED_CHARGE, &stock->flags);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002113}
2114
2115/*
2116 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01002117 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002118 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002119static void refill_stock(struct mem_cgroup *memcg, unsigned int nr_pages)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002120{
2121 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
2122
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002123 if (stock->cached != memcg) { /* reset if necessary */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002124 drain_stock(stock);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002125 stock->cached = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002126 }
Johannes Weiner11c9ea42011-03-23 16:42:34 -07002127 stock->nr_pages += nr_pages;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002128 put_cpu_var(memcg_stock);
2129}
2130
2131/*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002132 * Drains all per-CPU charge caches for given root_memcg resp. subtree
Michal Hockod38144b2011-07-26 16:08:28 -07002133 * of the hierarchy under it. sync flag says whether we should block
2134 * until the work is done.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002135 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002136static void drain_all_stock(struct mem_cgroup *root_memcg, bool sync)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002137{
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002138 int cpu, curcpu;
Michal Hockod38144b2011-07-26 16:08:28 -07002139
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002140 /* Notify other cpus that system-wide "drain" is running */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002141 get_online_cpus();
Johannes Weiner5af12d02011-08-25 15:59:07 -07002142 curcpu = get_cpu();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002143 for_each_online_cpu(cpu) {
2144 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002145 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002146
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002147 memcg = stock->cached;
2148 if (!memcg || !stock->nr_pages)
KAMEZAWA Hiroyuki26fe6162011-06-15 15:08:45 -07002149 continue;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002150 if (!mem_cgroup_same_or_subtree(root_memcg, memcg))
Michal Hocko3e920412011-07-26 16:08:29 -07002151 continue;
Michal Hockod1a05b62011-07-26 16:08:27 -07002152 if (!test_and_set_bit(FLUSHING_CACHED_CHARGE, &stock->flags)) {
2153 if (cpu == curcpu)
2154 drain_local_stock(&stock->work);
2155 else
2156 schedule_work_on(cpu, &stock->work);
2157 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002158 }
Johannes Weiner5af12d02011-08-25 15:59:07 -07002159 put_cpu();
Michal Hockod38144b2011-07-26 16:08:28 -07002160
2161 if (!sync)
2162 goto out;
2163
2164 for_each_online_cpu(cpu) {
2165 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002166 if (test_bit(FLUSHING_CACHED_CHARGE, &stock->flags))
Michal Hockod38144b2011-07-26 16:08:28 -07002167 flush_work(&stock->work);
2168 }
2169out:
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002170 put_online_cpus();
Michal Hockod38144b2011-07-26 16:08:28 -07002171}
2172
2173/*
2174 * Tries to drain stocked charges in other cpus. This function is asynchronous
2175 * and just put a work per cpu for draining localy on each cpu. Caller can
2176 * expects some charges will be back to res_counter later but cannot wait for
2177 * it.
2178 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002179static void drain_all_stock_async(struct mem_cgroup *root_memcg)
Michal Hockod38144b2011-07-26 16:08:28 -07002180{
Michal Hocko9f50fad2011-08-09 11:56:26 +02002181 /*
2182 * If someone calls draining, avoid adding more kworker runs.
2183 */
2184 if (!mutex_trylock(&percpu_charge_mutex))
2185 return;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002186 drain_all_stock(root_memcg, false);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002187 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002188}
2189
2190/* This is a synchronous drain interface. */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002191static void drain_all_stock_sync(struct mem_cgroup *root_memcg)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002192{
2193 /* called when force_empty is called */
Michal Hocko9f50fad2011-08-09 11:56:26 +02002194 mutex_lock(&percpu_charge_mutex);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002195 drain_all_stock(root_memcg, true);
Michal Hocko9f50fad2011-08-09 11:56:26 +02002196 mutex_unlock(&percpu_charge_mutex);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002197}
2198
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002199/*
2200 * This function drains percpu counter value from DEAD cpu and
2201 * move it to local cpu. Note that this function can be preempted.
2202 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002203static void mem_cgroup_drain_pcp_counter(struct mem_cgroup *memcg, int cpu)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002204{
2205 int i;
2206
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002207 spin_lock(&memcg->pcp_counter_lock);
Johannes Weiner61046212012-05-29 15:07:05 -07002208 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002209 long x = per_cpu(memcg->stat->count[i], cpu);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002210
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002211 per_cpu(memcg->stat->count[i], cpu) = 0;
2212 memcg->nocpu_base.count[i] += x;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002213 }
Johannes Weinere9f89742011-03-23 16:42:37 -07002214 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002215 unsigned long x = per_cpu(memcg->stat->events[i], cpu);
Johannes Weinere9f89742011-03-23 16:42:37 -07002216
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002217 per_cpu(memcg->stat->events[i], cpu) = 0;
2218 memcg->nocpu_base.events[i] += x;
Johannes Weinere9f89742011-03-23 16:42:37 -07002219 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002220 spin_unlock(&memcg->pcp_counter_lock);
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002221}
2222
2223static int __cpuinit memcg_cpu_hotplug_callback(struct notifier_block *nb,
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002224 unsigned long action,
2225 void *hcpu)
2226{
2227 int cpu = (unsigned long)hcpu;
2228 struct memcg_stock_pcp *stock;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002229 struct mem_cgroup *iter;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002230
KAMEZAWA Hiroyuki619d0942012-03-21 16:34:23 -07002231 if (action == CPU_ONLINE)
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002232 return NOTIFY_OK;
KAMEZAWA Hiroyuki1489eba2010-10-27 15:33:42 -07002233
Kirill A. Shutemovd8330492012-04-12 12:49:11 -07002234 if (action != CPU_DEAD && action != CPU_DEAD_FROZEN)
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002235 return NOTIFY_OK;
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002236
Johannes Weiner9f3a0d02012-01-12 17:17:48 -08002237 for_each_mem_cgroup(iter)
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07002238 mem_cgroup_drain_pcp_counter(iter, cpu);
2239
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002240 stock = &per_cpu(memcg_stock, cpu);
2241 drain_stock(stock);
2242 return NOTIFY_OK;
2243}
2244
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002245
2246/* See __mem_cgroup_try_charge() for details */
2247enum {
2248 CHARGE_OK, /* success */
2249 CHARGE_RETRY, /* need to retry but retry is not bad */
2250 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
2251 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
2252 CHARGE_OOM_DIE, /* the current is killed because of OOM */
2253};
2254
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002255static int mem_cgroup_do_charge(struct mem_cgroup *memcg, gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002256 unsigned int nr_pages, bool oom_check)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002257{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002258 unsigned long csize = nr_pages * PAGE_SIZE;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002259 struct mem_cgroup *mem_over_limit;
2260 struct res_counter *fail_res;
2261 unsigned long flags = 0;
2262 int ret;
2263
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002264 ret = res_counter_charge(&memcg->res, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002265
2266 if (likely(!ret)) {
2267 if (!do_swap_account)
2268 return CHARGE_OK;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002269 ret = res_counter_charge(&memcg->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002270 if (likely(!ret))
2271 return CHARGE_OK;
2272
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002273 res_counter_uncharge(&memcg->res, csize);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002274 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
2275 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
2276 } else
2277 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
Johannes Weiner9221edb2011-02-01 15:52:42 -08002278 /*
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002279 * nr_pages can be either a huge page (HPAGE_PMD_NR), a batch
2280 * of regular pages (CHARGE_BATCH), or a single regular page (1).
Johannes Weiner9221edb2011-02-01 15:52:42 -08002281 *
2282 * Never reclaim on behalf of optional batching, retry with a
2283 * single page instead.
2284 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002285 if (nr_pages == CHARGE_BATCH)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002286 return CHARGE_RETRY;
2287
2288 if (!(gfp_mask & __GFP_WAIT))
2289 return CHARGE_WOULDBLOCK;
2290
Johannes Weiner56600482012-01-12 17:17:59 -08002291 ret = mem_cgroup_reclaim(mem_over_limit, gfp_mask, flags);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002292 if (mem_cgroup_margin(mem_over_limit) >= nr_pages)
Johannes Weiner19942822011-02-01 15:52:43 -08002293 return CHARGE_RETRY;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002294 /*
Johannes Weiner19942822011-02-01 15:52:43 -08002295 * Even though the limit is exceeded at this point, reclaim
2296 * may have been able to free some pages. Retry the charge
2297 * before killing the task.
2298 *
2299 * Only for regular pages, though: huge pages are rather
2300 * unlikely to succeed so close to the limit, and we fall back
2301 * to regular pages anyway in case of failure.
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002302 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002303 if (nr_pages == 1 && ret)
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002304 return CHARGE_RETRY;
2305
2306 /*
2307 * At task move, charge accounts can be doubly counted. So, it's
2308 * better to wait until the end of task_move if something is going on.
2309 */
2310 if (mem_cgroup_wait_acct_move(mem_over_limit))
2311 return CHARGE_RETRY;
2312
2313 /* If we don't need to call oom-killer at el, return immediately */
2314 if (!oom_check)
2315 return CHARGE_NOMEM;
2316 /* check OOM */
David Rientjese845e192012-03-21 16:34:10 -07002317 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask, get_order(csize)))
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002318 return CHARGE_OOM_DIE;
2319
2320 return CHARGE_RETRY;
2321}
2322
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002323/*
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002324 * __mem_cgroup_try_charge() does
2325 * 1. detect memcg to be charged against from passed *mm and *ptr,
2326 * 2. update res_counter
2327 * 3. call memory reclaim if necessary.
2328 *
2329 * In some special case, if the task is fatal, fatal_signal_pending() or
2330 * has TIF_MEMDIE, this function returns -EINTR while writing root_mem_cgroup
2331 * to *ptr. There are two reasons for this. 1: fatal threads should quit as soon
2332 * as possible without any hazards. 2: all pages should have a valid
2333 * pc->mem_cgroup. If mm is NULL and the caller doesn't pass a valid memcg
2334 * pointer, that is treated as a charge to root_mem_cgroup.
2335 *
2336 * So __mem_cgroup_try_charge() will return
2337 * 0 ... on success, filling *ptr with a valid memcg pointer.
2338 * -ENOMEM ... charge failure because of resource limits.
2339 * -EINTR ... if thread is fatal. *ptr is filled with root_mem_cgroup.
2340 *
2341 * Unlike the exported interface, an "oom" parameter is added. if oom==true,
2342 * the oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002343 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002344static int __mem_cgroup_try_charge(struct mm_struct *mm,
Andrea Arcangeliec168512011-01-13 15:46:56 -08002345 gfp_t gfp_mask,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002346 unsigned int nr_pages,
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002347 struct mem_cgroup **ptr,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002348 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002349{
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002350 unsigned int batch = max(CHARGE_BATCH, nr_pages);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002351 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002352 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002353 int ret;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002354
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002355 /*
2356 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
2357 * in system level. So, allow to go ahead dying process in addition to
2358 * MEMDIE process.
2359 */
2360 if (unlikely(test_thread_flag(TIF_MEMDIE)
2361 || fatal_signal_pending(current)))
2362 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08002363
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002364 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08002365 * We always charge the cgroup the mm_struct belongs to.
2366 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002367 * thread group leader migrates. It's possible that mm is not
Johannes Weiner24467ca2012-07-31 16:45:40 -07002368 * set, if so charge the root memcg (happens for pagecache usage).
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002369 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002370 if (!*ptr && !mm)
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002371 *ptr = root_mem_cgroup;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002372again:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002373 if (*ptr) { /* css should be a valid one */
2374 memcg = *ptr;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002375 if (mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002376 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002377 if (nr_pages == 1 && consume_stock(memcg))
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002378 goto done;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002379 css_get(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002380 } else {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002381 struct task_struct *p;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002382
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002383 rcu_read_lock();
2384 p = rcu_dereference(mm->owner);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002385 /*
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002386 * Because we don't have task_lock(), "p" can exit.
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002387 * In that case, "memcg" can point to root or p can be NULL with
KAMEZAWA Hiroyukiebb76ce2010-12-29 14:07:11 -08002388 * race with swapoff. Then, we have small risk of mis-accouning.
2389 * But such kind of mis-account by race always happens because
2390 * we don't have cgroup_mutex(). It's overkill and we allo that
2391 * small race, here.
2392 * (*) swapoff at el will charge against mm-struct not against
2393 * task-struct. So, mm->owner can be NULL.
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002394 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002395 memcg = mem_cgroup_from_task(p);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002396 if (!memcg)
2397 memcg = root_mem_cgroup;
2398 if (mem_cgroup_is_root(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002399 rcu_read_unlock();
2400 goto done;
2401 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002402 if (nr_pages == 1 && consume_stock(memcg)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002403 /*
2404 * It seems dagerous to access memcg without css_get().
2405 * But considering how consume_stok works, it's not
2406 * necessary. If consume_stock success, some charges
2407 * from this memcg are cached on this cpu. So, we
2408 * don't need to call css_get()/css_tryget() before
2409 * calling consume_stock().
2410 */
2411 rcu_read_unlock();
2412 goto done;
2413 }
2414 /* after here, we may be blocked. we need to get refcnt */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002415 if (!css_tryget(&memcg->css)) {
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002416 rcu_read_unlock();
2417 goto again;
2418 }
2419 rcu_read_unlock();
2420 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002421
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002422 do {
2423 bool oom_check;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002424
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002425 /* If killed, bypass charge */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002426 if (fatal_signal_pending(current)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002427 css_put(&memcg->css);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002428 goto bypass;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002429 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002430
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002431 oom_check = false;
2432 if (oom && !nr_oom_retries) {
2433 oom_check = true;
2434 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
2435 }
Balbir Singh6d61ef42009-01-07 18:08:06 -08002436
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002437 ret = mem_cgroup_do_charge(memcg, gfp_mask, batch, oom_check);
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002438 switch (ret) {
2439 case CHARGE_OK:
2440 break;
2441 case CHARGE_RETRY: /* not in OOM situation but retry */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002442 batch = nr_pages;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002443 css_put(&memcg->css);
2444 memcg = NULL;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002445 goto again;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002446 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002447 css_put(&memcg->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002448 goto nomem;
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002449 case CHARGE_NOMEM: /* OOM routine works */
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002450 if (!oom) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002451 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002452 goto nomem;
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07002453 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002454 /* If oom, we never return -ENOMEM */
2455 nr_oom_retries--;
2456 break;
2457 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002458 css_put(&memcg->css);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002459 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08002460 }
KAMEZAWA Hiroyuki4b534332010-08-10 18:02:57 -07002461 } while (ret != CHARGE_OK);
2462
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002463 if (batch > nr_pages)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002464 refill_stock(memcg, batch - nr_pages);
2465 css_put(&memcg->css);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002466done:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002467 *ptr = memcg;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002468 return 0;
2469nomem:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002470 *ptr = NULL;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002471 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08002472bypass:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002473 *ptr = root_mem_cgroup;
2474 return -EINTR;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002475}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002476
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002477/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002478 * Somemtimes we have to undo a charge we got by try_charge().
2479 * This function is for that and do uncharge, put css's refcnt.
2480 * gotten by try_charge().
2481 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002482static void __mem_cgroup_cancel_charge(struct mem_cgroup *memcg,
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002483 unsigned int nr_pages)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002484{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002485 if (!mem_cgroup_is_root(memcg)) {
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002486 unsigned long bytes = nr_pages * PAGE_SIZE;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002487
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002488 res_counter_uncharge(&memcg->res, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002489 if (do_swap_account)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002490 res_counter_uncharge(&memcg->memsw, bytes);
Johannes Weinere7018b8d2011-03-23 16:42:33 -07002491 }
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002492}
2493
2494/*
KAMEZAWA Hiroyukid01dd172012-05-29 15:07:03 -07002495 * Cancel chrages in this cgroup....doesn't propagate to parent cgroup.
2496 * This is useful when moving usage to parent cgroup.
2497 */
2498static void __mem_cgroup_cancel_local_charge(struct mem_cgroup *memcg,
2499 unsigned int nr_pages)
2500{
2501 unsigned long bytes = nr_pages * PAGE_SIZE;
2502
2503 if (mem_cgroup_is_root(memcg))
2504 return;
2505
2506 res_counter_uncharge_until(&memcg->res, memcg->res.parent, bytes);
2507 if (do_swap_account)
2508 res_counter_uncharge_until(&memcg->memsw,
2509 memcg->memsw.parent, bytes);
2510}
2511
2512/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002513 * A helper function to get mem_cgroup from ID. must be called under
Tejun Heoe9316082012-11-05 09:16:58 -08002514 * rcu_read_lock(). The caller is responsible for calling css_tryget if
2515 * the mem_cgroup is used for charging. (dropping refcnt from swap can be
2516 * called against removed memcg.)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002517 */
2518static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
2519{
2520 struct cgroup_subsys_state *css;
2521
2522 /* ID 0 is unused ID */
2523 if (!id)
2524 return NULL;
2525 css = css_lookup(&mem_cgroup_subsys, id);
2526 if (!css)
2527 return NULL;
Wanpeng Lib2145142012-07-31 16:46:01 -07002528 return mem_cgroup_from_css(css);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002529}
2530
Wu Fengguange42d9d52009-12-16 12:19:59 +01002531struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002532{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002533 struct mem_cgroup *memcg = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002534 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002535 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002536 swp_entry_t ent;
2537
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002538 VM_BUG_ON(!PageLocked(page));
2539
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002540 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002541 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002542 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002543 memcg = pc->mem_cgroup;
2544 if (memcg && !css_tryget(&memcg->css))
2545 memcg = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002546 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002547 ent.val = page_private(page);
Bob Liu9fb4b7c2012-01-12 17:18:48 -08002548 id = lookup_swap_cgroup_id(ent);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002549 rcu_read_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002550 memcg = mem_cgroup_lookup(id);
2551 if (memcg && !css_tryget(&memcg->css))
2552 memcg = NULL;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002553 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07002554 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07002555 unlock_page_cgroup(pc);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002556 return memcg;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002557}
2558
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002559static void __mem_cgroup_commit_charge(struct mem_cgroup *memcg,
Johannes Weiner5564e882011-03-23 16:42:29 -07002560 struct page *page,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002561 unsigned int nr_pages,
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002562 enum charge_type ctype,
2563 bool lrucare)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002564{
Johannes Weinerce587e62012-04-24 20:22:33 +02002565 struct page_cgroup *pc = lookup_page_cgroup(page);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002566 struct zone *uninitialized_var(zone);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002567 struct lruvec *lruvec;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002568 bool was_on_lru = false;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002569 bool anon;
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002570
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002571 lock_page_cgroup(pc);
Johannes Weiner90deb782012-07-31 16:45:47 -07002572 VM_BUG_ON(PageCgroupUsed(pc));
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002573 /*
2574 * we don't need page_cgroup_lock about tail pages, becase they are not
2575 * accessed by any other context at this point.
2576 */
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002577
2578 /*
2579 * In some cases, SwapCache and FUSE(splice_buf->radixtree), the page
2580 * may already be on some other mem_cgroup's LRU. Take care of it.
2581 */
2582 if (lrucare) {
2583 zone = page_zone(page);
2584 spin_lock_irq(&zone->lru_lock);
2585 if (PageLRU(page)) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002586 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002587 ClearPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002588 del_page_from_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002589 was_on_lru = true;
2590 }
2591 }
2592
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002593 pc->mem_cgroup = memcg;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07002594 /*
2595 * We access a page_cgroup asynchronously without lock_page_cgroup().
2596 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
2597 * is accessed after testing USED bit. To make pc->mem_cgroup visible
2598 * before USED bit, we need memory barrier here.
2599 * See mem_cgroup_add_lru_list(), etc.
2600 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002601 smp_wmb();
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002602 SetPageCgroupUsed(pc);
Hugh Dickins3be91272008-02-07 00:14:19 -08002603
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002604 if (lrucare) {
2605 if (was_on_lru) {
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002606 lruvec = mem_cgroup_zone_lruvec(zone, pc->mem_cgroup);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002607 VM_BUG_ON(PageLRU(page));
2608 SetPageLRU(page);
Hugh Dickinsfa9add62012-05-29 15:07:09 -07002609 add_page_to_lru_list(page, lruvec, page_lru(page));
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002610 }
2611 spin_unlock_irq(&zone->lru_lock);
2612 }
2613
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002614 if (ctype == MEM_CGROUP_CHARGE_TYPE_ANON)
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002615 anon = true;
2616 else
2617 anon = false;
2618
2619 mem_cgroup_charge_statistics(memcg, anon, nr_pages);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002620 unlock_page_cgroup(pc);
Hugh Dickins9ce70c02012-03-05 14:59:16 -08002621
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002622 /*
2623 * "charge_statistics" updated event counter. Then, check it.
2624 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2625 * if they exceeds softlimit.
2626 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002627 memcg_check_events(memcg, page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002628}
2629
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002630#ifdef CONFIG_TRANSPARENT_HUGEPAGE
2631
Kirill A. Shutemova0db00f2012-05-29 15:06:56 -07002632#define PCGF_NOCOPY_AT_SPLIT (1 << PCG_LOCK | 1 << PCG_MIGRATION)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002633/*
2634 * Because tail pages are not marked as "used", set it. We're under
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002635 * zone->lru_lock, 'splitting on pmd' and compound_lock.
2636 * charge/uncharge will be never happen and move_account() is done under
2637 * compound_lock(), so we don't have to take care of races.
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002638 */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002639void mem_cgroup_split_huge_fixup(struct page *head)
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002640{
2641 struct page_cgroup *head_pc = lookup_page_cgroup(head);
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002642 struct page_cgroup *pc;
2643 int i;
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002644
KAMEZAWA Hiroyuki3d37c4a2011-01-25 15:07:28 -08002645 if (mem_cgroup_disabled())
2646 return;
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002647 for (i = 1; i < HPAGE_PMD_NR; i++) {
2648 pc = head_pc + i;
2649 pc->mem_cgroup = head_pc->mem_cgroup;
2650 smp_wmb();/* see __commit_charge() */
KAMEZAWA Hiroyukie94c8a92012-01-12 17:18:20 -08002651 pc->flags = head_pc->flags & ~PCGF_NOCOPY_AT_SPLIT;
2652 }
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002653}
Hugh Dickins12d27102012-01-12 17:19:52 -08002654#endif /* CONFIG_TRANSPARENT_HUGEPAGE */
KAMEZAWA Hiroyukica3e0212011-01-20 14:44:24 -08002655
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002656/**
Johannes Weinerde3638d2011-03-23 16:42:28 -07002657 * mem_cgroup_move_account - move account of the page
Johannes Weiner5564e882011-03-23 16:42:29 -07002658 * @page: the page
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002659 * @nr_pages: number of regular pages (>1 for huge pages)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002660 * @pc: page_cgroup of the page.
2661 * @from: mem_cgroup which the page is moved from.
2662 * @to: mem_cgroup which the page is moved to. @from != @to.
2663 *
2664 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002665 * - page is not on LRU (isolate_page() is useful.)
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002666 * - compound_lock is held when nr_pages > 1
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002667 *
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002668 * This function doesn't do "charge" to new cgroup and doesn't do "uncharge"
2669 * from old cgroup.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002670 */
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002671static int mem_cgroup_move_account(struct page *page,
2672 unsigned int nr_pages,
2673 struct page_cgroup *pc,
2674 struct mem_cgroup *from,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002675 struct mem_cgroup *to)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002676{
Johannes Weinerde3638d2011-03-23 16:42:28 -07002677 unsigned long flags;
2678 int ret;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002679 bool anon = PageAnon(page);
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002680
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002681 VM_BUG_ON(from == to);
Johannes Weiner5564e882011-03-23 16:42:29 -07002682 VM_BUG_ON(PageLRU(page));
Johannes Weinerde3638d2011-03-23 16:42:28 -07002683 /*
2684 * The page is isolated from LRU. So, collapse function
2685 * will not handle this page. But page splitting can happen.
2686 * Do this check under compound_page_lock(). The caller should
2687 * hold it.
2688 */
2689 ret = -EBUSY;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002690 if (nr_pages > 1 && !PageTransHuge(page))
Johannes Weinerde3638d2011-03-23 16:42:28 -07002691 goto out;
2692
2693 lock_page_cgroup(pc);
2694
2695 ret = -EINVAL;
2696 if (!PageCgroupUsed(pc) || pc->mem_cgroup != from)
2697 goto unlock;
2698
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002699 move_lock_mem_cgroup(from, &flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002700
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07002701 if (!anon && page_mapped(page)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08002702 /* Update mapped_file data for mem_cgroup */
2703 preempt_disable();
2704 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2705 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2706 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07002707 }
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002708 mem_cgroup_charge_statistics(from, anon, -nr_pages);
Balbir Singhd69b0422009-06-17 16:26:34 -07002709
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08002710 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002711 pc->mem_cgroup = to;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07002712 mem_cgroup_charge_statistics(to, anon, nr_pages);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07002713 move_unlock_mem_cgroup(from, &flags);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002714 ret = 0;
2715unlock:
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002716 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002717 /*
2718 * check events
2719 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002720 memcg_check_events(to, page);
2721 memcg_check_events(from, page);
Johannes Weinerde3638d2011-03-23 16:42:28 -07002722out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002723 return ret;
2724}
2725
Michal Hocko2ef37d32012-10-26 13:37:30 +02002726/**
2727 * mem_cgroup_move_parent - moves page to the parent group
2728 * @page: the page to move
2729 * @pc: page_cgroup of the page
2730 * @child: page's cgroup
2731 *
2732 * move charges to its parent or the root cgroup if the group has no
2733 * parent (aka use_hierarchy==0).
2734 * Although this might fail (get_page_unless_zero, isolate_lru_page or
2735 * mem_cgroup_move_account fails) the failure is always temporary and
2736 * it signals a race with a page removal/uncharge or migration. In the
2737 * first case the page is on the way out and it will vanish from the LRU
2738 * on the next attempt and the call should be retried later.
2739 * Isolation from the LRU fails only if page has been isolated from
2740 * the LRU since we looked at it and that usually means either global
2741 * reclaim or migration going on. The page will either get back to the
2742 * LRU or vanish.
2743 * Finaly mem_cgroup_move_account fails only if the page got uncharged
2744 * (!PageCgroupUsed) or moved to a different group. The page will
2745 * disappear in the next attempt.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002746 */
Johannes Weiner5564e882011-03-23 16:42:29 -07002747static int mem_cgroup_move_parent(struct page *page,
2748 struct page_cgroup *pc,
KAMEZAWA Hiroyuki6068bf02012-07-31 16:42:45 -07002749 struct mem_cgroup *child)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002750{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002751 struct mem_cgroup *parent;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002752 unsigned int nr_pages;
Andrew Morton4be44892011-03-23 16:42:39 -07002753 unsigned long uninitialized_var(flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002754 int ret;
2755
Michal Hockod8423012012-10-26 13:37:29 +02002756 VM_BUG_ON(mem_cgroup_is_root(child));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002757
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002758 ret = -EBUSY;
2759 if (!get_page_unless_zero(page))
2760 goto out;
2761 if (isolate_lru_page(page))
2762 goto put;
KAMEZAWA Hiroyuki52dbb902011-01-25 15:07:29 -08002763
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002764 nr_pages = hpage_nr_pages(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002765
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002766 parent = parent_mem_cgroup(child);
2767 /*
2768 * If no parent, move charges to root cgroup.
2769 */
2770 if (!parent)
2771 parent = root_mem_cgroup;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002772
Michal Hocko2ef37d32012-10-26 13:37:30 +02002773 if (nr_pages > 1) {
2774 VM_BUG_ON(!PageTransHuge(page));
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002775 flags = compound_lock_irqsave(page);
Michal Hocko2ef37d32012-10-26 13:37:30 +02002776 }
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002777
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002778 ret = mem_cgroup_move_account(page, nr_pages,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07002779 pc, child, parent);
KAMEZAWA Hiroyukicc926f72012-05-29 15:07:04 -07002780 if (!ret)
2781 __mem_cgroup_cancel_local_charge(child, nr_pages);
Jesper Juhl8dba4742011-01-25 15:07:24 -08002782
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002783 if (nr_pages > 1)
KAMEZAWA Hiroyuki987eba62011-01-20 14:44:25 -08002784 compound_unlock_irqrestore(page, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002785 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002786put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08002787 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08002788out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002789 return ret;
2790}
2791
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002792/*
2793 * Charge the memory controller for page usage.
2794 * Return
2795 * 0 if the charge was successful
2796 * < 0 if the cgroup is over its limit
2797 */
2798static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
Daisuke Nishimura73045c42010-08-10 18:02:59 -07002799 gfp_t gfp_mask, enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002800{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002801 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002802 unsigned int nr_pages = 1;
Johannes Weiner8493ae42011-02-01 15:52:44 -08002803 bool oom = true;
2804 int ret;
Andrea Arcangeliec168512011-01-13 15:46:56 -08002805
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002806 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002807 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002808 VM_BUG_ON(!PageTransHuge(page));
Johannes Weiner8493ae42011-02-01 15:52:44 -08002809 /*
2810 * Never OOM-kill a process for a huge page. The
2811 * fault handler will fall back to regular pages.
2812 */
2813 oom = false;
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08002814 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002815
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002816 ret = __mem_cgroup_try_charge(mm, gfp_mask, nr_pages, &memcg, oom);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002817 if (ret == -ENOMEM)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002818 return ret;
Johannes Weinerce587e62012-04-24 20:22:33 +02002819 __mem_cgroup_commit_charge(memcg, page, nr_pages, ctype, false);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002820 return 0;
2821}
2822
2823int mem_cgroup_newpage_charge(struct page *page,
2824 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002825{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002826 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002827 return 0;
Johannes Weiner7a0524c2012-01-12 17:18:43 -08002828 VM_BUG_ON(page_mapped(page));
2829 VM_BUG_ON(page->mapping && !PageAnon(page));
2830 VM_BUG_ON(!mm);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002831 return mem_cgroup_charge_common(page, mm, gfp_mask,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002832 MEM_CGROUP_CHARGE_TYPE_ANON);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002833}
2834
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002835/*
2836 * While swap-in, try_charge -> commit or cancel, the page is locked.
2837 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002838 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002839 * "commit()" or removed by "cancel()"
2840 */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002841static int __mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2842 struct page *page,
2843 gfp_t mask,
2844 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002845{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002846 struct mem_cgroup *memcg;
Johannes Weiner90deb782012-07-31 16:45:47 -07002847 struct page_cgroup *pc;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002848 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002849
Johannes Weiner90deb782012-07-31 16:45:47 -07002850 pc = lookup_page_cgroup(page);
2851 /*
2852 * Every swap fault against a single page tries to charge the
2853 * page, bail as early as possible. shmem_unuse() encounters
2854 * already charged pages, too. The USED bit is protected by
2855 * the page lock, which serializes swap cache removal, which
2856 * in turn serializes uncharging.
2857 */
2858 if (PageCgroupUsed(pc))
2859 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002860 if (!do_swap_account)
2861 goto charge_cur_mm;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002862 memcg = try_get_mem_cgroup_from_page(page);
2863 if (!memcg)
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002864 goto charge_cur_mm;
Johannes Weiner72835c82012-01-12 17:18:32 -08002865 *memcgp = memcg;
2866 ret = __mem_cgroup_try_charge(NULL, mask, 1, memcgp, true);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002867 css_put(&memcg->css);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002868 if (ret == -EINTR)
2869 ret = 0;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002870 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002871charge_cur_mm:
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08002872 ret = __mem_cgroup_try_charge(mm, mask, 1, memcgp, true);
2873 if (ret == -EINTR)
2874 ret = 0;
2875 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002876}
2877
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002878int mem_cgroup_try_charge_swapin(struct mm_struct *mm, struct page *page,
2879 gfp_t gfp_mask, struct mem_cgroup **memcgp)
2880{
2881 *memcgp = NULL;
2882 if (mem_cgroup_disabled())
2883 return 0;
Johannes Weinerbdf4f4d2012-07-31 16:45:50 -07002884 /*
2885 * A racing thread's fault, or swapoff, may have already
2886 * updated the pte, and even removed page from swap cache: in
2887 * those cases unuse_pte()'s pte_same() test will fail; but
2888 * there's also a KSM case which does need to charge the page.
2889 */
2890 if (!PageSwapCache(page)) {
2891 int ret;
2892
2893 ret = __mem_cgroup_try_charge(mm, gfp_mask, 1, memcgp, true);
2894 if (ret == -EINTR)
2895 ret = 0;
2896 return ret;
2897 }
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002898 return __mem_cgroup_try_charge_swapin(mm, page, gfp_mask, memcgp);
2899}
2900
Johannes Weiner827a03d2012-07-31 16:45:36 -07002901void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
2902{
2903 if (mem_cgroup_disabled())
2904 return;
2905 if (!memcg)
2906 return;
2907 __mem_cgroup_cancel_charge(memcg, 1);
2908}
2909
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002910static void
Johannes Weiner72835c82012-01-12 17:18:32 -08002911__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *memcg,
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002912 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002913{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002914 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002915 return;
Johannes Weiner72835c82012-01-12 17:18:32 -08002916 if (!memcg)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002917 return;
KAMEZAWA Hiroyuki5a6475a2011-03-23 16:42:42 -07002918
Johannes Weinerce587e62012-04-24 20:22:33 +02002919 __mem_cgroup_commit_charge(memcg, page, 1, ctype, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002920 /*
2921 * Now swap is on-memory. This means this page may be
2922 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002923 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2924 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2925 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002926 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002927 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002928 swp_entry_t ent = {.val = page_private(page)};
Hugh Dickins86493002012-05-29 15:06:52 -07002929 mem_cgroup_uncharge_swap(ent);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002930 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002931}
2932
Johannes Weiner72835c82012-01-12 17:18:32 -08002933void mem_cgroup_commit_charge_swapin(struct page *page,
2934 struct mem_cgroup *memcg)
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002935{
Johannes Weiner72835c82012-01-12 17:18:32 -08002936 __mem_cgroup_commit_charge_swapin(page, memcg,
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07002937 MEM_CGROUP_CHARGE_TYPE_ANON);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002938}
2939
Johannes Weiner827a03d2012-07-31 16:45:36 -07002940int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2941 gfp_t gfp_mask)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002942{
Johannes Weiner827a03d2012-07-31 16:45:36 -07002943 struct mem_cgroup *memcg = NULL;
2944 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
2945 int ret;
2946
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002947 if (mem_cgroup_disabled())
Johannes Weiner827a03d2012-07-31 16:45:36 -07002948 return 0;
2949 if (PageCompound(page))
2950 return 0;
2951
Johannes Weiner827a03d2012-07-31 16:45:36 -07002952 if (!PageSwapCache(page))
2953 ret = mem_cgroup_charge_common(page, mm, gfp_mask, type);
2954 else { /* page is swapcache/shmem */
Johannes Weiner0435a2f2012-07-31 16:45:43 -07002955 ret = __mem_cgroup_try_charge_swapin(mm, page,
2956 gfp_mask, &memcg);
Johannes Weiner827a03d2012-07-31 16:45:36 -07002957 if (!ret)
2958 __mem_cgroup_commit_charge_swapin(page, memcg, type);
2959 }
2960 return ret;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002961}
2962
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002963static void mem_cgroup_do_uncharge(struct mem_cgroup *memcg,
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002964 unsigned int nr_pages,
2965 const enum charge_type ctype)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002966{
2967 struct memcg_batch_info *batch = NULL;
2968 bool uncharge_memsw = true;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002969
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002970 /* If swapout, usage of swap doesn't decrease */
2971 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2972 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002973
2974 batch = &current->memcg_batch;
2975 /*
2976 * In usual, we do css_get() when we remember memcg pointer.
2977 * But in this case, we keep res->usage until end of a series of
2978 * uncharges. Then, it's ok to ignore memcg's refcnt.
2979 */
2980 if (!batch->memcg)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07002981 batch->memcg = memcg;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002982 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002983 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002984 * In those cases, all pages freed continuously can be expected to be in
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002985 * the same cgroup and we have chance to coalesce uncharges.
2986 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2987 * because we want to do uncharge as soon as possible.
2988 */
2989
2990 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2991 goto direct_uncharge;
2992
Johannes Weiner7ec99d62011-03-23 16:42:36 -07002993 if (nr_pages > 1)
Andrea Arcangeliec168512011-01-13 15:46:56 -08002994 goto direct_uncharge;
2995
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002996 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002997 * In typical case, batch->memcg == mem. This means we can
2998 * merge a series of uncharges to an uncharge of res_counter.
2999 * If not, we uncharge res_counter ony by one.
3000 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003001 if (batch->memcg != memcg)
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003002 goto direct_uncharge;
3003 /* remember freed charge and uncharge it later */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003004 batch->nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003005 if (uncharge_memsw)
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003006 batch->memsw_nr_pages++;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003007 return;
3008direct_uncharge:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003009 res_counter_uncharge(&memcg->res, nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003010 if (uncharge_memsw)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003011 res_counter_uncharge(&memcg->memsw, nr_pages * PAGE_SIZE);
3012 if (unlikely(batch->memcg != memcg))
3013 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003014}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08003015
Balbir Singh8697d332008-02-07 00:13:59 -08003016/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003017 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003018 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003019static struct mem_cgroup *
Johannes Weiner0030f532012-07-31 16:45:25 -07003020__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype,
3021 bool end_migration)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003022{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003023 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003024 unsigned int nr_pages = 1;
3025 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003026 bool anon;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003027
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003028 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003029 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07003030
Johannes Weiner0c59b892012-07-31 16:45:31 -07003031 VM_BUG_ON(PageSwapCache(page));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003032
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003033 if (PageTransHuge(page)) {
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003034 nr_pages <<= compound_order(page);
Andrea Arcangeli37c2ac72011-01-13 15:47:16 -08003035 VM_BUG_ON(!PageTransHuge(page));
3036 }
Balbir Singh8697d332008-02-07 00:13:59 -08003037 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08003038 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08003039 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003040 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003041 if (unlikely(!PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003042 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08003043
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003044 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003045
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003046 memcg = pc->mem_cgroup;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003047
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003048 if (!PageCgroupUsed(pc))
3049 goto unlock_out;
3050
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003051 anon = PageAnon(page);
3052
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003053 switch (ctype) {
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003054 case MEM_CGROUP_CHARGE_TYPE_ANON:
KAMEZAWA Hiroyuki2ff76f12012-03-21 16:34:25 -07003055 /*
3056 * Generally PageAnon tells if it's the anon statistics to be
3057 * updated; but sometimes e.g. mem_cgroup_uncharge_page() is
3058 * used before page reached the stage of being marked PageAnon.
3059 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003060 anon = true;
3061 /* fallthrough */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003062 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003063 /* See mem_cgroup_prepare_migration() */
Johannes Weiner0030f532012-07-31 16:45:25 -07003064 if (page_mapped(page))
3065 goto unlock_out;
3066 /*
3067 * Pages under migration may not be uncharged. But
3068 * end_migration() /must/ be the one uncharging the
3069 * unused post-migration page and so it has to call
3070 * here with the migration bit still set. See the
3071 * res_counter handling below.
3072 */
3073 if (!end_migration && PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003074 goto unlock_out;
3075 break;
3076 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
3077 if (!PageAnon(page)) { /* Shared memory */
3078 if (page->mapping && !page_is_file_cache(page))
3079 goto unlock_out;
3080 } else if (page_mapped(page)) /* Anon */
3081 goto unlock_out;
3082 break;
3083 default:
3084 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003085 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003086
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003087 mem_cgroup_charge_statistics(memcg, anon, -nr_pages);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003088
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003089 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08003090 /*
3091 * pc->mem_cgroup is not cleared here. It will be accessed when it's
3092 * freed from LRU. This is safe because uncharged page is expected not
3093 * to be reused (freed soon). Exception is SwapCache, it's handled by
3094 * special functions.
3095 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003096
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003097 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003098 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003099 * even after unlock, we have memcg->res.usage here and this memcg
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003100 * will never be freed.
3101 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003102 memcg_check_events(memcg, page);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003103 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003104 mem_cgroup_swap_statistics(memcg, true);
3105 mem_cgroup_get(memcg);
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003106 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003107 /*
3108 * Migration does not charge the res_counter for the
3109 * replacement page, so leave it alone when phasing out the
3110 * page that is unused after the migration.
3111 */
3112 if (!end_migration && !mem_cgroup_is_root(memcg))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003113 mem_cgroup_do_uncharge(memcg, nr_pages, ctype);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003114
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003115 return memcg;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003116
3117unlock_out:
3118 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003119 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08003120}
3121
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003122void mem_cgroup_uncharge_page(struct page *page)
3123{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003124 /* early check. */
3125 if (page_mapped(page))
3126 return;
Johannes Weiner40f23a22012-01-12 17:18:45 -08003127 VM_BUG_ON(page->mapping && !PageAnon(page));
Johannes Weiner0c59b892012-07-31 16:45:31 -07003128 if (PageSwapCache(page))
3129 return;
Johannes Weiner0030f532012-07-31 16:45:25 -07003130 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_ANON, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003131}
3132
3133void mem_cgroup_uncharge_cache_page(struct page *page)
3134{
3135 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07003136 VM_BUG_ON(page->mapping);
Johannes Weiner0030f532012-07-31 16:45:25 -07003137 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE, false);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003138}
3139
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003140/*
3141 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
3142 * In that cases, pages are freed continuously and we can expect pages
3143 * are in the same memcg. All these calls itself limits the number of
3144 * pages freed at once, then uncharge_start/end() is called properly.
3145 * This may be called prural(2) times in a context,
3146 */
3147
3148void mem_cgroup_uncharge_start(void)
3149{
3150 current->memcg_batch.do_batch++;
3151 /* We can do nest. */
3152 if (current->memcg_batch.do_batch == 1) {
3153 current->memcg_batch.memcg = NULL;
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003154 current->memcg_batch.nr_pages = 0;
3155 current->memcg_batch.memsw_nr_pages = 0;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003156 }
3157}
3158
3159void mem_cgroup_uncharge_end(void)
3160{
3161 struct memcg_batch_info *batch = &current->memcg_batch;
3162
3163 if (!batch->do_batch)
3164 return;
3165
3166 batch->do_batch--;
3167 if (batch->do_batch) /* If stacked, do nothing. */
3168 return;
3169
3170 if (!batch->memcg)
3171 return;
3172 /*
3173 * This "batch->memcg" is valid without any css_get/put etc...
3174 * bacause we hide charges behind us.
3175 */
Johannes Weiner7ffd4ca2011-03-23 16:42:35 -07003176 if (batch->nr_pages)
3177 res_counter_uncharge(&batch->memcg->res,
3178 batch->nr_pages * PAGE_SIZE);
3179 if (batch->memsw_nr_pages)
3180 res_counter_uncharge(&batch->memcg->memsw,
3181 batch->memsw_nr_pages * PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003182 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08003183 /* forget this pointer (for sanity check) */
3184 batch->memcg = NULL;
3185}
3186
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003187#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003188/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003189 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003190 * memcg information is recorded to swap_cgroup of "ent"
3191 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003192void
3193mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003194{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003195 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003196 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003197
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003198 if (!swapout) /* this was a swap cache but the swap is unused ! */
3199 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
3200
Johannes Weiner0030f532012-07-31 16:45:25 -07003201 memcg = __mem_cgroup_uncharge_common(page, ctype, false);
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07003202
KAMEZAWA Hiroyukif75ca962010-08-10 18:03:02 -07003203 /*
3204 * record memcg information, if swapout && memcg != NULL,
3205 * mem_cgroup_get() was called in uncharge().
3206 */
3207 if (do_swap_account && swapout && memcg)
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003208 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003209}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07003210#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08003211
Andrew Mortonc255a452012-07-31 16:43:02 -07003212#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003213/*
3214 * called from swap_entry_free(). remove record in swap_cgroup and
3215 * uncharge "memsw" account.
3216 */
3217void mem_cgroup_uncharge_swap(swp_entry_t ent)
3218{
3219 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003220 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003221
3222 if (!do_swap_account)
3223 return;
3224
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003225 id = swap_cgroup_record(ent, 0);
3226 rcu_read_lock();
3227 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003228 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003229 /*
3230 * We uncharge this because swap is freed.
3231 * This memcg can be obsolete one. We avoid calling css_tryget
3232 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003233 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07003234 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003235 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003236 mem_cgroup_put(memcg);
3237 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07003238 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003239}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003240
3241/**
3242 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
3243 * @entry: swap entry to be moved
3244 * @from: mem_cgroup which the entry is moved from
3245 * @to: mem_cgroup which the entry is moved to
3246 *
3247 * It succeeds only when the swap_cgroup's record for this entry is the same
3248 * as the mem_cgroup's id of @from.
3249 *
3250 * Returns 0 on success, -EINVAL on failure.
3251 *
3252 * The caller must have charged to @to, IOW, called res_counter_charge() about
3253 * both res and memsw, and called css_get().
3254 */
3255static int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003256 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003257{
3258 unsigned short old_id, new_id;
3259
3260 old_id = css_id(&from->css);
3261 new_id = css_id(&to->css);
3262
3263 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08003264 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003265 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003266 /*
3267 * This function is only called from task migration context now.
3268 * It postpones res_counter and refcount handling till the end
3269 * of task migration(mem_cgroup_clear_mc()) for performance
3270 * improvement. But we cannot postpone mem_cgroup_get(to)
3271 * because if the process that has been moved to @to does
3272 * swap-in, the refcount of @to might be decreased to 0.
3273 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08003274 mem_cgroup_get(to);
Daisuke Nishimura02491442010-03-10 15:22:17 -08003275 return 0;
3276 }
3277 return -EINVAL;
3278}
3279#else
3280static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Hugh Dickinse91cbb42012-05-29 15:06:51 -07003281 struct mem_cgroup *from, struct mem_cgroup *to)
Daisuke Nishimura02491442010-03-10 15:22:17 -08003282{
3283 return -EINVAL;
3284}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003285#endif
3286
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003287/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003288 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
3289 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003290 */
Johannes Weiner0030f532012-07-31 16:45:25 -07003291void mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
3292 struct mem_cgroup **memcgp)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003293{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003294 struct mem_cgroup *memcg = NULL;
Johannes Weiner7ec99d62011-03-23 16:42:36 -07003295 struct page_cgroup *pc;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003296 enum charge_type ctype;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003297
Johannes Weiner72835c82012-01-12 17:18:32 -08003298 *memcgp = NULL;
KAMEZAWA Hiroyuki56039ef2011-03-23 16:42:19 -07003299
Andrea Arcangeliec168512011-01-13 15:46:56 -08003300 VM_BUG_ON(PageTransHuge(page));
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003301 if (mem_cgroup_disabled())
Johannes Weiner0030f532012-07-31 16:45:25 -07003302 return;
Balbir Singh40779602008-04-04 14:29:59 -07003303
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003304 pc = lookup_page_cgroup(page);
3305 lock_page_cgroup(pc);
3306 if (PageCgroupUsed(pc)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003307 memcg = pc->mem_cgroup;
3308 css_get(&memcg->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003309 /*
3310 * At migrating an anonymous page, its mapcount goes down
3311 * to 0 and uncharge() will be called. But, even if it's fully
3312 * unmapped, migration may fail and this page has to be
3313 * charged again. We set MIGRATION flag here and delay uncharge
3314 * until end_migration() is called
3315 *
3316 * Corner Case Thinking
3317 * A)
3318 * When the old page was mapped as Anon and it's unmap-and-freed
3319 * while migration was ongoing.
3320 * If unmap finds the old page, uncharge() of it will be delayed
3321 * until end_migration(). If unmap finds a new page, it's
3322 * uncharged when it make mapcount to be 1->0. If unmap code
3323 * finds swap_migration_entry, the new page will not be mapped
3324 * and end_migration() will find it(mapcount==0).
3325 *
3326 * B)
3327 * When the old page was mapped but migraion fails, the kernel
3328 * remaps it. A charge for it is kept by MIGRATION flag even
3329 * if mapcount goes down to 0. We can do remap successfully
3330 * without charging it again.
3331 *
3332 * C)
3333 * The "old" page is under lock_page() until the end of
3334 * migration, so, the old page itself will not be swapped-out.
3335 * If the new page is swapped out before end_migraton, our
3336 * hook to usual swap-out path will catch the event.
3337 */
3338 if (PageAnon(page))
3339 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08003340 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003341 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003342 /*
3343 * If the page is not charged at this point,
3344 * we return here.
3345 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003346 if (!memcg)
Johannes Weiner0030f532012-07-31 16:45:25 -07003347 return;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003348
Johannes Weiner72835c82012-01-12 17:18:32 -08003349 *memcgp = memcg;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003350 /*
3351 * We charge new page before it's used/mapped. So, even if unlock_page()
3352 * is called before end_migration, we can catch all events on this new
3353 * page. In the case new page is migrated but not remapped, new page's
3354 * mapcount will be finally 0 and we call uncharge in end_migration().
3355 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003356 if (PageAnon(page))
Kamezawa Hiroyuki41326c12012-07-31 16:41:40 -07003357 ctype = MEM_CGROUP_CHARGE_TYPE_ANON;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003358 else
Johannes Weiner62ba7442012-07-31 16:45:39 -07003359 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Johannes Weiner0030f532012-07-31 16:45:25 -07003360 /*
3361 * The page is committed to the memcg, but it's not actually
3362 * charged to the res_counter since we plan on replacing the
3363 * old one and only one page is going to be left afterwards.
3364 */
Johannes Weinerce587e62012-04-24 20:22:33 +02003365 __mem_cgroup_commit_charge(memcg, newpage, 1, ctype, false);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003366}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08003367
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003368/* remove redundant charge if migration failed*/
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003369void mem_cgroup_end_migration(struct mem_cgroup *memcg,
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003370 struct page *oldpage, struct page *newpage, bool migration_ok)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07003371{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003372 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003373 struct page_cgroup *pc;
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003374 bool anon;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003375
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003376 if (!memcg)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003377 return;
Tejun Heob25ed602012-11-05 09:16:59 -08003378
Daisuke Nishimura50de1dd2011-01-13 15:47:43 -08003379 if (!migration_ok) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003380 used = oldpage;
3381 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003382 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003383 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003384 unused = oldpage;
3385 }
Johannes Weiner0030f532012-07-31 16:45:25 -07003386 anon = PageAnon(used);
Johannes Weiner7d188952012-07-31 16:45:34 -07003387 __mem_cgroup_uncharge_common(unused,
3388 anon ? MEM_CGROUP_CHARGE_TYPE_ANON
3389 : MEM_CGROUP_CHARGE_TYPE_CACHE,
3390 true);
Johannes Weiner0030f532012-07-31 16:45:25 -07003391 css_put(&memcg->css);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003392 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003393 * We disallowed uncharge of pages under migration because mapcount
3394 * of the page goes down to zero, temporarly.
3395 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07003396 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003397 pc = lookup_page_cgroup(oldpage);
3398 lock_page_cgroup(pc);
3399 ClearPageCgroupMigration(pc);
3400 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003401
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003402 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003403 * If a page is a file cache, radix-tree replacement is very atomic
3404 * and we can skip this check. When it was an Anon page, its mapcount
3405 * goes down to 0. But because we added MIGRATION flage, it's not
3406 * uncharged yet. There are several case but page->mapcount check
3407 * and USED bit check in mem_cgroup_uncharge_page() will do enough
3408 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08003409 */
KAMEZAWA Hiroyukib24028572012-03-21 16:34:22 -07003410 if (anon)
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07003411 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08003412}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003413
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003414/*
3415 * At replace page cache, newpage is not under any memcg but it's on
3416 * LRU. So, this function doesn't touch res_counter but handles LRU
3417 * in correct way. Both pages are locked so we cannot race with uncharge.
3418 */
3419void mem_cgroup_replace_page_cache(struct page *oldpage,
3420 struct page *newpage)
3421{
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003422 struct mem_cgroup *memcg = NULL;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003423 struct page_cgroup *pc;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003424 enum charge_type type = MEM_CGROUP_CHARGE_TYPE_CACHE;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003425
3426 if (mem_cgroup_disabled())
3427 return;
3428
3429 pc = lookup_page_cgroup(oldpage);
3430 /* fix accounting on old pages */
3431 lock_page_cgroup(pc);
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003432 if (PageCgroupUsed(pc)) {
3433 memcg = pc->mem_cgroup;
3434 mem_cgroup_charge_statistics(memcg, false, -1);
3435 ClearPageCgroupUsed(pc);
3436 }
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003437 unlock_page_cgroup(pc);
3438
Hugh Dickinsbde05d12012-05-29 15:06:38 -07003439 /*
3440 * When called from shmem_replace_page(), in some cases the
3441 * oldpage has already been charged, and in some cases not.
3442 */
3443 if (!memcg)
3444 return;
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003445 /*
3446 * Even if newpage->mapping was NULL before starting replacement,
3447 * the newpage may be on LRU(or pagevec for LRU) already. We lock
3448 * LRU while we overwrite pc->mem_cgroup.
3449 */
Johannes Weinerce587e62012-04-24 20:22:33 +02003450 __mem_cgroup_commit_charge(memcg, newpage, 1, type, true);
KAMEZAWA Hiroyukiab936cb2012-01-12 17:17:44 -08003451}
3452
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003453#ifdef CONFIG_DEBUG_VM
3454static struct page_cgroup *lookup_page_cgroup_used(struct page *page)
3455{
3456 struct page_cgroup *pc;
3457
3458 pc = lookup_page_cgroup(page);
Johannes Weinercfa44942012-01-12 17:18:38 -08003459 /*
3460 * Can be NULL while feeding pages into the page allocator for
3461 * the first time, i.e. during boot or memory hotplug;
3462 * or when mem_cgroup_disabled().
3463 */
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003464 if (likely(pc) && PageCgroupUsed(pc))
3465 return pc;
3466 return NULL;
3467}
3468
3469bool mem_cgroup_bad_page_check(struct page *page)
3470{
3471 if (mem_cgroup_disabled())
3472 return false;
3473
3474 return lookup_page_cgroup_used(page) != NULL;
3475}
3476
3477void mem_cgroup_print_bad_page(struct page *page)
3478{
3479 struct page_cgroup *pc;
3480
3481 pc = lookup_page_cgroup_used(page);
3482 if (pc) {
Hugh Dickins90b3fea2012-01-12 17:19:54 -08003483 printk(KERN_ALERT "pc:%p pc->flags:%lx pc->mem_cgroup:%p\n",
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003484 pc, pc->flags, pc->mem_cgroup);
Daisuke Nishimuraf212ad72011-03-23 16:42:25 -07003485 }
3486}
3487#endif
3488
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003489static DEFINE_MUTEX(set_limit_mutex);
3490
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08003491static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003492 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003493{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003494 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003495 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003496 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003497 int children = mem_cgroup_count_children(memcg);
3498 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003499 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003500
3501 /*
3502 * For keeping hierarchical_reclaim simple, how long we should retry
3503 * is depends on callers. We set our retry-count to be function
3504 * of # of children which we should visit in this loop.
3505 */
3506 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
3507
3508 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003509
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003510 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003511 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003512 if (signal_pending(current)) {
3513 ret = -EINTR;
3514 break;
3515 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003516 /*
3517 * Rather than hide all in some function, I do this in
3518 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003519 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003520 */
3521 mutex_lock(&set_limit_mutex);
3522 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3523 if (memswlimit < val) {
3524 ret = -EINVAL;
3525 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003526 break;
3527 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003528
3529 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3530 if (memlimit < val)
3531 enlarge = 1;
3532
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003533 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003534 if (!ret) {
3535 if (memswlimit == val)
3536 memcg->memsw_is_minimum = true;
3537 else
3538 memcg->memsw_is_minimum = false;
3539 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003540 mutex_unlock(&set_limit_mutex);
3541
3542 if (!ret)
3543 break;
3544
Johannes Weiner56600482012-01-12 17:17:59 -08003545 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3546 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003547 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
3548 /* Usage is reduced ? */
3549 if (curusage >= oldusage)
3550 retry_count--;
3551 else
3552 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003553 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003554 if (!ret && enlarge)
3555 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08003556
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003557 return ret;
3558}
3559
Li Zefan338c8432009-06-17 16:27:15 -07003560static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
3561 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003562{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003563 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003564 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003565 int children = mem_cgroup_count_children(memcg);
3566 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003567 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003568
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003569 /* see mem_cgroup_resize_res_limit */
3570 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
3571 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003572 while (retry_count) {
3573 if (signal_pending(current)) {
3574 ret = -EINTR;
3575 break;
3576 }
3577 /*
3578 * Rather than hide all in some function, I do this in
3579 * open coded manner. You see what this really does.
Wanpeng Liaaad1532012-07-31 16:43:23 -07003580 * We have to guarantee memcg->res.limit <= memcg->memsw.limit.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003581 */
3582 mutex_lock(&set_limit_mutex);
3583 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3584 if (memlimit > val) {
3585 ret = -EINVAL;
3586 mutex_unlock(&set_limit_mutex);
3587 break;
3588 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003589 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3590 if (memswlimit < val)
3591 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003592 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07003593 if (!ret) {
3594 if (memlimit == val)
3595 memcg->memsw_is_minimum = true;
3596 else
3597 memcg->memsw_is_minimum = false;
3598 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003599 mutex_unlock(&set_limit_mutex);
3600
3601 if (!ret)
3602 break;
3603
Johannes Weiner56600482012-01-12 17:17:59 -08003604 mem_cgroup_reclaim(memcg, GFP_KERNEL,
3605 MEM_CGROUP_RECLAIM_NOSWAP |
3606 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003607 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003608 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003609 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003610 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07003611 else
3612 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003613 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003614 if (!ret && enlarge)
3615 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003616 return ret;
3617}
3618
Balbir Singh4e416952009-09-23 15:56:39 -07003619unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
Ying Han0ae5e892011-05-26 16:25:25 -07003620 gfp_t gfp_mask,
3621 unsigned long *total_scanned)
Balbir Singh4e416952009-09-23 15:56:39 -07003622{
3623 unsigned long nr_reclaimed = 0;
3624 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
3625 unsigned long reclaimed;
3626 int loop = 0;
3627 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003628 unsigned long long excess;
Ying Han0ae5e892011-05-26 16:25:25 -07003629 unsigned long nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003630
3631 if (order > 0)
3632 return 0;
3633
KOSAKI Motohiro00918b62010-08-10 18:03:05 -07003634 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
Balbir Singh4e416952009-09-23 15:56:39 -07003635 /*
3636 * This loop can run a while, specially if mem_cgroup's continuously
3637 * keep exceeding their soft limit and putting the system under
3638 * pressure
3639 */
3640 do {
3641 if (next_mz)
3642 mz = next_mz;
3643 else
3644 mz = mem_cgroup_largest_soft_limit_node(mctz);
3645 if (!mz)
3646 break;
3647
Ying Han0ae5e892011-05-26 16:25:25 -07003648 nr_scanned = 0;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003649 reclaimed = mem_cgroup_soft_reclaim(mz->memcg, zone,
Johannes Weiner56600482012-01-12 17:17:59 -08003650 gfp_mask, &nr_scanned);
Balbir Singh4e416952009-09-23 15:56:39 -07003651 nr_reclaimed += reclaimed;
Ying Han0ae5e892011-05-26 16:25:25 -07003652 *total_scanned += nr_scanned;
Balbir Singh4e416952009-09-23 15:56:39 -07003653 spin_lock(&mctz->lock);
3654
3655 /*
3656 * If we failed to reclaim anything from this memory cgroup
3657 * it is time to move on to the next cgroup
3658 */
3659 next_mz = NULL;
3660 if (!reclaimed) {
3661 do {
3662 /*
3663 * Loop until we find yet another one.
3664 *
3665 * By the time we get the soft_limit lock
3666 * again, someone might have aded the
3667 * group back on the RB tree. Iterate to
3668 * make sure we get a different mem.
3669 * mem_cgroup_largest_soft_limit_node returns
3670 * NULL if no other cgroup is present on
3671 * the tree
3672 */
3673 next_mz =
3674 __mem_cgroup_largest_soft_limit_node(mctz);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003675 if (next_mz == mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003676 css_put(&next_mz->memcg->css);
Michal Hocko39cc98f2011-05-26 16:25:28 -07003677 else /* next_mz == NULL or other memcg */
Balbir Singh4e416952009-09-23 15:56:39 -07003678 break;
3679 } while (1);
3680 }
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003681 __mem_cgroup_remove_exceeded(mz->memcg, mz, mctz);
3682 excess = res_counter_soft_limit_excess(&mz->memcg->res);
Balbir Singh4e416952009-09-23 15:56:39 -07003683 /*
3684 * One school of thought says that we should not add
3685 * back the node to the tree if reclaim returns 0.
3686 * But our reclaim could return 0, simply because due
3687 * to priority we are exposing a smaller subset of
3688 * memory to reclaim from. Consider this as a longer
3689 * term TODO.
3690 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07003691 /* If excess == 0, no tree ops */
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003692 __mem_cgroup_insert_exceeded(mz->memcg, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07003693 spin_unlock(&mctz->lock);
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003694 css_put(&mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003695 loop++;
3696 /*
3697 * Could not reclaim anything and there are no more
3698 * mem cgroups to try or we seem to be looping without
3699 * reclaiming anything.
3700 */
3701 if (!nr_reclaimed &&
3702 (next_mz == NULL ||
3703 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3704 break;
3705 } while (!nr_reclaimed);
3706 if (next_mz)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07003707 css_put(&next_mz->memcg->css);
Balbir Singh4e416952009-09-23 15:56:39 -07003708 return nr_reclaimed;
3709}
3710
Michal Hocko2ef37d32012-10-26 13:37:30 +02003711/**
3712 * mem_cgroup_force_empty_list - clears LRU of a group
3713 * @memcg: group to clear
3714 * @node: NUMA node
3715 * @zid: zone id
3716 * @lru: lru to to clear
3717 *
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003718 * Traverse a specified page_cgroup list and try to drop them all. This doesn't
Michal Hocko2ef37d32012-10-26 13:37:30 +02003719 * reclaim the pages page themselves - pages are moved to the parent (or root)
3720 * group.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003721 */
Michal Hocko2ef37d32012-10-26 13:37:30 +02003722static void mem_cgroup_force_empty_list(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003723 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003724{
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003725 struct lruvec *lruvec;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003726 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003727 struct list_head *list;
Johannes Weiner925b7672012-01-12 17:18:15 -08003728 struct page *busy;
3729 struct zone *zone;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08003730
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003731 zone = &NODE_DATA(node)->node_zones[zid];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08003732 lruvec = mem_cgroup_zone_lruvec(zone, memcg);
3733 list = &lruvec->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003734
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003735 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003736 do {
Johannes Weiner925b7672012-01-12 17:18:15 -08003737 struct page_cgroup *pc;
Johannes Weiner5564e882011-03-23 16:42:29 -07003738 struct page *page;
3739
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003740 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003741 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003742 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003743 break;
3744 }
Johannes Weiner925b7672012-01-12 17:18:15 -08003745 page = list_entry(list->prev, struct page, lru);
3746 if (busy == page) {
3747 list_move(&page->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08003748 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003749 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003750 continue;
3751 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003752 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003753
Johannes Weiner925b7672012-01-12 17:18:15 -08003754 pc = lookup_page_cgroup(page);
Johannes Weiner5564e882011-03-23 16:42:29 -07003755
KAMEZAWA Hiroyuki3c935d12012-07-31 16:42:46 -07003756 if (mem_cgroup_move_parent(page, pc, memcg)) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003757 /* found lock contention or "pc" is obsolete. */
Johannes Weiner925b7672012-01-12 17:18:15 -08003758 busy = page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003759 cond_resched();
3760 } else
3761 busy = NULL;
Michal Hocko2ef37d32012-10-26 13:37:30 +02003762 } while (!list_empty(list));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003763}
3764
3765/*
Michal Hockoc26251f2012-10-26 13:37:28 +02003766 * make mem_cgroup's charge to be 0 if there is no task by moving
3767 * all the charges and pages to the parent.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003768 * This enables deleting this mem_cgroup.
Michal Hockoc26251f2012-10-26 13:37:28 +02003769 *
3770 * Caller is responsible for holding css reference on the memcg.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003771 */
Michal Hockoab5196c2012-10-26 13:37:32 +02003772static void mem_cgroup_reparent_charges(struct mem_cgroup *memcg)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003773{
Michal Hockoc26251f2012-10-26 13:37:28 +02003774 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08003775
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003776 do {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003777 /* This is for making all *used* pages to be on LRU. */
3778 lru_add_drain_all();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003779 drain_all_stock_sync(memcg);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003780 mem_cgroup_start_move(memcg);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08003781 for_each_node_state(node, N_MEMORY) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003782 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsf156ab92012-03-21 16:34:19 -07003783 enum lru_list lru;
3784 for_each_lru(lru) {
Michal Hocko2ef37d32012-10-26 13:37:30 +02003785 mem_cgroup_force_empty_list(memcg,
Hugh Dickinsf156ab92012-03-21 16:34:19 -07003786 node, zid, lru);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003787 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003788 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003789 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003790 mem_cgroup_end_move(memcg);
3791 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07003792 cond_resched();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003793
Michal Hocko2ef37d32012-10-26 13:37:30 +02003794 /*
3795 * This is a safety check because mem_cgroup_force_empty_list
3796 * could have raced with mem_cgroup_replace_page_cache callers
3797 * so the lru seemed empty but the page could have been added
3798 * right after the check. RES_USAGE should be safe as we always
3799 * charge before adding to the LRU.
3800 */
3801 } while (res_counter_read_u64(&memcg->res, RES_USAGE) > 0);
Michal Hockoc26251f2012-10-26 13:37:28 +02003802}
3803
3804/*
3805 * Reclaims as many pages from the given memcg as possible and moves
3806 * the rest to the parent.
3807 *
3808 * Caller is responsible for holding css reference for memcg.
3809 */
3810static int mem_cgroup_force_empty(struct mem_cgroup *memcg)
3811{
3812 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
3813 struct cgroup *cgrp = memcg->css.cgroup;
3814
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003815 /* returns EBUSY if there is a task or if we come here twice. */
Michal Hockoc26251f2012-10-26 13:37:28 +02003816 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
3817 return -EBUSY;
3818
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003819 /* we call try-to-free pages for make this cgroup empty */
3820 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003821 /* try to free all pages in this cgroup */
Glauber Costa569530f2012-04-12 12:49:13 -07003822 while (nr_retries && res_counter_read_u64(&memcg->res, RES_USAGE) > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003823 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003824
Michal Hockoc26251f2012-10-26 13:37:28 +02003825 if (signal_pending(current))
3826 return -EINTR;
3827
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003828 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL,
Johannes Weiner185efc02011-09-14 16:21:58 -07003829 false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003830 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003831 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003832 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003833 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003834 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003835
3836 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003837 lru_add_drain();
Michal Hockoab5196c2012-10-26 13:37:32 +02003838 mem_cgroup_reparent_charges(memcg);
3839
3840 return 0;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003841}
3842
Kirill A. Shutemov6bbda352012-05-29 15:06:55 -07003843static int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003844{
Michal Hockoc26251f2012-10-26 13:37:28 +02003845 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
3846 int ret;
3847
Michal Hockod8423012012-10-26 13:37:29 +02003848 if (mem_cgroup_is_root(memcg))
3849 return -EINVAL;
Michal Hockoc26251f2012-10-26 13:37:28 +02003850 css_get(&memcg->css);
3851 ret = mem_cgroup_force_empty(memcg);
3852 css_put(&memcg->css);
3853
3854 return ret;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003855}
3856
3857
Balbir Singh18f59ea2009-01-07 18:08:07 -08003858static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3859{
3860 return mem_cgroup_from_cont(cont)->use_hierarchy;
3861}
3862
3863static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3864 u64 val)
3865{
3866 int retval = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003867 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003868 struct cgroup *parent = cont->parent;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003869 struct mem_cgroup *parent_memcg = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003870
3871 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003872 parent_memcg = mem_cgroup_from_cont(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003873
3874 cgroup_lock();
Glauber Costa567fb432012-07-31 16:43:07 -07003875
3876 if (memcg->use_hierarchy == val)
3877 goto out;
3878
Balbir Singh18f59ea2009-01-07 18:08:07 -08003879 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003880 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003881 * in the child subtrees. If it is unset, then the change can
3882 * occur, provided the current cgroup has no children.
3883 *
3884 * For the root cgroup, parent_mem is NULL, we allow value to be
3885 * set if there are no children.
3886 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003887 if ((!parent_memcg || !parent_memcg->use_hierarchy) &&
Balbir Singh18f59ea2009-01-07 18:08:07 -08003888 (val == 1 || val == 0)) {
3889 if (list_empty(&cont->children))
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003890 memcg->use_hierarchy = val;
Balbir Singh18f59ea2009-01-07 18:08:07 -08003891 else
3892 retval = -EBUSY;
3893 } else
3894 retval = -EINVAL;
Glauber Costa567fb432012-07-31 16:43:07 -07003895
3896out:
Balbir Singh18f59ea2009-01-07 18:08:07 -08003897 cgroup_unlock();
3898
3899 return retval;
3900}
3901
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003902
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003903static unsigned long mem_cgroup_recursive_stat(struct mem_cgroup *memcg,
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003904 enum mem_cgroup_stat_index idx)
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003905{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003906 struct mem_cgroup *iter;
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003907 long val = 0;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003908
Johannes Weiner7a159cc2011-03-23 16:42:38 -07003909 /* Per-cpu values can be negative, use a signed accumulator */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003910 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003911 val += mem_cgroup_read_stat(iter, idx);
3912
3913 if (val < 0) /* race ? */
3914 val = 0;
3915 return val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003916}
3917
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003918static inline u64 mem_cgroup_usage(struct mem_cgroup *memcg, bool swap)
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003919{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003920 u64 val;
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003921
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003922 if (!mem_cgroup_is_root(memcg)) {
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003923 if (!swap)
Glauber Costa65c64ce2011-12-22 01:02:27 +00003924 return res_counter_read_u64(&memcg->res, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003925 else
Glauber Costa65c64ce2011-12-22 01:02:27 +00003926 return res_counter_read_u64(&memcg->memsw, RES_USAGE);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003927 }
3928
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003929 val = mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_CACHE);
3930 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_RSS);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003931
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07003932 if (swap)
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07003933 val += mem_cgroup_recursive_stat(memcg, MEM_CGROUP_STAT_SWAP);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003934
3935 return val << PAGE_SHIFT;
3936}
3937
Tejun Heoaf36f902012-04-01 12:09:55 -07003938static ssize_t mem_cgroup_read(struct cgroup *cont, struct cftype *cft,
3939 struct file *file, char __user *buf,
3940 size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003941{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003942 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Tejun Heoaf36f902012-04-01 12:09:55 -07003943 char str[64];
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003944 u64 val;
Tejun Heoaf36f902012-04-01 12:09:55 -07003945 int type, name, len;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003946
3947 type = MEMFILE_TYPE(cft->private);
3948 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07003949
3950 if (!do_swap_account && type == _MEMSWAP)
3951 return -EOPNOTSUPP;
3952
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003953 switch (type) {
3954 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003955 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003956 val = mem_cgroup_usage(memcg, false);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003957 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003958 val = res_counter_read_u64(&memcg->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003959 break;
3960 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003961 if (name == RES_USAGE)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003962 val = mem_cgroup_usage(memcg, true);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003963 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07003964 val = res_counter_read_u64(&memcg->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003965 break;
3966 default:
3967 BUG();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003968 }
Tejun Heoaf36f902012-04-01 12:09:55 -07003969
3970 len = scnprintf(str, sizeof(str), "%llu\n", (unsigned long long)val);
3971 return simple_read_from_buffer(buf, nbytes, ppos, str, len);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003972}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003973/*
3974 * The user of this function is...
3975 * RES_LIMIT.
3976 */
Paul Menage856c13a2008-07-25 01:47:04 -07003977static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3978 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003979{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003980 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003981 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003982 unsigned long long val;
3983 int ret;
3984
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003985 type = MEMFILE_TYPE(cft->private);
3986 name = MEMFILE_ATTR(cft->private);
Tejun Heoaf36f902012-04-01 12:09:55 -07003987
3988 if (!do_swap_account && type == _MEMSWAP)
3989 return -EOPNOTSUPP;
3990
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003991 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003992 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003993 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3994 ret = -EINVAL;
3995 break;
3996 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003997 /* This function does all necessary parse...reuse it */
3998 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003999 if (ret)
4000 break;
4001 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004002 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004003 else
4004 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004005 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07004006 case RES_SOFT_LIMIT:
4007 ret = res_counter_memparse_write_strategy(buffer, &val);
4008 if (ret)
4009 break;
4010 /*
4011 * For memsw, soft limits are hard to implement in terms
4012 * of semantics, for now, we support soft limits for
4013 * control without swap
4014 */
4015 if (type == _MEM)
4016 ret = res_counter_set_soft_limit(&memcg->res, val);
4017 else
4018 ret = -EINVAL;
4019 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07004020 default:
4021 ret = -EINVAL; /* should be BUG() ? */
4022 break;
4023 }
4024 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004025}
4026
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004027static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
4028 unsigned long long *mem_limit, unsigned long long *memsw_limit)
4029{
4030 struct cgroup *cgroup;
4031 unsigned long long min_limit, min_memsw_limit, tmp;
4032
4033 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
4034 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4035 cgroup = memcg->css.cgroup;
4036 if (!memcg->use_hierarchy)
4037 goto out;
4038
4039 while (cgroup->parent) {
4040 cgroup = cgroup->parent;
4041 memcg = mem_cgroup_from_cont(cgroup);
4042 if (!memcg->use_hierarchy)
4043 break;
4044 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
4045 min_limit = min(min_limit, tmp);
4046 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
4047 min_memsw_limit = min(min_memsw_limit, tmp);
4048 }
4049out:
4050 *mem_limit = min_limit;
4051 *memsw_limit = min_memsw_limit;
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004052}
4053
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004054static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004055{
Tejun Heoaf36f902012-04-01 12:09:55 -07004056 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004057 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004058
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004059 type = MEMFILE_TYPE(event);
4060 name = MEMFILE_ATTR(event);
Tejun Heoaf36f902012-04-01 12:09:55 -07004061
4062 if (!do_swap_account && type == _MEMSWAP)
4063 return -EOPNOTSUPP;
4064
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004065 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004066 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004067 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004068 res_counter_reset_max(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004069 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004070 res_counter_reset_max(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004071 break;
4072 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004073 if (type == _MEM)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004074 res_counter_reset_failcnt(&memcg->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004075 else
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004076 res_counter_reset_failcnt(&memcg->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004077 break;
4078 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07004079
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07004080 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004081}
4082
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004083static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
4084 struct cftype *cft)
4085{
4086 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
4087}
4088
Daisuke Nishimura02491442010-03-10 15:22:17 -08004089#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004090static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4091 struct cftype *cft, u64 val)
4092{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004093 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004094
4095 if (val >= (1 << NR_MOVE_TYPE))
4096 return -EINVAL;
4097 /*
4098 * We check this value several times in both in can_attach() and
4099 * attach(), so we need cgroup lock to prevent this value from being
4100 * inconsistent.
4101 */
4102 cgroup_lock();
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004103 memcg->move_charge_at_immigrate = val;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004104 cgroup_unlock();
4105
4106 return 0;
4107}
Daisuke Nishimura02491442010-03-10 15:22:17 -08004108#else
4109static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
4110 struct cftype *cft, u64 val)
4111{
4112 return -ENOSYS;
4113}
4114#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004115
Ying Han406eb0c2011-05-26 16:25:37 -07004116#ifdef CONFIG_NUMA
Wanpeng Liab215882012-07-31 16:43:09 -07004117static int memcg_numa_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weinerfada52c2012-05-29 15:07:06 -07004118 struct seq_file *m)
Ying Han406eb0c2011-05-26 16:25:37 -07004119{
4120 int nid;
4121 unsigned long total_nr, file_nr, anon_nr, unevictable_nr;
4122 unsigned long node_nr;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004123 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Ying Han406eb0c2011-05-26 16:25:37 -07004124
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004125 total_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004126 seq_printf(m, "total=%lu", total_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004127 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004128 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid, LRU_ALL);
Ying Han406eb0c2011-05-26 16:25:37 -07004129 seq_printf(m, " N%d=%lu", nid, node_nr);
4130 }
4131 seq_putc(m, '\n');
4132
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004133 file_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004134 seq_printf(m, "file=%lu", file_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004135 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004136 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004137 LRU_ALL_FILE);
Ying Han406eb0c2011-05-26 16:25:37 -07004138 seq_printf(m, " N%d=%lu", nid, node_nr);
4139 }
4140 seq_putc(m, '\n');
4141
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004142 anon_nr = mem_cgroup_nr_lru_pages(memcg, LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004143 seq_printf(m, "anon=%lu", anon_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004144 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004145 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004146 LRU_ALL_ANON);
Ying Han406eb0c2011-05-26 16:25:37 -07004147 seq_printf(m, " N%d=%lu", nid, node_nr);
4148 }
4149 seq_putc(m, '\n');
4150
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004151 unevictable_nr = mem_cgroup_nr_lru_pages(memcg, BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004152 seq_printf(m, "unevictable=%lu", unevictable_nr);
Lai Jiangshan31aaea42012-12-12 13:51:27 -08004153 for_each_node_state(nid, N_MEMORY) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004154 node_nr = mem_cgroup_node_nr_lru_pages(memcg, nid,
KAMEZAWA Hiroyukibb2a0de2011-07-26 16:08:22 -07004155 BIT(LRU_UNEVICTABLE));
Ying Han406eb0c2011-05-26 16:25:37 -07004156 seq_printf(m, " N%d=%lu", nid, node_nr);
4157 }
4158 seq_putc(m, '\n');
4159 return 0;
4160}
4161#endif /* CONFIG_NUMA */
4162
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004163static const char * const mem_cgroup_lru_names[] = {
4164 "inactive_anon",
4165 "active_anon",
4166 "inactive_file",
4167 "active_file",
4168 "unevictable",
4169};
4170
4171static inline void mem_cgroup_lru_names_not_uptodate(void)
4172{
4173 BUILD_BUG_ON(ARRAY_SIZE(mem_cgroup_lru_names) != NR_LRU_LISTS);
4174}
4175
Wanpeng Liab215882012-07-31 16:43:09 -07004176static int memcg_stat_show(struct cgroup *cont, struct cftype *cft,
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004177 struct seq_file *m)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004178{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004179 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004180 struct mem_cgroup *mi;
4181 unsigned int i;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004182
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004183 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004184 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004185 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004186 seq_printf(m, "%s %ld\n", mem_cgroup_stat_names[i],
4187 mem_cgroup_read_stat(memcg, i) * PAGE_SIZE);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004188 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004189
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004190 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++)
4191 seq_printf(m, "%s %lu\n", mem_cgroup_events_names[i],
4192 mem_cgroup_read_events(memcg, i));
4193
4194 for (i = 0; i < NR_LRU_LISTS; i++)
4195 seq_printf(m, "%s %lu\n", mem_cgroup_lru_names[i],
4196 mem_cgroup_nr_lru_pages(memcg, BIT(i)) * PAGE_SIZE);
4197
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004198 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004199 {
4200 unsigned long long limit, memsw_limit;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004201 memcg_get_hierarchical_limit(memcg, &limit, &memsw_limit);
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004202 seq_printf(m, "hierarchical_memory_limit %llu\n", limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004203 if (do_swap_account)
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004204 seq_printf(m, "hierarchical_memsw_limit %llu\n",
4205 memsw_limit);
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08004206 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004207
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004208 for (i = 0; i < MEM_CGROUP_STAT_NSTATS; i++) {
4209 long long val = 0;
4210
Kamezawa Hiroyukibff6bb82012-07-31 16:41:38 -07004211 if (i == MEM_CGROUP_STAT_SWAP && !do_swap_account)
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004212 continue;
Johannes Weineraf7c4b02012-05-29 15:07:08 -07004213 for_each_mem_cgroup_tree(mi, memcg)
4214 val += mem_cgroup_read_stat(mi, i) * PAGE_SIZE;
4215 seq_printf(m, "total_%s %lld\n", mem_cgroup_stat_names[i], val);
4216 }
4217
4218 for (i = 0; i < MEM_CGROUP_EVENTS_NSTATS; i++) {
4219 unsigned long long val = 0;
4220
4221 for_each_mem_cgroup_tree(mi, memcg)
4222 val += mem_cgroup_read_events(mi, i);
4223 seq_printf(m, "total_%s %llu\n",
4224 mem_cgroup_events_names[i], val);
4225 }
4226
4227 for (i = 0; i < NR_LRU_LISTS; i++) {
4228 unsigned long long val = 0;
4229
4230 for_each_mem_cgroup_tree(mi, memcg)
4231 val += mem_cgroup_nr_lru_pages(mi, BIT(i)) * PAGE_SIZE;
4232 seq_printf(m, "total_%s %llu\n", mem_cgroup_lru_names[i], val);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07004233 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07004234
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004235#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004236 {
4237 int nid, zid;
4238 struct mem_cgroup_per_zone *mz;
Hugh Dickins89abfab2012-05-29 15:06:53 -07004239 struct zone_reclaim_stat *rstat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004240 unsigned long recent_rotated[2] = {0, 0};
4241 unsigned long recent_scanned[2] = {0, 0};
4242
4243 for_each_online_node(nid)
4244 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004245 mz = mem_cgroup_zoneinfo(memcg, nid, zid);
Hugh Dickins89abfab2012-05-29 15:06:53 -07004246 rstat = &mz->lruvec.reclaim_stat;
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004247
Hugh Dickins89abfab2012-05-29 15:06:53 -07004248 recent_rotated[0] += rstat->recent_rotated[0];
4249 recent_rotated[1] += rstat->recent_rotated[1];
4250 recent_scanned[0] += rstat->recent_scanned[0];
4251 recent_scanned[1] += rstat->recent_scanned[1];
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004252 }
Johannes Weiner78ccf5b2012-05-29 15:07:06 -07004253 seq_printf(m, "recent_rotated_anon %lu\n", recent_rotated[0]);
4254 seq_printf(m, "recent_rotated_file %lu\n", recent_rotated[1]);
4255 seq_printf(m, "recent_scanned_anon %lu\n", recent_scanned[0]);
4256 seq_printf(m, "recent_scanned_file %lu\n", recent_scanned[1]);
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08004257 }
4258#endif
4259
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004260 return 0;
4261}
4262
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004263static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
4264{
4265 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4266
KAMEZAWA Hiroyuki1f4c0252011-07-26 16:08:21 -07004267 return mem_cgroup_swappiness(memcg);
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004268}
4269
4270static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
4271 u64 val)
4272{
4273 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4274 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08004275
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004276 if (val > 100)
4277 return -EINVAL;
4278
4279 if (cgrp->parent == NULL)
4280 return -EINVAL;
4281
4282 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08004283
4284 cgroup_lock();
4285
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004286 /* If under hierarchy, only empty-root can set this value */
4287 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08004288 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
4289 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004290 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08004291 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004292
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004293 memcg->swappiness = val;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004294
Li Zefan068b38c2009-01-15 13:51:26 -08004295 cgroup_unlock();
4296
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004297 return 0;
4298}
4299
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004300static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
4301{
4302 struct mem_cgroup_threshold_ary *t;
4303 u64 usage;
4304 int i;
4305
4306 rcu_read_lock();
4307 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004308 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004309 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004310 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004311
4312 if (!t)
4313 goto unlock;
4314
4315 usage = mem_cgroup_usage(memcg, swap);
4316
4317 /*
Sha Zhengju748dad32012-05-29 15:06:57 -07004318 * current_threshold points to threshold just below or equal to usage.
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004319 * If it's not true, a threshold was crossed after last
4320 * call of __mem_cgroup_threshold().
4321 */
Phil Carmody5407a562010-05-26 14:42:42 -07004322 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004323
4324 /*
4325 * Iterate backward over array of thresholds starting from
4326 * current_threshold and check if a threshold is crossed.
4327 * If none of thresholds below usage is crossed, we read
4328 * only one element of the array here.
4329 */
4330 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
4331 eventfd_signal(t->entries[i].eventfd, 1);
4332
4333 /* i = current_threshold + 1 */
4334 i++;
4335
4336 /*
4337 * Iterate forward over array of thresholds starting from
4338 * current_threshold+1 and check if a threshold is crossed.
4339 * If none of thresholds above usage is crossed, we read
4340 * only one element of the array here.
4341 */
4342 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
4343 eventfd_signal(t->entries[i].eventfd, 1);
4344
4345 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07004346 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004347unlock:
4348 rcu_read_unlock();
4349}
4350
4351static void mem_cgroup_threshold(struct mem_cgroup *memcg)
4352{
Kirill A. Shutemovad4ca5f2010-10-07 12:59:27 -07004353 while (memcg) {
4354 __mem_cgroup_threshold(memcg, false);
4355 if (do_swap_account)
4356 __mem_cgroup_threshold(memcg, true);
4357
4358 memcg = parent_mem_cgroup(memcg);
4359 }
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004360}
4361
4362static int compare_thresholds(const void *a, const void *b)
4363{
4364 const struct mem_cgroup_threshold *_a = a;
4365 const struct mem_cgroup_threshold *_b = b;
4366
4367 return _a->threshold - _b->threshold;
4368}
4369
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004370static int mem_cgroup_oom_notify_cb(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004371{
4372 struct mem_cgroup_eventfd_list *ev;
4373
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004374 list_for_each_entry(ev, &memcg->oom_notify, list)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004375 eventfd_signal(ev->eventfd, 1);
4376 return 0;
4377}
4378
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004379static void mem_cgroup_oom_notify(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004380{
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004381 struct mem_cgroup *iter;
4382
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004383 for_each_mem_cgroup_tree(iter, memcg)
KAMEZAWA Hiroyuki7d74b062010-10-27 15:33:41 -07004384 mem_cgroup_oom_notify_cb(iter);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004385}
4386
4387static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
4388 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004389{
4390 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004391 struct mem_cgroup_thresholds *thresholds;
4392 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004393 int type = MEMFILE_TYPE(cft->private);
4394 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004395 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004396
4397 ret = res_counter_memparse_write_strategy(args, &threshold);
4398 if (ret)
4399 return ret;
4400
4401 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004402
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004403 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004404 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004405 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004406 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004407 else
4408 BUG();
4409
4410 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4411
4412 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004413 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004414 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4415
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004416 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004417
4418 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004419 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004420 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004421 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004422 ret = -ENOMEM;
4423 goto unlock;
4424 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004425 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004426
4427 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004428 if (thresholds->primary) {
4429 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004430 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004431 }
4432
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004433 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004434 new->entries[size - 1].eventfd = eventfd;
4435 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004436
4437 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004438 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004439 compare_thresholds, NULL);
4440
4441 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004442 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004443 for (i = 0; i < size; i++) {
Sha Zhengju748dad32012-05-29 15:06:57 -07004444 if (new->entries[i].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004445 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004446 * new->current_threshold will not be used until
4447 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004448 * it here.
4449 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004450 ++new->current_threshold;
Sha Zhengju748dad32012-05-29 15:06:57 -07004451 } else
4452 break;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004453 }
4454
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004455 /* Free old spare buffer and save old primary buffer as spare */
4456 kfree(thresholds->spare);
4457 thresholds->spare = thresholds->primary;
4458
4459 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004460
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004461 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004462 synchronize_rcu();
4463
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004464unlock:
4465 mutex_unlock(&memcg->thresholds_lock);
4466
4467 return ret;
4468}
4469
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004470static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004471 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004472{
4473 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004474 struct mem_cgroup_thresholds *thresholds;
4475 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004476 int type = MEMFILE_TYPE(cft->private);
4477 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004478 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004479
4480 mutex_lock(&memcg->thresholds_lock);
4481 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004482 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004483 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004484 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004485 else
4486 BUG();
4487
Anton Vorontsov371528c2012-02-24 05:14:46 +04004488 if (!thresholds->primary)
4489 goto unlock;
4490
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004491 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
4492
4493 /* Check if a threshold crossed before removing */
4494 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
4495
4496 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004497 size = 0;
4498 for (i = 0; i < thresholds->primary->size; i++) {
4499 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004500 size++;
4501 }
4502
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004503 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004504
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004505 /* Set thresholds array to NULL if we don't have thresholds */
4506 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004507 kfree(new);
4508 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004509 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004510 }
4511
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004512 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004513
4514 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004515 new->current_threshold = -1;
4516 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
4517 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004518 continue;
4519
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004520 new->entries[j] = thresholds->primary->entries[i];
Sha Zhengju748dad32012-05-29 15:06:57 -07004521 if (new->entries[j].threshold <= usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004522 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004523 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004524 * until rcu_assign_pointer(), so it's safe to increment
4525 * it here.
4526 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004527 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004528 }
4529 j++;
4530 }
4531
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004532swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004533 /* Swap primary and spare array */
4534 thresholds->spare = thresholds->primary;
Sha Zhengju8c757762012-05-10 13:01:45 -07004535 /* If all events are unregistered, free the spare array */
4536 if (!new) {
4537 kfree(thresholds->spare);
4538 thresholds->spare = NULL;
4539 }
4540
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07004541 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004542
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004543 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004544 synchronize_rcu();
Anton Vorontsov371528c2012-02-24 05:14:46 +04004545unlock:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004546 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004547}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004548
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004549static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
4550 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
4551{
4552 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
4553 struct mem_cgroup_eventfd_list *event;
4554 int type = MEMFILE_TYPE(cft->private);
4555
4556 BUG_ON(type != _OOM_TYPE);
4557 event = kmalloc(sizeof(*event), GFP_KERNEL);
4558 if (!event)
4559 return -ENOMEM;
4560
Michal Hocko1af8efe2011-07-26 16:08:24 -07004561 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004562
4563 event->eventfd = eventfd;
4564 list_add(&event->list, &memcg->oom_notify);
4565
4566 /* already in OOM ? */
Michal Hocko79dfdac2011-07-26 16:08:23 -07004567 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004568 eventfd_signal(eventfd, 1);
Michal Hocko1af8efe2011-07-26 16:08:24 -07004569 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004570
4571 return 0;
4572}
4573
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07004574static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004575 struct cftype *cft, struct eventfd_ctx *eventfd)
4576{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004577 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004578 struct mem_cgroup_eventfd_list *ev, *tmp;
4579 int type = MEMFILE_TYPE(cft->private);
4580
4581 BUG_ON(type != _OOM_TYPE);
4582
Michal Hocko1af8efe2011-07-26 16:08:24 -07004583 spin_lock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004584
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004585 list_for_each_entry_safe(ev, tmp, &memcg->oom_notify, list) {
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004586 if (ev->eventfd == eventfd) {
4587 list_del(&ev->list);
4588 kfree(ev);
4589 }
4590 }
4591
Michal Hocko1af8efe2011-07-26 16:08:24 -07004592 spin_unlock(&memcg_oom_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004593}
4594
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004595static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
4596 struct cftype *cft, struct cgroup_map_cb *cb)
4597{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004598 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004599
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004600 cb->fill(cb, "oom_kill_disable", memcg->oom_kill_disable);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004601
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004602 if (atomic_read(&memcg->under_oom))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004603 cb->fill(cb, "under_oom", 1);
4604 else
4605 cb->fill(cb, "under_oom", 0);
4606 return 0;
4607}
4608
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004609static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
4610 struct cftype *cft, u64 val)
4611{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004612 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004613 struct mem_cgroup *parent;
4614
4615 /* cannot set to root cgroup and only 0 and 1 are allowed */
4616 if (!cgrp->parent || !((val == 0) || (val == 1)))
4617 return -EINVAL;
4618
4619 parent = mem_cgroup_from_cont(cgrp->parent);
4620
4621 cgroup_lock();
4622 /* oom-kill-disable is a flag for subhierarchy. */
4623 if ((parent->use_hierarchy) ||
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004624 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004625 cgroup_unlock();
4626 return -EINVAL;
4627 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004628 memcg->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07004629 if (!val)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004630 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004631 cgroup_unlock();
4632 return 0;
4633}
4634
Andrew Mortonc255a452012-07-31 16:43:02 -07004635#ifdef CONFIG_MEMCG_KMEM
Glauber Costacbe128e32012-04-09 19:36:34 -03004636static int memcg_init_kmem(struct mem_cgroup *memcg, struct cgroup_subsys *ss)
Glauber Costae5671df2011-12-11 21:47:01 +00004637{
Glauber Costa1d62e432012-04-09 19:36:33 -03004638 return mem_cgroup_sockets_init(memcg, ss);
Glauber Costae5671df2011-12-11 21:47:01 +00004639};
4640
Glauber Costa1d62e432012-04-09 19:36:33 -03004641static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004642{
Glauber Costa1d62e432012-04-09 19:36:33 -03004643 mem_cgroup_sockets_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004644}
Glauber Costae5671df2011-12-11 21:47:01 +00004645#else
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{
4648 return 0;
4649}
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004650
Glauber Costa1d62e432012-04-09 19:36:33 -03004651static void kmem_cgroup_destroy(struct mem_cgroup *memcg)
Glauber Costad1a4c0b2011-12-11 21:47:04 +00004652{
4653}
Glauber Costae5671df2011-12-11 21:47:01 +00004654#endif
4655
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004656static struct cftype mem_cgroup_files[] = {
4657 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004658 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004659 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07004660 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004661 .register_event = mem_cgroup_usage_register_event,
4662 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004663 },
4664 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004665 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004666 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004667 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004668 .read = mem_cgroup_read,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07004669 },
4670 {
Balbir Singh0eea1032008-02-07 00:13:57 -08004671 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004672 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07004673 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004674 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004675 },
4676 {
Balbir Singh296c81d2009-09-23 15:56:36 -07004677 .name = "soft_limit_in_bytes",
4678 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
4679 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004680 .read = mem_cgroup_read,
Balbir Singh296c81d2009-09-23 15:56:36 -07004681 },
4682 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004683 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004684 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07004685 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004686 .read = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004687 },
Balbir Singh8697d332008-02-07 00:13:59 -08004688 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004689 .name = "stat",
Wanpeng Liab215882012-07-31 16:43:09 -07004690 .read_seq_string = memcg_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08004691 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08004692 {
4693 .name = "force_empty",
4694 .trigger = mem_cgroup_force_empty_write,
4695 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08004696 {
4697 .name = "use_hierarchy",
4698 .write_u64 = mem_cgroup_hierarchy_write,
4699 .read_u64 = mem_cgroup_hierarchy_read,
4700 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004701 {
4702 .name = "swappiness",
4703 .read_u64 = mem_cgroup_swappiness_read,
4704 .write_u64 = mem_cgroup_swappiness_write,
4705 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004706 {
4707 .name = "move_charge_at_immigrate",
4708 .read_u64 = mem_cgroup_move_charge_read,
4709 .write_u64 = mem_cgroup_move_charge_write,
4710 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004711 {
4712 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004713 .read_map = mem_cgroup_oom_control_read,
4714 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004715 .register_event = mem_cgroup_oom_register_event,
4716 .unregister_event = mem_cgroup_oom_unregister_event,
4717 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4718 },
Ying Han406eb0c2011-05-26 16:25:37 -07004719#ifdef CONFIG_NUMA
4720 {
4721 .name = "numa_stat",
Wanpeng Liab215882012-07-31 16:43:09 -07004722 .read_seq_string = memcg_numa_stat_show,
Ying Han406eb0c2011-05-26 16:25:37 -07004723 },
4724#endif
Andrew Mortonc255a452012-07-31 16:43:02 -07004725#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004726 {
4727 .name = "memsw.usage_in_bytes",
4728 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
Tejun Heoaf36f902012-04-01 12:09:55 -07004729 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004730 .register_event = mem_cgroup_usage_register_event,
4731 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004732 },
4733 {
4734 .name = "memsw.max_usage_in_bytes",
4735 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4736 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004737 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004738 },
4739 {
4740 .name = "memsw.limit_in_bytes",
4741 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4742 .write_string = mem_cgroup_write,
Tejun Heoaf36f902012-04-01 12:09:55 -07004743 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004744 },
4745 {
4746 .name = "memsw.failcnt",
4747 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4748 .trigger = mem_cgroup_reset,
Tejun Heoaf36f902012-04-01 12:09:55 -07004749 .read = mem_cgroup_read,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004750 },
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004751#endif
Tejun Heo6bc10342012-04-01 12:09:55 -07004752 { }, /* terminate */
Tejun Heoaf36f902012-04-01 12:09:55 -07004753};
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004754
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004755static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004756{
4757 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004758 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004759 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004760 /*
4761 * This routine is called against possible nodes.
4762 * But it's BUG to call kmalloc() against offline node.
4763 *
4764 * TODO: this routine can waste much memory for nodes which will
4765 * never be onlined. It's better to use memory hotplug callback
4766 * function.
4767 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07004768 if (!node_state(node, N_NORMAL_MEMORY))
4769 tmp = -1;
Jesper Juhl17295c82011-01-13 15:47:42 -08004770 pn = kzalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004771 if (!pn)
4772 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004773
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004774 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4775 mz = &pn->zoneinfo[zone];
Hugh Dickinsbea8c152012-11-16 14:14:54 -08004776 lruvec_init(&mz->lruvec);
Balbir Singhf64c3f52009-09-23 15:56:37 -07004777 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07004778 mz->on_tree = false;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004779 mz->memcg = memcg;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004780 }
Igor Mammedov0a619e52011-11-02 13:38:21 -07004781 memcg->info.nodeinfo[node] = pn;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004782 return 0;
4783}
4784
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004785static void free_mem_cgroup_per_zone_info(struct mem_cgroup *memcg, int node)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004786{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004787 kfree(memcg->info.nodeinfo[node]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08004788}
4789
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004790static struct mem_cgroup *mem_cgroup_alloc(void)
4791{
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004792 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004793 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004794
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08004795 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004796 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004797 memcg = kzalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004798 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004799 memcg = vzalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004800
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004801 if (!memcg)
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07004802 return NULL;
4803
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004804 memcg->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4805 if (!memcg->stat)
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004806 goto out_free;
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004807 spin_lock_init(&memcg->pcp_counter_lock);
4808 return memcg;
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004809
4810out_free:
4811 if (size < PAGE_SIZE)
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004812 kfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004813 else
Hugh Dickinsd79154b2012-03-21 16:34:18 -07004814 vfree(memcg);
Dan Carpenterd2e61b82010-11-11 14:05:12 -08004815 return NULL;
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004816}
4817
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004818/*
Glauber Costa3afe36b2012-05-29 15:07:10 -07004819 * Helpers for freeing a kmalloc()ed/vzalloc()ed mem_cgroup by RCU,
Hugh Dickins59927fb2012-03-15 15:17:07 -07004820 * but in process context. The work_freeing structure is overlaid
4821 * on the rcu_freeing structure, which itself is overlaid on memsw.
4822 */
Glauber Costa3afe36b2012-05-29 15:07:10 -07004823static void free_work(struct work_struct *work)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004824{
4825 struct mem_cgroup *memcg;
Glauber Costa3afe36b2012-05-29 15:07:10 -07004826 int size = sizeof(struct mem_cgroup);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004827
4828 memcg = container_of(work, struct mem_cgroup, work_freeing);
Glauber Costa3f134612012-05-29 15:07:11 -07004829 /*
4830 * We need to make sure that (at least for now), the jump label
4831 * destruction code runs outside of the cgroup lock. This is because
4832 * get_online_cpus(), which is called from the static_branch update,
4833 * can't be called inside the cgroup_lock. cpusets are the ones
4834 * enforcing this dependency, so if they ever change, we might as well.
4835 *
4836 * schedule_work() will guarantee this happens. Be careful if you need
4837 * to move this code around, and make sure it is outside
4838 * the cgroup_lock.
4839 */
4840 disarm_sock_keys(memcg);
Glauber Costa3afe36b2012-05-29 15:07:10 -07004841 if (size < PAGE_SIZE)
4842 kfree(memcg);
4843 else
4844 vfree(memcg);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004845}
Glauber Costa3afe36b2012-05-29 15:07:10 -07004846
4847static void free_rcu(struct rcu_head *rcu_head)
Hugh Dickins59927fb2012-03-15 15:17:07 -07004848{
4849 struct mem_cgroup *memcg;
4850
4851 memcg = container_of(rcu_head, struct mem_cgroup, rcu_freeing);
Glauber Costa3afe36b2012-05-29 15:07:10 -07004852 INIT_WORK(&memcg->work_freeing, free_work);
Hugh Dickins59927fb2012-03-15 15:17:07 -07004853 schedule_work(&memcg->work_freeing);
4854}
4855
4856/*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004857 * At destroying mem_cgroup, references from swap_cgroup can remain.
4858 * (scanning all at force_empty is too costly...)
4859 *
4860 * Instead of clearing all references at force_empty, we remember
4861 * the number of reference from swap_cgroup and free mem_cgroup when
4862 * it goes down to 0.
4863 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004864 * Removal of cgroup itself succeeds regardless of refs from swap.
4865 */
4866
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004867static void __mem_cgroup_free(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004868{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004869 int node;
4870
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004871 mem_cgroup_remove_from_trees(memcg);
4872 free_css_id(&mem_cgroup_subsys, &memcg->css);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004873
Bob Liu3ed28fa2012-01-12 17:19:04 -08004874 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004875 free_mem_cgroup_per_zone_info(memcg, node);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08004876
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004877 free_percpu(memcg->stat);
Glauber Costa3afe36b2012-05-29 15:07:10 -07004878 call_rcu(&memcg->rcu_freeing, free_rcu);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004879}
4880
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004881static void mem_cgroup_get(struct mem_cgroup *memcg)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004882{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004883 atomic_inc(&memcg->refcnt);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004884}
4885
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004886static void __mem_cgroup_put(struct mem_cgroup *memcg, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004887{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004888 if (atomic_sub_and_test(count, &memcg->refcnt)) {
4889 struct mem_cgroup *parent = parent_mem_cgroup(memcg);
4890 __mem_cgroup_free(memcg);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004891 if (parent)
4892 mem_cgroup_put(parent);
4893 }
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)
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004897{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004898 __mem_cgroup_put(memcg, 1);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004899}
4900
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004901/*
4902 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4903 */
Glauber Costae1aab162011-12-11 21:47:03 +00004904struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004905{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004906 if (!memcg->res.parent)
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004907 return NULL;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004908 return mem_cgroup_from_res_counter(memcg->res.parent, res);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004909}
Glauber Costae1aab162011-12-11 21:47:03 +00004910EXPORT_SYMBOL(parent_mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004911
Andrew Mortonc255a452012-07-31 16:43:02 -07004912#ifdef CONFIG_MEMCG_SWAP
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004913static void __init enable_swap_cgroup(void)
4914{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004915 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004916 do_swap_account = 1;
4917}
4918#else
4919static void __init enable_swap_cgroup(void)
4920{
4921}
4922#endif
4923
Balbir Singhf64c3f52009-09-23 15:56:37 -07004924static int mem_cgroup_soft_limit_tree_init(void)
4925{
4926 struct mem_cgroup_tree_per_node *rtpn;
4927 struct mem_cgroup_tree_per_zone *rtpz;
4928 int tmp, node, zone;
4929
Bob Liu3ed28fa2012-01-12 17:19:04 -08004930 for_each_node(node) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07004931 tmp = node;
4932 if (!node_state(node, N_NORMAL_MEMORY))
4933 tmp = -1;
4934 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4935 if (!rtpn)
Michal Hockoc3cecc62012-01-12 17:18:50 -08004936 goto err_cleanup;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004937
4938 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4939
4940 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4941 rtpz = &rtpn->rb_tree_per_zone[zone];
4942 rtpz->rb_root = RB_ROOT;
4943 spin_lock_init(&rtpz->lock);
4944 }
4945 }
4946 return 0;
Michal Hockoc3cecc62012-01-12 17:18:50 -08004947
4948err_cleanup:
Bob Liu3ed28fa2012-01-12 17:19:04 -08004949 for_each_node(node) {
Michal Hockoc3cecc62012-01-12 17:18:50 -08004950 if (!soft_limit_tree.rb_tree_per_node[node])
4951 break;
4952 kfree(soft_limit_tree.rb_tree_per_node[node]);
4953 soft_limit_tree.rb_tree_per_node[node] = NULL;
4954 }
4955 return 1;
4956
Balbir Singhf64c3f52009-09-23 15:56:37 -07004957}
4958
Li Zefan0eb253e2009-01-15 13:51:25 -08004959static struct cgroup_subsys_state * __ref
Tejun Heo92fb9742012-11-19 08:13:38 -08004960mem_cgroup_css_alloc(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004961{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004962 struct mem_cgroup *memcg, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004963 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004964 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004965
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004966 memcg = mem_cgroup_alloc();
4967 if (!memcg)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004968 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004969
Bob Liu3ed28fa2012-01-12 17:19:04 -08004970 for_each_node(node)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004971 if (alloc_mem_cgroup_per_zone_info(memcg, node))
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004972 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004973
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004974 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004975 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004976 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004977 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004978 parent = NULL;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004979 if (mem_cgroup_soft_limit_tree_init())
4980 goto free_out;
Hillf Dantona41c58a2011-12-19 17:11:57 -08004981 root_mem_cgroup = memcg;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004982 for_each_possible_cpu(cpu) {
4983 struct memcg_stock_pcp *stock =
4984 &per_cpu(memcg_stock, cpu);
4985 INIT_WORK(&stock->work, drain_local_stock);
4986 }
KAMEZAWA Hiroyuki711d3d22010-10-27 15:33:42 -07004987 hotcpu_notifier(memcg_cpu_hotplug_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004988 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004989 parent = mem_cgroup_from_cont(cont->parent);
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004990 memcg->use_hierarchy = parent->use_hierarchy;
4991 memcg->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004992 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004993
Balbir Singh18f59ea2009-01-07 18:08:07 -08004994 if (parent && parent->use_hierarchy) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07004995 res_counter_init(&memcg->res, &parent->res);
4996 res_counter_init(&memcg->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004997 /*
4998 * We increment refcnt of the parent to ensure that we can
4999 * safely access it on res_counter_charge/uncharge.
5000 * This refcnt will be decremented when freeing this
5001 * mem_cgroup(see mem_cgroup_put).
5002 */
5003 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08005004 } else {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005005 res_counter_init(&memcg->res, NULL);
5006 res_counter_init(&memcg->memsw, NULL);
Tejun Heo8c7f6ed2012-09-13 12:20:58 -07005007 /*
5008 * Deeper hierachy with use_hierarchy == false doesn't make
5009 * much sense so let cgroup subsystem know about this
5010 * unfortunate state in our controller.
5011 */
5012 if (parent && parent != root_mem_cgroup)
5013 mem_cgroup_subsys.broken_hierarchy = true;
Balbir Singh18f59ea2009-01-07 18:08:07 -08005014 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005015 memcg->last_scanned_node = MAX_NUMNODES;
5016 INIT_LIST_HEAD(&memcg->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08005017
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08005018 if (parent)
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005019 memcg->swappiness = mem_cgroup_swappiness(parent);
5020 atomic_set(&memcg->refcnt, 1);
5021 memcg->move_charge_at_immigrate = 0;
5022 mutex_init(&memcg->thresholds_lock);
KAMEZAWA Hiroyuki312734c02012-03-21 16:34:24 -07005023 spin_lock_init(&memcg->move_lock);
Glauber Costacbe128e32012-04-09 19:36:34 -03005024
5025 error = memcg_init_kmem(memcg, &mem_cgroup_subsys);
5026 if (error) {
5027 /*
5028 * We call put now because our (and parent's) refcnts
5029 * are already in place. mem_cgroup_put() will internally
5030 * call __mem_cgroup_free, so return directly
5031 */
5032 mem_cgroup_put(memcg);
5033 return ERR_PTR(error);
5034 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005035 return &memcg->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005036free_out:
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005037 __mem_cgroup_free(memcg);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005038 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005039}
5040
Tejun Heo92fb9742012-11-19 08:13:38 -08005041static void mem_cgroup_css_offline(struct cgroup *cont)
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005042{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005043 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07005044
Michal Hockoab5196c2012-10-26 13:37:32 +02005045 mem_cgroup_reparent_charges(memcg);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08005046}
5047
Tejun Heo92fb9742012-11-19 08:13:38 -08005048static void mem_cgroup_css_free(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005049{
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005050 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08005051
Glauber Costa1d62e432012-04-09 19:36:33 -03005052 kmem_cgroup_destroy(memcg);
Glauber Costad1a4c0b2011-12-11 21:47:04 +00005053
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005054 mem_cgroup_put(memcg);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005055}
5056
Daisuke Nishimura02491442010-03-10 15:22:17 -08005057#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005058/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005059#define PRECHARGE_COUNT_AT_ONCE 256
5060static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005061{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005062 int ret = 0;
5063 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005064 struct mem_cgroup *memcg = mc.to;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005065
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005066 if (mem_cgroup_is_root(memcg)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005067 mc.precharge += count;
5068 /* we don't need css_get for root */
5069 return ret;
5070 }
5071 /* try to charge at once */
5072 if (count > 1) {
5073 struct res_counter *dummy;
5074 /*
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005075 * "memcg" cannot be under rmdir() because we've already checked
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005076 * by cgroup_lock_live_cgroup() that it is not removed and we
5077 * are still under the same cgroup_mutex. So we can postpone
5078 * css_get().
5079 */
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005080 if (res_counter_charge(&memcg->res, PAGE_SIZE * count, &dummy))
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005081 goto one_by_one;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005082 if (do_swap_account && res_counter_charge(&memcg->memsw,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005083 PAGE_SIZE * count, &dummy)) {
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005084 res_counter_uncharge(&memcg->res, PAGE_SIZE * count);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005085 goto one_by_one;
5086 }
5087 mc.precharge += count;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005088 return ret;
5089 }
5090one_by_one:
5091 /* fall back to one by one charge */
5092 while (count--) {
5093 if (signal_pending(current)) {
5094 ret = -EINTR;
5095 break;
5096 }
5097 if (!batch_count--) {
5098 batch_count = PRECHARGE_COUNT_AT_ONCE;
5099 cond_resched();
5100 }
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005101 ret = __mem_cgroup_try_charge(NULL,
5102 GFP_KERNEL, 1, &memcg, false);
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005103 if (ret)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005104 /* mem_cgroup_clear_mc() will do uncharge later */
KAMEZAWA Hiroyuki38c5d722012-01-12 17:19:01 -08005105 return ret;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005106 mc.precharge++;
5107 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005108 return ret;
5109}
5110
5111/**
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005112 * get_mctgt_type - get target type of moving charge
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005113 * @vma: the vma the pte to be checked belongs
5114 * @addr: the address corresponding to the pte to be checked
5115 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08005116 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005117 *
5118 * Returns
5119 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
5120 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
5121 * move charge. if @target is not NULL, the page is stored in target->page
5122 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08005123 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
5124 * target for charge migration. if @target is not NULL, the entry is stored
5125 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005126 *
5127 * Called with pte lock held.
5128 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005129union mc_target {
5130 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005131 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005132};
5133
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005134enum mc_target_type {
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005135 MC_TARGET_NONE = 0,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005136 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08005137 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005138};
5139
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005140static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
5141 unsigned long addr, pte_t ptent)
5142{
5143 struct page *page = vm_normal_page(vma, addr, ptent);
5144
5145 if (!page || !page_mapped(page))
5146 return NULL;
5147 if (PageAnon(page)) {
5148 /* we don't move shared anon */
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005149 if (!move_anon())
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005150 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005151 } else if (!move_file())
5152 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005153 return NULL;
5154 if (!get_page_unless_zero(page))
5155 return NULL;
5156
5157 return page;
5158}
5159
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005160#ifdef CONFIG_SWAP
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005161static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5162 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5163{
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005164 struct page *page = NULL;
5165 swp_entry_t ent = pte_to_swp_entry(ptent);
5166
5167 if (!move_anon() || non_swap_entry(ent))
5168 return NULL;
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005169 /*
5170 * Because lookup_swap_cache() updates some statistics counter,
5171 * we call find_get_page() with swapper_space directly.
5172 */
5173 page = find_get_page(&swapper_space, ent.val);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005174 if (do_swap_account)
5175 entry->val = ent.val;
5176
5177 return page;
5178}
KAMEZAWA Hiroyuki4b913552012-05-29 15:06:51 -07005179#else
5180static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
5181 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5182{
5183 return NULL;
5184}
5185#endif
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005186
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005187static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
5188 unsigned long addr, pte_t ptent, swp_entry_t *entry)
5189{
5190 struct page *page = NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005191 struct address_space *mapping;
5192 pgoff_t pgoff;
5193
5194 if (!vma->vm_file) /* anonymous vma */
5195 return NULL;
5196 if (!move_file())
5197 return NULL;
5198
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005199 mapping = vma->vm_file->f_mapping;
5200 if (pte_none(ptent))
5201 pgoff = linear_page_index(vma, addr);
5202 else /* pte_file(ptent) is true */
5203 pgoff = pte_to_pgoff(ptent);
5204
5205 /* page is moved even if it's not RSS of this task(page-faulted). */
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005206 page = find_get_page(mapping, pgoff);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005207
Hugh Dickinsaa3b1892011-08-03 16:21:24 -07005208#ifdef CONFIG_SWAP
5209 /* shmem/tmpfs may report page out on swap: account for that too. */
5210 if (radix_tree_exceptional_entry(page)) {
5211 swp_entry_t swap = radix_to_swp_entry(page);
5212 if (do_swap_account)
5213 *entry = swap;
5214 page = find_get_page(&swapper_space, swap.val);
5215 }
5216#endif
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005217 return page;
5218}
5219
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005220static enum mc_target_type get_mctgt_type(struct vm_area_struct *vma,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005221 unsigned long addr, pte_t ptent, union mc_target *target)
5222{
Daisuke Nishimura02491442010-03-10 15:22:17 -08005223 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005224 struct page_cgroup *pc;
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005225 enum mc_target_type ret = MC_TARGET_NONE;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005226 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005227
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005228 if (pte_present(ptent))
5229 page = mc_handle_present_pte(vma, addr, ptent);
5230 else if (is_swap_pte(ptent))
5231 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07005232 else if (pte_none(ptent) || pte_file(ptent))
5233 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005234
5235 if (!page && !ent.val)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005236 return ret;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005237 if (page) {
5238 pc = lookup_page_cgroup(page);
5239 /*
5240 * Do only loose check w/o page_cgroup lock.
5241 * mem_cgroup_move_account() checks the pc is valid or not under
5242 * the lock.
5243 */
5244 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5245 ret = MC_TARGET_PAGE;
5246 if (target)
5247 target->page = page;
5248 }
5249 if (!ret || !target)
5250 put_page(page);
5251 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07005252 /* There is a swap entry and a page doesn't exist or isn't charged */
5253 if (ent.val && !ret &&
Bob Liu9fb4b7c2012-01-12 17:18:48 -08005254 css_id(&mc.from->css) == lookup_swap_cgroup_id(ent)) {
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07005255 ret = MC_TARGET_SWAP;
5256 if (target)
5257 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005258 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005259 return ret;
5260}
5261
Naoya Horiguchi12724852012-03-21 16:34:28 -07005262#ifdef CONFIG_TRANSPARENT_HUGEPAGE
5263/*
5264 * We don't consider swapping or file mapped pages because THP does not
5265 * support them for now.
5266 * Caller should make sure that pmd_trans_huge(pmd) is true.
5267 */
5268static enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5269 unsigned long addr, pmd_t pmd, union mc_target *target)
5270{
5271 struct page *page = NULL;
5272 struct page_cgroup *pc;
5273 enum mc_target_type ret = MC_TARGET_NONE;
5274
5275 page = pmd_page(pmd);
5276 VM_BUG_ON(!page || !PageHead(page));
5277 if (!move_anon())
5278 return ret;
5279 pc = lookup_page_cgroup(page);
5280 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
5281 ret = MC_TARGET_PAGE;
5282 if (target) {
5283 get_page(page);
5284 target->page = page;
5285 }
5286 }
5287 return ret;
5288}
5289#else
5290static inline enum mc_target_type get_mctgt_type_thp(struct vm_area_struct *vma,
5291 unsigned long addr, pmd_t pmd, union mc_target *target)
5292{
5293 return MC_TARGET_NONE;
5294}
5295#endif
5296
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005297static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
5298 unsigned long addr, unsigned long end,
5299 struct mm_walk *walk)
5300{
5301 struct vm_area_struct *vma = walk->private;
5302 pte_t *pte;
5303 spinlock_t *ptl;
5304
Naoya Horiguchi12724852012-03-21 16:34:28 -07005305 if (pmd_trans_huge_lock(pmd, vma) == 1) {
5306 if (get_mctgt_type_thp(vma, addr, *pmd, NULL) == MC_TARGET_PAGE)
5307 mc.precharge += HPAGE_PMD_NR;
5308 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005309 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005310 }
Dave Hansen03319322011-03-22 16:32:56 -07005311
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005312 if (pmd_trans_unstable(pmd))
5313 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005314 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5315 for (; addr != end; pte++, addr += PAGE_SIZE)
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005316 if (get_mctgt_type(vma, addr, *pte, NULL))
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005317 mc.precharge++; /* increment precharge temporarily */
5318 pte_unmap_unlock(pte - 1, ptl);
5319 cond_resched();
5320
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005321 return 0;
5322}
5323
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005324static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
5325{
5326 unsigned long precharge;
5327 struct vm_area_struct *vma;
5328
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005329 down_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005330 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5331 struct mm_walk mem_cgroup_count_precharge_walk = {
5332 .pmd_entry = mem_cgroup_count_precharge_pte_range,
5333 .mm = mm,
5334 .private = vma,
5335 };
5336 if (is_vm_hugetlb_page(vma))
5337 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005338 walk_page_range(vma->vm_start, vma->vm_end,
5339 &mem_cgroup_count_precharge_walk);
5340 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005341 up_read(&mm->mmap_sem);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005342
5343 precharge = mc.precharge;
5344 mc.precharge = 0;
5345
5346 return precharge;
5347}
5348
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005349static int mem_cgroup_precharge_mc(struct mm_struct *mm)
5350{
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005351 unsigned long precharge = mem_cgroup_count_precharge(mm);
5352
5353 VM_BUG_ON(mc.moving_task);
5354 mc.moving_task = current;
5355 return mem_cgroup_do_precharge(precharge);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005356}
5357
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005358/* cancels all extra charges on mc.from and mc.to, and wakes up all waiters. */
5359static void __mem_cgroup_clear_mc(void)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005360{
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005361 struct mem_cgroup *from = mc.from;
5362 struct mem_cgroup *to = mc.to;
5363
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005364 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005365 if (mc.precharge) {
5366 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
5367 mc.precharge = 0;
5368 }
5369 /*
5370 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
5371 * we must uncharge here.
5372 */
5373 if (mc.moved_charge) {
5374 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
5375 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005376 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005377 /* we must fixup refcnts and charges */
5378 if (mc.moved_swap) {
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005379 /* uncharge swap account from the old cgroup */
5380 if (!mem_cgroup_is_root(mc.from))
5381 res_counter_uncharge(&mc.from->memsw,
5382 PAGE_SIZE * mc.moved_swap);
5383 __mem_cgroup_put(mc.from, mc.moved_swap);
5384
5385 if (!mem_cgroup_is_root(mc.to)) {
5386 /*
5387 * we charged both to->res and to->memsw, so we should
5388 * uncharge to->res.
5389 */
5390 res_counter_uncharge(&mc.to->res,
5391 PAGE_SIZE * mc.moved_swap);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005392 }
5393 /* we've already done mem_cgroup_get(mc.to) */
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005394 mc.moved_swap = 0;
5395 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005396 memcg_oom_recover(from);
5397 memcg_oom_recover(to);
5398 wake_up_all(&mc.waitq);
5399}
5400
5401static void mem_cgroup_clear_mc(void)
5402{
5403 struct mem_cgroup *from = mc.from;
5404
5405 /*
5406 * we must clear moving_task before waking up waiters at the end of
5407 * task migration.
5408 */
5409 mc.moving_task = NULL;
5410 __mem_cgroup_clear_mc();
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005411 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005412 mc.from = NULL;
5413 mc.to = NULL;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005414 spin_unlock(&mc.lock);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005415 mem_cgroup_end_move(from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005416}
5417
Li Zefan761b3ef2012-01-31 13:47:36 +08005418static int mem_cgroup_can_attach(struct cgroup *cgroup,
5419 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005420{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005421 struct task_struct *p = cgroup_taskset_first(tset);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005422 int ret = 0;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005423 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgroup);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005424
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005425 if (memcg->move_charge_at_immigrate) {
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005426 struct mm_struct *mm;
5427 struct mem_cgroup *from = mem_cgroup_from_task(p);
5428
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005429 VM_BUG_ON(from == memcg);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005430
5431 mm = get_task_mm(p);
5432 if (!mm)
5433 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005434 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005435 if (mm->owner == p) {
5436 VM_BUG_ON(mc.from);
5437 VM_BUG_ON(mc.to);
5438 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005439 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005440 VM_BUG_ON(mc.moved_swap);
KAMEZAWA Hiroyuki32047e22010-10-27 15:33:40 -07005441 mem_cgroup_start_move(from);
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005442 spin_lock(&mc.lock);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005443 mc.from = from;
Raghavendra K Tc0ff4b82011-11-02 13:38:15 -07005444 mc.to = memcg;
KAMEZAWA Hiroyuki2bd9bb22010-08-10 18:02:58 -07005445 spin_unlock(&mc.lock);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005446 /* We set mc.moving_task later */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005447
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005448 ret = mem_cgroup_precharge_mc(mm);
5449 if (ret)
5450 mem_cgroup_clear_mc();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005451 }
5452 mmput(mm);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005453 }
5454 return ret;
5455}
5456
Li Zefan761b3ef2012-01-31 13:47:36 +08005457static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5458 struct cgroup_taskset *tset)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005459{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005460 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005461}
5462
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005463static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
5464 unsigned long addr, unsigned long end,
5465 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005466{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005467 int ret = 0;
5468 struct vm_area_struct *vma = walk->private;
5469 pte_t *pte;
5470 spinlock_t *ptl;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005471 enum mc_target_type target_type;
5472 union mc_target target;
5473 struct page *page;
5474 struct page_cgroup *pc;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005475
Naoya Horiguchi12724852012-03-21 16:34:28 -07005476 /*
5477 * We don't take compound_lock() here but no race with splitting thp
5478 * happens because:
5479 * - if pmd_trans_huge_lock() returns 1, the relevant thp is not
5480 * under splitting, which means there's no concurrent thp split,
5481 * - if another thread runs into split_huge_page() just after we
5482 * entered this if-block, the thread must wait for page table lock
5483 * to be unlocked in __split_huge_page_splitting(), where the main
5484 * part of thp split is not executed yet.
5485 */
5486 if (pmd_trans_huge_lock(pmd, vma) == 1) {
Hugh Dickins62ade862012-05-18 11:28:34 -07005487 if (mc.precharge < HPAGE_PMD_NR) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005488 spin_unlock(&vma->vm_mm->page_table_lock);
5489 return 0;
5490 }
5491 target_type = get_mctgt_type_thp(vma, addr, *pmd, &target);
5492 if (target_type == MC_TARGET_PAGE) {
5493 page = target.page;
5494 if (!isolate_lru_page(page)) {
5495 pc = lookup_page_cgroup(page);
5496 if (!mem_cgroup_move_account(page, HPAGE_PMD_NR,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005497 pc, mc.from, mc.to)) {
Naoya Horiguchi12724852012-03-21 16:34:28 -07005498 mc.precharge -= HPAGE_PMD_NR;
5499 mc.moved_charge += HPAGE_PMD_NR;
5500 }
5501 putback_lru_page(page);
5502 }
5503 put_page(page);
5504 }
5505 spin_unlock(&vma->vm_mm->page_table_lock);
Andrea Arcangeli1a5a9902012-03-21 16:33:42 -07005506 return 0;
Naoya Horiguchi12724852012-03-21 16:34:28 -07005507 }
5508
Andrea Arcangeli45f83ce2012-03-28 14:42:40 -07005509 if (pmd_trans_unstable(pmd))
5510 return 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005511retry:
5512 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
5513 for (; addr != end; addr += PAGE_SIZE) {
5514 pte_t ptent = *(pte++);
Daisuke Nishimura02491442010-03-10 15:22:17 -08005515 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005516
5517 if (!mc.precharge)
5518 break;
5519
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005520 switch (get_mctgt_type(vma, addr, ptent, &target)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005521 case MC_TARGET_PAGE:
5522 page = target.page;
5523 if (isolate_lru_page(page))
5524 goto put;
5525 pc = lookup_page_cgroup(page);
Johannes Weiner7ec99d62011-03-23 16:42:36 -07005526 if (!mem_cgroup_move_account(page, 1, pc,
KAMEZAWA Hiroyuki2f3479b2012-05-29 15:07:04 -07005527 mc.from, mc.to)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005528 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005529 /* we uncharge from mc.from later. */
5530 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005531 }
5532 putback_lru_page(page);
Naoya Horiguchi8d32ff82012-03-21 16:34:27 -07005533put: /* get_mctgt_type() gets the page */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005534 put_page(page);
5535 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08005536 case MC_TARGET_SWAP:
5537 ent = target.ent;
Hugh Dickinse91cbb42012-05-29 15:06:51 -07005538 if (!mem_cgroup_move_swap_account(ent, mc.from, mc.to)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08005539 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08005540 /* we fixup refcnts and charges later. */
5541 mc.moved_swap++;
5542 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08005543 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005544 default:
5545 break;
5546 }
5547 }
5548 pte_unmap_unlock(pte - 1, ptl);
5549 cond_resched();
5550
5551 if (addr != end) {
5552 /*
5553 * We have consumed all precharges we got in can_attach().
5554 * We try charge one by one, but don't do any additional
5555 * charges to mc.to if we have failed in charge once in attach()
5556 * phase.
5557 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08005558 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005559 if (!ret)
5560 goto retry;
5561 }
5562
5563 return ret;
5564}
5565
5566static void mem_cgroup_move_charge(struct mm_struct *mm)
5567{
5568 struct vm_area_struct *vma;
5569
5570 lru_add_drain_all();
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005571retry:
5572 if (unlikely(!down_read_trylock(&mm->mmap_sem))) {
5573 /*
5574 * Someone who are holding the mmap_sem might be waiting in
5575 * waitq. So we cancel all extra charges, wake up all waiters,
5576 * and retry. Because we cancel precharges, we might not be able
5577 * to move enough charges, but moving charge is a best-effort
5578 * feature anyway, so it wouldn't be a big problem.
5579 */
5580 __mem_cgroup_clear_mc();
5581 cond_resched();
5582 goto retry;
5583 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005584 for (vma = mm->mmap; vma; vma = vma->vm_next) {
5585 int ret;
5586 struct mm_walk mem_cgroup_move_charge_walk = {
5587 .pmd_entry = mem_cgroup_move_charge_pte_range,
5588 .mm = mm,
5589 .private = vma,
5590 };
5591 if (is_vm_hugetlb_page(vma))
5592 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08005593 ret = walk_page_range(vma->vm_start, vma->vm_end,
5594 &mem_cgroup_move_charge_walk);
5595 if (ret)
5596 /*
5597 * means we have consumed all precharges and failed in
5598 * doing additional charge. Just abandon here.
5599 */
5600 break;
5601 }
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005602 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005603}
5604
Li Zefan761b3ef2012-01-31 13:47:36 +08005605static void mem_cgroup_move_task(struct cgroup *cont,
5606 struct cgroup_taskset *tset)
Balbir Singh67e465a2008-02-07 00:13:54 -08005607{
Tejun Heo2f7ee562011-12-12 18:12:21 -08005608 struct task_struct *p = cgroup_taskset_first(tset);
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005609 struct mm_struct *mm = get_task_mm(p);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005610
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005611 if (mm) {
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005612 if (mc.to)
5613 mem_cgroup_move_charge(mm);
Daisuke Nishimuradfe076b2011-01-13 15:47:41 -08005614 mmput(mm);
5615 }
KOSAKI Motohiroa4336582011-06-15 15:08:13 -07005616 if (mc.to)
5617 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08005618}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005619#else /* !CONFIG_MMU */
Li Zefan761b3ef2012-01-31 13:47:36 +08005620static int mem_cgroup_can_attach(struct cgroup *cgroup,
5621 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005622{
5623 return 0;
5624}
Li Zefan761b3ef2012-01-31 13:47:36 +08005625static void mem_cgroup_cancel_attach(struct cgroup *cgroup,
5626 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005627{
5628}
Li Zefan761b3ef2012-01-31 13:47:36 +08005629static void mem_cgroup_move_task(struct cgroup *cont,
5630 struct cgroup_taskset *tset)
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07005631{
5632}
5633#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08005634
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005635struct cgroup_subsys mem_cgroup_subsys = {
5636 .name = "memory",
5637 .subsys_id = mem_cgroup_subsys_id,
Tejun Heo92fb9742012-11-19 08:13:38 -08005638 .css_alloc = mem_cgroup_css_alloc,
5639 .css_offline = mem_cgroup_css_offline,
5640 .css_free = mem_cgroup_css_free,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08005641 .can_attach = mem_cgroup_can_attach,
5642 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08005643 .attach = mem_cgroup_move_task,
Tejun Heo6bc10342012-04-01 12:09:55 -07005644 .base_cftypes = mem_cgroup_files,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08005645 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07005646 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08005647};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005648
Andrew Mortonc255a452012-07-31 16:43:02 -07005649#ifdef CONFIG_MEMCG_SWAP
Michal Hockoa42c3902010-11-24 12:57:08 -08005650static int __init enable_swap_account(char *s)
5651{
5652 /* consider enabled if no parameter or 1 is given */
Michal Hockoa2c89902011-05-24 17:12:50 -07005653 if (!strcmp(s, "1"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005654 really_do_swap_account = 1;
Michal Hockoa2c89902011-05-24 17:12:50 -07005655 else if (!strcmp(s, "0"))
Michal Hockoa42c3902010-11-24 12:57:08 -08005656 really_do_swap_account = 0;
5657 return 1;
5658}
Michal Hockoa2c89902011-05-24 17:12:50 -07005659__setup("swapaccount=", enable_swap_account);
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005660
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08005661#endif