blob: df1234c0dac313cc44ddbbfff5d97a8133b1916a [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>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080036#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070037#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070038#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080039#include <linux/swap.h>
Daisuke Nishimura02491442010-03-10 15:22:17 -080040#include <linux/swapops.h>
Balbir Singh66e17072008-02-07 00:13:56 -080041#include <linux/spinlock.h>
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080042#include <linux/eventfd.h>
43#include <linux/sort.h>
Balbir Singh66e17072008-02-07 00:13:56 -080044#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080045#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070046#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070047#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070048#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080049#include <linux/cpu.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080050#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080051
Balbir Singh8697d332008-02-07 00:13:59 -080052#include <asm/uaccess.h>
53
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070054struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070055#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070056struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080057
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080058#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070059/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080060int do_swap_account __read_mostly;
61static int really_do_swap_account __initdata = 1; /* for remember boot option*/
62#else
63#define do_swap_account (0)
64#endif
65
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -080066/*
67 * Per memcg event counter is incremented at every pagein/pageout. This counter
68 * is used for trigger some periodic events. This is straightforward and better
69 * than using jiffies etc. to handle periodic memcg event.
70 *
71 * These values will be used as !((event) & ((1 <<(thresh)) - 1))
72 */
73#define THRESHOLDS_EVENTS_THRESH (7) /* once in 128 */
74#define SOFTLIMIT_EVENTS_THRESH (10) /* once in 1024 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080075
Balbir Singh8cdea7c2008-02-07 00:13:50 -080076/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080077 * Statistics for memory cgroup.
78 */
79enum mem_cgroup_stat_index {
80 /*
81 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
82 */
83 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070084 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080085 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070086 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
87 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070088 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -080089 MEM_CGROUP_EVENTS, /* incremented at every pagein/pageout */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080090
91 MEM_CGROUP_STAT_NSTATS,
92};
93
94struct mem_cgroup_stat_cpu {
95 s64 count[MEM_CGROUP_STAT_NSTATS];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080096};
97
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080098/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080099 * per-zone information in memory controller.
100 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800101struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800102 /*
103 * spin_lock to protect the per cgroup LRU
104 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700105 struct list_head lists[NR_LRU_LISTS];
106 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800107
108 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700109 struct rb_node tree_node; /* RB tree node */
110 unsigned long long usage_in_excess;/* Set to the value by which */
111 /* the soft limit is exceeded*/
112 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700113 struct mem_cgroup *mem; /* Back pointer, we cannot */
114 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800115};
116/* Macro for accessing counter */
117#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
118
119struct mem_cgroup_per_node {
120 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
121};
122
123struct mem_cgroup_lru_info {
124 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
125};
126
127/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700128 * Cgroups above their limits are maintained in a RB-Tree, independent of
129 * their hierarchy representation
130 */
131
132struct mem_cgroup_tree_per_zone {
133 struct rb_root rb_root;
134 spinlock_t lock;
135};
136
137struct mem_cgroup_tree_per_node {
138 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
139};
140
141struct mem_cgroup_tree {
142 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
143};
144
145static struct mem_cgroup_tree soft_limit_tree __read_mostly;
146
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800147struct mem_cgroup_threshold {
148 struct eventfd_ctx *eventfd;
149 u64 threshold;
150};
151
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700152/* For threshold */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800153struct mem_cgroup_threshold_ary {
154 /* An array index points to threshold just below usage. */
Phil Carmody5407a562010-05-26 14:42:42 -0700155 int current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800156 /* Size of entries[] */
157 unsigned int size;
158 /* Array of thresholds */
159 struct mem_cgroup_threshold entries[0];
160};
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700161/* for OOM */
162struct mem_cgroup_eventfd_list {
163 struct list_head list;
164 struct eventfd_ctx *eventfd;
165};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800166
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800167static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700168static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800169
Balbir Singhf64c3f52009-09-23 15:56:37 -0700170/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800171 * The memory controller data structure. The memory controller controls both
172 * page cache and RSS per cgroup. We would eventually like to provide
173 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
174 * to help the administrator determine what knobs to tune.
175 *
176 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800177 * we hit the water mark. May be even add a low water mark, such that
178 * no reclaim occurs from a cgroup at it's low water mark, this is
179 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800180 */
181struct mem_cgroup {
182 struct cgroup_subsys_state css;
183 /*
184 * the counter to account for memory usage
185 */
186 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800187 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800188 * the counter to account for mem+swap usage.
189 */
190 struct res_counter memsw;
191 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800192 * Per cgroup active and inactive list, similar to the
193 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800194 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800195 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800196
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800197 /*
198 protect against reclaim related member.
199 */
200 spinlock_t reclaim_param_lock;
201
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800202 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800203
204 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200205 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700206 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800207 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700208 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800209 /*
210 * Should the accounting and control be hierarchical, per subtree?
211 */
212 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800213 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800214 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800215
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800216 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700217 /* OOM-Killer disable */
218 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800219
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700220 /* set when res.limit == memsw.limit */
221 bool memsw_is_minimum;
222
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800223 /* protect arrays of thresholds */
224 struct mutex thresholds_lock;
225
226 /* thresholds for memory usage. RCU-protected */
227 struct mem_cgroup_threshold_ary *thresholds;
228
229 /* thresholds for mem+swap usage. RCU-protected */
230 struct mem_cgroup_threshold_ary *memsw_thresholds;
231
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700232 /* For oom notifier event fd */
233 struct list_head oom_notify;
234
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800235 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800236 * Should we move charges of a task when a task is moved into this
237 * mem_cgroup ? And what type of charges should we move ?
238 */
239 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800240 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800241 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800242 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800243 struct mem_cgroup_stat_cpu *stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800244};
245
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800246/* Stuffs for move charges at task migration. */
247/*
248 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
249 * left-shifted bitmap of these types.
250 */
251enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800252 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700253 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800254 NR_MOVE_TYPE,
255};
256
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800257/* "mc" and its members are protected by cgroup_mutex */
258static struct move_charge_struct {
259 struct mem_cgroup *from;
260 struct mem_cgroup *to;
261 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800262 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800263 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800264 struct task_struct *moving_task; /* a task moving charges */
265 wait_queue_head_t waitq; /* a waitq for other context */
266} mc = {
267 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
268};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800269
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700270static bool move_anon(void)
271{
272 return test_bit(MOVE_CHARGE_TYPE_ANON,
273 &mc.to->move_charge_at_immigrate);
274}
275
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700276static bool move_file(void)
277{
278 return test_bit(MOVE_CHARGE_TYPE_FILE,
279 &mc.to->move_charge_at_immigrate);
280}
281
Balbir Singh4e416952009-09-23 15:56:39 -0700282/*
283 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
284 * limit reclaim to prevent infinite loops, if they ever occur.
285 */
286#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
287#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
288
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800289enum charge_type {
290 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
291 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700292 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700293 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800294 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -0700295 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700296 NR_CHARGE_TYPE,
297};
298
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700299/* only for here (for easy reading.) */
300#define PCGF_CACHE (1UL << PCG_CACHE)
301#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700302#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700303/* Not used, but added here for completeness */
304#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800305
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800306/* for encoding cft->private value on file */
307#define _MEM (0)
308#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700309#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800310#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
311#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
312#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700313/* Used for OOM nofiier */
314#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800315
Balbir Singh75822b42009-09-23 15:56:38 -0700316/*
317 * Reclaim flags for mem_cgroup_hierarchical_reclaim
318 */
319#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
320#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
321#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
322#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700323#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
324#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700325
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800326static void mem_cgroup_get(struct mem_cgroup *mem);
327static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800328static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800329static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800330
Balbir Singhf64c3f52009-09-23 15:56:37 -0700331static struct mem_cgroup_per_zone *
332mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
333{
334 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
335}
336
Wu Fengguangd3242362009-12-16 12:19:59 +0100337struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
338{
339 return &mem->css;
340}
341
Balbir Singhf64c3f52009-09-23 15:56:37 -0700342static struct mem_cgroup_per_zone *
343page_cgroup_zoneinfo(struct page_cgroup *pc)
344{
345 struct mem_cgroup *mem = pc->mem_cgroup;
346 int nid = page_cgroup_nid(pc);
347 int zid = page_cgroup_zid(pc);
348
349 if (!mem)
350 return NULL;
351
352 return mem_cgroup_zoneinfo(mem, nid, zid);
353}
354
355static struct mem_cgroup_tree_per_zone *
356soft_limit_tree_node_zone(int nid, int zid)
357{
358 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
359}
360
361static struct mem_cgroup_tree_per_zone *
362soft_limit_tree_from_page(struct page *page)
363{
364 int nid = page_to_nid(page);
365 int zid = page_zonenum(page);
366
367 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
368}
369
370static void
Balbir Singh4e416952009-09-23 15:56:39 -0700371__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700372 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700373 struct mem_cgroup_tree_per_zone *mctz,
374 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700375{
376 struct rb_node **p = &mctz->rb_root.rb_node;
377 struct rb_node *parent = NULL;
378 struct mem_cgroup_per_zone *mz_node;
379
380 if (mz->on_tree)
381 return;
382
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700383 mz->usage_in_excess = new_usage_in_excess;
384 if (!mz->usage_in_excess)
385 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700386 while (*p) {
387 parent = *p;
388 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
389 tree_node);
390 if (mz->usage_in_excess < mz_node->usage_in_excess)
391 p = &(*p)->rb_left;
392 /*
393 * We can't avoid mem cgroups that are over their soft
394 * limit by the same amount
395 */
396 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
397 p = &(*p)->rb_right;
398 }
399 rb_link_node(&mz->tree_node, parent, p);
400 rb_insert_color(&mz->tree_node, &mctz->rb_root);
401 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700402}
403
404static void
405__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
406 struct mem_cgroup_per_zone *mz,
407 struct mem_cgroup_tree_per_zone *mctz)
408{
409 if (!mz->on_tree)
410 return;
411 rb_erase(&mz->tree_node, &mctz->rb_root);
412 mz->on_tree = false;
413}
414
415static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700416mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
417 struct mem_cgroup_per_zone *mz,
418 struct mem_cgroup_tree_per_zone *mctz)
419{
420 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700421 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700422 spin_unlock(&mctz->lock);
423}
424
Balbir Singhf64c3f52009-09-23 15:56:37 -0700425
426static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
427{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700428 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700429 struct mem_cgroup_per_zone *mz;
430 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700431 int nid = page_to_nid(page);
432 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700433 mctz = soft_limit_tree_from_page(page);
434
435 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700436 * Necessary to update all ancestors when hierarchy is used.
437 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700438 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700439 for (; mem; mem = parent_mem_cgroup(mem)) {
440 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700441 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700442 /*
443 * We have to update the tree if mz is on RB-tree or
444 * mem is over its softlimit.
445 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700446 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700447 spin_lock(&mctz->lock);
448 /* if on-tree, remove it */
449 if (mz->on_tree)
450 __mem_cgroup_remove_exceeded(mem, mz, mctz);
451 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700452 * Insert again. mz->usage_in_excess will be updated.
453 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700454 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700455 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700456 spin_unlock(&mctz->lock);
457 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700458 }
459}
460
461static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
462{
463 int node, zone;
464 struct mem_cgroup_per_zone *mz;
465 struct mem_cgroup_tree_per_zone *mctz;
466
467 for_each_node_state(node, N_POSSIBLE) {
468 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
469 mz = mem_cgroup_zoneinfo(mem, node, zone);
470 mctz = soft_limit_tree_node_zone(node, zone);
471 mem_cgroup_remove_exceeded(mem, mz, mctz);
472 }
473 }
474}
475
Balbir Singh4e416952009-09-23 15:56:39 -0700476static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
477{
478 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
479}
480
481static struct mem_cgroup_per_zone *
482__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
483{
484 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700485 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700486
487retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700488 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700489 rightmost = rb_last(&mctz->rb_root);
490 if (!rightmost)
491 goto done; /* Nothing to reclaim from */
492
493 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
494 /*
495 * Remove the node now but someone else can add it back,
496 * we will to add it back at the end of reclaim to its correct
497 * position in the tree.
498 */
499 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
500 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
501 !css_tryget(&mz->mem->css))
502 goto retry;
503done:
504 return mz;
505}
506
507static struct mem_cgroup_per_zone *
508mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
509{
510 struct mem_cgroup_per_zone *mz;
511
512 spin_lock(&mctz->lock);
513 mz = __mem_cgroup_largest_soft_limit_node(mctz);
514 spin_unlock(&mctz->lock);
515 return mz;
516}
517
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800518static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
519 enum mem_cgroup_stat_index idx)
520{
521 int cpu;
522 s64 val = 0;
523
524 for_each_possible_cpu(cpu)
525 val += per_cpu(mem->stat->count[idx], cpu);
526 return val;
527}
528
529static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
530{
531 s64 ret;
532
533 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
534 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
535 return ret;
536}
537
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700538static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
539 bool charge)
540{
541 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800542 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700543}
544
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700545static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
546 struct page_cgroup *pc,
547 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800548{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700549 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800550
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800551 preempt_disable();
552
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700553 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800554 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800555 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800556 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700557
558 if (charge)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800559 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
Balaji Rao55e462b2008-05-01 04:35:12 -0700560 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800561 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800562 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800563
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800564 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800565}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800566
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700567static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700568 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800569{
570 int nid, zid;
571 struct mem_cgroup_per_zone *mz;
572 u64 total = 0;
573
574 for_each_online_node(nid)
575 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
576 mz = mem_cgroup_zoneinfo(mem, nid, zid);
577 total += MEM_CGROUP_ZSTAT(mz, idx);
578 }
579 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800580}
581
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800582static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
583{
584 s64 val;
585
586 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
587
588 return !(val & ((1 << event_mask_shift) - 1));
589}
590
591/*
592 * Check events in order.
593 *
594 */
595static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
596{
597 /* threshold event is triggered in finer grain than soft limit */
598 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
599 mem_cgroup_threshold(mem);
600 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
601 mem_cgroup_update_tree(mem, page);
602 }
603}
604
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800605static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800606{
607 return container_of(cgroup_subsys_state(cont,
608 mem_cgroup_subsys_id), struct mem_cgroup,
609 css);
610}
611
Balbir Singhcf475ad2008-04-29 01:00:16 -0700612struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800613{
Balbir Singh31a78f22008-09-28 23:09:31 +0100614 /*
615 * mm_update_next_owner() may clear mm->owner to NULL
616 * if it races with swapoff, page migration, etc.
617 * So this can be called with p == NULL.
618 */
619 if (unlikely(!p))
620 return NULL;
621
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800622 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
623 struct mem_cgroup, css);
624}
625
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800626static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
627{
628 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700629
630 if (!mm)
631 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800632 /*
633 * Because we have no locks, mm->owner's may be being moved to other
634 * cgroup. We use css_tryget() here even if this looks
635 * pessimistic (rather than adding locks here).
636 */
637 rcu_read_lock();
638 do {
639 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
640 if (unlikely(!mem))
641 break;
642 } while (!css_tryget(&mem->css));
643 rcu_read_unlock();
644 return mem;
645}
646
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700647/*
648 * Call callback function against all cgroup under hierarchy tree.
649 */
650static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
651 int (*func)(struct mem_cgroup *, void *))
652{
653 int found, ret, nextid;
654 struct cgroup_subsys_state *css;
655 struct mem_cgroup *mem;
656
657 if (!root->use_hierarchy)
658 return (*func)(root, data);
659
660 nextid = 1;
661 do {
662 ret = 0;
663 mem = NULL;
664
665 rcu_read_lock();
666 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
667 &found);
668 if (css && css_tryget(css))
669 mem = container_of(css, struct mem_cgroup, css);
670 rcu_read_unlock();
671
672 if (mem) {
673 ret = (*func)(mem, data);
674 css_put(&mem->css);
675 }
676 nextid = found + 1;
677 } while (!ret && css);
678
679 return ret;
680}
681
Balbir Singh4b3bde42009-09-23 15:56:32 -0700682static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
683{
684 return (mem == root_mem_cgroup);
685}
686
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800687/*
688 * Following LRU functions are allowed to be used without PCG_LOCK.
689 * Operations are called by routine of global LRU independently from memcg.
690 * What we have to take care of here is validness of pc->mem_cgroup.
691 *
692 * Changes to pc->mem_cgroup happens when
693 * 1. charge
694 * 2. moving account
695 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
696 * It is added to LRU before charge.
697 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
698 * When moving account, the page is not on LRU. It's isolated.
699 */
700
701void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800702{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800703 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800704 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700705
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800706 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800707 return;
708 pc = lookup_page_cgroup(page);
709 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700710 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800711 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700712 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800713 /*
714 * We don't check PCG_USED bit. It's cleared when the "page" is finally
715 * removed from global LRU.
716 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800717 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700718 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700719 if (mem_cgroup_is_root(pc->mem_cgroup))
720 return;
721 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800722 list_del_init(&pc->lru);
723 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800724}
725
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800726void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800727{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800728 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800729}
730
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800731void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800732{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800733 struct mem_cgroup_per_zone *mz;
734 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800735
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800736 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700737 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700738
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800739 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800740 /*
741 * Used bit is set without atomic ops but after smp_wmb().
742 * For making pc->mem_cgroup visible, insert smp_rmb() here.
743 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800744 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700745 /* unused or root page is not rotated. */
746 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800747 return;
748 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700749 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800750}
751
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800752void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
753{
754 struct page_cgroup *pc;
755 struct mem_cgroup_per_zone *mz;
756
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800757 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800758 return;
759 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700760 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800761 /*
762 * Used bit is set without atomic ops but after smp_wmb().
763 * For making pc->mem_cgroup visible, insert smp_rmb() here.
764 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800765 smp_rmb();
766 if (!PageCgroupUsed(pc))
767 return;
768
769 mz = page_cgroup_zoneinfo(pc);
770 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700771 SetPageCgroupAcctLRU(pc);
772 if (mem_cgroup_is_root(pc->mem_cgroup))
773 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800774 list_add(&pc->lru, &mz->lists[lru]);
775}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800776
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800777/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800778 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
779 * lru because the page may.be reused after it's fully uncharged (because of
780 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
781 * it again. This function is only used to charge SwapCache. It's done under
782 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800783 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800784static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800785{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800786 unsigned long flags;
787 struct zone *zone = page_zone(page);
788 struct page_cgroup *pc = lookup_page_cgroup(page);
789
790 spin_lock_irqsave(&zone->lru_lock, flags);
791 /*
792 * Forget old LRU when this page_cgroup is *not* used. This Used bit
793 * is guarded by lock_page() because the page is SwapCache.
794 */
795 if (!PageCgroupUsed(pc))
796 mem_cgroup_del_lru_list(page, page_lru(page));
797 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800798}
799
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800800static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
801{
802 unsigned long flags;
803 struct zone *zone = page_zone(page);
804 struct page_cgroup *pc = lookup_page_cgroup(page);
805
806 spin_lock_irqsave(&zone->lru_lock, flags);
807 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700808 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800809 mem_cgroup_add_lru_list(page, page_lru(page));
810 spin_unlock_irqrestore(&zone->lru_lock, flags);
811}
812
813
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800814void mem_cgroup_move_lists(struct page *page,
815 enum lru_list from, enum lru_list to)
816{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800817 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800818 return;
819 mem_cgroup_del_lru_list(page, from);
820 mem_cgroup_add_lru_list(page, to);
821}
822
David Rientjes4c4a2212008-02-07 00:14:06 -0800823int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
824{
825 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700826 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800827
828 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700829 rcu_read_lock();
830 curr = try_get_mem_cgroup_from_mm(task->mm);
831 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800832 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700833 if (!curr)
834 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800835 /*
836 * We should check use_hierarchy of "mem" not "curr". Because checking
837 * use_hierarchy of "curr" here make this function true if hierarchy is
838 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
839 * hierarchy(even if use_hierarchy is disabled in "mem").
840 */
841 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700842 ret = css_is_ancestor(&curr->css, &mem->css);
843 else
844 ret = (curr == mem);
845 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800846 return ret;
847}
848
Balbir Singh66e17072008-02-07 00:13:56 -0800849/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800850 * prev_priority control...this will be used in memory reclaim path.
851 */
852int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
853{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800854 int prev_priority;
855
856 spin_lock(&mem->reclaim_param_lock);
857 prev_priority = mem->prev_priority;
858 spin_unlock(&mem->reclaim_param_lock);
859
860 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800861}
862
863void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
864{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800865 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800866 if (priority < mem->prev_priority)
867 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800868 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800869}
870
871void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
872{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800873 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800874 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800875 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800876}
877
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800878static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800879{
880 unsigned long active;
881 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800882 unsigned long gb;
883 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800884
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700885 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
886 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800887
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800888 gb = (inactive + active) >> (30 - PAGE_SHIFT);
889 if (gb)
890 inactive_ratio = int_sqrt(10 * gb);
891 else
892 inactive_ratio = 1;
893
894 if (present_pages) {
895 present_pages[0] = inactive;
896 present_pages[1] = active;
897 }
898
899 return inactive_ratio;
900}
901
902int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
903{
904 unsigned long active;
905 unsigned long inactive;
906 unsigned long present_pages[2];
907 unsigned long inactive_ratio;
908
909 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
910
911 inactive = present_pages[0];
912 active = present_pages[1];
913
914 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800915 return 1;
916
917 return 0;
918}
919
Rik van Riel56e49d22009-06-16 15:32:28 -0700920int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
921{
922 unsigned long active;
923 unsigned long inactive;
924
925 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
926 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
927
928 return (active > inactive);
929}
930
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800931unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
932 struct zone *zone,
933 enum lru_list lru)
934{
935 int nid = zone->zone_pgdat->node_id;
936 int zid = zone_idx(zone);
937 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
938
939 return MEM_CGROUP_ZSTAT(mz, lru);
940}
941
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800942struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
943 struct zone *zone)
944{
945 int nid = zone->zone_pgdat->node_id;
946 int zid = zone_idx(zone);
947 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
948
949 return &mz->reclaim_stat;
950}
951
952struct zone_reclaim_stat *
953mem_cgroup_get_reclaim_stat_from_page(struct page *page)
954{
955 struct page_cgroup *pc;
956 struct mem_cgroup_per_zone *mz;
957
958 if (mem_cgroup_disabled())
959 return NULL;
960
961 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800962 /*
963 * Used bit is set without atomic ops but after smp_wmb().
964 * For making pc->mem_cgroup visible, insert smp_rmb() here.
965 */
966 smp_rmb();
967 if (!PageCgroupUsed(pc))
968 return NULL;
969
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800970 mz = page_cgroup_zoneinfo(pc);
971 if (!mz)
972 return NULL;
973
974 return &mz->reclaim_stat;
975}
976
Balbir Singh66e17072008-02-07 00:13:56 -0800977unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
978 struct list_head *dst,
979 unsigned long *scanned, int order,
980 int mode, struct zone *z,
981 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700982 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800983{
984 unsigned long nr_taken = 0;
985 struct page *page;
986 unsigned long scan;
987 LIST_HEAD(pc_list);
988 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800989 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800990 int nid = z->zone_pgdat->node_id;
991 int zid = zone_idx(z);
992 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700993 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700994 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800995
Balbir Singhcf475ad2008-04-29 01:00:16 -0700996 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800997 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700998 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800999
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001000 scan = 0;
1001 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001002 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001003 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001004
1005 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001006 if (unlikely(!PageCgroupUsed(pc)))
1007 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -08001008 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001009 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001010
Hugh Dickins436c65412008-02-07 00:14:12 -08001011 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001012 ret = __isolate_lru_page(page, mode, file);
1013 switch (ret) {
1014 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001015 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001016 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -08001017 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001018 break;
1019 case -EBUSY:
1020 /* we don't affect global LRU but rotate in our LRU */
1021 mem_cgroup_rotate_lru_list(page, page_lru(page));
1022 break;
1023 default:
1024 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001025 }
1026 }
1027
Balbir Singh66e17072008-02-07 00:13:56 -08001028 *scanned = scan;
1029 return nr_taken;
1030}
1031
Balbir Singh6d61ef42009-01-07 18:08:06 -08001032#define mem_cgroup_from_res_counter(counter, member) \
1033 container_of(counter, struct mem_cgroup, member)
1034
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001035static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1036{
1037 if (do_swap_account) {
1038 if (res_counter_check_under_limit(&mem->res) &&
1039 res_counter_check_under_limit(&mem->memsw))
1040 return true;
1041 } else
1042 if (res_counter_check_under_limit(&mem->res))
1043 return true;
1044 return false;
1045}
1046
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001047static unsigned int get_swappiness(struct mem_cgroup *memcg)
1048{
1049 struct cgroup *cgrp = memcg->css.cgroup;
1050 unsigned int swappiness;
1051
1052 /* root ? */
1053 if (cgrp->parent == NULL)
1054 return vm_swappiness;
1055
1056 spin_lock(&memcg->reclaim_param_lock);
1057 swappiness = memcg->swappiness;
1058 spin_unlock(&memcg->reclaim_param_lock);
1059
1060 return swappiness;
1061}
1062
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001063static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1064{
1065 int *val = data;
1066 (*val)++;
1067 return 0;
1068}
Balbir Singhe2224322009-04-02 16:57:39 -07001069
1070/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001071 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001072 * @memcg: The memory cgroup that went over limit
1073 * @p: Task that is going to be killed
1074 *
1075 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1076 * enabled
1077 */
1078void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1079{
1080 struct cgroup *task_cgrp;
1081 struct cgroup *mem_cgrp;
1082 /*
1083 * Need a buffer in BSS, can't rely on allocations. The code relies
1084 * on the assumption that OOM is serialized for memory controller.
1085 * If this assumption is broken, revisit this code.
1086 */
1087 static char memcg_name[PATH_MAX];
1088 int ret;
1089
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001090 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001091 return;
1092
1093
1094 rcu_read_lock();
1095
1096 mem_cgrp = memcg->css.cgroup;
1097 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1098
1099 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1100 if (ret < 0) {
1101 /*
1102 * Unfortunately, we are unable to convert to a useful name
1103 * But we'll still print out the usage information
1104 */
1105 rcu_read_unlock();
1106 goto done;
1107 }
1108 rcu_read_unlock();
1109
1110 printk(KERN_INFO "Task in %s killed", memcg_name);
1111
1112 rcu_read_lock();
1113 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1114 if (ret < 0) {
1115 rcu_read_unlock();
1116 goto done;
1117 }
1118 rcu_read_unlock();
1119
1120 /*
1121 * Continues from above, so we don't need an KERN_ level
1122 */
1123 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1124done:
1125
1126 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1127 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1128 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1129 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1130 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1131 "failcnt %llu\n",
1132 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1133 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1134 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1135}
1136
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001137/*
1138 * This function returns the number of memcg under hierarchy tree. Returns
1139 * 1(self count) if no children.
1140 */
1141static int mem_cgroup_count_children(struct mem_cgroup *mem)
1142{
1143 int num = 0;
1144 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1145 return num;
1146}
1147
Balbir Singh6d61ef42009-01-07 18:08:06 -08001148/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001149 * Visit the first child (need not be the first child as per the ordering
1150 * of the cgroup list, since we track last_scanned_child) of @mem and use
1151 * that to reclaim free pages from.
1152 */
1153static struct mem_cgroup *
1154mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1155{
1156 struct mem_cgroup *ret = NULL;
1157 struct cgroup_subsys_state *css;
1158 int nextid, found;
1159
1160 if (!root_mem->use_hierarchy) {
1161 css_get(&root_mem->css);
1162 ret = root_mem;
1163 }
1164
1165 while (!ret) {
1166 rcu_read_lock();
1167 nextid = root_mem->last_scanned_child + 1;
1168 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1169 &found);
1170 if (css && css_tryget(css))
1171 ret = container_of(css, struct mem_cgroup, css);
1172
1173 rcu_read_unlock();
1174 /* Updates scanning parameter */
1175 spin_lock(&root_mem->reclaim_param_lock);
1176 if (!css) {
1177 /* this means start scan from ID:1 */
1178 root_mem->last_scanned_child = 0;
1179 } else
1180 root_mem->last_scanned_child = found;
1181 spin_unlock(&root_mem->reclaim_param_lock);
1182 }
1183
1184 return ret;
1185}
1186
1187/*
1188 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1189 * we reclaimed from, so that we don't end up penalizing one child extensively
1190 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001191 *
1192 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001193 *
1194 * We give up and return to the caller when we visit root_mem twice.
1195 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001196 *
1197 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001198 */
1199static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001200 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001201 gfp_t gfp_mask,
1202 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001203{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001204 struct mem_cgroup *victim;
1205 int ret, total = 0;
1206 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001207 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1208 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001209 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1210 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001211
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001212 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1213 if (root_mem->memsw_is_minimum)
1214 noswap = true;
1215
Balbir Singh4e416952009-09-23 15:56:39 -07001216 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001217 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001218 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001219 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001220 if (loop >= 1)
1221 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001222 if (loop >= 2) {
1223 /*
1224 * If we have not been able to reclaim
1225 * anything, it might because there are
1226 * no reclaimable pages under this hierarchy
1227 */
1228 if (!check_soft || !total) {
1229 css_put(&victim->css);
1230 break;
1231 }
1232 /*
1233 * We want to do more targetted reclaim.
1234 * excess >> 2 is not to excessive so as to
1235 * reclaim too much, nor too less that we keep
1236 * coming back to reclaim from this cgroup
1237 */
1238 if (total >= (excess >> 2) ||
1239 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1240 css_put(&victim->css);
1241 break;
1242 }
1243 }
1244 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001245 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001246 /* this cgroup's local usage == 0 */
1247 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001248 continue;
1249 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001250 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001251 if (check_soft)
1252 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1253 noswap, get_swappiness(victim), zone,
1254 zone->zone_pgdat->node_id);
1255 else
1256 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1257 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001258 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001259 /*
1260 * At shrinking usage, we can't check we should stop here or
1261 * reclaim more. It's depends on callers. last_scanned_child
1262 * will work enough for keeping fairness under tree.
1263 */
1264 if (shrink)
1265 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001266 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001267 if (check_soft) {
1268 if (res_counter_check_under_soft_limit(&root_mem->res))
1269 return total;
1270 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001271 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001272 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001273 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001274}
1275
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001276static int mem_cgroup_oom_lock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001277{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001278 int *val = (int *)data;
1279 int x;
1280 /*
1281 * Logically, we can stop scanning immediately when we find
1282 * a memcg is already locked. But condidering unlock ops and
1283 * creation/removal of memcg, scan-all is simple operation.
1284 */
1285 x = atomic_inc_return(&mem->oom_lock);
1286 *val = max(x, *val);
1287 return 0;
1288}
1289/*
1290 * Check OOM-Killer is already running under our hierarchy.
1291 * If someone is running, return false.
1292 */
1293static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1294{
1295 int lock_count = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001296
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001297 mem_cgroup_walk_tree(mem, &lock_count, mem_cgroup_oom_lock_cb);
1298
1299 if (lock_count == 1)
1300 return true;
1301 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001302}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001303
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001304static int mem_cgroup_oom_unlock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001305{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001306 /*
1307 * When a new child is created while the hierarchy is under oom,
1308 * mem_cgroup_oom_lock() may not be called. We have to use
1309 * atomic_add_unless() here.
1310 */
1311 atomic_add_unless(&mem->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001312 return 0;
1313}
1314
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001315static void mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001316{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001317 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_unlock_cb);
1318}
1319
1320static DEFINE_MUTEX(memcg_oom_mutex);
1321static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1322
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001323struct oom_wait_info {
1324 struct mem_cgroup *mem;
1325 wait_queue_t wait;
1326};
1327
1328static int memcg_oom_wake_function(wait_queue_t *wait,
1329 unsigned mode, int sync, void *arg)
1330{
1331 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1332 struct oom_wait_info *oom_wait_info;
1333
1334 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1335
1336 if (oom_wait_info->mem == wake_mem)
1337 goto wakeup;
1338 /* if no hierarchy, no match */
1339 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1340 return 0;
1341 /*
1342 * Both of oom_wait_info->mem and wake_mem are stable under us.
1343 * Then we can use css_is_ancestor without taking care of RCU.
1344 */
1345 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1346 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1347 return 0;
1348
1349wakeup:
1350 return autoremove_wake_function(wait, mode, sync, arg);
1351}
1352
1353static void memcg_wakeup_oom(struct mem_cgroup *mem)
1354{
1355 /* for filtering, pass "mem" as argument. */
1356 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1357}
1358
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001359static void memcg_oom_recover(struct mem_cgroup *mem)
1360{
1361 if (mem->oom_kill_disable && atomic_read(&mem->oom_lock))
1362 memcg_wakeup_oom(mem);
1363}
1364
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001365/*
1366 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1367 */
1368bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1369{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001370 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001371 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001372
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001373 owait.mem = mem;
1374 owait.wait.flags = 0;
1375 owait.wait.func = memcg_oom_wake_function;
1376 owait.wait.private = current;
1377 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001378 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001379 /* At first, try to OOM lock hierarchy under mem.*/
1380 mutex_lock(&memcg_oom_mutex);
1381 locked = mem_cgroup_oom_lock(mem);
1382 /*
1383 * Even if signal_pending(), we can't quit charge() loop without
1384 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1385 * under OOM is always welcomed, use TASK_KILLABLE here.
1386 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001387 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1388 if (!locked || mem->oom_kill_disable)
1389 need_to_kill = false;
1390 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001391 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001392 mutex_unlock(&memcg_oom_mutex);
1393
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001394 if (need_to_kill) {
1395 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001396 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001397 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001398 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001399 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001400 }
1401 mutex_lock(&memcg_oom_mutex);
1402 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001403 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001404 mutex_unlock(&memcg_oom_mutex);
1405
1406 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1407 return false;
1408 /* Give chance to dying process */
1409 schedule_timeout(1);
1410 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001411}
1412
Balbir Singhd69b0422009-06-17 16:26:34 -07001413/*
1414 * Currently used to update mapped file statistics, but the routine can be
1415 * generalized to update other statistics as well.
1416 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001417void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001418{
1419 struct mem_cgroup *mem;
Balbir Singhd69b0422009-06-17 16:26:34 -07001420 struct page_cgroup *pc;
1421
Balbir Singhd69b0422009-06-17 16:26:34 -07001422 pc = lookup_page_cgroup(page);
1423 if (unlikely(!pc))
1424 return;
1425
1426 lock_page_cgroup(pc);
1427 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001428 if (!mem || !PageCgroupUsed(pc))
Balbir Singhd69b0422009-06-17 16:26:34 -07001429 goto done;
1430
1431 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001432 * Preemption is already disabled. We can use __this_cpu_xxx
Balbir Singhd69b0422009-06-17 16:26:34 -07001433 */
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001434 if (val > 0) {
1435 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1436 SetPageCgroupFileMapped(pc);
1437 } else {
1438 __this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1439 ClearPageCgroupFileMapped(pc);
1440 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001441
Balbir Singhd69b0422009-06-17 16:26:34 -07001442done:
1443 unlock_page_cgroup(pc);
1444}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001445
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001446/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001447 * size of first charge trial. "32" comes from vmscan.c's magic value.
1448 * TODO: maybe necessary to use big numbers in big irons.
1449 */
1450#define CHARGE_SIZE (32 * PAGE_SIZE)
1451struct memcg_stock_pcp {
1452 struct mem_cgroup *cached; /* this never be root cgroup */
1453 int charge;
1454 struct work_struct work;
1455};
1456static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1457static atomic_t memcg_drain_count;
1458
1459/*
1460 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1461 * from local stock and true is returned. If the stock is 0 or charges from a
1462 * cgroup which is not current target, returns false. This stock will be
1463 * refilled.
1464 */
1465static bool consume_stock(struct mem_cgroup *mem)
1466{
1467 struct memcg_stock_pcp *stock;
1468 bool ret = true;
1469
1470 stock = &get_cpu_var(memcg_stock);
1471 if (mem == stock->cached && stock->charge)
1472 stock->charge -= PAGE_SIZE;
1473 else /* need to call res_counter_charge */
1474 ret = false;
1475 put_cpu_var(memcg_stock);
1476 return ret;
1477}
1478
1479/*
1480 * Returns stocks cached in percpu to res_counter and reset cached information.
1481 */
1482static void drain_stock(struct memcg_stock_pcp *stock)
1483{
1484 struct mem_cgroup *old = stock->cached;
1485
1486 if (stock->charge) {
1487 res_counter_uncharge(&old->res, stock->charge);
1488 if (do_swap_account)
1489 res_counter_uncharge(&old->memsw, stock->charge);
1490 }
1491 stock->cached = NULL;
1492 stock->charge = 0;
1493}
1494
1495/*
1496 * This must be called under preempt disabled or must be called by
1497 * a thread which is pinned to local cpu.
1498 */
1499static void drain_local_stock(struct work_struct *dummy)
1500{
1501 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1502 drain_stock(stock);
1503}
1504
1505/*
1506 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001507 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001508 */
1509static void refill_stock(struct mem_cgroup *mem, int val)
1510{
1511 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1512
1513 if (stock->cached != mem) { /* reset if necessary */
1514 drain_stock(stock);
1515 stock->cached = mem;
1516 }
1517 stock->charge += val;
1518 put_cpu_var(memcg_stock);
1519}
1520
1521/*
1522 * Tries to drain stocked charges in other cpus. This function is asynchronous
1523 * and just put a work per cpu for draining localy on each cpu. Caller can
1524 * expects some charges will be back to res_counter later but cannot wait for
1525 * it.
1526 */
1527static void drain_all_stock_async(void)
1528{
1529 int cpu;
1530 /* This function is for scheduling "drain" in asynchronous way.
1531 * The result of "drain" is not directly handled by callers. Then,
1532 * if someone is calling drain, we don't have to call drain more.
1533 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1534 * there is a race. We just do loose check here.
1535 */
1536 if (atomic_read(&memcg_drain_count))
1537 return;
1538 /* Notify other cpus that system-wide "drain" is running */
1539 atomic_inc(&memcg_drain_count);
1540 get_online_cpus();
1541 for_each_online_cpu(cpu) {
1542 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1543 schedule_work_on(cpu, &stock->work);
1544 }
1545 put_online_cpus();
1546 atomic_dec(&memcg_drain_count);
1547 /* We don't wait for flush_work */
1548}
1549
1550/* This is a synchronous drain interface. */
1551static void drain_all_stock_sync(void)
1552{
1553 /* called when force_empty is called */
1554 atomic_inc(&memcg_drain_count);
1555 schedule_on_each_cpu(drain_local_stock);
1556 atomic_dec(&memcg_drain_count);
1557}
1558
1559static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1560 unsigned long action,
1561 void *hcpu)
1562{
1563 int cpu = (unsigned long)hcpu;
1564 struct memcg_stock_pcp *stock;
1565
1566 if (action != CPU_DEAD)
1567 return NOTIFY_OK;
1568 stock = &per_cpu(memcg_stock, cpu);
1569 drain_stock(stock);
1570 return NOTIFY_OK;
1571}
1572
1573/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001574 * Unlike exported interface, "oom" parameter is added. if oom==true,
1575 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001576 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001577static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001578 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001579{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001580 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001581 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001582 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001583 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001584
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001585 /*
1586 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1587 * in system level. So, allow to go ahead dying process in addition to
1588 * MEMDIE process.
1589 */
1590 if (unlikely(test_thread_flag(TIF_MEMDIE)
1591 || fatal_signal_pending(current)))
1592 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001593
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001594 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08001595 * We always charge the cgroup the mm_struct belongs to.
1596 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001597 * thread group leader migrates. It's possible that mm is not
1598 * set, if so charge the init_mm (happens for pagecache usage).
1599 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001600 mem = *memcg;
1601 if (likely(!mem)) {
1602 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001603 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001604 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001605 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001606 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001607 if (unlikely(!mem))
1608 return 0;
1609
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001610 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001611 if (mem_cgroup_is_root(mem))
1612 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001613
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001614 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001615 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001616 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001617
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001618 if (consume_stock(mem))
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001619 goto done;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001620
1621 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001622 if (likely(!ret)) {
1623 if (!do_swap_account)
1624 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001625 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001626 if (likely(!ret))
1627 break;
1628 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001629 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001630 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001631 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1632 memsw);
1633 } else
1634 /* mem counter fails */
1635 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1636 res);
1637
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001638 /* reduce request size and retry */
1639 if (csize > PAGE_SIZE) {
1640 csize = PAGE_SIZE;
1641 continue;
1642 }
Hugh Dickins3be912772008-02-07 00:14:19 -08001643 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001644 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001645
Balbir Singh4e416952009-09-23 15:56:39 -07001646 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1647 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001648 if (ret)
1649 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001650
1651 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001652 * try_to_free_mem_cgroup_pages() might not give us a full
1653 * picture of reclaim. Some pages are reclaimed and might be
1654 * moved to swap cache or just unmapped from the cgroup.
1655 * Check the limit again to see if the reclaim reduced the
1656 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001657 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001658 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001659 if (mem_cgroup_check_under_limit(mem_over_limit))
1660 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -08001661
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001662 /* try to avoid oom while someone is moving charge */
1663 if (mc.moving_task && current != mc.moving_task) {
1664 struct mem_cgroup *from, *to;
1665 bool do_continue = false;
1666 /*
1667 * There is a small race that "from" or "to" can be
1668 * freed by rmdir, so we use css_tryget().
1669 */
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001670 from = mc.from;
1671 to = mc.to;
1672 if (from && css_tryget(&from->css)) {
1673 if (mem_over_limit->use_hierarchy)
1674 do_continue = css_is_ancestor(
1675 &from->css,
1676 &mem_over_limit->css);
1677 else
1678 do_continue = (from == mem_over_limit);
1679 css_put(&from->css);
1680 }
1681 if (!do_continue && to && css_tryget(&to->css)) {
1682 if (mem_over_limit->use_hierarchy)
1683 do_continue = css_is_ancestor(
1684 &to->css,
1685 &mem_over_limit->css);
1686 else
1687 do_continue = (to == mem_over_limit);
1688 css_put(&to->css);
1689 }
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001690 if (do_continue) {
1691 DEFINE_WAIT(wait);
1692 prepare_to_wait(&mc.waitq, &wait,
1693 TASK_INTERRUPTIBLE);
1694 /* moving charge context might have finished. */
1695 if (mc.moving_task)
1696 schedule();
1697 finish_wait(&mc.waitq, &wait);
1698 continue;
1699 }
1700 }
1701
Hugh Dickins3be912772008-02-07 00:14:19 -08001702 if (!nr_retries--) {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001703 if (!oom)
1704 goto nomem;
1705 if (mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) {
1706 nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1707 continue;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001708 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001709 /* When we reach here, current task is dying .*/
1710 css_put(&mem->css);
1711 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08001712 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001713 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001714 if (csize > PAGE_SIZE)
1715 refill_stock(mem, csize - PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001716done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001717 return 0;
1718nomem:
1719 css_put(&mem->css);
1720 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001721bypass:
1722 *memcg = NULL;
1723 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001724}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001725
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001726/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001727 * Somemtimes we have to undo a charge we got by try_charge().
1728 * This function is for that and do uncharge, put css's refcnt.
1729 * gotten by try_charge().
1730 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001731static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1732 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001733{
1734 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001735 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001736 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001737 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1738 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
1739 WARN_ON_ONCE(count > INT_MAX);
1740 __css_put(&mem->css, (int)count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001741 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001742 /* we don't need css_put for root */
1743}
1744
1745static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1746{
1747 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001748}
1749
1750/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001751 * A helper function to get mem_cgroup from ID. must be called under
1752 * rcu_read_lock(). The caller must check css_is_removed() or some if
1753 * it's concern. (dropping refcnt from swap can be called against removed
1754 * memcg.)
1755 */
1756static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1757{
1758 struct cgroup_subsys_state *css;
1759
1760 /* ID 0 is unused ID */
1761 if (!id)
1762 return NULL;
1763 css = css_lookup(&mem_cgroup_subsys, id);
1764 if (!css)
1765 return NULL;
1766 return container_of(css, struct mem_cgroup, css);
1767}
1768
Wu Fengguange42d9d52009-12-16 12:19:59 +01001769struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001770{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001771 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001772 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001773 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001774 swp_entry_t ent;
1775
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001776 VM_BUG_ON(!PageLocked(page));
1777
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001778 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07001779 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001780 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001781 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001782 if (mem && !css_tryget(&mem->css))
1783 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001784 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001785 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001786 id = lookup_swap_cgroup(ent);
1787 rcu_read_lock();
1788 mem = mem_cgroup_lookup(id);
1789 if (mem && !css_tryget(&mem->css))
1790 mem = NULL;
1791 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001792 }
Daisuke Nishimurac0bd3f632009-04-30 15:08:11 -07001793 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001794 return mem;
1795}
1796
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001797/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001798 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001799 * USED state. If already USED, uncharge and return.
1800 */
1801
1802static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1803 struct page_cgroup *pc,
1804 enum charge_type ctype)
1805{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001806 /* try_charge() can return NULL to *memcg, taking care of it. */
1807 if (!mem)
1808 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001809
1810 lock_page_cgroup(pc);
1811 if (unlikely(PageCgroupUsed(pc))) {
1812 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001813 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001814 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001815 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001816
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001817 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001818 /*
1819 * We access a page_cgroup asynchronously without lock_page_cgroup().
1820 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1821 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1822 * before USED bit, we need memory barrier here.
1823 * See mem_cgroup_add_lru_list(), etc.
1824 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001825 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001826 switch (ctype) {
1827 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1828 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1829 SetPageCgroupCache(pc);
1830 SetPageCgroupUsed(pc);
1831 break;
1832 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1833 ClearPageCgroupCache(pc);
1834 SetPageCgroupUsed(pc);
1835 break;
1836 default:
1837 break;
1838 }
Hugh Dickins3be912772008-02-07 00:14:19 -08001839
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001840 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001841
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001842 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001843 /*
1844 * "charge_statistics" updated event counter. Then, check it.
1845 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1846 * if they exceeds softlimit.
1847 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001848 memcg_check_events(mem, pc->page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001849}
1850
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001851/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001852 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001853 * @pc: page_cgroup of the page.
1854 * @from: mem_cgroup which the page is moved from.
1855 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001856 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001857 *
1858 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001859 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001860 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001861 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001862 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1863 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1864 * true, this function does "uncharge" from old cgroup, but it doesn't if
1865 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001866 */
1867
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001868static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001869 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001870{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001871 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001872 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001873 VM_BUG_ON(!PageCgroupLocked(pc));
1874 VM_BUG_ON(!PageCgroupUsed(pc));
1875 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001876
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001877 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001878 /* Update mapped_file data for mem_cgroup */
1879 preempt_disable();
1880 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1881 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1882 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07001883 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001884 mem_cgroup_charge_statistics(from, pc, false);
1885 if (uncharge)
1886 /* This is not "cancel", but cancel_charge does all we need. */
1887 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07001888
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001889 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001890 pc->mem_cgroup = to;
1891 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001892 /*
1893 * We charges against "to" which may not have any tasks. Then, "to"
1894 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001895 * this function is just force_empty() and move charge, so it's
1896 * garanteed that "to" is never removed. So, we don't check rmdir
1897 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001898 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001899}
1900
1901/*
1902 * check whether the @pc is valid for moving account and call
1903 * __mem_cgroup_move_account()
1904 */
1905static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001906 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001907{
1908 int ret = -EINVAL;
1909 lock_page_cgroup(pc);
1910 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001911 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001912 ret = 0;
1913 }
1914 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001915 /*
1916 * check events
1917 */
1918 memcg_check_events(to, pc->page);
1919 memcg_check_events(from, pc->page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001920 return ret;
1921}
1922
1923/*
1924 * move charges to its parent.
1925 */
1926
1927static int mem_cgroup_move_parent(struct page_cgroup *pc,
1928 struct mem_cgroup *child,
1929 gfp_t gfp_mask)
1930{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001931 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001932 struct cgroup *cg = child->css.cgroup;
1933 struct cgroup *pcg = cg->parent;
1934 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001935 int ret;
1936
1937 /* Is ROOT ? */
1938 if (!pcg)
1939 return -EINVAL;
1940
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001941 ret = -EBUSY;
1942 if (!get_page_unless_zero(page))
1943 goto out;
1944 if (isolate_lru_page(page))
1945 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001946
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001947 parent = mem_cgroup_from_cont(pcg);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001948 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001949 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001950 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001951
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001952 ret = mem_cgroup_move_account(pc, child, parent, true);
1953 if (ret)
1954 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001955put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001956 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001957put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001958 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001959out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001960 return ret;
1961}
1962
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001963/*
1964 * Charge the memory controller for page usage.
1965 * Return
1966 * 0 if the charge was successful
1967 * < 0 if the cgroup is over its limit
1968 */
1969static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1970 gfp_t gfp_mask, enum charge_type ctype,
1971 struct mem_cgroup *memcg)
1972{
1973 struct mem_cgroup *mem;
1974 struct page_cgroup *pc;
1975 int ret;
1976
1977 pc = lookup_page_cgroup(page);
1978 /* can happen at boot */
1979 if (unlikely(!pc))
1980 return 0;
1981 prefetchw(pc);
1982
1983 mem = memcg;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001984 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001985 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001986 return ret;
1987
1988 __mem_cgroup_commit_charge(mem, pc, ctype);
1989 return 0;
1990}
1991
1992int mem_cgroup_newpage_charge(struct page *page,
1993 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001994{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001995 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001996 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001997 if (PageCompound(page))
1998 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001999 /*
2000 * If already mapped, we don't have to account.
2001 * If page cache, page->mapping has address_space.
2002 * But page->mapping may have out-of-use anon_vma pointer,
2003 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2004 * is NULL.
2005 */
2006 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2007 return 0;
2008 if (unlikely(!mm))
2009 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002010 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002011 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002012}
2013
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002014static void
2015__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2016 enum charge_type ctype);
2017
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002018int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2019 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002020{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002021 struct mem_cgroup *mem = NULL;
2022 int ret;
2023
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002024 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002025 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002026 if (PageCompound(page))
2027 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002028 /*
2029 * Corner case handling. This is called from add_to_page_cache()
2030 * in usual. But some FS (shmem) precharges this page before calling it
2031 * and call add_to_page_cache() with GFP_NOWAIT.
2032 *
2033 * For GFP_NOWAIT case, the page may be pre-charged before calling
2034 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2035 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002036 * And when the page is SwapCache, it should take swap information
2037 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002038 */
2039 if (!(gfp_mask & __GFP_WAIT)) {
2040 struct page_cgroup *pc;
2041
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002042
2043 pc = lookup_page_cgroup(page);
2044 if (!pc)
2045 return 0;
2046 lock_page_cgroup(pc);
2047 if (PageCgroupUsed(pc)) {
2048 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002049 return 0;
2050 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002051 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002052 }
2053
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002054 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08002055 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002056
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07002057 if (page_is_file_cache(page))
2058 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002059 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002060
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002061 /* shmem */
2062 if (PageSwapCache(page)) {
2063 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2064 if (!ret)
2065 __mem_cgroup_commit_charge_swapin(page, mem,
2066 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2067 } else
2068 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
2069 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002070
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002071 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002072}
2073
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002074/*
2075 * While swap-in, try_charge -> commit or cancel, the page is locked.
2076 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002077 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002078 * "commit()" or removed by "cancel()"
2079 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002080int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2081 struct page *page,
2082 gfp_t mask, struct mem_cgroup **ptr)
2083{
2084 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002085 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002086
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002087 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002088 return 0;
2089
2090 if (!do_swap_account)
2091 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002092 /*
2093 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002094 * the pte, and even removed page from swap cache: in those cases
2095 * do_swap_page()'s pte_same() test will fail; but there's also a
2096 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002097 */
2098 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002099 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002100 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002101 if (!mem)
2102 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002103 *ptr = mem;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002104 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002105 /* drop extra refcnt from tryget */
2106 css_put(&mem->css);
2107 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002108charge_cur_mm:
2109 if (unlikely(!mm))
2110 mm = &init_mm;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002111 return __mem_cgroup_try_charge(mm, mask, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002112}
2113
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002114static void
2115__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2116 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002117{
2118 struct page_cgroup *pc;
2119
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002120 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002121 return;
2122 if (!ptr)
2123 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002124 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002125 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002126 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002127 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002128 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002129 /*
2130 * Now swap is on-memory. This means this page may be
2131 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002132 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2133 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2134 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002135 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002136 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002137 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002138 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002139 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002140
2141 id = swap_cgroup_record(ent, 0);
2142 rcu_read_lock();
2143 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002144 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002145 /*
2146 * This recorded memcg can be obsolete one. So, avoid
2147 * calling css_tryget
2148 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002149 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002150 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002151 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002152 mem_cgroup_put(memcg);
2153 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002154 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002155 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002156 /*
2157 * At swapin, we may charge account against cgroup which has no tasks.
2158 * So, rmdir()->pre_destroy() can be called while we do this charge.
2159 * In that case, we need to call pre_destroy() again. check it here.
2160 */
2161 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002162}
2163
Daisuke Nishimura83aae4c72009-04-02 16:57:48 -07002164void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2165{
2166 __mem_cgroup_commit_charge_swapin(page, ptr,
2167 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2168}
2169
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002170void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2171{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002172 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002173 return;
2174 if (!mem)
2175 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002176 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002177}
2178
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002179static void
2180__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2181{
2182 struct memcg_batch_info *batch = NULL;
2183 bool uncharge_memsw = true;
2184 /* If swapout, usage of swap doesn't decrease */
2185 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2186 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002187
2188 batch = &current->memcg_batch;
2189 /*
2190 * In usual, we do css_get() when we remember memcg pointer.
2191 * But in this case, we keep res->usage until end of a series of
2192 * uncharges. Then, it's ok to ignore memcg's refcnt.
2193 */
2194 if (!batch->memcg)
2195 batch->memcg = mem;
2196 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002197 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2198 * In those cases, all pages freed continously can be expected to be in
2199 * the same cgroup and we have chance to coalesce uncharges.
2200 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2201 * because we want to do uncharge as soon as possible.
2202 */
2203
2204 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2205 goto direct_uncharge;
2206
2207 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002208 * In typical case, batch->memcg == mem. This means we can
2209 * merge a series of uncharges to an uncharge of res_counter.
2210 * If not, we uncharge res_counter ony by one.
2211 */
2212 if (batch->memcg != mem)
2213 goto direct_uncharge;
2214 /* remember freed charge and uncharge it later */
2215 batch->bytes += PAGE_SIZE;
2216 if (uncharge_memsw)
2217 batch->memsw_bytes += PAGE_SIZE;
2218 return;
2219direct_uncharge:
2220 res_counter_uncharge(&mem->res, PAGE_SIZE);
2221 if (uncharge_memsw)
2222 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002223 if (unlikely(batch->memcg != mem))
2224 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002225 return;
2226}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002227
Balbir Singh8697d332008-02-07 00:13:59 -08002228/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002229 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002230 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002231static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002232__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002233{
Hugh Dickins82895462008-03-04 14:29:08 -08002234 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002235 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002236 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002237
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002238 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002239 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002240
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002241 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002242 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002243
Balbir Singh8697d332008-02-07 00:13:59 -08002244 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002245 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002246 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002247 pc = lookup_page_cgroup(page);
2248 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002249 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002250
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002251 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002252
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002253 mem = pc->mem_cgroup;
2254
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002255 if (!PageCgroupUsed(pc))
2256 goto unlock_out;
2257
2258 switch (ctype) {
2259 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002260 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002261 /* See mem_cgroup_prepare_migration() */
2262 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002263 goto unlock_out;
2264 break;
2265 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2266 if (!PageAnon(page)) { /* Shared memory */
2267 if (page->mapping && !page_is_file_cache(page))
2268 goto unlock_out;
2269 } else if (page_mapped(page)) /* Anon */
2270 goto unlock_out;
2271 break;
2272 default:
2273 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002274 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002275
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002276 if (!mem_cgroup_is_root(mem))
2277 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002278 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2279 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002280 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002281
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002282 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002283 /*
2284 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2285 * freed from LRU. This is safe because uncharged page is expected not
2286 * to be reused (freed soon). Exception is SwapCache, it's handled by
2287 * special functions.
2288 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002289
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002290 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002291 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002292
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002293 memcg_check_events(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002294 /* at swapout, this memcg will be accessed to record to swap */
2295 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2296 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002297
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002298 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002299
2300unlock_out:
2301 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002302 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002303}
2304
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002305void mem_cgroup_uncharge_page(struct page *page)
2306{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002307 /* early check. */
2308 if (page_mapped(page))
2309 return;
2310 if (page->mapping && !PageAnon(page))
2311 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002312 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2313}
2314
2315void mem_cgroup_uncharge_cache_page(struct page *page)
2316{
2317 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002318 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002319 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2320}
2321
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002322/*
2323 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2324 * In that cases, pages are freed continuously and we can expect pages
2325 * are in the same memcg. All these calls itself limits the number of
2326 * pages freed at once, then uncharge_start/end() is called properly.
2327 * This may be called prural(2) times in a context,
2328 */
2329
2330void mem_cgroup_uncharge_start(void)
2331{
2332 current->memcg_batch.do_batch++;
2333 /* We can do nest. */
2334 if (current->memcg_batch.do_batch == 1) {
2335 current->memcg_batch.memcg = NULL;
2336 current->memcg_batch.bytes = 0;
2337 current->memcg_batch.memsw_bytes = 0;
2338 }
2339}
2340
2341void mem_cgroup_uncharge_end(void)
2342{
2343 struct memcg_batch_info *batch = &current->memcg_batch;
2344
2345 if (!batch->do_batch)
2346 return;
2347
2348 batch->do_batch--;
2349 if (batch->do_batch) /* If stacked, do nothing. */
2350 return;
2351
2352 if (!batch->memcg)
2353 return;
2354 /*
2355 * This "batch->memcg" is valid without any css_get/put etc...
2356 * bacause we hide charges behind us.
2357 */
2358 if (batch->bytes)
2359 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2360 if (batch->memsw_bytes)
2361 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002362 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002363 /* forget this pointer (for sanity check) */
2364 batch->memcg = NULL;
2365}
2366
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002367#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002368/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002369 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002370 * memcg information is recorded to swap_cgroup of "ent"
2371 */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002372void
2373mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002374{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002375 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002376 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002377
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002378 if (!swapout) /* this was a swap cache but the swap is unused ! */
2379 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2380
2381 memcg = __mem_cgroup_uncharge_common(page, ctype);
2382
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002383 /* record memcg information */
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002384 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002385 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002386 mem_cgroup_get(memcg);
2387 }
KAMEZAWA Hiroyuki8a9478ca2009-06-17 16:27:17 -07002388 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002389 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002390}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002391#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002392
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002393#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2394/*
2395 * called from swap_entry_free(). remove record in swap_cgroup and
2396 * uncharge "memsw" account.
2397 */
2398void mem_cgroup_uncharge_swap(swp_entry_t ent)
2399{
2400 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002401 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002402
2403 if (!do_swap_account)
2404 return;
2405
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002406 id = swap_cgroup_record(ent, 0);
2407 rcu_read_lock();
2408 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002409 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002410 /*
2411 * We uncharge this because swap is freed.
2412 * This memcg can be obsolete one. We avoid calling css_tryget
2413 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002414 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002415 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002416 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002417 mem_cgroup_put(memcg);
2418 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002419 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002420}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002421
2422/**
2423 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2424 * @entry: swap entry to be moved
2425 * @from: mem_cgroup which the entry is moved from
2426 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002427 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002428 *
2429 * It succeeds only when the swap_cgroup's record for this entry is the same
2430 * as the mem_cgroup's id of @from.
2431 *
2432 * Returns 0 on success, -EINVAL on failure.
2433 *
2434 * The caller must have charged to @to, IOW, called res_counter_charge() about
2435 * both res and memsw, and called css_get().
2436 */
2437static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002438 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002439{
2440 unsigned short old_id, new_id;
2441
2442 old_id = css_id(&from->css);
2443 new_id = css_id(&to->css);
2444
2445 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002446 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002447 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002448 /*
2449 * This function is only called from task migration context now.
2450 * It postpones res_counter and refcount handling till the end
2451 * of task migration(mem_cgroup_clear_mc()) for performance
2452 * improvement. But we cannot postpone mem_cgroup_get(to)
2453 * because if the process that has been moved to @to does
2454 * swap-in, the refcount of @to might be decreased to 0.
2455 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002456 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002457 if (need_fixup) {
2458 if (!mem_cgroup_is_root(from))
2459 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2460 mem_cgroup_put(from);
2461 /*
2462 * we charged both to->res and to->memsw, so we should
2463 * uncharge to->res.
2464 */
2465 if (!mem_cgroup_is_root(to))
2466 res_counter_uncharge(&to->res, PAGE_SIZE);
2467 css_put(&to->css);
2468 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002469 return 0;
2470 }
2471 return -EINVAL;
2472}
2473#else
2474static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002475 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002476{
2477 return -EINVAL;
2478}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002479#endif
2480
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002481/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002482 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2483 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002484 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002485int mem_cgroup_prepare_migration(struct page *page,
2486 struct page *newpage, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002487{
2488 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002489 struct mem_cgroup *mem = NULL;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002490 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002491 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002492
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002493 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002494 return 0;
2495
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002496 pc = lookup_page_cgroup(page);
2497 lock_page_cgroup(pc);
2498 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002499 mem = pc->mem_cgroup;
2500 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002501 /*
2502 * At migrating an anonymous page, its mapcount goes down
2503 * to 0 and uncharge() will be called. But, even if it's fully
2504 * unmapped, migration may fail and this page has to be
2505 * charged again. We set MIGRATION flag here and delay uncharge
2506 * until end_migration() is called
2507 *
2508 * Corner Case Thinking
2509 * A)
2510 * When the old page was mapped as Anon and it's unmap-and-freed
2511 * while migration was ongoing.
2512 * If unmap finds the old page, uncharge() of it will be delayed
2513 * until end_migration(). If unmap finds a new page, it's
2514 * uncharged when it make mapcount to be 1->0. If unmap code
2515 * finds swap_migration_entry, the new page will not be mapped
2516 * and end_migration() will find it(mapcount==0).
2517 *
2518 * B)
2519 * When the old page was mapped but migraion fails, the kernel
2520 * remaps it. A charge for it is kept by MIGRATION flag even
2521 * if mapcount goes down to 0. We can do remap successfully
2522 * without charging it again.
2523 *
2524 * C)
2525 * The "old" page is under lock_page() until the end of
2526 * migration, so, the old page itself will not be swapped-out.
2527 * If the new page is swapped out before end_migraton, our
2528 * hook to usual swap-out path will catch the event.
2529 */
2530 if (PageAnon(page))
2531 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002532 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002533 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002534 /*
2535 * If the page is not charged at this point,
2536 * we return here.
2537 */
2538 if (!mem)
2539 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002540
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04002541 *ptr = mem;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002542 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2543 css_put(&mem->css);/* drop extra refcnt */
2544 if (ret || *ptr == NULL) {
2545 if (PageAnon(page)) {
2546 lock_page_cgroup(pc);
2547 ClearPageCgroupMigration(pc);
2548 unlock_page_cgroup(pc);
2549 /*
2550 * The old page may be fully unmapped while we kept it.
2551 */
2552 mem_cgroup_uncharge_page(page);
2553 }
2554 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002555 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002556 /*
2557 * We charge new page before it's used/mapped. So, even if unlock_page()
2558 * is called before end_migration, we can catch all events on this new
2559 * page. In the case new page is migrated but not remapped, new page's
2560 * mapcount will be finally 0 and we call uncharge in end_migration().
2561 */
2562 pc = lookup_page_cgroup(newpage);
2563 if (PageAnon(page))
2564 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2565 else if (page_is_file_cache(page))
2566 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2567 else
2568 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2569 __mem_cgroup_commit_charge(mem, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002570 return ret;
2571}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002572
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002573/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002574void mem_cgroup_end_migration(struct mem_cgroup *mem,
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002575 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002576{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002577 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002578 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002579
2580 if (!mem)
2581 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002582 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002583 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002584 /* at migration success, oldpage->mapping is NULL. */
2585 if (oldpage->mapping) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002586 used = oldpage;
2587 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002588 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002589 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002590 unused = oldpage;
2591 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002592 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002593 * We disallowed uncharge of pages under migration because mapcount
2594 * of the page goes down to zero, temporarly.
2595 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002596 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002597 pc = lookup_page_cgroup(oldpage);
2598 lock_page_cgroup(pc);
2599 ClearPageCgroupMigration(pc);
2600 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002601
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002602 if (unused != oldpage)
2603 pc = lookup_page_cgroup(unused);
2604 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2605
2606 pc = lookup_page_cgroup(used);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002607 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002608 * If a page is a file cache, radix-tree replacement is very atomic
2609 * and we can skip this check. When it was an Anon page, its mapcount
2610 * goes down to 0. But because we added MIGRATION flage, it's not
2611 * uncharged yet. There are several case but page->mapcount check
2612 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2613 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002614 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002615 if (PageAnon(used))
2616 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002617 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002618 * At migration, we may charge account against cgroup which has no
2619 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002620 * So, rmdir()->pre_destroy() can be called while we do this charge.
2621 * In that case, we need to call pre_destroy() again. check it here.
2622 */
2623 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002624}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002625
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002626/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002627 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2628 * Calling hierarchical_reclaim is not enough because we should update
2629 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2630 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2631 * not from the memcg which this page would be charged to.
2632 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002633 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002634int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002635 struct mm_struct *mm,
2636 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002637{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002638 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002639 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002640
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002641 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002642 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002643
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002644 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2645 if (!ret)
2646 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002647
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002648 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002649}
2650
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002651static DEFINE_MUTEX(set_limit_mutex);
2652
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002653static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002654 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002655{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002656 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002657 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002658 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002659 int children = mem_cgroup_count_children(memcg);
2660 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002661 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002662
2663 /*
2664 * For keeping hierarchical_reclaim simple, how long we should retry
2665 * is depends on callers. We set our retry-count to be function
2666 * of # of children which we should visit in this loop.
2667 */
2668 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2669
2670 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002671
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002672 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002673 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002674 if (signal_pending(current)) {
2675 ret = -EINTR;
2676 break;
2677 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002678 /*
2679 * Rather than hide all in some function, I do this in
2680 * open coded manner. You see what this really does.
2681 * We have to guarantee mem->res.limit < mem->memsw.limit.
2682 */
2683 mutex_lock(&set_limit_mutex);
2684 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2685 if (memswlimit < val) {
2686 ret = -EINVAL;
2687 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002688 break;
2689 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002690
2691 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2692 if (memlimit < val)
2693 enlarge = 1;
2694
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002695 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002696 if (!ret) {
2697 if (memswlimit == val)
2698 memcg->memsw_is_minimum = true;
2699 else
2700 memcg->memsw_is_minimum = false;
2701 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002702 mutex_unlock(&set_limit_mutex);
2703
2704 if (!ret)
2705 break;
2706
Bob Liuaa20d482009-12-15 16:47:14 -08002707 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002708 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002709 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2710 /* Usage is reduced ? */
2711 if (curusage >= oldusage)
2712 retry_count--;
2713 else
2714 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002715 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002716 if (!ret && enlarge)
2717 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002718
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002719 return ret;
2720}
2721
Li Zefan338c8432009-06-17 16:27:15 -07002722static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2723 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002724{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002725 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002726 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002727 int children = mem_cgroup_count_children(memcg);
2728 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002729 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002730
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002731 /* see mem_cgroup_resize_res_limit */
2732 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2733 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002734 while (retry_count) {
2735 if (signal_pending(current)) {
2736 ret = -EINTR;
2737 break;
2738 }
2739 /*
2740 * Rather than hide all in some function, I do this in
2741 * open coded manner. You see what this really does.
2742 * We have to guarantee mem->res.limit < mem->memsw.limit.
2743 */
2744 mutex_lock(&set_limit_mutex);
2745 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2746 if (memlimit > val) {
2747 ret = -EINVAL;
2748 mutex_unlock(&set_limit_mutex);
2749 break;
2750 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002751 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2752 if (memswlimit < val)
2753 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002754 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002755 if (!ret) {
2756 if (memlimit == val)
2757 memcg->memsw_is_minimum = true;
2758 else
2759 memcg->memsw_is_minimum = false;
2760 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002761 mutex_unlock(&set_limit_mutex);
2762
2763 if (!ret)
2764 break;
2765
Balbir Singh4e416952009-09-23 15:56:39 -07002766 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002767 MEM_CGROUP_RECLAIM_NOSWAP |
2768 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002769 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002770 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002771 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002772 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002773 else
2774 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002775 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002776 if (!ret && enlarge)
2777 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002778 return ret;
2779}
2780
Balbir Singh4e416952009-09-23 15:56:39 -07002781unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2782 gfp_t gfp_mask, int nid,
2783 int zid)
2784{
2785 unsigned long nr_reclaimed = 0;
2786 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2787 unsigned long reclaimed;
2788 int loop = 0;
2789 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002790 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002791
2792 if (order > 0)
2793 return 0;
2794
2795 mctz = soft_limit_tree_node_zone(nid, zid);
2796 /*
2797 * This loop can run a while, specially if mem_cgroup's continuously
2798 * keep exceeding their soft limit and putting the system under
2799 * pressure
2800 */
2801 do {
2802 if (next_mz)
2803 mz = next_mz;
2804 else
2805 mz = mem_cgroup_largest_soft_limit_node(mctz);
2806 if (!mz)
2807 break;
2808
2809 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2810 gfp_mask,
2811 MEM_CGROUP_RECLAIM_SOFT);
2812 nr_reclaimed += reclaimed;
2813 spin_lock(&mctz->lock);
2814
2815 /*
2816 * If we failed to reclaim anything from this memory cgroup
2817 * it is time to move on to the next cgroup
2818 */
2819 next_mz = NULL;
2820 if (!reclaimed) {
2821 do {
2822 /*
2823 * Loop until we find yet another one.
2824 *
2825 * By the time we get the soft_limit lock
2826 * again, someone might have aded the
2827 * group back on the RB tree. Iterate to
2828 * make sure we get a different mem.
2829 * mem_cgroup_largest_soft_limit_node returns
2830 * NULL if no other cgroup is present on
2831 * the tree
2832 */
2833 next_mz =
2834 __mem_cgroup_largest_soft_limit_node(mctz);
2835 if (next_mz == mz) {
2836 css_put(&next_mz->mem->css);
2837 next_mz = NULL;
2838 } else /* next_mz == NULL or other memcg */
2839 break;
2840 } while (1);
2841 }
Balbir Singh4e416952009-09-23 15:56:39 -07002842 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002843 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002844 /*
2845 * One school of thought says that we should not add
2846 * back the node to the tree if reclaim returns 0.
2847 * But our reclaim could return 0, simply because due
2848 * to priority we are exposing a smaller subset of
2849 * memory to reclaim from. Consider this as a longer
2850 * term TODO.
2851 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002852 /* If excess == 0, no tree ops */
2853 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002854 spin_unlock(&mctz->lock);
2855 css_put(&mz->mem->css);
2856 loop++;
2857 /*
2858 * Could not reclaim anything and there are no more
2859 * mem cgroups to try or we seem to be looping without
2860 * reclaiming anything.
2861 */
2862 if (!nr_reclaimed &&
2863 (next_mz == NULL ||
2864 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2865 break;
2866 } while (!nr_reclaimed);
2867 if (next_mz)
2868 css_put(&next_mz->mem->css);
2869 return nr_reclaimed;
2870}
2871
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002872/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002873 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002874 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2875 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002876static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002877 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002878{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002879 struct zone *zone;
2880 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002881 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002882 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002883 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002884 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002885
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002886 zone = &NODE_DATA(node)->node_zones[zid];
2887 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002888 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002889
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002890 loop = MEM_CGROUP_ZSTAT(mz, lru);
2891 /* give some margin against EBUSY etc...*/
2892 loop += 256;
2893 busy = NULL;
2894 while (loop--) {
2895 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002896 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002897 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002898 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002899 break;
2900 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002901 pc = list_entry(list->prev, struct page_cgroup, lru);
2902 if (busy == pc) {
2903 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08002904 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002905 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002906 continue;
2907 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002908 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002909
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002910 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002911 if (ret == -ENOMEM)
2912 break;
2913
2914 if (ret == -EBUSY || ret == -EINVAL) {
2915 /* found lock contention or "pc" is obsolete. */
2916 busy = pc;
2917 cond_resched();
2918 } else
2919 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002920 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002921
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002922 if (!ret && !list_empty(list))
2923 return -EBUSY;
2924 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002925}
2926
2927/*
2928 * make mem_cgroup's charge to be 0 if there is no task.
2929 * This enables deleting this mem_cgroup.
2930 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002931static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002932{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002933 int ret;
2934 int node, zid, shrink;
2935 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002936 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002937
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002938 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002939
2940 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002941 /* should free all ? */
2942 if (free_all)
2943 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002944move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002945 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002946 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002947 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002948 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002949 ret = -EINTR;
2950 if (signal_pending(current))
2951 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002952 /* This is for making all *used* pages to be on LRU. */
2953 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002954 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002955 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002956 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002957 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002958 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002959 for_each_lru(l) {
2960 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002961 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002962 if (ret)
2963 break;
2964 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002965 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002966 if (ret)
2967 break;
2968 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002969 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002970 /* it seems parent cgroup doesn't have enough mem */
2971 if (ret == -ENOMEM)
2972 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002973 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002974 /* "ret" should also be checked to ensure all lists are empty. */
2975 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002976out:
2977 css_put(&mem->css);
2978 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002979
2980try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002981 /* returns EBUSY if there is a task or if we come here twice. */
2982 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002983 ret = -EBUSY;
2984 goto out;
2985 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002986 /* we call try-to-free pages for make this cgroup empty */
2987 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002988 /* try to free all pages in this cgroup */
2989 shrink = 1;
2990 while (nr_retries && mem->res.usage > 0) {
2991 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002992
2993 if (signal_pending(current)) {
2994 ret = -EINTR;
2995 goto out;
2996 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002997 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2998 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002999 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003000 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003001 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003002 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003003 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003004
3005 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003006 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003007 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003008 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003009}
3010
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003011int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3012{
3013 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3014}
3015
3016
Balbir Singh18f59ea2009-01-07 18:08:07 -08003017static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3018{
3019 return mem_cgroup_from_cont(cont)->use_hierarchy;
3020}
3021
3022static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3023 u64 val)
3024{
3025 int retval = 0;
3026 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3027 struct cgroup *parent = cont->parent;
3028 struct mem_cgroup *parent_mem = NULL;
3029
3030 if (parent)
3031 parent_mem = mem_cgroup_from_cont(parent);
3032
3033 cgroup_lock();
3034 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003035 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003036 * in the child subtrees. If it is unset, then the change can
3037 * occur, provided the current cgroup has no children.
3038 *
3039 * For the root cgroup, parent_mem is NULL, we allow value to be
3040 * set if there are no children.
3041 */
3042 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3043 (val == 1 || val == 0)) {
3044 if (list_empty(&cont->children))
3045 mem->use_hierarchy = val;
3046 else
3047 retval = -EBUSY;
3048 } else
3049 retval = -EINVAL;
3050 cgroup_unlock();
3051
3052 return retval;
3053}
3054
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003055struct mem_cgroup_idx_data {
3056 s64 val;
3057 enum mem_cgroup_stat_index idx;
3058};
3059
3060static int
3061mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
3062{
3063 struct mem_cgroup_idx_data *d = data;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003064 d->val += mem_cgroup_read_stat(mem, d->idx);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003065 return 0;
3066}
3067
3068static void
3069mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3070 enum mem_cgroup_stat_index idx, s64 *val)
3071{
3072 struct mem_cgroup_idx_data d;
3073 d.idx = idx;
3074 d.val = 0;
3075 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
3076 *val = d.val;
3077}
3078
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003079static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3080{
3081 u64 idx_val, val;
3082
3083 if (!mem_cgroup_is_root(mem)) {
3084 if (!swap)
3085 return res_counter_read_u64(&mem->res, RES_USAGE);
3086 else
3087 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3088 }
3089
3090 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
3091 val = idx_val;
3092 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
3093 val += idx_val;
3094
3095 if (swap) {
3096 mem_cgroup_get_recursive_idx_stat(mem,
3097 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
3098 val += idx_val;
3099 }
3100
3101 return val << PAGE_SHIFT;
3102}
3103
Paul Menage2c3daa722008-04-29 00:59:58 -07003104static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003105{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003106 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003107 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003108 int type, name;
3109
3110 type = MEMFILE_TYPE(cft->private);
3111 name = MEMFILE_ATTR(cft->private);
3112 switch (type) {
3113 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003114 if (name == RES_USAGE)
3115 val = mem_cgroup_usage(mem, false);
3116 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003117 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003118 break;
3119 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003120 if (name == RES_USAGE)
3121 val = mem_cgroup_usage(mem, true);
3122 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003123 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003124 break;
3125 default:
3126 BUG();
3127 break;
3128 }
3129 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003130}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003131/*
3132 * The user of this function is...
3133 * RES_LIMIT.
3134 */
Paul Menage856c13a2008-07-25 01:47:04 -07003135static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3136 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003137{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003138 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003139 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003140 unsigned long long val;
3141 int ret;
3142
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003143 type = MEMFILE_TYPE(cft->private);
3144 name = MEMFILE_ATTR(cft->private);
3145 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003146 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003147 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3148 ret = -EINVAL;
3149 break;
3150 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003151 /* This function does all necessary parse...reuse it */
3152 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003153 if (ret)
3154 break;
3155 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003156 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003157 else
3158 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003159 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003160 case RES_SOFT_LIMIT:
3161 ret = res_counter_memparse_write_strategy(buffer, &val);
3162 if (ret)
3163 break;
3164 /*
3165 * For memsw, soft limits are hard to implement in terms
3166 * of semantics, for now, we support soft limits for
3167 * control without swap
3168 */
3169 if (type == _MEM)
3170 ret = res_counter_set_soft_limit(&memcg->res, val);
3171 else
3172 ret = -EINVAL;
3173 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003174 default:
3175 ret = -EINVAL; /* should be BUG() ? */
3176 break;
3177 }
3178 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003179}
3180
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003181static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3182 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3183{
3184 struct cgroup *cgroup;
3185 unsigned long long min_limit, min_memsw_limit, tmp;
3186
3187 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3188 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3189 cgroup = memcg->css.cgroup;
3190 if (!memcg->use_hierarchy)
3191 goto out;
3192
3193 while (cgroup->parent) {
3194 cgroup = cgroup->parent;
3195 memcg = mem_cgroup_from_cont(cgroup);
3196 if (!memcg->use_hierarchy)
3197 break;
3198 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3199 min_limit = min(min_limit, tmp);
3200 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3201 min_memsw_limit = min(min_memsw_limit, tmp);
3202 }
3203out:
3204 *mem_limit = min_limit;
3205 *memsw_limit = min_memsw_limit;
3206 return;
3207}
3208
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003209static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003210{
3211 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003212 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003213
3214 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003215 type = MEMFILE_TYPE(event);
3216 name = MEMFILE_ATTR(event);
3217 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003218 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003219 if (type == _MEM)
3220 res_counter_reset_max(&mem->res);
3221 else
3222 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003223 break;
3224 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003225 if (type == _MEM)
3226 res_counter_reset_failcnt(&mem->res);
3227 else
3228 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003229 break;
3230 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003231
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003232 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003233}
3234
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003235static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3236 struct cftype *cft)
3237{
3238 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3239}
3240
Daisuke Nishimura02491442010-03-10 15:22:17 -08003241#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003242static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3243 struct cftype *cft, u64 val)
3244{
3245 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3246
3247 if (val >= (1 << NR_MOVE_TYPE))
3248 return -EINVAL;
3249 /*
3250 * We check this value several times in both in can_attach() and
3251 * attach(), so we need cgroup lock to prevent this value from being
3252 * inconsistent.
3253 */
3254 cgroup_lock();
3255 mem->move_charge_at_immigrate = val;
3256 cgroup_unlock();
3257
3258 return 0;
3259}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003260#else
3261static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3262 struct cftype *cft, u64 val)
3263{
3264 return -ENOSYS;
3265}
3266#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003267
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003268
3269/* For read statistics */
3270enum {
3271 MCS_CACHE,
3272 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003273 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003274 MCS_PGPGIN,
3275 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003276 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003277 MCS_INACTIVE_ANON,
3278 MCS_ACTIVE_ANON,
3279 MCS_INACTIVE_FILE,
3280 MCS_ACTIVE_FILE,
3281 MCS_UNEVICTABLE,
3282 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003283};
3284
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003285struct mcs_total_stat {
3286 s64 stat[NR_MCS_STAT];
3287};
3288
3289struct {
3290 char *local_name;
3291 char *total_name;
3292} memcg_stat_strings[NR_MCS_STAT] = {
3293 {"cache", "total_cache"},
3294 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003295 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003296 {"pgpgin", "total_pgpgin"},
3297 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003298 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003299 {"inactive_anon", "total_inactive_anon"},
3300 {"active_anon", "total_active_anon"},
3301 {"inactive_file", "total_inactive_file"},
3302 {"active_file", "total_active_file"},
3303 {"unevictable", "total_unevictable"}
3304};
3305
3306
3307static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
3308{
3309 struct mcs_total_stat *s = data;
3310 s64 val;
3311
3312 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003313 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003314 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003315 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003316 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003317 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003318 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003319 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003320 s->stat[MCS_PGPGIN] += val;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003321 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003322 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003323 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003324 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003325 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3326 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003327
3328 /* per zone stat */
3329 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3330 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3331 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3332 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3333 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3334 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3335 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3336 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3337 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3338 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3339 return 0;
3340}
3341
3342static void
3343mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3344{
3345 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3346}
3347
Paul Menagec64745c2008-04-29 01:00:02 -07003348static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3349 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003350{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003351 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003352 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003353 int i;
3354
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003355 memset(&mystat, 0, sizeof(mystat));
3356 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003357
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003358 for (i = 0; i < NR_MCS_STAT; i++) {
3359 if (i == MCS_SWAP && !do_swap_account)
3360 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003361 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003362 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003363
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003364 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003365 {
3366 unsigned long long limit, memsw_limit;
3367 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3368 cb->fill(cb, "hierarchical_memory_limit", limit);
3369 if (do_swap_account)
3370 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3371 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003372
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003373 memset(&mystat, 0, sizeof(mystat));
3374 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003375 for (i = 0; i < NR_MCS_STAT; i++) {
3376 if (i == MCS_SWAP && !do_swap_account)
3377 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003378 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003379 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003380
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003381#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003382 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003383
3384 {
3385 int nid, zid;
3386 struct mem_cgroup_per_zone *mz;
3387 unsigned long recent_rotated[2] = {0, 0};
3388 unsigned long recent_scanned[2] = {0, 0};
3389
3390 for_each_online_node(nid)
3391 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3392 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3393
3394 recent_rotated[0] +=
3395 mz->reclaim_stat.recent_rotated[0];
3396 recent_rotated[1] +=
3397 mz->reclaim_stat.recent_rotated[1];
3398 recent_scanned[0] +=
3399 mz->reclaim_stat.recent_scanned[0];
3400 recent_scanned[1] +=
3401 mz->reclaim_stat.recent_scanned[1];
3402 }
3403 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3404 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3405 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3406 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3407 }
3408#endif
3409
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003410 return 0;
3411}
3412
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003413static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3414{
3415 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3416
3417 return get_swappiness(memcg);
3418}
3419
3420static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3421 u64 val)
3422{
3423 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3424 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003425
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003426 if (val > 100)
3427 return -EINVAL;
3428
3429 if (cgrp->parent == NULL)
3430 return -EINVAL;
3431
3432 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003433
3434 cgroup_lock();
3435
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003436 /* If under hierarchy, only empty-root can set this value */
3437 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003438 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3439 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003440 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003441 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003442
3443 spin_lock(&memcg->reclaim_param_lock);
3444 memcg->swappiness = val;
3445 spin_unlock(&memcg->reclaim_param_lock);
3446
Li Zefan068b38c2009-01-15 13:51:26 -08003447 cgroup_unlock();
3448
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003449 return 0;
3450}
3451
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003452static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3453{
3454 struct mem_cgroup_threshold_ary *t;
3455 u64 usage;
3456 int i;
3457
3458 rcu_read_lock();
3459 if (!swap)
3460 t = rcu_dereference(memcg->thresholds);
3461 else
3462 t = rcu_dereference(memcg->memsw_thresholds);
3463
3464 if (!t)
3465 goto unlock;
3466
3467 usage = mem_cgroup_usage(memcg, swap);
3468
3469 /*
3470 * current_threshold points to threshold just below usage.
3471 * If it's not true, a threshold was crossed after last
3472 * call of __mem_cgroup_threshold().
3473 */
Phil Carmody5407a562010-05-26 14:42:42 -07003474 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003475
3476 /*
3477 * Iterate backward over array of thresholds starting from
3478 * current_threshold and check if a threshold is crossed.
3479 * If none of thresholds below usage is crossed, we read
3480 * only one element of the array here.
3481 */
3482 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3483 eventfd_signal(t->entries[i].eventfd, 1);
3484
3485 /* i = current_threshold + 1 */
3486 i++;
3487
3488 /*
3489 * Iterate forward over array of thresholds starting from
3490 * current_threshold+1 and check if a threshold is crossed.
3491 * If none of thresholds above usage is crossed, we read
3492 * only one element of the array here.
3493 */
3494 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3495 eventfd_signal(t->entries[i].eventfd, 1);
3496
3497 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003498 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003499unlock:
3500 rcu_read_unlock();
3501}
3502
3503static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3504{
3505 __mem_cgroup_threshold(memcg, false);
3506 if (do_swap_account)
3507 __mem_cgroup_threshold(memcg, true);
3508}
3509
3510static int compare_thresholds(const void *a, const void *b)
3511{
3512 const struct mem_cgroup_threshold *_a = a;
3513 const struct mem_cgroup_threshold *_b = b;
3514
3515 return _a->threshold - _b->threshold;
3516}
3517
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003518static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem, void *data)
3519{
3520 struct mem_cgroup_eventfd_list *ev;
3521
3522 list_for_each_entry(ev, &mem->oom_notify, list)
3523 eventfd_signal(ev->eventfd, 1);
3524 return 0;
3525}
3526
3527static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3528{
3529 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_notify_cb);
3530}
3531
3532static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3533 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003534{
3535 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3536 struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
3537 int type = MEMFILE_TYPE(cft->private);
3538 u64 threshold, usage;
3539 int size;
3540 int i, ret;
3541
3542 ret = res_counter_memparse_write_strategy(args, &threshold);
3543 if (ret)
3544 return ret;
3545
3546 mutex_lock(&memcg->thresholds_lock);
3547 if (type == _MEM)
3548 thresholds = memcg->thresholds;
3549 else if (type == _MEMSWAP)
3550 thresholds = memcg->memsw_thresholds;
3551 else
3552 BUG();
3553
3554 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3555
3556 /* Check if a threshold crossed before adding a new one */
3557 if (thresholds)
3558 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3559
3560 if (thresholds)
3561 size = thresholds->size + 1;
3562 else
3563 size = 1;
3564
3565 /* Allocate memory for new array of thresholds */
3566 thresholds_new = kmalloc(sizeof(*thresholds_new) +
3567 size * sizeof(struct mem_cgroup_threshold),
3568 GFP_KERNEL);
3569 if (!thresholds_new) {
3570 ret = -ENOMEM;
3571 goto unlock;
3572 }
3573 thresholds_new->size = size;
3574
3575 /* Copy thresholds (if any) to new array */
3576 if (thresholds)
3577 memcpy(thresholds_new->entries, thresholds->entries,
3578 thresholds->size *
3579 sizeof(struct mem_cgroup_threshold));
3580 /* Add new threshold */
3581 thresholds_new->entries[size - 1].eventfd = eventfd;
3582 thresholds_new->entries[size - 1].threshold = threshold;
3583
3584 /* Sort thresholds. Registering of new threshold isn't time-critical */
3585 sort(thresholds_new->entries, size,
3586 sizeof(struct mem_cgroup_threshold),
3587 compare_thresholds, NULL);
3588
3589 /* Find current threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003590 thresholds_new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003591 for (i = 0; i < size; i++) {
3592 if (thresholds_new->entries[i].threshold < usage) {
3593 /*
3594 * thresholds_new->current_threshold will not be used
3595 * until rcu_assign_pointer(), so it's safe to increment
3596 * it here.
3597 */
Phil Carmody5407a562010-05-26 14:42:42 -07003598 ++thresholds_new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003599 }
3600 }
3601
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003602 if (type == _MEM)
3603 rcu_assign_pointer(memcg->thresholds, thresholds_new);
3604 else
3605 rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
3606
3607 /* To be sure that nobody uses thresholds before freeing it */
3608 synchronize_rcu();
3609
3610 kfree(thresholds);
3611unlock:
3612 mutex_unlock(&memcg->thresholds_lock);
3613
3614 return ret;
3615}
3616
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003617static int mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
3618 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003619{
3620 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3621 struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
3622 int type = MEMFILE_TYPE(cft->private);
3623 u64 usage;
3624 int size = 0;
Phil Carmody315c1992010-05-26 14:42:43 -07003625 int i, j, ret = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003626
3627 mutex_lock(&memcg->thresholds_lock);
3628 if (type == _MEM)
3629 thresholds = memcg->thresholds;
3630 else if (type == _MEMSWAP)
3631 thresholds = memcg->memsw_thresholds;
3632 else
3633 BUG();
3634
3635 /*
3636 * Something went wrong if we trying to unregister a threshold
3637 * if we don't have thresholds
3638 */
3639 BUG_ON(!thresholds);
3640
3641 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3642
3643 /* Check if a threshold crossed before removing */
3644 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3645
3646 /* Calculate new number of threshold */
3647 for (i = 0; i < thresholds->size; i++) {
3648 if (thresholds->entries[i].eventfd != eventfd)
3649 size++;
3650 }
3651
3652 /* Set thresholds array to NULL if we don't have thresholds */
3653 if (!size) {
3654 thresholds_new = NULL;
3655 goto assign;
3656 }
3657
3658 /* Allocate memory for new array of thresholds */
3659 thresholds_new = kmalloc(sizeof(*thresholds_new) +
3660 size * sizeof(struct mem_cgroup_threshold),
3661 GFP_KERNEL);
3662 if (!thresholds_new) {
3663 ret = -ENOMEM;
3664 goto unlock;
3665 }
3666 thresholds_new->size = size;
3667
3668 /* Copy thresholds and find current threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003669 thresholds_new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003670 for (i = 0, j = 0; i < thresholds->size; i++) {
3671 if (thresholds->entries[i].eventfd == eventfd)
3672 continue;
3673
3674 thresholds_new->entries[j] = thresholds->entries[i];
3675 if (thresholds_new->entries[j].threshold < usage) {
3676 /*
3677 * thresholds_new->current_threshold will not be used
3678 * until rcu_assign_pointer(), so it's safe to increment
3679 * it here.
3680 */
Phil Carmody5407a562010-05-26 14:42:42 -07003681 ++thresholds_new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003682 }
3683 j++;
3684 }
3685
3686assign:
3687 if (type == _MEM)
3688 rcu_assign_pointer(memcg->thresholds, thresholds_new);
3689 else
3690 rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
3691
3692 /* To be sure that nobody uses thresholds before freeing it */
3693 synchronize_rcu();
3694
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003695 kfree(thresholds);
3696unlock:
3697 mutex_unlock(&memcg->thresholds_lock);
3698
3699 return ret;
3700}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003701
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003702static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3703 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3704{
3705 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3706 struct mem_cgroup_eventfd_list *event;
3707 int type = MEMFILE_TYPE(cft->private);
3708
3709 BUG_ON(type != _OOM_TYPE);
3710 event = kmalloc(sizeof(*event), GFP_KERNEL);
3711 if (!event)
3712 return -ENOMEM;
3713
3714 mutex_lock(&memcg_oom_mutex);
3715
3716 event->eventfd = eventfd;
3717 list_add(&event->list, &memcg->oom_notify);
3718
3719 /* already in OOM ? */
3720 if (atomic_read(&memcg->oom_lock))
3721 eventfd_signal(eventfd, 1);
3722 mutex_unlock(&memcg_oom_mutex);
3723
3724 return 0;
3725}
3726
3727static int mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
3728 struct cftype *cft, struct eventfd_ctx *eventfd)
3729{
3730 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3731 struct mem_cgroup_eventfd_list *ev, *tmp;
3732 int type = MEMFILE_TYPE(cft->private);
3733
3734 BUG_ON(type != _OOM_TYPE);
3735
3736 mutex_lock(&memcg_oom_mutex);
3737
3738 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
3739 if (ev->eventfd == eventfd) {
3740 list_del(&ev->list);
3741 kfree(ev);
3742 }
3743 }
3744
3745 mutex_unlock(&memcg_oom_mutex);
3746
3747 return 0;
3748}
3749
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003750static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3751 struct cftype *cft, struct cgroup_map_cb *cb)
3752{
3753 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3754
3755 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
3756
3757 if (atomic_read(&mem->oom_lock))
3758 cb->fill(cb, "under_oom", 1);
3759 else
3760 cb->fill(cb, "under_oom", 0);
3761 return 0;
3762}
3763
3764/*
3765 */
3766static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
3767 struct cftype *cft, u64 val)
3768{
3769 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3770 struct mem_cgroup *parent;
3771
3772 /* cannot set to root cgroup and only 0 and 1 are allowed */
3773 if (!cgrp->parent || !((val == 0) || (val == 1)))
3774 return -EINVAL;
3775
3776 parent = mem_cgroup_from_cont(cgrp->parent);
3777
3778 cgroup_lock();
3779 /* oom-kill-disable is a flag for subhierarchy. */
3780 if ((parent->use_hierarchy) ||
3781 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
3782 cgroup_unlock();
3783 return -EINVAL;
3784 }
3785 mem->oom_kill_disable = val;
3786 cgroup_unlock();
3787 return 0;
3788}
3789
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003790static struct cftype mem_cgroup_files[] = {
3791 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003792 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003793 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa722008-04-29 00:59:58 -07003794 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003795 .register_event = mem_cgroup_usage_register_event,
3796 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003797 },
3798 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003799 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003800 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003801 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003802 .read_u64 = mem_cgroup_read,
3803 },
3804 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003805 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003806 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003807 .write_string = mem_cgroup_write,
Paul Menage2c3daa722008-04-29 00:59:58 -07003808 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003809 },
3810 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003811 .name = "soft_limit_in_bytes",
3812 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3813 .write_string = mem_cgroup_write,
3814 .read_u64 = mem_cgroup_read,
3815 },
3816 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003817 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003818 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003819 .trigger = mem_cgroup_reset,
Paul Menage2c3daa722008-04-29 00:59:58 -07003820 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003821 },
Balbir Singh8697d332008-02-07 00:13:59 -08003822 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003823 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003824 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003825 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003826 {
3827 .name = "force_empty",
3828 .trigger = mem_cgroup_force_empty_write,
3829 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003830 {
3831 .name = "use_hierarchy",
3832 .write_u64 = mem_cgroup_hierarchy_write,
3833 .read_u64 = mem_cgroup_hierarchy_read,
3834 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003835 {
3836 .name = "swappiness",
3837 .read_u64 = mem_cgroup_swappiness_read,
3838 .write_u64 = mem_cgroup_swappiness_write,
3839 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003840 {
3841 .name = "move_charge_at_immigrate",
3842 .read_u64 = mem_cgroup_move_charge_read,
3843 .write_u64 = mem_cgroup_move_charge_write,
3844 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003845 {
3846 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003847 .read_map = mem_cgroup_oom_control_read,
3848 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003849 .register_event = mem_cgroup_oom_register_event,
3850 .unregister_event = mem_cgroup_oom_unregister_event,
3851 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
3852 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003853};
3854
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003855#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3856static struct cftype memsw_cgroup_files[] = {
3857 {
3858 .name = "memsw.usage_in_bytes",
3859 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3860 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003861 .register_event = mem_cgroup_usage_register_event,
3862 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003863 },
3864 {
3865 .name = "memsw.max_usage_in_bytes",
3866 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3867 .trigger = mem_cgroup_reset,
3868 .read_u64 = mem_cgroup_read,
3869 },
3870 {
3871 .name = "memsw.limit_in_bytes",
3872 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3873 .write_string = mem_cgroup_write,
3874 .read_u64 = mem_cgroup_read,
3875 },
3876 {
3877 .name = "memsw.failcnt",
3878 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3879 .trigger = mem_cgroup_reset,
3880 .read_u64 = mem_cgroup_read,
3881 },
3882};
3883
3884static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3885{
3886 if (!do_swap_account)
3887 return 0;
3888 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3889 ARRAY_SIZE(memsw_cgroup_files));
3890};
3891#else
3892static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3893{
3894 return 0;
3895}
3896#endif
3897
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003898static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3899{
3900 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003901 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003902 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003903 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003904 /*
3905 * This routine is called against possible nodes.
3906 * But it's BUG to call kmalloc() against offline node.
3907 *
3908 * TODO: this routine can waste much memory for nodes which will
3909 * never be onlined. It's better to use memory hotplug callback
3910 * function.
3911 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003912 if (!node_state(node, N_NORMAL_MEMORY))
3913 tmp = -1;
3914 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003915 if (!pn)
3916 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003917
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003918 mem->info.nodeinfo[node] = pn;
3919 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003920
3921 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3922 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003923 for_each_lru(l)
3924 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003925 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003926 mz->on_tree = false;
3927 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003928 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003929 return 0;
3930}
3931
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003932static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3933{
3934 kfree(mem->info.nodeinfo[node]);
3935}
3936
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003937static struct mem_cgroup *mem_cgroup_alloc(void)
3938{
3939 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003940 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003941
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003942 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003943 if (size < PAGE_SIZE)
3944 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003945 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003946 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003947
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07003948 if (!mem)
3949 return NULL;
3950
3951 memset(mem, 0, size);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003952 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
3953 if (!mem->stat) {
3954 if (size < PAGE_SIZE)
3955 kfree(mem);
3956 else
3957 vfree(mem);
3958 mem = NULL;
3959 }
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003960 return mem;
3961}
3962
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003963/*
3964 * At destroying mem_cgroup, references from swap_cgroup can remain.
3965 * (scanning all at force_empty is too costly...)
3966 *
3967 * Instead of clearing all references at force_empty, we remember
3968 * the number of reference from swap_cgroup and free mem_cgroup when
3969 * it goes down to 0.
3970 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003971 * Removal of cgroup itself succeeds regardless of refs from swap.
3972 */
3973
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003974static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003975{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003976 int node;
3977
Balbir Singhf64c3f52009-09-23 15:56:37 -07003978 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003979 free_css_id(&mem_cgroup_subsys, &mem->css);
3980
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003981 for_each_node_state(node, N_POSSIBLE)
3982 free_mem_cgroup_per_zone_info(mem, node);
3983
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003984 free_percpu(mem->stat);
3985 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003986 kfree(mem);
3987 else
3988 vfree(mem);
3989}
3990
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003991static void mem_cgroup_get(struct mem_cgroup *mem)
3992{
3993 atomic_inc(&mem->refcnt);
3994}
3995
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003996static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003997{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003998 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003999 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004000 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004001 if (parent)
4002 mem_cgroup_put(parent);
4003 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004004}
4005
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004006static void mem_cgroup_put(struct mem_cgroup *mem)
4007{
4008 __mem_cgroup_put(mem, 1);
4009}
4010
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004011/*
4012 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4013 */
4014static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4015{
4016 if (!mem->res.parent)
4017 return NULL;
4018 return mem_cgroup_from_res_counter(mem->res.parent, res);
4019}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004020
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004021#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4022static void __init enable_swap_cgroup(void)
4023{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004024 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004025 do_swap_account = 1;
4026}
4027#else
4028static void __init enable_swap_cgroup(void)
4029{
4030}
4031#endif
4032
Balbir Singhf64c3f52009-09-23 15:56:37 -07004033static int mem_cgroup_soft_limit_tree_init(void)
4034{
4035 struct mem_cgroup_tree_per_node *rtpn;
4036 struct mem_cgroup_tree_per_zone *rtpz;
4037 int tmp, node, zone;
4038
4039 for_each_node_state(node, N_POSSIBLE) {
4040 tmp = node;
4041 if (!node_state(node, N_NORMAL_MEMORY))
4042 tmp = -1;
4043 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4044 if (!rtpn)
4045 return 1;
4046
4047 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4048
4049 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4050 rtpz = &rtpn->rb_tree_per_zone[zone];
4051 rtpz->rb_root = RB_ROOT;
4052 spin_lock_init(&rtpz->lock);
4053 }
4054 }
4055 return 0;
4056}
4057
Li Zefan0eb253e2009-01-15 13:51:25 -08004058static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004059mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4060{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004061 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004062 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004063 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004064
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004065 mem = mem_cgroup_alloc();
4066 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004067 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004068
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004069 for_each_node_state(node, N_POSSIBLE)
4070 if (alloc_mem_cgroup_per_zone_info(mem, node))
4071 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004072
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004073 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004074 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004075 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004076 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004077 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004078 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004079 if (mem_cgroup_soft_limit_tree_init())
4080 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004081 for_each_possible_cpu(cpu) {
4082 struct memcg_stock_pcp *stock =
4083 &per_cpu(memcg_stock, cpu);
4084 INIT_WORK(&stock->work, drain_local_stock);
4085 }
4086 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004087 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004088 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004089 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004090 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004091 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004092
Balbir Singh18f59ea2009-01-07 18:08:07 -08004093 if (parent && parent->use_hierarchy) {
4094 res_counter_init(&mem->res, &parent->res);
4095 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004096 /*
4097 * We increment refcnt of the parent to ensure that we can
4098 * safely access it on res_counter_charge/uncharge.
4099 * This refcnt will be decremented when freeing this
4100 * mem_cgroup(see mem_cgroup_put).
4101 */
4102 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004103 } else {
4104 res_counter_init(&mem->res, NULL);
4105 res_counter_init(&mem->memsw, NULL);
4106 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004107 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08004108 spin_lock_init(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004109 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004110
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004111 if (parent)
4112 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004113 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004114 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004115 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004116 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004117free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004118 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004119 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004120 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004121}
4122
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004123static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004124 struct cgroup *cont)
4125{
4126 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004127
4128 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004129}
4130
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004131static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4132 struct cgroup *cont)
4133{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004134 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004135
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004136 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004137}
4138
4139static int mem_cgroup_populate(struct cgroup_subsys *ss,
4140 struct cgroup *cont)
4141{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004142 int ret;
4143
4144 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4145 ARRAY_SIZE(mem_cgroup_files));
4146
4147 if (!ret)
4148 ret = register_memsw_files(cont, ss);
4149 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004150}
4151
Daisuke Nishimura02491442010-03-10 15:22:17 -08004152#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004153/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004154#define PRECHARGE_COUNT_AT_ONCE 256
4155static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004156{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004157 int ret = 0;
4158 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004159 struct mem_cgroup *mem = mc.to;
4160
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004161 if (mem_cgroup_is_root(mem)) {
4162 mc.precharge += count;
4163 /* we don't need css_get for root */
4164 return ret;
4165 }
4166 /* try to charge at once */
4167 if (count > 1) {
4168 struct res_counter *dummy;
4169 /*
4170 * "mem" cannot be under rmdir() because we've already checked
4171 * by cgroup_lock_live_cgroup() that it is not removed and we
4172 * are still under the same cgroup_mutex. So we can postpone
4173 * css_get().
4174 */
4175 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4176 goto one_by_one;
4177 if (do_swap_account && res_counter_charge(&mem->memsw,
4178 PAGE_SIZE * count, &dummy)) {
4179 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4180 goto one_by_one;
4181 }
4182 mc.precharge += count;
4183 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
4184 WARN_ON_ONCE(count > INT_MAX);
4185 __css_get(&mem->css, (int)count);
4186 return ret;
4187 }
4188one_by_one:
4189 /* fall back to one by one charge */
4190 while (count--) {
4191 if (signal_pending(current)) {
4192 ret = -EINTR;
4193 break;
4194 }
4195 if (!batch_count--) {
4196 batch_count = PRECHARGE_COUNT_AT_ONCE;
4197 cond_resched();
4198 }
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08004199 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004200 if (ret || !mem)
4201 /* mem_cgroup_clear_mc() will do uncharge later */
4202 return -ENOMEM;
4203 mc.precharge++;
4204 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004205 return ret;
4206}
4207
4208/**
4209 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4210 * @vma: the vma the pte to be checked belongs
4211 * @addr: the address corresponding to the pte to be checked
4212 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004213 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004214 *
4215 * Returns
4216 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4217 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4218 * move charge. if @target is not NULL, the page is stored in target->page
4219 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004220 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4221 * target for charge migration. if @target is not NULL, the entry is stored
4222 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004223 *
4224 * Called with pte lock held.
4225 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004226union mc_target {
4227 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004228 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004229};
4230
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004231enum mc_target_type {
4232 MC_TARGET_NONE, /* not used */
4233 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004234 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004235};
4236
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004237static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4238 unsigned long addr, pte_t ptent)
4239{
4240 struct page *page = vm_normal_page(vma, addr, ptent);
4241
4242 if (!page || !page_mapped(page))
4243 return NULL;
4244 if (PageAnon(page)) {
4245 /* we don't move shared anon */
4246 if (!move_anon() || page_mapcount(page) > 2)
4247 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004248 } else if (!move_file())
4249 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004250 return NULL;
4251 if (!get_page_unless_zero(page))
4252 return NULL;
4253
4254 return page;
4255}
4256
4257static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4258 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4259{
4260 int usage_count;
4261 struct page *page = NULL;
4262 swp_entry_t ent = pte_to_swp_entry(ptent);
4263
4264 if (!move_anon() || non_swap_entry(ent))
4265 return NULL;
4266 usage_count = mem_cgroup_count_swap_user(ent, &page);
4267 if (usage_count > 1) { /* we don't move shared anon */
4268 if (page)
4269 put_page(page);
4270 return NULL;
4271 }
4272 if (do_swap_account)
4273 entry->val = ent.val;
4274
4275 return page;
4276}
4277
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004278static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4279 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4280{
4281 struct page *page = NULL;
4282 struct inode *inode;
4283 struct address_space *mapping;
4284 pgoff_t pgoff;
4285
4286 if (!vma->vm_file) /* anonymous vma */
4287 return NULL;
4288 if (!move_file())
4289 return NULL;
4290
4291 inode = vma->vm_file->f_path.dentry->d_inode;
4292 mapping = vma->vm_file->f_mapping;
4293 if (pte_none(ptent))
4294 pgoff = linear_page_index(vma, addr);
4295 else /* pte_file(ptent) is true */
4296 pgoff = pte_to_pgoff(ptent);
4297
4298 /* page is moved even if it's not RSS of this task(page-faulted). */
4299 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4300 page = find_get_page(mapping, pgoff);
4301 } else { /* shmem/tmpfs file. we should take account of swap too. */
4302 swp_entry_t ent;
4303 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4304 if (do_swap_account)
4305 entry->val = ent.val;
4306 }
4307
4308 return page;
4309}
4310
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004311static int is_target_pte_for_mc(struct vm_area_struct *vma,
4312 unsigned long addr, pte_t ptent, union mc_target *target)
4313{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004314 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004315 struct page_cgroup *pc;
4316 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004317 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004318
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004319 if (pte_present(ptent))
4320 page = mc_handle_present_pte(vma, addr, ptent);
4321 else if (is_swap_pte(ptent))
4322 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004323 else if (pte_none(ptent) || pte_file(ptent))
4324 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004325
4326 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004327 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004328 if (page) {
4329 pc = lookup_page_cgroup(page);
4330 /*
4331 * Do only loose check w/o page_cgroup lock.
4332 * mem_cgroup_move_account() checks the pc is valid or not under
4333 * the lock.
4334 */
4335 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4336 ret = MC_TARGET_PAGE;
4337 if (target)
4338 target->page = page;
4339 }
4340 if (!ret || !target)
4341 put_page(page);
4342 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004343 /* There is a swap entry and a page doesn't exist or isn't charged */
4344 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004345 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4346 ret = MC_TARGET_SWAP;
4347 if (target)
4348 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004349 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004350 return ret;
4351}
4352
4353static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4354 unsigned long addr, unsigned long end,
4355 struct mm_walk *walk)
4356{
4357 struct vm_area_struct *vma = walk->private;
4358 pte_t *pte;
4359 spinlock_t *ptl;
4360
4361 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4362 for (; addr != end; pte++, addr += PAGE_SIZE)
4363 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4364 mc.precharge++; /* increment precharge temporarily */
4365 pte_unmap_unlock(pte - 1, ptl);
4366 cond_resched();
4367
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004368 return 0;
4369}
4370
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004371static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4372{
4373 unsigned long precharge;
4374 struct vm_area_struct *vma;
4375
4376 down_read(&mm->mmap_sem);
4377 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4378 struct mm_walk mem_cgroup_count_precharge_walk = {
4379 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4380 .mm = mm,
4381 .private = vma,
4382 };
4383 if (is_vm_hugetlb_page(vma))
4384 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004385 walk_page_range(vma->vm_start, vma->vm_end,
4386 &mem_cgroup_count_precharge_walk);
4387 }
4388 up_read(&mm->mmap_sem);
4389
4390 precharge = mc.precharge;
4391 mc.precharge = 0;
4392
4393 return precharge;
4394}
4395
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004396static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4397{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004398 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004399}
4400
4401static void mem_cgroup_clear_mc(void)
4402{
4403 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004404 if (mc.precharge) {
4405 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4406 mc.precharge = 0;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004407 memcg_oom_recover(mc.to);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004408 }
4409 /*
4410 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4411 * we must uncharge here.
4412 */
4413 if (mc.moved_charge) {
4414 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4415 mc.moved_charge = 0;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004416 memcg_oom_recover(mc.from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004417 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004418 /* we must fixup refcnts and charges */
4419 if (mc.moved_swap) {
4420 WARN_ON_ONCE(mc.moved_swap > INT_MAX);
4421 /* uncharge swap account from the old cgroup */
4422 if (!mem_cgroup_is_root(mc.from))
4423 res_counter_uncharge(&mc.from->memsw,
4424 PAGE_SIZE * mc.moved_swap);
4425 __mem_cgroup_put(mc.from, mc.moved_swap);
4426
4427 if (!mem_cgroup_is_root(mc.to)) {
4428 /*
4429 * we charged both to->res and to->memsw, so we should
4430 * uncharge to->res.
4431 */
4432 res_counter_uncharge(&mc.to->res,
4433 PAGE_SIZE * mc.moved_swap);
4434 VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags));
4435 __css_put(&mc.to->css, mc.moved_swap);
4436 }
4437 /* we've already done mem_cgroup_get(mc.to) */
4438
4439 mc.moved_swap = 0;
4440 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004441 mc.from = NULL;
4442 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004443 mc.moving_task = NULL;
4444 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004445}
4446
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004447static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4448 struct cgroup *cgroup,
4449 struct task_struct *p,
4450 bool threadgroup)
4451{
4452 int ret = 0;
4453 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4454
4455 if (mem->move_charge_at_immigrate) {
4456 struct mm_struct *mm;
4457 struct mem_cgroup *from = mem_cgroup_from_task(p);
4458
4459 VM_BUG_ON(from == mem);
4460
4461 mm = get_task_mm(p);
4462 if (!mm)
4463 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004464 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004465 if (mm->owner == p) {
4466 VM_BUG_ON(mc.from);
4467 VM_BUG_ON(mc.to);
4468 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004469 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004470 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004471 VM_BUG_ON(mc.moving_task);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004472 mc.from = from;
4473 mc.to = mem;
4474 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004475 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004476 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004477 mc.moving_task = current;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004478
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004479 ret = mem_cgroup_precharge_mc(mm);
4480 if (ret)
4481 mem_cgroup_clear_mc();
4482 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004483 mmput(mm);
4484 }
4485 return ret;
4486}
4487
4488static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4489 struct cgroup *cgroup,
4490 struct task_struct *p,
4491 bool threadgroup)
4492{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004493 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004494}
4495
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004496static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4497 unsigned long addr, unsigned long end,
4498 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004499{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004500 int ret = 0;
4501 struct vm_area_struct *vma = walk->private;
4502 pte_t *pte;
4503 spinlock_t *ptl;
4504
4505retry:
4506 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4507 for (; addr != end; addr += PAGE_SIZE) {
4508 pte_t ptent = *(pte++);
4509 union mc_target target;
4510 int type;
4511 struct page *page;
4512 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004513 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004514
4515 if (!mc.precharge)
4516 break;
4517
4518 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4519 switch (type) {
4520 case MC_TARGET_PAGE:
4521 page = target.page;
4522 if (isolate_lru_page(page))
4523 goto put;
4524 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004525 if (!mem_cgroup_move_account(pc,
4526 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004527 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004528 /* we uncharge from mc.from later. */
4529 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004530 }
4531 putback_lru_page(page);
4532put: /* is_target_pte_for_mc() gets the page */
4533 put_page(page);
4534 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004535 case MC_TARGET_SWAP:
4536 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004537 if (!mem_cgroup_move_swap_account(ent,
4538 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004539 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004540 /* we fixup refcnts and charges later. */
4541 mc.moved_swap++;
4542 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004543 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004544 default:
4545 break;
4546 }
4547 }
4548 pte_unmap_unlock(pte - 1, ptl);
4549 cond_resched();
4550
4551 if (addr != end) {
4552 /*
4553 * We have consumed all precharges we got in can_attach().
4554 * We try charge one by one, but don't do any additional
4555 * charges to mc.to if we have failed in charge once in attach()
4556 * phase.
4557 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004558 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004559 if (!ret)
4560 goto retry;
4561 }
4562
4563 return ret;
4564}
4565
4566static void mem_cgroup_move_charge(struct mm_struct *mm)
4567{
4568 struct vm_area_struct *vma;
4569
4570 lru_add_drain_all();
4571 down_read(&mm->mmap_sem);
4572 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4573 int ret;
4574 struct mm_walk mem_cgroup_move_charge_walk = {
4575 .pmd_entry = mem_cgroup_move_charge_pte_range,
4576 .mm = mm,
4577 .private = vma,
4578 };
4579 if (is_vm_hugetlb_page(vma))
4580 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004581 ret = walk_page_range(vma->vm_start, vma->vm_end,
4582 &mem_cgroup_move_charge_walk);
4583 if (ret)
4584 /*
4585 * means we have consumed all precharges and failed in
4586 * doing additional charge. Just abandon here.
4587 */
4588 break;
4589 }
4590 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004591}
4592
Balbir Singh67e465a2008-02-07 00:13:54 -08004593static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4594 struct cgroup *cont,
4595 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004596 struct task_struct *p,
4597 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004598{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004599 struct mm_struct *mm;
4600
4601 if (!mc.to)
4602 /* no need to move charge */
4603 return;
4604
4605 mm = get_task_mm(p);
4606 if (mm) {
4607 mem_cgroup_move_charge(mm);
4608 mmput(mm);
4609 }
4610 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004611}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07004612#else /* !CONFIG_MMU */
4613static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4614 struct cgroup *cgroup,
4615 struct task_struct *p,
4616 bool threadgroup)
4617{
4618 return 0;
4619}
4620static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4621 struct cgroup *cgroup,
4622 struct task_struct *p,
4623 bool threadgroup)
4624{
4625}
4626static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4627 struct cgroup *cont,
4628 struct cgroup *old_cont,
4629 struct task_struct *p,
4630 bool threadgroup)
4631{
4632}
4633#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08004634
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004635struct cgroup_subsys mem_cgroup_subsys = {
4636 .name = "memory",
4637 .subsys_id = mem_cgroup_subsys_id,
4638 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004639 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004640 .destroy = mem_cgroup_destroy,
4641 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004642 .can_attach = mem_cgroup_can_attach,
4643 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004644 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004645 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004646 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004647};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004648
4649#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4650
4651static int __init disable_swap_account(char *s)
4652{
4653 really_do_swap_account = 0;
4654 return 1;
4655}
4656__setup("noswapaccount", disable_swap_account);
4657#endif