blob: de54ea0094a1be4e512b0ac602e28d2a3ac4a695 [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};
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700161
162struct mem_cgroup_thresholds {
163 /* Primary thresholds array */
164 struct mem_cgroup_threshold_ary *primary;
165 /*
166 * Spare threshold array.
167 * This is needed to make mem_cgroup_unregister_event() "never fail".
168 * It must be able to store at least primary->size - 1 entries.
169 */
170 struct mem_cgroup_threshold_ary *spare;
171};
172
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700173/* for OOM */
174struct mem_cgroup_eventfd_list {
175 struct list_head list;
176 struct eventfd_ctx *eventfd;
177};
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800178
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800179static void mem_cgroup_threshold(struct mem_cgroup *mem);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700180static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800181
Balbir Singhf64c3f52009-09-23 15:56:37 -0700182/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800183 * The memory controller data structure. The memory controller controls both
184 * page cache and RSS per cgroup. We would eventually like to provide
185 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
186 * to help the administrator determine what knobs to tune.
187 *
188 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800189 * we hit the water mark. May be even add a low water mark, such that
190 * no reclaim occurs from a cgroup at it's low water mark, this is
191 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800192 */
193struct mem_cgroup {
194 struct cgroup_subsys_state css;
195 /*
196 * the counter to account for memory usage
197 */
198 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800199 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800200 * the counter to account for mem+swap usage.
201 */
202 struct res_counter memsw;
203 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800204 * Per cgroup active and inactive list, similar to the
205 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800206 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800207 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800208
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800209 /*
210 protect against reclaim related member.
211 */
212 spinlock_t reclaim_param_lock;
213
Balbir Singh6d61ef42009-01-07 18:08:06 -0800214 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200215 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700216 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800217 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700218 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800219 /*
220 * Should the accounting and control be hierarchical, per subtree?
221 */
222 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800223 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800224 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800225
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800226 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700227 /* OOM-Killer disable */
228 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800229
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700230 /* set when res.limit == memsw.limit */
231 bool memsw_is_minimum;
232
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800233 /* protect arrays of thresholds */
234 struct mutex thresholds_lock;
235
236 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700237 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700238
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800239 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700240 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700241
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700242 /* For oom notifier event fd */
243 struct list_head oom_notify;
244
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800245 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800246 * Should we move charges of a task when a task is moved into this
247 * mem_cgroup ? And what type of charges should we move ?
248 */
249 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800250 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800251 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800252 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800253 struct mem_cgroup_stat_cpu *stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800254};
255
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800256/* Stuffs for move charges at task migration. */
257/*
258 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
259 * left-shifted bitmap of these types.
260 */
261enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800262 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700263 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800264 NR_MOVE_TYPE,
265};
266
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800267/* "mc" and its members are protected by cgroup_mutex */
268static struct move_charge_struct {
269 struct mem_cgroup *from;
270 struct mem_cgroup *to;
271 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800272 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800273 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800274 struct task_struct *moving_task; /* a task moving charges */
275 wait_queue_head_t waitq; /* a waitq for other context */
276} mc = {
277 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
278};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800279
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700280static bool move_anon(void)
281{
282 return test_bit(MOVE_CHARGE_TYPE_ANON,
283 &mc.to->move_charge_at_immigrate);
284}
285
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700286static bool move_file(void)
287{
288 return test_bit(MOVE_CHARGE_TYPE_FILE,
289 &mc.to->move_charge_at_immigrate);
290}
291
Balbir Singh4e416952009-09-23 15:56:39 -0700292/*
293 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
294 * limit reclaim to prevent infinite loops, if they ever occur.
295 */
296#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
297#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
298
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800299enum charge_type {
300 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
301 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700302 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700303 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800304 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700305 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700306 NR_CHARGE_TYPE,
307};
308
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700309/* only for here (for easy reading.) */
310#define PCGF_CACHE (1UL << PCG_CACHE)
311#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700312#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700313/* Not used, but added here for completeness */
314#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800315
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800316/* for encoding cft->private value on file */
317#define _MEM (0)
318#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700319#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800320#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
321#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
322#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700323/* Used for OOM nofiier */
324#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800325
Balbir Singh75822b42009-09-23 15:56:38 -0700326/*
327 * Reclaim flags for mem_cgroup_hierarchical_reclaim
328 */
329#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
330#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
331#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
332#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700333#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
334#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700335
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800336static void mem_cgroup_get(struct mem_cgroup *mem);
337static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800338static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800339static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800340
Balbir Singhf64c3f52009-09-23 15:56:37 -0700341static struct mem_cgroup_per_zone *
342mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
343{
344 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
345}
346
Wu Fengguangd3242362009-12-16 12:19:59 +0100347struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
348{
349 return &mem->css;
350}
351
Balbir Singhf64c3f52009-09-23 15:56:37 -0700352static struct mem_cgroup_per_zone *
353page_cgroup_zoneinfo(struct page_cgroup *pc)
354{
355 struct mem_cgroup *mem = pc->mem_cgroup;
356 int nid = page_cgroup_nid(pc);
357 int zid = page_cgroup_zid(pc);
358
359 if (!mem)
360 return NULL;
361
362 return mem_cgroup_zoneinfo(mem, nid, zid);
363}
364
365static struct mem_cgroup_tree_per_zone *
366soft_limit_tree_node_zone(int nid, int zid)
367{
368 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
369}
370
371static struct mem_cgroup_tree_per_zone *
372soft_limit_tree_from_page(struct page *page)
373{
374 int nid = page_to_nid(page);
375 int zid = page_zonenum(page);
376
377 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
378}
379
380static void
Balbir Singh4e416952009-09-23 15:56:39 -0700381__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700382 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700383 struct mem_cgroup_tree_per_zone *mctz,
384 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700385{
386 struct rb_node **p = &mctz->rb_root.rb_node;
387 struct rb_node *parent = NULL;
388 struct mem_cgroup_per_zone *mz_node;
389
390 if (mz->on_tree)
391 return;
392
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700393 mz->usage_in_excess = new_usage_in_excess;
394 if (!mz->usage_in_excess)
395 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700396 while (*p) {
397 parent = *p;
398 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
399 tree_node);
400 if (mz->usage_in_excess < mz_node->usage_in_excess)
401 p = &(*p)->rb_left;
402 /*
403 * We can't avoid mem cgroups that are over their soft
404 * limit by the same amount
405 */
406 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
407 p = &(*p)->rb_right;
408 }
409 rb_link_node(&mz->tree_node, parent, p);
410 rb_insert_color(&mz->tree_node, &mctz->rb_root);
411 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700412}
413
414static void
415__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
416 struct mem_cgroup_per_zone *mz,
417 struct mem_cgroup_tree_per_zone *mctz)
418{
419 if (!mz->on_tree)
420 return;
421 rb_erase(&mz->tree_node, &mctz->rb_root);
422 mz->on_tree = false;
423}
424
425static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700426mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
427 struct mem_cgroup_per_zone *mz,
428 struct mem_cgroup_tree_per_zone *mctz)
429{
430 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700431 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700432 spin_unlock(&mctz->lock);
433}
434
Balbir Singhf64c3f52009-09-23 15:56:37 -0700435
436static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
437{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700438 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700439 struct mem_cgroup_per_zone *mz;
440 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700441 int nid = page_to_nid(page);
442 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700443 mctz = soft_limit_tree_from_page(page);
444
445 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700446 * Necessary to update all ancestors when hierarchy is used.
447 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700448 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700449 for (; mem; mem = parent_mem_cgroup(mem)) {
450 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700451 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700452 /*
453 * We have to update the tree if mz is on RB-tree or
454 * mem is over its softlimit.
455 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700456 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700457 spin_lock(&mctz->lock);
458 /* if on-tree, remove it */
459 if (mz->on_tree)
460 __mem_cgroup_remove_exceeded(mem, mz, mctz);
461 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700462 * Insert again. mz->usage_in_excess will be updated.
463 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700464 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700465 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700466 spin_unlock(&mctz->lock);
467 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700468 }
469}
470
471static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
472{
473 int node, zone;
474 struct mem_cgroup_per_zone *mz;
475 struct mem_cgroup_tree_per_zone *mctz;
476
477 for_each_node_state(node, N_POSSIBLE) {
478 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
479 mz = mem_cgroup_zoneinfo(mem, node, zone);
480 mctz = soft_limit_tree_node_zone(node, zone);
481 mem_cgroup_remove_exceeded(mem, mz, mctz);
482 }
483 }
484}
485
Balbir Singh4e416952009-09-23 15:56:39 -0700486static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
487{
488 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
489}
490
491static struct mem_cgroup_per_zone *
492__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
493{
494 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700495 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700496
497retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700498 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700499 rightmost = rb_last(&mctz->rb_root);
500 if (!rightmost)
501 goto done; /* Nothing to reclaim from */
502
503 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
504 /*
505 * Remove the node now but someone else can add it back,
506 * we will to add it back at the end of reclaim to its correct
507 * position in the tree.
508 */
509 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
510 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
511 !css_tryget(&mz->mem->css))
512 goto retry;
513done:
514 return mz;
515}
516
517static struct mem_cgroup_per_zone *
518mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
519{
520 struct mem_cgroup_per_zone *mz;
521
522 spin_lock(&mctz->lock);
523 mz = __mem_cgroup_largest_soft_limit_node(mctz);
524 spin_unlock(&mctz->lock);
525 return mz;
526}
527
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800528static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
529 enum mem_cgroup_stat_index idx)
530{
531 int cpu;
532 s64 val = 0;
533
534 for_each_possible_cpu(cpu)
535 val += per_cpu(mem->stat->count[idx], cpu);
536 return val;
537}
538
539static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
540{
541 s64 ret;
542
543 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
544 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
545 return ret;
546}
547
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700548static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
549 bool charge)
550{
551 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800552 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700553}
554
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700555static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
556 struct page_cgroup *pc,
557 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800558{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700559 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800560
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800561 preempt_disable();
562
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700563 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800564 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800565 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800566 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700567
568 if (charge)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800569 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
Balaji Rao55e462b2008-05-01 04:35:12 -0700570 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800571 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800572 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800573
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800574 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800575}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800576
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700577static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700578 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800579{
580 int nid, zid;
581 struct mem_cgroup_per_zone *mz;
582 u64 total = 0;
583
584 for_each_online_node(nid)
585 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
586 mz = mem_cgroup_zoneinfo(mem, nid, zid);
587 total += MEM_CGROUP_ZSTAT(mz, idx);
588 }
589 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800590}
591
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800592static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
593{
594 s64 val;
595
596 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
597
598 return !(val & ((1 << event_mask_shift) - 1));
599}
600
601/*
602 * Check events in order.
603 *
604 */
605static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
606{
607 /* threshold event is triggered in finer grain than soft limit */
608 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
609 mem_cgroup_threshold(mem);
610 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
611 mem_cgroup_update_tree(mem, page);
612 }
613}
614
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800615static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800616{
617 return container_of(cgroup_subsys_state(cont,
618 mem_cgroup_subsys_id), struct mem_cgroup,
619 css);
620}
621
Balbir Singhcf475ad2008-04-29 01:00:16 -0700622struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800623{
Balbir Singh31a78f22008-09-28 23:09:31 +0100624 /*
625 * mm_update_next_owner() may clear mm->owner to NULL
626 * if it races with swapoff, page migration, etc.
627 * So this can be called with p == NULL.
628 */
629 if (unlikely(!p))
630 return NULL;
631
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800632 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
633 struct mem_cgroup, css);
634}
635
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800636static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
637{
638 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700639
640 if (!mm)
641 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800642 /*
643 * Because we have no locks, mm->owner's may be being moved to other
644 * cgroup. We use css_tryget() here even if this looks
645 * pessimistic (rather than adding locks here).
646 */
647 rcu_read_lock();
648 do {
649 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
650 if (unlikely(!mem))
651 break;
652 } while (!css_tryget(&mem->css));
653 rcu_read_unlock();
654 return mem;
655}
656
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700657/*
658 * Call callback function against all cgroup under hierarchy tree.
659 */
660static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
661 int (*func)(struct mem_cgroup *, void *))
662{
663 int found, ret, nextid;
664 struct cgroup_subsys_state *css;
665 struct mem_cgroup *mem;
666
667 if (!root->use_hierarchy)
668 return (*func)(root, data);
669
670 nextid = 1;
671 do {
672 ret = 0;
673 mem = NULL;
674
675 rcu_read_lock();
676 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
677 &found);
678 if (css && css_tryget(css))
679 mem = container_of(css, struct mem_cgroup, css);
680 rcu_read_unlock();
681
682 if (mem) {
683 ret = (*func)(mem, data);
684 css_put(&mem->css);
685 }
686 nextid = found + 1;
687 } while (!ret && css);
688
689 return ret;
690}
691
Balbir Singh4b3bde42009-09-23 15:56:32 -0700692static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
693{
694 return (mem == root_mem_cgroup);
695}
696
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800697/*
698 * Following LRU functions are allowed to be used without PCG_LOCK.
699 * Operations are called by routine of global LRU independently from memcg.
700 * What we have to take care of here is validness of pc->mem_cgroup.
701 *
702 * Changes to pc->mem_cgroup happens when
703 * 1. charge
704 * 2. moving account
705 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
706 * It is added to LRU before charge.
707 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
708 * When moving account, the page is not on LRU. It's isolated.
709 */
710
711void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800712{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800713 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800714 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700715
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800716 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800717 return;
718 pc = lookup_page_cgroup(page);
719 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700720 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800721 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700722 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800723 /*
724 * We don't check PCG_USED bit. It's cleared when the "page" is finally
725 * removed from global LRU.
726 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800727 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700728 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700729 if (mem_cgroup_is_root(pc->mem_cgroup))
730 return;
731 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800732 list_del_init(&pc->lru);
733 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800734}
735
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800736void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800737{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800738 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800739}
740
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800741void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800742{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800743 struct mem_cgroup_per_zone *mz;
744 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800745
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800746 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700747 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700748
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800749 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800750 /*
751 * Used bit is set without atomic ops but after smp_wmb().
752 * For making pc->mem_cgroup visible, insert smp_rmb() here.
753 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800754 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700755 /* unused or root page is not rotated. */
756 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800757 return;
758 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700759 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800760}
761
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800762void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
763{
764 struct page_cgroup *pc;
765 struct mem_cgroup_per_zone *mz;
766
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800767 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800768 return;
769 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700770 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800771 /*
772 * Used bit is set without atomic ops but after smp_wmb().
773 * For making pc->mem_cgroup visible, insert smp_rmb() here.
774 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800775 smp_rmb();
776 if (!PageCgroupUsed(pc))
777 return;
778
779 mz = page_cgroup_zoneinfo(pc);
780 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700781 SetPageCgroupAcctLRU(pc);
782 if (mem_cgroup_is_root(pc->mem_cgroup))
783 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800784 list_add(&pc->lru, &mz->lists[lru]);
785}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800786
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800787/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800788 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
789 * lru because the page may.be reused after it's fully uncharged (because of
790 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
791 * it again. This function is only used to charge SwapCache. It's done under
792 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800793 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800794static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800795{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800796 unsigned long flags;
797 struct zone *zone = page_zone(page);
798 struct page_cgroup *pc = lookup_page_cgroup(page);
799
800 spin_lock_irqsave(&zone->lru_lock, flags);
801 /*
802 * Forget old LRU when this page_cgroup is *not* used. This Used bit
803 * is guarded by lock_page() because the page is SwapCache.
804 */
805 if (!PageCgroupUsed(pc))
806 mem_cgroup_del_lru_list(page, page_lru(page));
807 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800808}
809
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800810static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
811{
812 unsigned long flags;
813 struct zone *zone = page_zone(page);
814 struct page_cgroup *pc = lookup_page_cgroup(page);
815
816 spin_lock_irqsave(&zone->lru_lock, flags);
817 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700818 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800819 mem_cgroup_add_lru_list(page, page_lru(page));
820 spin_unlock_irqrestore(&zone->lru_lock, flags);
821}
822
823
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800824void mem_cgroup_move_lists(struct page *page,
825 enum lru_list from, enum lru_list to)
826{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800827 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800828 return;
829 mem_cgroup_del_lru_list(page, from);
830 mem_cgroup_add_lru_list(page, to);
831}
832
David Rientjes4c4a2212008-02-07 00:14:06 -0800833int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
834{
835 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700836 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800837
838 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700839 rcu_read_lock();
840 curr = try_get_mem_cgroup_from_mm(task->mm);
841 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800842 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700843 if (!curr)
844 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800845 /*
846 * We should check use_hierarchy of "mem" not "curr". Because checking
847 * use_hierarchy of "curr" here make this function true if hierarchy is
848 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
849 * hierarchy(even if use_hierarchy is disabled in "mem").
850 */
851 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700852 ret = css_is_ancestor(&curr->css, &mem->css);
853 else
854 ret = (curr == mem);
855 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800856 return ret;
857}
858
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800859static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800860{
861 unsigned long active;
862 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800863 unsigned long gb;
864 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800865
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700866 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
867 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800868
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800869 gb = (inactive + active) >> (30 - PAGE_SHIFT);
870 if (gb)
871 inactive_ratio = int_sqrt(10 * gb);
872 else
873 inactive_ratio = 1;
874
875 if (present_pages) {
876 present_pages[0] = inactive;
877 present_pages[1] = active;
878 }
879
880 return inactive_ratio;
881}
882
883int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
884{
885 unsigned long active;
886 unsigned long inactive;
887 unsigned long present_pages[2];
888 unsigned long inactive_ratio;
889
890 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
891
892 inactive = present_pages[0];
893 active = present_pages[1];
894
895 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800896 return 1;
897
898 return 0;
899}
900
Rik van Riel56e49d22009-06-16 15:32:28 -0700901int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
902{
903 unsigned long active;
904 unsigned long inactive;
905
906 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
907 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
908
909 return (active > inactive);
910}
911
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800912unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
913 struct zone *zone,
914 enum lru_list lru)
915{
916 int nid = zone->zone_pgdat->node_id;
917 int zid = zone_idx(zone);
918 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
919
920 return MEM_CGROUP_ZSTAT(mz, lru);
921}
922
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800923struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
924 struct zone *zone)
925{
926 int nid = zone->zone_pgdat->node_id;
927 int zid = zone_idx(zone);
928 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
929
930 return &mz->reclaim_stat;
931}
932
933struct zone_reclaim_stat *
934mem_cgroup_get_reclaim_stat_from_page(struct page *page)
935{
936 struct page_cgroup *pc;
937 struct mem_cgroup_per_zone *mz;
938
939 if (mem_cgroup_disabled())
940 return NULL;
941
942 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800943 /*
944 * Used bit is set without atomic ops but after smp_wmb().
945 * For making pc->mem_cgroup visible, insert smp_rmb() here.
946 */
947 smp_rmb();
948 if (!PageCgroupUsed(pc))
949 return NULL;
950
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800951 mz = page_cgroup_zoneinfo(pc);
952 if (!mz)
953 return NULL;
954
955 return &mz->reclaim_stat;
956}
957
Balbir Singh66e17072008-02-07 00:13:56 -0800958unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
959 struct list_head *dst,
960 unsigned long *scanned, int order,
961 int mode, struct zone *z,
962 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700963 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800964{
965 unsigned long nr_taken = 0;
966 struct page *page;
967 unsigned long scan;
968 LIST_HEAD(pc_list);
969 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800970 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800971 int nid = z->zone_pgdat->node_id;
972 int zid = zone_idx(z);
973 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700974 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700975 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800976
Balbir Singhcf475ad2008-04-29 01:00:16 -0700977 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800978 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700979 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800980
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800981 scan = 0;
982 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800983 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800984 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800985
986 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700987 if (unlikely(!PageCgroupUsed(pc)))
988 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800989 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800990 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800991
Hugh Dickins436c65412008-02-07 00:14:12 -0800992 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700993 ret = __isolate_lru_page(page, mode, file);
994 switch (ret) {
995 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800996 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700997 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800998 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700999 break;
1000 case -EBUSY:
1001 /* we don't affect global LRU but rotate in our LRU */
1002 mem_cgroup_rotate_lru_list(page, page_lru(page));
1003 break;
1004 default:
1005 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001006 }
1007 }
1008
Balbir Singh66e17072008-02-07 00:13:56 -08001009 *scanned = scan;
1010 return nr_taken;
1011}
1012
Balbir Singh6d61ef42009-01-07 18:08:06 -08001013#define mem_cgroup_from_res_counter(counter, member) \
1014 container_of(counter, struct mem_cgroup, member)
1015
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001016static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1017{
1018 if (do_swap_account) {
1019 if (res_counter_check_under_limit(&mem->res) &&
1020 res_counter_check_under_limit(&mem->memsw))
1021 return true;
1022 } else
1023 if (res_counter_check_under_limit(&mem->res))
1024 return true;
1025 return false;
1026}
1027
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001028static unsigned int get_swappiness(struct mem_cgroup *memcg)
1029{
1030 struct cgroup *cgrp = memcg->css.cgroup;
1031 unsigned int swappiness;
1032
1033 /* root ? */
1034 if (cgrp->parent == NULL)
1035 return vm_swappiness;
1036
1037 spin_lock(&memcg->reclaim_param_lock);
1038 swappiness = memcg->swappiness;
1039 spin_unlock(&memcg->reclaim_param_lock);
1040
1041 return swappiness;
1042}
1043
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001044static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1045{
1046 int *val = data;
1047 (*val)++;
1048 return 0;
1049}
Balbir Singhe2224322009-04-02 16:57:39 -07001050
1051/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001052 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001053 * @memcg: The memory cgroup that went over limit
1054 * @p: Task that is going to be killed
1055 *
1056 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1057 * enabled
1058 */
1059void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1060{
1061 struct cgroup *task_cgrp;
1062 struct cgroup *mem_cgrp;
1063 /*
1064 * Need a buffer in BSS, can't rely on allocations. The code relies
1065 * on the assumption that OOM is serialized for memory controller.
1066 * If this assumption is broken, revisit this code.
1067 */
1068 static char memcg_name[PATH_MAX];
1069 int ret;
1070
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001071 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001072 return;
1073
1074
1075 rcu_read_lock();
1076
1077 mem_cgrp = memcg->css.cgroup;
1078 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1079
1080 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1081 if (ret < 0) {
1082 /*
1083 * Unfortunately, we are unable to convert to a useful name
1084 * But we'll still print out the usage information
1085 */
1086 rcu_read_unlock();
1087 goto done;
1088 }
1089 rcu_read_unlock();
1090
1091 printk(KERN_INFO "Task in %s killed", memcg_name);
1092
1093 rcu_read_lock();
1094 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1095 if (ret < 0) {
1096 rcu_read_unlock();
1097 goto done;
1098 }
1099 rcu_read_unlock();
1100
1101 /*
1102 * Continues from above, so we don't need an KERN_ level
1103 */
1104 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1105done:
1106
1107 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1108 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1109 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1110 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1111 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1112 "failcnt %llu\n",
1113 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1114 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1115 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1116}
1117
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001118/*
1119 * This function returns the number of memcg under hierarchy tree. Returns
1120 * 1(self count) if no children.
1121 */
1122static int mem_cgroup_count_children(struct mem_cgroup *mem)
1123{
1124 int num = 0;
1125 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1126 return num;
1127}
1128
Balbir Singh6d61ef42009-01-07 18:08:06 -08001129/*
David Rientjesa63d83f2010-08-09 17:19:46 -07001130 * Return the memory (and swap, if configured) limit for a memcg.
1131 */
1132u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1133{
1134 u64 limit;
1135 u64 memsw;
1136
1137 limit = res_counter_read_u64(&memcg->res, RES_LIMIT) +
1138 total_swap_pages;
1139 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1140 /*
1141 * If memsw is finite and limits the amount of swap space available
1142 * to this memcg, return that limit.
1143 */
1144 return min(limit, memsw);
1145}
1146
1147/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001148 * Visit the first child (need not be the first child as per the ordering
1149 * of the cgroup list, since we track last_scanned_child) of @mem and use
1150 * that to reclaim free pages from.
1151 */
1152static struct mem_cgroup *
1153mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1154{
1155 struct mem_cgroup *ret = NULL;
1156 struct cgroup_subsys_state *css;
1157 int nextid, found;
1158
1159 if (!root_mem->use_hierarchy) {
1160 css_get(&root_mem->css);
1161 ret = root_mem;
1162 }
1163
1164 while (!ret) {
1165 rcu_read_lock();
1166 nextid = root_mem->last_scanned_child + 1;
1167 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1168 &found);
1169 if (css && css_tryget(css))
1170 ret = container_of(css, struct mem_cgroup, css);
1171
1172 rcu_read_unlock();
1173 /* Updates scanning parameter */
1174 spin_lock(&root_mem->reclaim_param_lock);
1175 if (!css) {
1176 /* this means start scan from ID:1 */
1177 root_mem->last_scanned_child = 0;
1178 } else
1179 root_mem->last_scanned_child = found;
1180 spin_unlock(&root_mem->reclaim_param_lock);
1181 }
1182
1183 return ret;
1184}
1185
1186/*
1187 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1188 * we reclaimed from, so that we don't end up penalizing one child extensively
1189 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001190 *
1191 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001192 *
1193 * We give up and return to the caller when we visit root_mem twice.
1194 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001195 *
1196 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001197 */
1198static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001199 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001200 gfp_t gfp_mask,
1201 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001202{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001203 struct mem_cgroup *victim;
1204 int ret, total = 0;
1205 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001206 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1207 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001208 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1209 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001210
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001211 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1212 if (root_mem->memsw_is_minimum)
1213 noswap = true;
1214
Balbir Singh4e416952009-09-23 15:56:39 -07001215 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001216 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001217 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001218 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001219 if (loop >= 1)
1220 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001221 if (loop >= 2) {
1222 /*
1223 * If we have not been able to reclaim
1224 * anything, it might because there are
1225 * no reclaimable pages under this hierarchy
1226 */
1227 if (!check_soft || !total) {
1228 css_put(&victim->css);
1229 break;
1230 }
1231 /*
1232 * We want to do more targetted reclaim.
1233 * excess >> 2 is not to excessive so as to
1234 * reclaim too much, nor too less that we keep
1235 * coming back to reclaim from this cgroup
1236 */
1237 if (total >= (excess >> 2) ||
1238 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1239 css_put(&victim->css);
1240 break;
1241 }
1242 }
1243 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001244 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001245 /* this cgroup's local usage == 0 */
1246 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001247 continue;
1248 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001249 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001250 if (check_soft)
1251 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1252 noswap, get_swappiness(victim), zone,
1253 zone->zone_pgdat->node_id);
1254 else
1255 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1256 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001257 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001258 /*
1259 * At shrinking usage, we can't check we should stop here or
1260 * reclaim more. It's depends on callers. last_scanned_child
1261 * will work enough for keeping fairness under tree.
1262 */
1263 if (shrink)
1264 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001265 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001266 if (check_soft) {
1267 if (res_counter_check_under_soft_limit(&root_mem->res))
1268 return total;
1269 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001270 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001271 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001272 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001273}
1274
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001275static int mem_cgroup_oom_lock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001276{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001277 int *val = (int *)data;
1278 int x;
1279 /*
1280 * Logically, we can stop scanning immediately when we find
1281 * a memcg is already locked. But condidering unlock ops and
1282 * creation/removal of memcg, scan-all is simple operation.
1283 */
1284 x = atomic_inc_return(&mem->oom_lock);
1285 *val = max(x, *val);
1286 return 0;
1287}
1288/*
1289 * Check OOM-Killer is already running under our hierarchy.
1290 * If someone is running, return false.
1291 */
1292static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1293{
1294 int lock_count = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001295
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001296 mem_cgroup_walk_tree(mem, &lock_count, mem_cgroup_oom_lock_cb);
1297
1298 if (lock_count == 1)
1299 return true;
1300 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001301}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001302
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001303static int mem_cgroup_oom_unlock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001304{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001305 /*
1306 * When a new child is created while the hierarchy is under oom,
1307 * mem_cgroup_oom_lock() may not be called. We have to use
1308 * atomic_add_unless() here.
1309 */
1310 atomic_add_unless(&mem->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001311 return 0;
1312}
1313
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001314static void mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001315{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001316 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_unlock_cb);
1317}
1318
1319static DEFINE_MUTEX(memcg_oom_mutex);
1320static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1321
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001322struct oom_wait_info {
1323 struct mem_cgroup *mem;
1324 wait_queue_t wait;
1325};
1326
1327static int memcg_oom_wake_function(wait_queue_t *wait,
1328 unsigned mode, int sync, void *arg)
1329{
1330 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1331 struct oom_wait_info *oom_wait_info;
1332
1333 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1334
1335 if (oom_wait_info->mem == wake_mem)
1336 goto wakeup;
1337 /* if no hierarchy, no match */
1338 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1339 return 0;
1340 /*
1341 * Both of oom_wait_info->mem and wake_mem are stable under us.
1342 * Then we can use css_is_ancestor without taking care of RCU.
1343 */
1344 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1345 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1346 return 0;
1347
1348wakeup:
1349 return autoremove_wake_function(wait, mode, sync, arg);
1350}
1351
1352static void memcg_wakeup_oom(struct mem_cgroup *mem)
1353{
1354 /* for filtering, pass "mem" as argument. */
1355 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1356}
1357
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001358static void memcg_oom_recover(struct mem_cgroup *mem)
1359{
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07001360 if (atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001361 memcg_wakeup_oom(mem);
1362}
1363
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001364/*
1365 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1366 */
1367bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1368{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001369 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001370 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001371
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001372 owait.mem = mem;
1373 owait.wait.flags = 0;
1374 owait.wait.func = memcg_oom_wake_function;
1375 owait.wait.private = current;
1376 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001377 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001378 /* At first, try to OOM lock hierarchy under mem.*/
1379 mutex_lock(&memcg_oom_mutex);
1380 locked = mem_cgroup_oom_lock(mem);
1381 /*
1382 * Even if signal_pending(), we can't quit charge() loop without
1383 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1384 * under OOM is always welcomed, use TASK_KILLABLE here.
1385 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001386 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1387 if (!locked || mem->oom_kill_disable)
1388 need_to_kill = false;
1389 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001390 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001391 mutex_unlock(&memcg_oom_mutex);
1392
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001393 if (need_to_kill) {
1394 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001395 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001396 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001397 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001398 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001399 }
1400 mutex_lock(&memcg_oom_mutex);
1401 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001402 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001403 mutex_unlock(&memcg_oom_mutex);
1404
1405 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1406 return false;
1407 /* Give chance to dying process */
1408 schedule_timeout(1);
1409 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001410}
1411
Balbir Singhd69b0422009-06-17 16:26:34 -07001412/*
1413 * Currently used to update mapped file statistics, but the routine can be
1414 * generalized to update other statistics as well.
1415 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001416void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001417{
1418 struct mem_cgroup *mem;
Balbir Singhd69b0422009-06-17 16:26:34 -07001419 struct page_cgroup *pc;
1420
Balbir Singhd69b0422009-06-17 16:26:34 -07001421 pc = lookup_page_cgroup(page);
1422 if (unlikely(!pc))
1423 return;
1424
1425 lock_page_cgroup(pc);
1426 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001427 if (!mem || !PageCgroupUsed(pc))
Balbir Singhd69b0422009-06-17 16:26:34 -07001428 goto done;
1429
1430 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001431 * Preemption is already disabled. We can use __this_cpu_xxx
Balbir Singhd69b0422009-06-17 16:26:34 -07001432 */
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001433 if (val > 0) {
1434 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1435 SetPageCgroupFileMapped(pc);
1436 } else {
1437 __this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1438 ClearPageCgroupFileMapped(pc);
1439 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001440
Balbir Singhd69b0422009-06-17 16:26:34 -07001441done:
1442 unlock_page_cgroup(pc);
1443}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001444
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001445/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001446 * size of first charge trial. "32" comes from vmscan.c's magic value.
1447 * TODO: maybe necessary to use big numbers in big irons.
1448 */
1449#define CHARGE_SIZE (32 * PAGE_SIZE)
1450struct memcg_stock_pcp {
1451 struct mem_cgroup *cached; /* this never be root cgroup */
1452 int charge;
1453 struct work_struct work;
1454};
1455static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1456static atomic_t memcg_drain_count;
1457
1458/*
1459 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1460 * from local stock and true is returned. If the stock is 0 or charges from a
1461 * cgroup which is not current target, returns false. This stock will be
1462 * refilled.
1463 */
1464static bool consume_stock(struct mem_cgroup *mem)
1465{
1466 struct memcg_stock_pcp *stock;
1467 bool ret = true;
1468
1469 stock = &get_cpu_var(memcg_stock);
1470 if (mem == stock->cached && stock->charge)
1471 stock->charge -= PAGE_SIZE;
1472 else /* need to call res_counter_charge */
1473 ret = false;
1474 put_cpu_var(memcg_stock);
1475 return ret;
1476}
1477
1478/*
1479 * Returns stocks cached in percpu to res_counter and reset cached information.
1480 */
1481static void drain_stock(struct memcg_stock_pcp *stock)
1482{
1483 struct mem_cgroup *old = stock->cached;
1484
1485 if (stock->charge) {
1486 res_counter_uncharge(&old->res, stock->charge);
1487 if (do_swap_account)
1488 res_counter_uncharge(&old->memsw, stock->charge);
1489 }
1490 stock->cached = NULL;
1491 stock->charge = 0;
1492}
1493
1494/*
1495 * This must be called under preempt disabled or must be called by
1496 * a thread which is pinned to local cpu.
1497 */
1498static void drain_local_stock(struct work_struct *dummy)
1499{
1500 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1501 drain_stock(stock);
1502}
1503
1504/*
1505 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001506 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001507 */
1508static void refill_stock(struct mem_cgroup *mem, int val)
1509{
1510 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1511
1512 if (stock->cached != mem) { /* reset if necessary */
1513 drain_stock(stock);
1514 stock->cached = mem;
1515 }
1516 stock->charge += val;
1517 put_cpu_var(memcg_stock);
1518}
1519
1520/*
1521 * Tries to drain stocked charges in other cpus. This function is asynchronous
1522 * and just put a work per cpu for draining localy on each cpu. Caller can
1523 * expects some charges will be back to res_counter later but cannot wait for
1524 * it.
1525 */
1526static void drain_all_stock_async(void)
1527{
1528 int cpu;
1529 /* This function is for scheduling "drain" in asynchronous way.
1530 * The result of "drain" is not directly handled by callers. Then,
1531 * if someone is calling drain, we don't have to call drain more.
1532 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1533 * there is a race. We just do loose check here.
1534 */
1535 if (atomic_read(&memcg_drain_count))
1536 return;
1537 /* Notify other cpus that system-wide "drain" is running */
1538 atomic_inc(&memcg_drain_count);
1539 get_online_cpus();
1540 for_each_online_cpu(cpu) {
1541 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1542 schedule_work_on(cpu, &stock->work);
1543 }
1544 put_online_cpus();
1545 atomic_dec(&memcg_drain_count);
1546 /* We don't wait for flush_work */
1547}
1548
1549/* This is a synchronous drain interface. */
1550static void drain_all_stock_sync(void)
1551{
1552 /* called when force_empty is called */
1553 atomic_inc(&memcg_drain_count);
1554 schedule_on_each_cpu(drain_local_stock);
1555 atomic_dec(&memcg_drain_count);
1556}
1557
1558static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1559 unsigned long action,
1560 void *hcpu)
1561{
1562 int cpu = (unsigned long)hcpu;
1563 struct memcg_stock_pcp *stock;
1564
1565 if (action != CPU_DEAD)
1566 return NOTIFY_OK;
1567 stock = &per_cpu(memcg_stock, cpu);
1568 drain_stock(stock);
1569 return NOTIFY_OK;
1570}
1571
1572/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001573 * Unlike exported interface, "oom" parameter is added. if oom==true,
1574 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001575 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001576static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001577 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001578{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001579 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001580 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001581 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001582 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001583
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001584 /*
1585 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1586 * in system level. So, allow to go ahead dying process in addition to
1587 * MEMDIE process.
1588 */
1589 if (unlikely(test_thread_flag(TIF_MEMDIE)
1590 || fatal_signal_pending(current)))
1591 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001592
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001593 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001594 * We always charge the cgroup the mm_struct belongs to.
1595 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001596 * thread group leader migrates. It's possible that mm is not
1597 * set, if so charge the init_mm (happens for pagecache usage).
1598 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001599 mem = *memcg;
1600 if (likely(!mem)) {
1601 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001602 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001603 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001604 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001605 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001606 if (unlikely(!mem))
1607 return 0;
1608
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001609 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001610 if (mem_cgroup_is_root(mem))
1611 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001612
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001613 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001614 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001615 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001616
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001617 if (consume_stock(mem))
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001618 goto done;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001619
1620 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001621 if (likely(!ret)) {
1622 if (!do_swap_account)
1623 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001624 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001625 if (likely(!ret))
1626 break;
1627 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001628 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001629 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001630 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1631 memsw);
1632 } else
1633 /* mem counter fails */
1634 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1635 res);
1636
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001637 /* reduce request size and retry */
1638 if (csize > PAGE_SIZE) {
1639 csize = PAGE_SIZE;
1640 continue;
1641 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001642 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001643 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001644
Balbir Singh4e416952009-09-23 15:56:39 -07001645 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1646 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001647 if (ret)
1648 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001649
1650 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001651 * try_to_free_mem_cgroup_pages() might not give us a full
1652 * picture of reclaim. Some pages are reclaimed and might be
1653 * moved to swap cache or just unmapped from the cgroup.
1654 * Check the limit again to see if the reclaim reduced the
1655 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001656 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001657 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001658 if (mem_cgroup_check_under_limit(mem_over_limit))
1659 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001660
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001661 /* try to avoid oom while someone is moving charge */
1662 if (mc.moving_task && current != mc.moving_task) {
1663 struct mem_cgroup *from, *to;
1664 bool do_continue = false;
1665 /*
1666 * There is a small race that "from" or "to" can be
1667 * freed by rmdir, so we use css_tryget().
1668 */
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001669 from = mc.from;
1670 to = mc.to;
1671 if (from && css_tryget(&from->css)) {
1672 if (mem_over_limit->use_hierarchy)
1673 do_continue = css_is_ancestor(
1674 &from->css,
1675 &mem_over_limit->css);
1676 else
1677 do_continue = (from == mem_over_limit);
1678 css_put(&from->css);
1679 }
1680 if (!do_continue && to && css_tryget(&to->css)) {
1681 if (mem_over_limit->use_hierarchy)
1682 do_continue = css_is_ancestor(
1683 &to->css,
1684 &mem_over_limit->css);
1685 else
1686 do_continue = (to == mem_over_limit);
1687 css_put(&to->css);
1688 }
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001689 if (do_continue) {
1690 DEFINE_WAIT(wait);
1691 prepare_to_wait(&mc.waitq, &wait,
1692 TASK_INTERRUPTIBLE);
1693 /* moving charge context might have finished. */
1694 if (mc.moving_task)
1695 schedule();
1696 finish_wait(&mc.waitq, &wait);
1697 continue;
1698 }
1699 }
1700
Hugh Dickins3be91272008-02-07 00:14:19 -08001701 if (!nr_retries--) {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001702 if (!oom)
1703 goto nomem;
1704 if (mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) {
1705 nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1706 continue;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001707 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001708 /* When we reach here, current task is dying .*/
1709 css_put(&mem->css);
1710 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08001711 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001712 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001713 if (csize > PAGE_SIZE)
1714 refill_stock(mem, csize - PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001715done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001716 return 0;
1717nomem:
1718 css_put(&mem->css);
1719 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001720bypass:
1721 *memcg = NULL;
1722 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001723}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001724
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001725/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001726 * Somemtimes we have to undo a charge we got by try_charge().
1727 * This function is for that and do uncharge, put css's refcnt.
1728 * gotten by try_charge().
1729 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001730static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1731 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001732{
1733 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001734 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001735 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001736 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1737 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
1738 WARN_ON_ONCE(count > INT_MAX);
1739 __css_put(&mem->css, (int)count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001740 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001741 /* we don't need css_put for root */
1742}
1743
1744static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1745{
1746 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001747}
1748
1749/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001750 * A helper function to get mem_cgroup from ID. must be called under
1751 * rcu_read_lock(). The caller must check css_is_removed() or some if
1752 * it's concern. (dropping refcnt from swap can be called against removed
1753 * memcg.)
1754 */
1755static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1756{
1757 struct cgroup_subsys_state *css;
1758
1759 /* ID 0 is unused ID */
1760 if (!id)
1761 return NULL;
1762 css = css_lookup(&mem_cgroup_subsys, id);
1763 if (!css)
1764 return NULL;
1765 return container_of(css, struct mem_cgroup, css);
1766}
1767
Wu Fengguange42d9d52009-12-16 12:19:59 +01001768struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001769{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001770 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001771 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001772 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001773 swp_entry_t ent;
1774
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001775 VM_BUG_ON(!PageLocked(page));
1776
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001777 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001778 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001779 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001780 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001781 if (mem && !css_tryget(&mem->css))
1782 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001783 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001784 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001785 id = lookup_swap_cgroup(ent);
1786 rcu_read_lock();
1787 mem = mem_cgroup_lookup(id);
1788 if (mem && !css_tryget(&mem->css))
1789 mem = NULL;
1790 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001791 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001792 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001793 return mem;
1794}
1795
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001796/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001797 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001798 * USED state. If already USED, uncharge and return.
1799 */
1800
1801static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1802 struct page_cgroup *pc,
1803 enum charge_type ctype)
1804{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001805 /* try_charge() can return NULL to *memcg, taking care of it. */
1806 if (!mem)
1807 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001808
1809 lock_page_cgroup(pc);
1810 if (unlikely(PageCgroupUsed(pc))) {
1811 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001812 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001813 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001814 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001815
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001816 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001817 /*
1818 * We access a page_cgroup asynchronously without lock_page_cgroup().
1819 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1820 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1821 * before USED bit, we need memory barrier here.
1822 * See mem_cgroup_add_lru_list(), etc.
1823 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001824 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001825 switch (ctype) {
1826 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1827 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1828 SetPageCgroupCache(pc);
1829 SetPageCgroupUsed(pc);
1830 break;
1831 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1832 ClearPageCgroupCache(pc);
1833 SetPageCgroupUsed(pc);
1834 break;
1835 default:
1836 break;
1837 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001838
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001839 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001840
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001841 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001842 /*
1843 * "charge_statistics" updated event counter. Then, check it.
1844 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1845 * if they exceeds softlimit.
1846 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001847 memcg_check_events(mem, pc->page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001848}
1849
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001850/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001851 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001852 * @pc: page_cgroup of the page.
1853 * @from: mem_cgroup which the page is moved from.
1854 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001855 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001856 *
1857 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001858 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001859 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001860 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001861 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1862 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1863 * true, this function does "uncharge" from old cgroup, but it doesn't if
1864 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001865 */
1866
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001867static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001868 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001869{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001870 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001871 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001872 VM_BUG_ON(!PageCgroupLocked(pc));
1873 VM_BUG_ON(!PageCgroupUsed(pc));
1874 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001875
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001876 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001877 /* Update mapped_file data for mem_cgroup */
1878 preempt_disable();
1879 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1880 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1881 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07001882 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001883 mem_cgroup_charge_statistics(from, pc, false);
1884 if (uncharge)
1885 /* This is not "cancel", but cancel_charge does all we need. */
1886 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07001887
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001888 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001889 pc->mem_cgroup = to;
1890 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001891 /*
1892 * We charges against "to" which may not have any tasks. Then, "to"
1893 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001894 * this function is just force_empty() and move charge, so it's
1895 * garanteed that "to" is never removed. So, we don't check rmdir
1896 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001897 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001898}
1899
1900/*
1901 * check whether the @pc is valid for moving account and call
1902 * __mem_cgroup_move_account()
1903 */
1904static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001905 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001906{
1907 int ret = -EINVAL;
1908 lock_page_cgroup(pc);
1909 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001910 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001911 ret = 0;
1912 }
1913 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001914 /*
1915 * check events
1916 */
1917 memcg_check_events(to, pc->page);
1918 memcg_check_events(from, pc->page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001919 return ret;
1920}
1921
1922/*
1923 * move charges to its parent.
1924 */
1925
1926static int mem_cgroup_move_parent(struct page_cgroup *pc,
1927 struct mem_cgroup *child,
1928 gfp_t gfp_mask)
1929{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001930 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001931 struct cgroup *cg = child->css.cgroup;
1932 struct cgroup *pcg = cg->parent;
1933 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001934 int ret;
1935
1936 /* Is ROOT ? */
1937 if (!pcg)
1938 return -EINVAL;
1939
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001940 ret = -EBUSY;
1941 if (!get_page_unless_zero(page))
1942 goto out;
1943 if (isolate_lru_page(page))
1944 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001945
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001946 parent = mem_cgroup_from_cont(pcg);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001947 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001948 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001949 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001950
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001951 ret = mem_cgroup_move_account(pc, child, parent, true);
1952 if (ret)
1953 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001954put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001955 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001956put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001957 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001958out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001959 return ret;
1960}
1961
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001962/*
1963 * Charge the memory controller for page usage.
1964 * Return
1965 * 0 if the charge was successful
1966 * < 0 if the cgroup is over its limit
1967 */
1968static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1969 gfp_t gfp_mask, enum charge_type ctype,
1970 struct mem_cgroup *memcg)
1971{
1972 struct mem_cgroup *mem;
1973 struct page_cgroup *pc;
1974 int ret;
1975
1976 pc = lookup_page_cgroup(page);
1977 /* can happen at boot */
1978 if (unlikely(!pc))
1979 return 0;
1980 prefetchw(pc);
1981
1982 mem = memcg;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001983 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001984 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001985 return ret;
1986
1987 __mem_cgroup_commit_charge(mem, pc, ctype);
1988 return 0;
1989}
1990
1991int mem_cgroup_newpage_charge(struct page *page,
1992 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001993{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001994 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001995 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001996 if (PageCompound(page))
1997 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001998 /*
1999 * If already mapped, we don't have to account.
2000 * If page cache, page->mapping has address_space.
2001 * But page->mapping may have out-of-use anon_vma pointer,
2002 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2003 * is NULL.
2004 */
2005 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2006 return 0;
2007 if (unlikely(!mm))
2008 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002009 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002010 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002011}
2012
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002013static void
2014__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2015 enum charge_type ctype);
2016
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002017int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2018 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002019{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002020 struct mem_cgroup *mem = NULL;
2021 int ret;
2022
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002023 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002024 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002025 if (PageCompound(page))
2026 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002027 /*
2028 * Corner case handling. This is called from add_to_page_cache()
2029 * in usual. But some FS (shmem) precharges this page before calling it
2030 * and call add_to_page_cache() with GFP_NOWAIT.
2031 *
2032 * For GFP_NOWAIT case, the page may be pre-charged before calling
2033 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2034 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002035 * And when the page is SwapCache, it should take swap information
2036 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002037 */
2038 if (!(gfp_mask & __GFP_WAIT)) {
2039 struct page_cgroup *pc;
2040
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002041
2042 pc = lookup_page_cgroup(page);
2043 if (!pc)
2044 return 0;
2045 lock_page_cgroup(pc);
2046 if (PageCgroupUsed(pc)) {
2047 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002048 return 0;
2049 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002050 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002051 }
2052
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002053 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08002054 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002055
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07002056 if (page_is_file_cache(page))
2057 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002058 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002059
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002060 /* shmem */
2061 if (PageSwapCache(page)) {
2062 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2063 if (!ret)
2064 __mem_cgroup_commit_charge_swapin(page, mem,
2065 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2066 } else
2067 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
2068 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002069
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002070 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002071}
2072
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002073/*
2074 * While swap-in, try_charge -> commit or cancel, the page is locked.
2075 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002076 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002077 * "commit()" or removed by "cancel()"
2078 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002079int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2080 struct page *page,
2081 gfp_t mask, struct mem_cgroup **ptr)
2082{
2083 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002084 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002085
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002086 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002087 return 0;
2088
2089 if (!do_swap_account)
2090 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002091 /*
2092 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002093 * the pte, and even removed page from swap cache: in those cases
2094 * do_swap_page()'s pte_same() test will fail; but there's also a
2095 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002096 */
2097 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002098 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002099 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002100 if (!mem)
2101 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002102 *ptr = mem;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002103 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002104 /* drop extra refcnt from tryget */
2105 css_put(&mem->css);
2106 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002107charge_cur_mm:
2108 if (unlikely(!mm))
2109 mm = &init_mm;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002110 return __mem_cgroup_try_charge(mm, mask, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002111}
2112
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002113static void
2114__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2115 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002116{
2117 struct page_cgroup *pc;
2118
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002119 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002120 return;
2121 if (!ptr)
2122 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002123 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002124 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002125 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002126 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002127 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002128 /*
2129 * Now swap is on-memory. This means this page may be
2130 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002131 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2132 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2133 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002134 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002135 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002136 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002137 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002138 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002139
2140 id = swap_cgroup_record(ent, 0);
2141 rcu_read_lock();
2142 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002143 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002144 /*
2145 * This recorded memcg can be obsolete one. So, avoid
2146 * calling css_tryget
2147 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002148 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002149 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002150 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002151 mem_cgroup_put(memcg);
2152 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002153 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002154 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002155 /*
2156 * At swapin, we may charge account against cgroup which has no tasks.
2157 * So, rmdir()->pre_destroy() can be called while we do this charge.
2158 * In that case, we need to call pre_destroy() again. check it here.
2159 */
2160 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002161}
2162
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002163void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2164{
2165 __mem_cgroup_commit_charge_swapin(page, ptr,
2166 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2167}
2168
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002169void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2170{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002171 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002172 return;
2173 if (!mem)
2174 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002175 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002176}
2177
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002178static void
2179__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2180{
2181 struct memcg_batch_info *batch = NULL;
2182 bool uncharge_memsw = true;
2183 /* If swapout, usage of swap doesn't decrease */
2184 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2185 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002186
2187 batch = &current->memcg_batch;
2188 /*
2189 * In usual, we do css_get() when we remember memcg pointer.
2190 * But in this case, we keep res->usage until end of a series of
2191 * uncharges. Then, it's ok to ignore memcg's refcnt.
2192 */
2193 if (!batch->memcg)
2194 batch->memcg = mem;
2195 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002196 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2197 * In those cases, all pages freed continously can be expected to be in
2198 * the same cgroup and we have chance to coalesce uncharges.
2199 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2200 * because we want to do uncharge as soon as possible.
2201 */
2202
2203 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2204 goto direct_uncharge;
2205
2206 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002207 * In typical case, batch->memcg == mem. This means we can
2208 * merge a series of uncharges to an uncharge of res_counter.
2209 * If not, we uncharge res_counter ony by one.
2210 */
2211 if (batch->memcg != mem)
2212 goto direct_uncharge;
2213 /* remember freed charge and uncharge it later */
2214 batch->bytes += PAGE_SIZE;
2215 if (uncharge_memsw)
2216 batch->memsw_bytes += PAGE_SIZE;
2217 return;
2218direct_uncharge:
2219 res_counter_uncharge(&mem->res, PAGE_SIZE);
2220 if (uncharge_memsw)
2221 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002222 if (unlikely(batch->memcg != mem))
2223 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002224 return;
2225}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002226
Balbir Singh8697d332008-02-07 00:13:59 -08002227/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002228 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002229 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002230static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002231__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002232{
Hugh Dickins82895462008-03-04 14:29:08 -08002233 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002234 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002235 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002236
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002237 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002238 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002239
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002240 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002241 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002242
Balbir Singh8697d332008-02-07 00:13:59 -08002243 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002244 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002245 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002246 pc = lookup_page_cgroup(page);
2247 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002248 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002249
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002250 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002251
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002252 mem = pc->mem_cgroup;
2253
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002254 if (!PageCgroupUsed(pc))
2255 goto unlock_out;
2256
2257 switch (ctype) {
2258 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002259 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002260 /* See mem_cgroup_prepare_migration() */
2261 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002262 goto unlock_out;
2263 break;
2264 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2265 if (!PageAnon(page)) { /* Shared memory */
2266 if (page->mapping && !page_is_file_cache(page))
2267 goto unlock_out;
2268 } else if (page_mapped(page)) /* Anon */
2269 goto unlock_out;
2270 break;
2271 default:
2272 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002273 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002274
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002275 if (!mem_cgroup_is_root(mem))
2276 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002277 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2278 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002279 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002280
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002281 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002282 /*
2283 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2284 * freed from LRU. This is safe because uncharged page is expected not
2285 * to be reused (freed soon). Exception is SwapCache, it's handled by
2286 * special functions.
2287 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002288
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002289 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002290 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002291
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002292 memcg_check_events(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002293 /* at swapout, this memcg will be accessed to record to swap */
2294 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2295 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002296
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002297 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002298
2299unlock_out:
2300 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002301 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002302}
2303
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002304void mem_cgroup_uncharge_page(struct page *page)
2305{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002306 /* early check. */
2307 if (page_mapped(page))
2308 return;
2309 if (page->mapping && !PageAnon(page))
2310 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002311 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2312}
2313
2314void mem_cgroup_uncharge_cache_page(struct page *page)
2315{
2316 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002317 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002318 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2319}
2320
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002321/*
2322 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2323 * In that cases, pages are freed continuously and we can expect pages
2324 * are in the same memcg. All these calls itself limits the number of
2325 * pages freed at once, then uncharge_start/end() is called properly.
2326 * This may be called prural(2) times in a context,
2327 */
2328
2329void mem_cgroup_uncharge_start(void)
2330{
2331 current->memcg_batch.do_batch++;
2332 /* We can do nest. */
2333 if (current->memcg_batch.do_batch == 1) {
2334 current->memcg_batch.memcg = NULL;
2335 current->memcg_batch.bytes = 0;
2336 current->memcg_batch.memsw_bytes = 0;
2337 }
2338}
2339
2340void mem_cgroup_uncharge_end(void)
2341{
2342 struct memcg_batch_info *batch = &current->memcg_batch;
2343
2344 if (!batch->do_batch)
2345 return;
2346
2347 batch->do_batch--;
2348 if (batch->do_batch) /* If stacked, do nothing. */
2349 return;
2350
2351 if (!batch->memcg)
2352 return;
2353 /*
2354 * This "batch->memcg" is valid without any css_get/put etc...
2355 * bacause we hide charges behind us.
2356 */
2357 if (batch->bytes)
2358 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2359 if (batch->memsw_bytes)
2360 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002361 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002362 /* forget this pointer (for sanity check) */
2363 batch->memcg = NULL;
2364}
2365
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002366#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002367/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002368 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002369 * memcg information is recorded to swap_cgroup of "ent"
2370 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002371void
2372mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002373{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002374 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002375 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002376
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002377 if (!swapout) /* this was a swap cache but the swap is unused ! */
2378 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2379
2380 memcg = __mem_cgroup_uncharge_common(page, ctype);
2381
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002382 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002383 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002384 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002385 mem_cgroup_get(memcg);
2386 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002387 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002388 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002389}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002390#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002391
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002392#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2393/*
2394 * called from swap_entry_free(). remove record in swap_cgroup and
2395 * uncharge "memsw" account.
2396 */
2397void mem_cgroup_uncharge_swap(swp_entry_t ent)
2398{
2399 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002400 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002401
2402 if (!do_swap_account)
2403 return;
2404
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002405 id = swap_cgroup_record(ent, 0);
2406 rcu_read_lock();
2407 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002408 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002409 /*
2410 * We uncharge this because swap is freed.
2411 * This memcg can be obsolete one. We avoid calling css_tryget
2412 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002413 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002414 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002415 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002416 mem_cgroup_put(memcg);
2417 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002418 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002419}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002420
2421/**
2422 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2423 * @entry: swap entry to be moved
2424 * @from: mem_cgroup which the entry is moved from
2425 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002426 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002427 *
2428 * It succeeds only when the swap_cgroup's record for this entry is the same
2429 * as the mem_cgroup's id of @from.
2430 *
2431 * Returns 0 on success, -EINVAL on failure.
2432 *
2433 * The caller must have charged to @to, IOW, called res_counter_charge() about
2434 * both res and memsw, and called css_get().
2435 */
2436static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002437 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002438{
2439 unsigned short old_id, new_id;
2440
2441 old_id = css_id(&from->css);
2442 new_id = css_id(&to->css);
2443
2444 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002445 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002446 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002447 /*
2448 * This function is only called from task migration context now.
2449 * It postpones res_counter and refcount handling till the end
2450 * of task migration(mem_cgroup_clear_mc()) for performance
2451 * improvement. But we cannot postpone mem_cgroup_get(to)
2452 * because if the process that has been moved to @to does
2453 * swap-in, the refcount of @to might be decreased to 0.
2454 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002455 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002456 if (need_fixup) {
2457 if (!mem_cgroup_is_root(from))
2458 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2459 mem_cgroup_put(from);
2460 /*
2461 * we charged both to->res and to->memsw, so we should
2462 * uncharge to->res.
2463 */
2464 if (!mem_cgroup_is_root(to))
2465 res_counter_uncharge(&to->res, PAGE_SIZE);
2466 css_put(&to->css);
2467 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002468 return 0;
2469 }
2470 return -EINVAL;
2471}
2472#else
2473static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002474 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002475{
2476 return -EINVAL;
2477}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002478#endif
2479
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002480/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002481 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2482 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002483 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002484int mem_cgroup_prepare_migration(struct page *page,
2485 struct page *newpage, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002486{
2487 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002488 struct mem_cgroup *mem = NULL;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002489 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002490 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002491
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002492 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002493 return 0;
2494
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002495 pc = lookup_page_cgroup(page);
2496 lock_page_cgroup(pc);
2497 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002498 mem = pc->mem_cgroup;
2499 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002500 /*
2501 * At migrating an anonymous page, its mapcount goes down
2502 * to 0 and uncharge() will be called. But, even if it's fully
2503 * unmapped, migration may fail and this page has to be
2504 * charged again. We set MIGRATION flag here and delay uncharge
2505 * until end_migration() is called
2506 *
2507 * Corner Case Thinking
2508 * A)
2509 * When the old page was mapped as Anon and it's unmap-and-freed
2510 * while migration was ongoing.
2511 * If unmap finds the old page, uncharge() of it will be delayed
2512 * until end_migration(). If unmap finds a new page, it's
2513 * uncharged when it make mapcount to be 1->0. If unmap code
2514 * finds swap_migration_entry, the new page will not be mapped
2515 * and end_migration() will find it(mapcount==0).
2516 *
2517 * B)
2518 * When the old page was mapped but migraion fails, the kernel
2519 * remaps it. A charge for it is kept by MIGRATION flag even
2520 * if mapcount goes down to 0. We can do remap successfully
2521 * without charging it again.
2522 *
2523 * C)
2524 * The "old" page is under lock_page() until the end of
2525 * migration, so, the old page itself will not be swapped-out.
2526 * If the new page is swapped out before end_migraton, our
2527 * hook to usual swap-out path will catch the event.
2528 */
2529 if (PageAnon(page))
2530 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002531 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002532 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002533 /*
2534 * If the page is not charged at this point,
2535 * we return here.
2536 */
2537 if (!mem)
2538 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002539
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04002540 *ptr = mem;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002541 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2542 css_put(&mem->css);/* drop extra refcnt */
2543 if (ret || *ptr == NULL) {
2544 if (PageAnon(page)) {
2545 lock_page_cgroup(pc);
2546 ClearPageCgroupMigration(pc);
2547 unlock_page_cgroup(pc);
2548 /*
2549 * The old page may be fully unmapped while we kept it.
2550 */
2551 mem_cgroup_uncharge_page(page);
2552 }
2553 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002554 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002555 /*
2556 * We charge new page before it's used/mapped. So, even if unlock_page()
2557 * is called before end_migration, we can catch all events on this new
2558 * page. In the case new page is migrated but not remapped, new page's
2559 * mapcount will be finally 0 and we call uncharge in end_migration().
2560 */
2561 pc = lookup_page_cgroup(newpage);
2562 if (PageAnon(page))
2563 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2564 else if (page_is_file_cache(page))
2565 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2566 else
2567 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2568 __mem_cgroup_commit_charge(mem, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002569 return ret;
2570}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002571
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002572/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002573void mem_cgroup_end_migration(struct mem_cgroup *mem,
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002574 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002575{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002576 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002577 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002578
2579 if (!mem)
2580 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002581 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002582 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002583 /* at migration success, oldpage->mapping is NULL. */
2584 if (oldpage->mapping) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002585 used = oldpage;
2586 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002587 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002588 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002589 unused = oldpage;
2590 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002591 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002592 * We disallowed uncharge of pages under migration because mapcount
2593 * of the page goes down to zero, temporarly.
2594 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002595 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002596 pc = lookup_page_cgroup(oldpage);
2597 lock_page_cgroup(pc);
2598 ClearPageCgroupMigration(pc);
2599 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002600
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002601 if (unused != oldpage)
2602 pc = lookup_page_cgroup(unused);
2603 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2604
2605 pc = lookup_page_cgroup(used);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002606 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002607 * If a page is a file cache, radix-tree replacement is very atomic
2608 * and we can skip this check. When it was an Anon page, its mapcount
2609 * goes down to 0. But because we added MIGRATION flage, it's not
2610 * uncharged yet. There are several case but page->mapcount check
2611 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2612 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002613 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002614 if (PageAnon(used))
2615 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002616 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002617 * At migration, we may charge account against cgroup which has no
2618 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002619 * So, rmdir()->pre_destroy() can be called while we do this charge.
2620 * In that case, we need to call pre_destroy() again. check it here.
2621 */
2622 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002623}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002624
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002625/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002626 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2627 * Calling hierarchical_reclaim is not enough because we should update
2628 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2629 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2630 * not from the memcg which this page would be charged to.
2631 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002632 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002633int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002634 struct mm_struct *mm,
2635 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002636{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002637 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002638 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002639
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002640 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002641 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002642
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002643 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2644 if (!ret)
2645 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002646
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002647 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002648}
2649
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002650static DEFINE_MUTEX(set_limit_mutex);
2651
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002652static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002653 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002654{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002655 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002656 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002657 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002658 int children = mem_cgroup_count_children(memcg);
2659 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002660 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002661
2662 /*
2663 * For keeping hierarchical_reclaim simple, how long we should retry
2664 * is depends on callers. We set our retry-count to be function
2665 * of # of children which we should visit in this loop.
2666 */
2667 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2668
2669 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002670
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002671 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002672 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002673 if (signal_pending(current)) {
2674 ret = -EINTR;
2675 break;
2676 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002677 /*
2678 * Rather than hide all in some function, I do this in
2679 * open coded manner. You see what this really does.
2680 * We have to guarantee mem->res.limit < mem->memsw.limit.
2681 */
2682 mutex_lock(&set_limit_mutex);
2683 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2684 if (memswlimit < val) {
2685 ret = -EINVAL;
2686 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002687 break;
2688 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002689
2690 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2691 if (memlimit < val)
2692 enlarge = 1;
2693
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002694 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002695 if (!ret) {
2696 if (memswlimit == val)
2697 memcg->memsw_is_minimum = true;
2698 else
2699 memcg->memsw_is_minimum = false;
2700 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002701 mutex_unlock(&set_limit_mutex);
2702
2703 if (!ret)
2704 break;
2705
Bob Liuaa20d482009-12-15 16:47:14 -08002706 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002707 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002708 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2709 /* Usage is reduced ? */
2710 if (curusage >= oldusage)
2711 retry_count--;
2712 else
2713 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002714 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002715 if (!ret && enlarge)
2716 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002717
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002718 return ret;
2719}
2720
Li Zefan338c8432009-06-17 16:27:15 -07002721static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2722 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002723{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002724 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002725 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002726 int children = mem_cgroup_count_children(memcg);
2727 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002728 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002729
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002730 /* see mem_cgroup_resize_res_limit */
2731 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2732 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002733 while (retry_count) {
2734 if (signal_pending(current)) {
2735 ret = -EINTR;
2736 break;
2737 }
2738 /*
2739 * Rather than hide all in some function, I do this in
2740 * open coded manner. You see what this really does.
2741 * We have to guarantee mem->res.limit < mem->memsw.limit.
2742 */
2743 mutex_lock(&set_limit_mutex);
2744 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2745 if (memlimit > val) {
2746 ret = -EINVAL;
2747 mutex_unlock(&set_limit_mutex);
2748 break;
2749 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002750 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2751 if (memswlimit < val)
2752 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002753 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002754 if (!ret) {
2755 if (memlimit == val)
2756 memcg->memsw_is_minimum = true;
2757 else
2758 memcg->memsw_is_minimum = false;
2759 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002760 mutex_unlock(&set_limit_mutex);
2761
2762 if (!ret)
2763 break;
2764
Balbir Singh4e416952009-09-23 15:56:39 -07002765 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002766 MEM_CGROUP_RECLAIM_NOSWAP |
2767 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002768 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002769 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002770 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002771 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002772 else
2773 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002774 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002775 if (!ret && enlarge)
2776 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002777 return ret;
2778}
2779
Balbir Singh4e416952009-09-23 15:56:39 -07002780unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2781 gfp_t gfp_mask, int nid,
2782 int zid)
2783{
2784 unsigned long nr_reclaimed = 0;
2785 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2786 unsigned long reclaimed;
2787 int loop = 0;
2788 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002789 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002790
2791 if (order > 0)
2792 return 0;
2793
2794 mctz = soft_limit_tree_node_zone(nid, zid);
2795 /*
2796 * This loop can run a while, specially if mem_cgroup's continuously
2797 * keep exceeding their soft limit and putting the system under
2798 * pressure
2799 */
2800 do {
2801 if (next_mz)
2802 mz = next_mz;
2803 else
2804 mz = mem_cgroup_largest_soft_limit_node(mctz);
2805 if (!mz)
2806 break;
2807
2808 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2809 gfp_mask,
2810 MEM_CGROUP_RECLAIM_SOFT);
2811 nr_reclaimed += reclaimed;
2812 spin_lock(&mctz->lock);
2813
2814 /*
2815 * If we failed to reclaim anything from this memory cgroup
2816 * it is time to move on to the next cgroup
2817 */
2818 next_mz = NULL;
2819 if (!reclaimed) {
2820 do {
2821 /*
2822 * Loop until we find yet another one.
2823 *
2824 * By the time we get the soft_limit lock
2825 * again, someone might have aded the
2826 * group back on the RB tree. Iterate to
2827 * make sure we get a different mem.
2828 * mem_cgroup_largest_soft_limit_node returns
2829 * NULL if no other cgroup is present on
2830 * the tree
2831 */
2832 next_mz =
2833 __mem_cgroup_largest_soft_limit_node(mctz);
2834 if (next_mz == mz) {
2835 css_put(&next_mz->mem->css);
2836 next_mz = NULL;
2837 } else /* next_mz == NULL or other memcg */
2838 break;
2839 } while (1);
2840 }
Balbir Singh4e416952009-09-23 15:56:39 -07002841 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002842 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002843 /*
2844 * One school of thought says that we should not add
2845 * back the node to the tree if reclaim returns 0.
2846 * But our reclaim could return 0, simply because due
2847 * to priority we are exposing a smaller subset of
2848 * memory to reclaim from. Consider this as a longer
2849 * term TODO.
2850 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002851 /* If excess == 0, no tree ops */
2852 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002853 spin_unlock(&mctz->lock);
2854 css_put(&mz->mem->css);
2855 loop++;
2856 /*
2857 * Could not reclaim anything and there are no more
2858 * mem cgroups to try or we seem to be looping without
2859 * reclaiming anything.
2860 */
2861 if (!nr_reclaimed &&
2862 (next_mz == NULL ||
2863 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2864 break;
2865 } while (!nr_reclaimed);
2866 if (next_mz)
2867 css_put(&next_mz->mem->css);
2868 return nr_reclaimed;
2869}
2870
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002871/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002872 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002873 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2874 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002875static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002876 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002877{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002878 struct zone *zone;
2879 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002880 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002881 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002882 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002883 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002884
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002885 zone = &NODE_DATA(node)->node_zones[zid];
2886 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002887 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002888
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002889 loop = MEM_CGROUP_ZSTAT(mz, lru);
2890 /* give some margin against EBUSY etc...*/
2891 loop += 256;
2892 busy = NULL;
2893 while (loop--) {
2894 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002895 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002896 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002897 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002898 break;
2899 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002900 pc = list_entry(list->prev, struct page_cgroup, lru);
2901 if (busy == pc) {
2902 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08002903 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002904 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002905 continue;
2906 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002907 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002908
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002909 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002910 if (ret == -ENOMEM)
2911 break;
2912
2913 if (ret == -EBUSY || ret == -EINVAL) {
2914 /* found lock contention or "pc" is obsolete. */
2915 busy = pc;
2916 cond_resched();
2917 } else
2918 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002919 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002920
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002921 if (!ret && !list_empty(list))
2922 return -EBUSY;
2923 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002924}
2925
2926/*
2927 * make mem_cgroup's charge to be 0 if there is no task.
2928 * This enables deleting this mem_cgroup.
2929 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002930static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002931{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002932 int ret;
2933 int node, zid, shrink;
2934 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002935 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002936
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002937 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002938
2939 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002940 /* should free all ? */
2941 if (free_all)
2942 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002943move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002944 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002945 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002946 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002947 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002948 ret = -EINTR;
2949 if (signal_pending(current))
2950 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002951 /* This is for making all *used* pages to be on LRU. */
2952 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002953 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002954 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002955 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002956 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002957 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002958 for_each_lru(l) {
2959 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002960 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002961 if (ret)
2962 break;
2963 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002964 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002965 if (ret)
2966 break;
2967 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002968 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002969 /* it seems parent cgroup doesn't have enough mem */
2970 if (ret == -ENOMEM)
2971 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002972 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002973 /* "ret" should also be checked to ensure all lists are empty. */
2974 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002975out:
2976 css_put(&mem->css);
2977 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002978
2979try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002980 /* returns EBUSY if there is a task or if we come here twice. */
2981 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002982 ret = -EBUSY;
2983 goto out;
2984 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002985 /* we call try-to-free pages for make this cgroup empty */
2986 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002987 /* try to free all pages in this cgroup */
2988 shrink = 1;
2989 while (nr_retries && mem->res.usage > 0) {
2990 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002991
2992 if (signal_pending(current)) {
2993 ret = -EINTR;
2994 goto out;
2995 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002996 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2997 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002998 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002999 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003000 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003001 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003002 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003003
3004 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003005 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003006 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003007 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003008}
3009
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003010int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3011{
3012 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3013}
3014
3015
Balbir Singh18f59ea2009-01-07 18:08:07 -08003016static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3017{
3018 return mem_cgroup_from_cont(cont)->use_hierarchy;
3019}
3020
3021static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3022 u64 val)
3023{
3024 int retval = 0;
3025 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3026 struct cgroup *parent = cont->parent;
3027 struct mem_cgroup *parent_mem = NULL;
3028
3029 if (parent)
3030 parent_mem = mem_cgroup_from_cont(parent);
3031
3032 cgroup_lock();
3033 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003034 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003035 * in the child subtrees. If it is unset, then the change can
3036 * occur, provided the current cgroup has no children.
3037 *
3038 * For the root cgroup, parent_mem is NULL, we allow value to be
3039 * set if there are no children.
3040 */
3041 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3042 (val == 1 || val == 0)) {
3043 if (list_empty(&cont->children))
3044 mem->use_hierarchy = val;
3045 else
3046 retval = -EBUSY;
3047 } else
3048 retval = -EINVAL;
3049 cgroup_unlock();
3050
3051 return retval;
3052}
3053
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003054struct mem_cgroup_idx_data {
3055 s64 val;
3056 enum mem_cgroup_stat_index idx;
3057};
3058
3059static int
3060mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
3061{
3062 struct mem_cgroup_idx_data *d = data;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003063 d->val += mem_cgroup_read_stat(mem, d->idx);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003064 return 0;
3065}
3066
3067static void
3068mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3069 enum mem_cgroup_stat_index idx, s64 *val)
3070{
3071 struct mem_cgroup_idx_data d;
3072 d.idx = idx;
3073 d.val = 0;
3074 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
3075 *val = d.val;
3076}
3077
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003078static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3079{
3080 u64 idx_val, val;
3081
3082 if (!mem_cgroup_is_root(mem)) {
3083 if (!swap)
3084 return res_counter_read_u64(&mem->res, RES_USAGE);
3085 else
3086 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3087 }
3088
3089 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
3090 val = idx_val;
3091 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
3092 val += idx_val;
3093
3094 if (swap) {
3095 mem_cgroup_get_recursive_idx_stat(mem,
3096 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
3097 val += idx_val;
3098 }
3099
3100 return val << PAGE_SHIFT;
3101}
3102
Paul Menage2c3daa72008-04-29 00:59:58 -07003103static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003104{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003105 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003106 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003107 int type, name;
3108
3109 type = MEMFILE_TYPE(cft->private);
3110 name = MEMFILE_ATTR(cft->private);
3111 switch (type) {
3112 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003113 if (name == RES_USAGE)
3114 val = mem_cgroup_usage(mem, false);
3115 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003116 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003117 break;
3118 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003119 if (name == RES_USAGE)
3120 val = mem_cgroup_usage(mem, true);
3121 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003122 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003123 break;
3124 default:
3125 BUG();
3126 break;
3127 }
3128 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003129}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003130/*
3131 * The user of this function is...
3132 * RES_LIMIT.
3133 */
Paul Menage856c13a2008-07-25 01:47:04 -07003134static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3135 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003136{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003137 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003138 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003139 unsigned long long val;
3140 int ret;
3141
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003142 type = MEMFILE_TYPE(cft->private);
3143 name = MEMFILE_ATTR(cft->private);
3144 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003145 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003146 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3147 ret = -EINVAL;
3148 break;
3149 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003150 /* This function does all necessary parse...reuse it */
3151 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003152 if (ret)
3153 break;
3154 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003155 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003156 else
3157 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003158 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003159 case RES_SOFT_LIMIT:
3160 ret = res_counter_memparse_write_strategy(buffer, &val);
3161 if (ret)
3162 break;
3163 /*
3164 * For memsw, soft limits are hard to implement in terms
3165 * of semantics, for now, we support soft limits for
3166 * control without swap
3167 */
3168 if (type == _MEM)
3169 ret = res_counter_set_soft_limit(&memcg->res, val);
3170 else
3171 ret = -EINVAL;
3172 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003173 default:
3174 ret = -EINVAL; /* should be BUG() ? */
3175 break;
3176 }
3177 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003178}
3179
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003180static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3181 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3182{
3183 struct cgroup *cgroup;
3184 unsigned long long min_limit, min_memsw_limit, tmp;
3185
3186 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3187 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3188 cgroup = memcg->css.cgroup;
3189 if (!memcg->use_hierarchy)
3190 goto out;
3191
3192 while (cgroup->parent) {
3193 cgroup = cgroup->parent;
3194 memcg = mem_cgroup_from_cont(cgroup);
3195 if (!memcg->use_hierarchy)
3196 break;
3197 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3198 min_limit = min(min_limit, tmp);
3199 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3200 min_memsw_limit = min(min_memsw_limit, tmp);
3201 }
3202out:
3203 *mem_limit = min_limit;
3204 *memsw_limit = min_memsw_limit;
3205 return;
3206}
3207
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003208static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003209{
3210 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003211 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003212
3213 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003214 type = MEMFILE_TYPE(event);
3215 name = MEMFILE_ATTR(event);
3216 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003217 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003218 if (type == _MEM)
3219 res_counter_reset_max(&mem->res);
3220 else
3221 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003222 break;
3223 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003224 if (type == _MEM)
3225 res_counter_reset_failcnt(&mem->res);
3226 else
3227 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003228 break;
3229 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003230
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003231 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003232}
3233
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003234static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3235 struct cftype *cft)
3236{
3237 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3238}
3239
Daisuke Nishimura02491442010-03-10 15:22:17 -08003240#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003241static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3242 struct cftype *cft, u64 val)
3243{
3244 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3245
3246 if (val >= (1 << NR_MOVE_TYPE))
3247 return -EINVAL;
3248 /*
3249 * We check this value several times in both in can_attach() and
3250 * attach(), so we need cgroup lock to prevent this value from being
3251 * inconsistent.
3252 */
3253 cgroup_lock();
3254 mem->move_charge_at_immigrate = val;
3255 cgroup_unlock();
3256
3257 return 0;
3258}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003259#else
3260static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3261 struct cftype *cft, u64 val)
3262{
3263 return -ENOSYS;
3264}
3265#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003266
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003267
3268/* For read statistics */
3269enum {
3270 MCS_CACHE,
3271 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003272 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003273 MCS_PGPGIN,
3274 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003275 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003276 MCS_INACTIVE_ANON,
3277 MCS_ACTIVE_ANON,
3278 MCS_INACTIVE_FILE,
3279 MCS_ACTIVE_FILE,
3280 MCS_UNEVICTABLE,
3281 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003282};
3283
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003284struct mcs_total_stat {
3285 s64 stat[NR_MCS_STAT];
3286};
3287
3288struct {
3289 char *local_name;
3290 char *total_name;
3291} memcg_stat_strings[NR_MCS_STAT] = {
3292 {"cache", "total_cache"},
3293 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003294 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003295 {"pgpgin", "total_pgpgin"},
3296 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003297 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003298 {"inactive_anon", "total_inactive_anon"},
3299 {"active_anon", "total_active_anon"},
3300 {"inactive_file", "total_inactive_file"},
3301 {"active_file", "total_active_file"},
3302 {"unevictable", "total_unevictable"}
3303};
3304
3305
3306static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
3307{
3308 struct mcs_total_stat *s = data;
3309 s64 val;
3310
3311 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003312 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003313 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003314 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003315 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003316 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003317 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003318 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003319 s->stat[MCS_PGPGIN] += val;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003320 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003321 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003322 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003323 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003324 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3325 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003326
3327 /* per zone stat */
3328 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3329 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3330 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3331 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3332 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3333 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3334 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3335 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3336 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3337 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3338 return 0;
3339}
3340
3341static void
3342mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3343{
3344 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3345}
3346
Paul Menagec64745c2008-04-29 01:00:02 -07003347static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3348 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003349{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003350 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003351 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003352 int i;
3353
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003354 memset(&mystat, 0, sizeof(mystat));
3355 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003356
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003357 for (i = 0; i < NR_MCS_STAT; i++) {
3358 if (i == MCS_SWAP && !do_swap_account)
3359 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003360 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003361 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003362
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003363 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003364 {
3365 unsigned long long limit, memsw_limit;
3366 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3367 cb->fill(cb, "hierarchical_memory_limit", limit);
3368 if (do_swap_account)
3369 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3370 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003371
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003372 memset(&mystat, 0, sizeof(mystat));
3373 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003374 for (i = 0; i < NR_MCS_STAT; i++) {
3375 if (i == MCS_SWAP && !do_swap_account)
3376 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003377 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003378 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003379
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003380#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003381 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003382
3383 {
3384 int nid, zid;
3385 struct mem_cgroup_per_zone *mz;
3386 unsigned long recent_rotated[2] = {0, 0};
3387 unsigned long recent_scanned[2] = {0, 0};
3388
3389 for_each_online_node(nid)
3390 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3391 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3392
3393 recent_rotated[0] +=
3394 mz->reclaim_stat.recent_rotated[0];
3395 recent_rotated[1] +=
3396 mz->reclaim_stat.recent_rotated[1];
3397 recent_scanned[0] +=
3398 mz->reclaim_stat.recent_scanned[0];
3399 recent_scanned[1] +=
3400 mz->reclaim_stat.recent_scanned[1];
3401 }
3402 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3403 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3404 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3405 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3406 }
3407#endif
3408
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003409 return 0;
3410}
3411
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003412static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3413{
3414 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3415
3416 return get_swappiness(memcg);
3417}
3418
3419static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3420 u64 val)
3421{
3422 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3423 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003424
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003425 if (val > 100)
3426 return -EINVAL;
3427
3428 if (cgrp->parent == NULL)
3429 return -EINVAL;
3430
3431 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003432
3433 cgroup_lock();
3434
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003435 /* If under hierarchy, only empty-root can set this value */
3436 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003437 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3438 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003439 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003440 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003441
3442 spin_lock(&memcg->reclaim_param_lock);
3443 memcg->swappiness = val;
3444 spin_unlock(&memcg->reclaim_param_lock);
3445
Li Zefan068b38c2009-01-15 13:51:26 -08003446 cgroup_unlock();
3447
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003448 return 0;
3449}
3450
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003451static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3452{
3453 struct mem_cgroup_threshold_ary *t;
3454 u64 usage;
3455 int i;
3456
3457 rcu_read_lock();
3458 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003459 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003460 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003461 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003462
3463 if (!t)
3464 goto unlock;
3465
3466 usage = mem_cgroup_usage(memcg, swap);
3467
3468 /*
3469 * current_threshold points to threshold just below usage.
3470 * If it's not true, a threshold was crossed after last
3471 * call of __mem_cgroup_threshold().
3472 */
Phil Carmody5407a562010-05-26 14:42:42 -07003473 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003474
3475 /*
3476 * Iterate backward over array of thresholds starting from
3477 * current_threshold and check if a threshold is crossed.
3478 * If none of thresholds below usage is crossed, we read
3479 * only one element of the array here.
3480 */
3481 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3482 eventfd_signal(t->entries[i].eventfd, 1);
3483
3484 /* i = current_threshold + 1 */
3485 i++;
3486
3487 /*
3488 * Iterate forward over array of thresholds starting from
3489 * current_threshold+1 and check if a threshold is crossed.
3490 * If none of thresholds above usage is crossed, we read
3491 * only one element of the array here.
3492 */
3493 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3494 eventfd_signal(t->entries[i].eventfd, 1);
3495
3496 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003497 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003498unlock:
3499 rcu_read_unlock();
3500}
3501
3502static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3503{
3504 __mem_cgroup_threshold(memcg, false);
3505 if (do_swap_account)
3506 __mem_cgroup_threshold(memcg, true);
3507}
3508
3509static int compare_thresholds(const void *a, const void *b)
3510{
3511 const struct mem_cgroup_threshold *_a = a;
3512 const struct mem_cgroup_threshold *_b = b;
3513
3514 return _a->threshold - _b->threshold;
3515}
3516
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003517static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem, void *data)
3518{
3519 struct mem_cgroup_eventfd_list *ev;
3520
3521 list_for_each_entry(ev, &mem->oom_notify, list)
3522 eventfd_signal(ev->eventfd, 1);
3523 return 0;
3524}
3525
3526static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3527{
3528 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_notify_cb);
3529}
3530
3531static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3532 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003533{
3534 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003535 struct mem_cgroup_thresholds *thresholds;
3536 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003537 int type = MEMFILE_TYPE(cft->private);
3538 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003539 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003540
3541 ret = res_counter_memparse_write_strategy(args, &threshold);
3542 if (ret)
3543 return ret;
3544
3545 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003546
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003547 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003548 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003549 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003550 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003551 else
3552 BUG();
3553
3554 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3555
3556 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003557 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003558 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3559
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003560 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003561
3562 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003563 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003564 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003565 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003566 ret = -ENOMEM;
3567 goto unlock;
3568 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003569 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003570
3571 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003572 if (thresholds->primary) {
3573 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003574 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003575 }
3576
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003577 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003578 new->entries[size - 1].eventfd = eventfd;
3579 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003580
3581 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003582 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003583 compare_thresholds, NULL);
3584
3585 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003586 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003587 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003588 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003589 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003590 * new->current_threshold will not be used until
3591 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003592 * it here.
3593 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003594 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003595 }
3596 }
3597
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003598 /* Free old spare buffer and save old primary buffer as spare */
3599 kfree(thresholds->spare);
3600 thresholds->spare = thresholds->primary;
3601
3602 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003603
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003604 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003605 synchronize_rcu();
3606
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003607unlock:
3608 mutex_unlock(&memcg->thresholds_lock);
3609
3610 return ret;
3611}
3612
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003613static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003614 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003615{
3616 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003617 struct mem_cgroup_thresholds *thresholds;
3618 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003619 int type = MEMFILE_TYPE(cft->private);
3620 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003621 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003622
3623 mutex_lock(&memcg->thresholds_lock);
3624 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003625 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003626 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003627 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003628 else
3629 BUG();
3630
3631 /*
3632 * Something went wrong if we trying to unregister a threshold
3633 * if we don't have thresholds
3634 */
3635 BUG_ON(!thresholds);
3636
3637 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3638
3639 /* Check if a threshold crossed before removing */
3640 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3641
3642 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003643 size = 0;
3644 for (i = 0; i < thresholds->primary->size; i++) {
3645 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003646 size++;
3647 }
3648
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003649 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003650
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003651 /* Set thresholds array to NULL if we don't have thresholds */
3652 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003653 kfree(new);
3654 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003655 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003656 }
3657
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003658 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003659
3660 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003661 new->current_threshold = -1;
3662 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3663 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003664 continue;
3665
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003666 new->entries[j] = thresholds->primary->entries[i];
3667 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003668 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003669 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003670 * until rcu_assign_pointer(), so it's safe to increment
3671 * it here.
3672 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003673 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003674 }
3675 j++;
3676 }
3677
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003678swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003679 /* Swap primary and spare array */
3680 thresholds->spare = thresholds->primary;
3681 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003682
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003683 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003684 synchronize_rcu();
3685
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003686 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003687}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003688
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003689static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3690 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3691{
3692 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3693 struct mem_cgroup_eventfd_list *event;
3694 int type = MEMFILE_TYPE(cft->private);
3695
3696 BUG_ON(type != _OOM_TYPE);
3697 event = kmalloc(sizeof(*event), GFP_KERNEL);
3698 if (!event)
3699 return -ENOMEM;
3700
3701 mutex_lock(&memcg_oom_mutex);
3702
3703 event->eventfd = eventfd;
3704 list_add(&event->list, &memcg->oom_notify);
3705
3706 /* already in OOM ? */
3707 if (atomic_read(&memcg->oom_lock))
3708 eventfd_signal(eventfd, 1);
3709 mutex_unlock(&memcg_oom_mutex);
3710
3711 return 0;
3712}
3713
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003714static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003715 struct cftype *cft, struct eventfd_ctx *eventfd)
3716{
3717 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3718 struct mem_cgroup_eventfd_list *ev, *tmp;
3719 int type = MEMFILE_TYPE(cft->private);
3720
3721 BUG_ON(type != _OOM_TYPE);
3722
3723 mutex_lock(&memcg_oom_mutex);
3724
3725 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
3726 if (ev->eventfd == eventfd) {
3727 list_del(&ev->list);
3728 kfree(ev);
3729 }
3730 }
3731
3732 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003733}
3734
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003735static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3736 struct cftype *cft, struct cgroup_map_cb *cb)
3737{
3738 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3739
3740 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
3741
3742 if (atomic_read(&mem->oom_lock))
3743 cb->fill(cb, "under_oom", 1);
3744 else
3745 cb->fill(cb, "under_oom", 0);
3746 return 0;
3747}
3748
3749/*
3750 */
3751static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
3752 struct cftype *cft, u64 val)
3753{
3754 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3755 struct mem_cgroup *parent;
3756
3757 /* cannot set to root cgroup and only 0 and 1 are allowed */
3758 if (!cgrp->parent || !((val == 0) || (val == 1)))
3759 return -EINVAL;
3760
3761 parent = mem_cgroup_from_cont(cgrp->parent);
3762
3763 cgroup_lock();
3764 /* oom-kill-disable is a flag for subhierarchy. */
3765 if ((parent->use_hierarchy) ||
3766 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
3767 cgroup_unlock();
3768 return -EINVAL;
3769 }
3770 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07003771 if (!val)
3772 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003773 cgroup_unlock();
3774 return 0;
3775}
3776
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003777static struct cftype mem_cgroup_files[] = {
3778 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003779 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003780 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003781 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003782 .register_event = mem_cgroup_usage_register_event,
3783 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003784 },
3785 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003786 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003787 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003788 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003789 .read_u64 = mem_cgroup_read,
3790 },
3791 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003792 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003793 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003794 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003795 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003796 },
3797 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003798 .name = "soft_limit_in_bytes",
3799 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3800 .write_string = mem_cgroup_write,
3801 .read_u64 = mem_cgroup_read,
3802 },
3803 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003804 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003805 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003806 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003807 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003808 },
Balbir Singh8697d332008-02-07 00:13:59 -08003809 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003810 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003811 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003812 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003813 {
3814 .name = "force_empty",
3815 .trigger = mem_cgroup_force_empty_write,
3816 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003817 {
3818 .name = "use_hierarchy",
3819 .write_u64 = mem_cgroup_hierarchy_write,
3820 .read_u64 = mem_cgroup_hierarchy_read,
3821 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003822 {
3823 .name = "swappiness",
3824 .read_u64 = mem_cgroup_swappiness_read,
3825 .write_u64 = mem_cgroup_swappiness_write,
3826 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003827 {
3828 .name = "move_charge_at_immigrate",
3829 .read_u64 = mem_cgroup_move_charge_read,
3830 .write_u64 = mem_cgroup_move_charge_write,
3831 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003832 {
3833 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003834 .read_map = mem_cgroup_oom_control_read,
3835 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003836 .register_event = mem_cgroup_oom_register_event,
3837 .unregister_event = mem_cgroup_oom_unregister_event,
3838 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
3839 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003840};
3841
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003842#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3843static struct cftype memsw_cgroup_files[] = {
3844 {
3845 .name = "memsw.usage_in_bytes",
3846 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3847 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003848 .register_event = mem_cgroup_usage_register_event,
3849 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003850 },
3851 {
3852 .name = "memsw.max_usage_in_bytes",
3853 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3854 .trigger = mem_cgroup_reset,
3855 .read_u64 = mem_cgroup_read,
3856 },
3857 {
3858 .name = "memsw.limit_in_bytes",
3859 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3860 .write_string = mem_cgroup_write,
3861 .read_u64 = mem_cgroup_read,
3862 },
3863 {
3864 .name = "memsw.failcnt",
3865 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3866 .trigger = mem_cgroup_reset,
3867 .read_u64 = mem_cgroup_read,
3868 },
3869};
3870
3871static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3872{
3873 if (!do_swap_account)
3874 return 0;
3875 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3876 ARRAY_SIZE(memsw_cgroup_files));
3877};
3878#else
3879static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3880{
3881 return 0;
3882}
3883#endif
3884
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003885static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3886{
3887 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003888 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003889 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003890 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003891 /*
3892 * This routine is called against possible nodes.
3893 * But it's BUG to call kmalloc() against offline node.
3894 *
3895 * TODO: this routine can waste much memory for nodes which will
3896 * never be onlined. It's better to use memory hotplug callback
3897 * function.
3898 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003899 if (!node_state(node, N_NORMAL_MEMORY))
3900 tmp = -1;
3901 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003902 if (!pn)
3903 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003904
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003905 mem->info.nodeinfo[node] = pn;
3906 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003907
3908 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3909 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003910 for_each_lru(l)
3911 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003912 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003913 mz->on_tree = false;
3914 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003915 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003916 return 0;
3917}
3918
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003919static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3920{
3921 kfree(mem->info.nodeinfo[node]);
3922}
3923
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003924static struct mem_cgroup *mem_cgroup_alloc(void)
3925{
3926 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003927 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003928
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003929 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003930 if (size < PAGE_SIZE)
3931 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003932 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003933 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003934
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07003935 if (!mem)
3936 return NULL;
3937
3938 memset(mem, 0, size);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003939 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
3940 if (!mem->stat) {
3941 if (size < PAGE_SIZE)
3942 kfree(mem);
3943 else
3944 vfree(mem);
3945 mem = NULL;
3946 }
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003947 return mem;
3948}
3949
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003950/*
3951 * At destroying mem_cgroup, references from swap_cgroup can remain.
3952 * (scanning all at force_empty is too costly...)
3953 *
3954 * Instead of clearing all references at force_empty, we remember
3955 * the number of reference from swap_cgroup and free mem_cgroup when
3956 * it goes down to 0.
3957 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003958 * Removal of cgroup itself succeeds regardless of refs from swap.
3959 */
3960
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003961static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003962{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003963 int node;
3964
Balbir Singhf64c3f52009-09-23 15:56:37 -07003965 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003966 free_css_id(&mem_cgroup_subsys, &mem->css);
3967
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003968 for_each_node_state(node, N_POSSIBLE)
3969 free_mem_cgroup_per_zone_info(mem, node);
3970
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003971 free_percpu(mem->stat);
3972 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003973 kfree(mem);
3974 else
3975 vfree(mem);
3976}
3977
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003978static void mem_cgroup_get(struct mem_cgroup *mem)
3979{
3980 atomic_inc(&mem->refcnt);
3981}
3982
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003983static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003984{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003985 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003986 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003987 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003988 if (parent)
3989 mem_cgroup_put(parent);
3990 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003991}
3992
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003993static void mem_cgroup_put(struct mem_cgroup *mem)
3994{
3995 __mem_cgroup_put(mem, 1);
3996}
3997
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003998/*
3999 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4000 */
4001static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4002{
4003 if (!mem->res.parent)
4004 return NULL;
4005 return mem_cgroup_from_res_counter(mem->res.parent, res);
4006}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004007
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004008#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4009static void __init enable_swap_cgroup(void)
4010{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004011 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004012 do_swap_account = 1;
4013}
4014#else
4015static void __init enable_swap_cgroup(void)
4016{
4017}
4018#endif
4019
Balbir Singhf64c3f52009-09-23 15:56:37 -07004020static int mem_cgroup_soft_limit_tree_init(void)
4021{
4022 struct mem_cgroup_tree_per_node *rtpn;
4023 struct mem_cgroup_tree_per_zone *rtpz;
4024 int tmp, node, zone;
4025
4026 for_each_node_state(node, N_POSSIBLE) {
4027 tmp = node;
4028 if (!node_state(node, N_NORMAL_MEMORY))
4029 tmp = -1;
4030 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4031 if (!rtpn)
4032 return 1;
4033
4034 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4035
4036 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4037 rtpz = &rtpn->rb_tree_per_zone[zone];
4038 rtpz->rb_root = RB_ROOT;
4039 spin_lock_init(&rtpz->lock);
4040 }
4041 }
4042 return 0;
4043}
4044
Li Zefan0eb253e2009-01-15 13:51:25 -08004045static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004046mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4047{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004048 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004049 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004050 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004051
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004052 mem = mem_cgroup_alloc();
4053 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004054 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004055
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004056 for_each_node_state(node, N_POSSIBLE)
4057 if (alloc_mem_cgroup_per_zone_info(mem, node))
4058 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004059
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004060 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004061 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004062 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004063 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004064 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004065 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004066 if (mem_cgroup_soft_limit_tree_init())
4067 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004068 for_each_possible_cpu(cpu) {
4069 struct memcg_stock_pcp *stock =
4070 &per_cpu(memcg_stock, cpu);
4071 INIT_WORK(&stock->work, drain_local_stock);
4072 }
4073 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004074 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004075 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004076 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004077 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004078 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004079
Balbir Singh18f59ea2009-01-07 18:08:07 -08004080 if (parent && parent->use_hierarchy) {
4081 res_counter_init(&mem->res, &parent->res);
4082 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004083 /*
4084 * We increment refcnt of the parent to ensure that we can
4085 * safely access it on res_counter_charge/uncharge.
4086 * This refcnt will be decremented when freeing this
4087 * mem_cgroup(see mem_cgroup_put).
4088 */
4089 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004090 } else {
4091 res_counter_init(&mem->res, NULL);
4092 res_counter_init(&mem->memsw, NULL);
4093 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004094 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08004095 spin_lock_init(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004096 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004097
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004098 if (parent)
4099 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004100 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004101 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004102 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004103 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004104free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004105 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004106 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004107 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004108}
4109
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004110static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004111 struct cgroup *cont)
4112{
4113 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004114
4115 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004116}
4117
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004118static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4119 struct cgroup *cont)
4120{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004121 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004122
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004123 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004124}
4125
4126static int mem_cgroup_populate(struct cgroup_subsys *ss,
4127 struct cgroup *cont)
4128{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004129 int ret;
4130
4131 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4132 ARRAY_SIZE(mem_cgroup_files));
4133
4134 if (!ret)
4135 ret = register_memsw_files(cont, ss);
4136 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004137}
4138
Daisuke Nishimura02491442010-03-10 15:22:17 -08004139#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004140/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004141#define PRECHARGE_COUNT_AT_ONCE 256
4142static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004143{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004144 int ret = 0;
4145 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004146 struct mem_cgroup *mem = mc.to;
4147
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004148 if (mem_cgroup_is_root(mem)) {
4149 mc.precharge += count;
4150 /* we don't need css_get for root */
4151 return ret;
4152 }
4153 /* try to charge at once */
4154 if (count > 1) {
4155 struct res_counter *dummy;
4156 /*
4157 * "mem" cannot be under rmdir() because we've already checked
4158 * by cgroup_lock_live_cgroup() that it is not removed and we
4159 * are still under the same cgroup_mutex. So we can postpone
4160 * css_get().
4161 */
4162 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4163 goto one_by_one;
4164 if (do_swap_account && res_counter_charge(&mem->memsw,
4165 PAGE_SIZE * count, &dummy)) {
4166 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4167 goto one_by_one;
4168 }
4169 mc.precharge += count;
4170 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
4171 WARN_ON_ONCE(count > INT_MAX);
4172 __css_get(&mem->css, (int)count);
4173 return ret;
4174 }
4175one_by_one:
4176 /* fall back to one by one charge */
4177 while (count--) {
4178 if (signal_pending(current)) {
4179 ret = -EINTR;
4180 break;
4181 }
4182 if (!batch_count--) {
4183 batch_count = PRECHARGE_COUNT_AT_ONCE;
4184 cond_resched();
4185 }
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08004186 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004187 if (ret || !mem)
4188 /* mem_cgroup_clear_mc() will do uncharge later */
4189 return -ENOMEM;
4190 mc.precharge++;
4191 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004192 return ret;
4193}
4194
4195/**
4196 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4197 * @vma: the vma the pte to be checked belongs
4198 * @addr: the address corresponding to the pte to be checked
4199 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004200 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004201 *
4202 * Returns
4203 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4204 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4205 * move charge. if @target is not NULL, the page is stored in target->page
4206 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004207 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4208 * target for charge migration. if @target is not NULL, the entry is stored
4209 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004210 *
4211 * Called with pte lock held.
4212 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004213union mc_target {
4214 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004215 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004216};
4217
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004218enum mc_target_type {
4219 MC_TARGET_NONE, /* not used */
4220 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004221 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004222};
4223
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004224static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4225 unsigned long addr, pte_t ptent)
4226{
4227 struct page *page = vm_normal_page(vma, addr, ptent);
4228
4229 if (!page || !page_mapped(page))
4230 return NULL;
4231 if (PageAnon(page)) {
4232 /* we don't move shared anon */
4233 if (!move_anon() || page_mapcount(page) > 2)
4234 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004235 } else if (!move_file())
4236 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004237 return NULL;
4238 if (!get_page_unless_zero(page))
4239 return NULL;
4240
4241 return page;
4242}
4243
4244static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4245 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4246{
4247 int usage_count;
4248 struct page *page = NULL;
4249 swp_entry_t ent = pte_to_swp_entry(ptent);
4250
4251 if (!move_anon() || non_swap_entry(ent))
4252 return NULL;
4253 usage_count = mem_cgroup_count_swap_user(ent, &page);
4254 if (usage_count > 1) { /* we don't move shared anon */
4255 if (page)
4256 put_page(page);
4257 return NULL;
4258 }
4259 if (do_swap_account)
4260 entry->val = ent.val;
4261
4262 return page;
4263}
4264
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004265static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4266 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4267{
4268 struct page *page = NULL;
4269 struct inode *inode;
4270 struct address_space *mapping;
4271 pgoff_t pgoff;
4272
4273 if (!vma->vm_file) /* anonymous vma */
4274 return NULL;
4275 if (!move_file())
4276 return NULL;
4277
4278 inode = vma->vm_file->f_path.dentry->d_inode;
4279 mapping = vma->vm_file->f_mapping;
4280 if (pte_none(ptent))
4281 pgoff = linear_page_index(vma, addr);
4282 else /* pte_file(ptent) is true */
4283 pgoff = pte_to_pgoff(ptent);
4284
4285 /* page is moved even if it's not RSS of this task(page-faulted). */
4286 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4287 page = find_get_page(mapping, pgoff);
4288 } else { /* shmem/tmpfs file. we should take account of swap too. */
4289 swp_entry_t ent;
4290 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4291 if (do_swap_account)
4292 entry->val = ent.val;
4293 }
4294
4295 return page;
4296}
4297
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004298static int is_target_pte_for_mc(struct vm_area_struct *vma,
4299 unsigned long addr, pte_t ptent, union mc_target *target)
4300{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004301 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004302 struct page_cgroup *pc;
4303 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004304 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004305
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004306 if (pte_present(ptent))
4307 page = mc_handle_present_pte(vma, addr, ptent);
4308 else if (is_swap_pte(ptent))
4309 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004310 else if (pte_none(ptent) || pte_file(ptent))
4311 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004312
4313 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004314 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004315 if (page) {
4316 pc = lookup_page_cgroup(page);
4317 /*
4318 * Do only loose check w/o page_cgroup lock.
4319 * mem_cgroup_move_account() checks the pc is valid or not under
4320 * the lock.
4321 */
4322 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4323 ret = MC_TARGET_PAGE;
4324 if (target)
4325 target->page = page;
4326 }
4327 if (!ret || !target)
4328 put_page(page);
4329 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004330 /* There is a swap entry and a page doesn't exist or isn't charged */
4331 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004332 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4333 ret = MC_TARGET_SWAP;
4334 if (target)
4335 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004336 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004337 return ret;
4338}
4339
4340static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4341 unsigned long addr, unsigned long end,
4342 struct mm_walk *walk)
4343{
4344 struct vm_area_struct *vma = walk->private;
4345 pte_t *pte;
4346 spinlock_t *ptl;
4347
4348 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4349 for (; addr != end; pte++, addr += PAGE_SIZE)
4350 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4351 mc.precharge++; /* increment precharge temporarily */
4352 pte_unmap_unlock(pte - 1, ptl);
4353 cond_resched();
4354
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004355 return 0;
4356}
4357
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004358static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4359{
4360 unsigned long precharge;
4361 struct vm_area_struct *vma;
4362
4363 down_read(&mm->mmap_sem);
4364 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4365 struct mm_walk mem_cgroup_count_precharge_walk = {
4366 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4367 .mm = mm,
4368 .private = vma,
4369 };
4370 if (is_vm_hugetlb_page(vma))
4371 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004372 walk_page_range(vma->vm_start, vma->vm_end,
4373 &mem_cgroup_count_precharge_walk);
4374 }
4375 up_read(&mm->mmap_sem);
4376
4377 precharge = mc.precharge;
4378 mc.precharge = 0;
4379
4380 return precharge;
4381}
4382
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004383static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4384{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004385 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004386}
4387
4388static void mem_cgroup_clear_mc(void)
4389{
4390 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004391 if (mc.precharge) {
4392 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4393 mc.precharge = 0;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004394 memcg_oom_recover(mc.to);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004395 }
4396 /*
4397 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4398 * we must uncharge here.
4399 */
4400 if (mc.moved_charge) {
4401 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4402 mc.moved_charge = 0;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004403 memcg_oom_recover(mc.from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004404 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004405 /* we must fixup refcnts and charges */
4406 if (mc.moved_swap) {
4407 WARN_ON_ONCE(mc.moved_swap > INT_MAX);
4408 /* uncharge swap account from the old cgroup */
4409 if (!mem_cgroup_is_root(mc.from))
4410 res_counter_uncharge(&mc.from->memsw,
4411 PAGE_SIZE * mc.moved_swap);
4412 __mem_cgroup_put(mc.from, mc.moved_swap);
4413
4414 if (!mem_cgroup_is_root(mc.to)) {
4415 /*
4416 * we charged both to->res and to->memsw, so we should
4417 * uncharge to->res.
4418 */
4419 res_counter_uncharge(&mc.to->res,
4420 PAGE_SIZE * mc.moved_swap);
4421 VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags));
4422 __css_put(&mc.to->css, mc.moved_swap);
4423 }
4424 /* we've already done mem_cgroup_get(mc.to) */
4425
4426 mc.moved_swap = 0;
4427 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004428 mc.from = NULL;
4429 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004430 mc.moving_task = NULL;
4431 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004432}
4433
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004434static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4435 struct cgroup *cgroup,
4436 struct task_struct *p,
4437 bool threadgroup)
4438{
4439 int ret = 0;
4440 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4441
4442 if (mem->move_charge_at_immigrate) {
4443 struct mm_struct *mm;
4444 struct mem_cgroup *from = mem_cgroup_from_task(p);
4445
4446 VM_BUG_ON(from == mem);
4447
4448 mm = get_task_mm(p);
4449 if (!mm)
4450 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004451 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004452 if (mm->owner == p) {
4453 VM_BUG_ON(mc.from);
4454 VM_BUG_ON(mc.to);
4455 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004456 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004457 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004458 VM_BUG_ON(mc.moving_task);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004459 mc.from = from;
4460 mc.to = mem;
4461 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004462 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004463 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004464 mc.moving_task = current;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004465
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004466 ret = mem_cgroup_precharge_mc(mm);
4467 if (ret)
4468 mem_cgroup_clear_mc();
4469 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004470 mmput(mm);
4471 }
4472 return ret;
4473}
4474
4475static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4476 struct cgroup *cgroup,
4477 struct task_struct *p,
4478 bool threadgroup)
4479{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004480 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004481}
4482
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004483static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4484 unsigned long addr, unsigned long end,
4485 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004486{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004487 int ret = 0;
4488 struct vm_area_struct *vma = walk->private;
4489 pte_t *pte;
4490 spinlock_t *ptl;
4491
4492retry:
4493 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4494 for (; addr != end; addr += PAGE_SIZE) {
4495 pte_t ptent = *(pte++);
4496 union mc_target target;
4497 int type;
4498 struct page *page;
4499 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004500 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004501
4502 if (!mc.precharge)
4503 break;
4504
4505 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4506 switch (type) {
4507 case MC_TARGET_PAGE:
4508 page = target.page;
4509 if (isolate_lru_page(page))
4510 goto put;
4511 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004512 if (!mem_cgroup_move_account(pc,
4513 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004514 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004515 /* we uncharge from mc.from later. */
4516 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004517 }
4518 putback_lru_page(page);
4519put: /* is_target_pte_for_mc() gets the page */
4520 put_page(page);
4521 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004522 case MC_TARGET_SWAP:
4523 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004524 if (!mem_cgroup_move_swap_account(ent,
4525 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004526 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004527 /* we fixup refcnts and charges later. */
4528 mc.moved_swap++;
4529 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004530 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004531 default:
4532 break;
4533 }
4534 }
4535 pte_unmap_unlock(pte - 1, ptl);
4536 cond_resched();
4537
4538 if (addr != end) {
4539 /*
4540 * We have consumed all precharges we got in can_attach().
4541 * We try charge one by one, but don't do any additional
4542 * charges to mc.to if we have failed in charge once in attach()
4543 * phase.
4544 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004545 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004546 if (!ret)
4547 goto retry;
4548 }
4549
4550 return ret;
4551}
4552
4553static void mem_cgroup_move_charge(struct mm_struct *mm)
4554{
4555 struct vm_area_struct *vma;
4556
4557 lru_add_drain_all();
4558 down_read(&mm->mmap_sem);
4559 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4560 int ret;
4561 struct mm_walk mem_cgroup_move_charge_walk = {
4562 .pmd_entry = mem_cgroup_move_charge_pte_range,
4563 .mm = mm,
4564 .private = vma,
4565 };
4566 if (is_vm_hugetlb_page(vma))
4567 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004568 ret = walk_page_range(vma->vm_start, vma->vm_end,
4569 &mem_cgroup_move_charge_walk);
4570 if (ret)
4571 /*
4572 * means we have consumed all precharges and failed in
4573 * doing additional charge. Just abandon here.
4574 */
4575 break;
4576 }
4577 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004578}
4579
Balbir Singh67e465a2008-02-07 00:13:54 -08004580static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4581 struct cgroup *cont,
4582 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004583 struct task_struct *p,
4584 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004585{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004586 struct mm_struct *mm;
4587
4588 if (!mc.to)
4589 /* no need to move charge */
4590 return;
4591
4592 mm = get_task_mm(p);
4593 if (mm) {
4594 mem_cgroup_move_charge(mm);
4595 mmput(mm);
4596 }
4597 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004598}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07004599#else /* !CONFIG_MMU */
4600static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4601 struct cgroup *cgroup,
4602 struct task_struct *p,
4603 bool threadgroup)
4604{
4605 return 0;
4606}
4607static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4608 struct cgroup *cgroup,
4609 struct task_struct *p,
4610 bool threadgroup)
4611{
4612}
4613static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4614 struct cgroup *cont,
4615 struct cgroup *old_cont,
4616 struct task_struct *p,
4617 bool threadgroup)
4618{
4619}
4620#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08004621
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004622struct cgroup_subsys mem_cgroup_subsys = {
4623 .name = "memory",
4624 .subsys_id = mem_cgroup_subsys_id,
4625 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004626 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004627 .destroy = mem_cgroup_destroy,
4628 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004629 .can_attach = mem_cgroup_can_attach,
4630 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004631 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004632 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004633 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004634};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004635
4636#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4637
4638static int __init disable_swap_account(char *s)
4639{
4640 really_do_swap_account = 0;
4641 return 1;
4642}
4643__setup("noswapaccount", disable_swap_account);
4644#endif