blob: 20a8193a7af8ed275667e682fd22101a0e40bb93 [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
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800214 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800215
216 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200217 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700218 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800219 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700220 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800221 /*
222 * Should the accounting and control be hierarchical, per subtree?
223 */
224 bool use_hierarchy;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -0800225 atomic_t oom_lock;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800226 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800227
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800228 unsigned int swappiness;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -0700229 /* OOM-Killer disable */
230 int oom_kill_disable;
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800231
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700232 /* set when res.limit == memsw.limit */
233 bool memsw_is_minimum;
234
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800235 /* protect arrays of thresholds */
236 struct mutex thresholds_lock;
237
238 /* thresholds for memory usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700239 struct mem_cgroup_thresholds thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700240
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800241 /* thresholds for mem+swap usage. RCU-protected */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -0700242 struct mem_cgroup_thresholds memsw_thresholds;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -0700243
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700244 /* For oom notifier event fd */
245 struct list_head oom_notify;
246
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800247 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800248 * Should we move charges of a task when a task is moved into this
249 * mem_cgroup ? And what type of charges should we move ?
250 */
251 unsigned long move_charge_at_immigrate;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800252 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800253 * percpu counter.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800254 */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800255 struct mem_cgroup_stat_cpu *stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800256};
257
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800258/* Stuffs for move charges at task migration. */
259/*
260 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
261 * left-shifted bitmap of these types.
262 */
263enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800264 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700265 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800266 NR_MOVE_TYPE,
267};
268
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800269/* "mc" and its members are protected by cgroup_mutex */
270static struct move_charge_struct {
271 struct mem_cgroup *from;
272 struct mem_cgroup *to;
273 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800274 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800275 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800276 struct task_struct *moving_task; /* a task moving charges */
277 wait_queue_head_t waitq; /* a waitq for other context */
278} mc = {
279 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
280};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800281
Daisuke Nishimura90254a62010-05-26 14:42:38 -0700282static bool move_anon(void)
283{
284 return test_bit(MOVE_CHARGE_TYPE_ANON,
285 &mc.to->move_charge_at_immigrate);
286}
287
Daisuke Nishimura87946a72010-05-26 14:42:39 -0700288static bool move_file(void)
289{
290 return test_bit(MOVE_CHARGE_TYPE_FILE,
291 &mc.to->move_charge_at_immigrate);
292}
293
Balbir Singh4e416952009-09-23 15:56:39 -0700294/*
295 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
296 * limit reclaim to prevent infinite loops, if they ever occur.
297 */
298#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
299#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
300
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800301enum charge_type {
302 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
303 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700304 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700305 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800306 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700307 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700308 NR_CHARGE_TYPE,
309};
310
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700311/* only for here (for easy reading.) */
312#define PCGF_CACHE (1UL << PCG_CACHE)
313#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700314#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700315/* Not used, but added here for completeness */
316#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800317
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800318/* for encoding cft->private value on file */
319#define _MEM (0)
320#define _MEMSWAP (1)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700321#define _OOM_TYPE (2)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800322#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
323#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
324#define MEMFILE_ATTR(val) ((val) & 0xffff)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -0700325/* Used for OOM nofiier */
326#define OOM_CONTROL (0)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800327
Balbir Singh75822b42009-09-23 15:56:38 -0700328/*
329 * Reclaim flags for mem_cgroup_hierarchical_reclaim
330 */
331#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
332#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
333#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
334#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700335#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
336#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700337
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800338static void mem_cgroup_get(struct mem_cgroup *mem);
339static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800340static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800341static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800342
Balbir Singhf64c3f52009-09-23 15:56:37 -0700343static struct mem_cgroup_per_zone *
344mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
345{
346 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
347}
348
Wu Fengguangd3242362009-12-16 12:19:59 +0100349struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
350{
351 return &mem->css;
352}
353
Balbir Singhf64c3f52009-09-23 15:56:37 -0700354static struct mem_cgroup_per_zone *
355page_cgroup_zoneinfo(struct page_cgroup *pc)
356{
357 struct mem_cgroup *mem = pc->mem_cgroup;
358 int nid = page_cgroup_nid(pc);
359 int zid = page_cgroup_zid(pc);
360
361 if (!mem)
362 return NULL;
363
364 return mem_cgroup_zoneinfo(mem, nid, zid);
365}
366
367static struct mem_cgroup_tree_per_zone *
368soft_limit_tree_node_zone(int nid, int zid)
369{
370 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
371}
372
373static struct mem_cgroup_tree_per_zone *
374soft_limit_tree_from_page(struct page *page)
375{
376 int nid = page_to_nid(page);
377 int zid = page_zonenum(page);
378
379 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
380}
381
382static void
Balbir Singh4e416952009-09-23 15:56:39 -0700383__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700384 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700385 struct mem_cgroup_tree_per_zone *mctz,
386 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700387{
388 struct rb_node **p = &mctz->rb_root.rb_node;
389 struct rb_node *parent = NULL;
390 struct mem_cgroup_per_zone *mz_node;
391
392 if (mz->on_tree)
393 return;
394
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700395 mz->usage_in_excess = new_usage_in_excess;
396 if (!mz->usage_in_excess)
397 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700398 while (*p) {
399 parent = *p;
400 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
401 tree_node);
402 if (mz->usage_in_excess < mz_node->usage_in_excess)
403 p = &(*p)->rb_left;
404 /*
405 * We can't avoid mem cgroups that are over their soft
406 * limit by the same amount
407 */
408 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
409 p = &(*p)->rb_right;
410 }
411 rb_link_node(&mz->tree_node, parent, p);
412 rb_insert_color(&mz->tree_node, &mctz->rb_root);
413 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700414}
415
416static void
417__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
418 struct mem_cgroup_per_zone *mz,
419 struct mem_cgroup_tree_per_zone *mctz)
420{
421 if (!mz->on_tree)
422 return;
423 rb_erase(&mz->tree_node, &mctz->rb_root);
424 mz->on_tree = false;
425}
426
427static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700428mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
429 struct mem_cgroup_per_zone *mz,
430 struct mem_cgroup_tree_per_zone *mctz)
431{
432 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700433 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700434 spin_unlock(&mctz->lock);
435}
436
Balbir Singhf64c3f52009-09-23 15:56:37 -0700437
438static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
439{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700440 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700441 struct mem_cgroup_per_zone *mz;
442 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700443 int nid = page_to_nid(page);
444 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700445 mctz = soft_limit_tree_from_page(page);
446
447 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700448 * Necessary to update all ancestors when hierarchy is used.
449 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700450 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700451 for (; mem; mem = parent_mem_cgroup(mem)) {
452 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700453 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700454 /*
455 * We have to update the tree if mz is on RB-tree or
456 * mem is over its softlimit.
457 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700458 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700459 spin_lock(&mctz->lock);
460 /* if on-tree, remove it */
461 if (mz->on_tree)
462 __mem_cgroup_remove_exceeded(mem, mz, mctz);
463 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700464 * Insert again. mz->usage_in_excess will be updated.
465 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700466 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700467 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700468 spin_unlock(&mctz->lock);
469 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700470 }
471}
472
473static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
474{
475 int node, zone;
476 struct mem_cgroup_per_zone *mz;
477 struct mem_cgroup_tree_per_zone *mctz;
478
479 for_each_node_state(node, N_POSSIBLE) {
480 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
481 mz = mem_cgroup_zoneinfo(mem, node, zone);
482 mctz = soft_limit_tree_node_zone(node, zone);
483 mem_cgroup_remove_exceeded(mem, mz, mctz);
484 }
485 }
486}
487
Balbir Singh4e416952009-09-23 15:56:39 -0700488static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
489{
490 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
491}
492
493static struct mem_cgroup_per_zone *
494__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
495{
496 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700497 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700498
499retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700500 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700501 rightmost = rb_last(&mctz->rb_root);
502 if (!rightmost)
503 goto done; /* Nothing to reclaim from */
504
505 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
506 /*
507 * Remove the node now but someone else can add it back,
508 * we will to add it back at the end of reclaim to its correct
509 * position in the tree.
510 */
511 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
512 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
513 !css_tryget(&mz->mem->css))
514 goto retry;
515done:
516 return mz;
517}
518
519static struct mem_cgroup_per_zone *
520mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
521{
522 struct mem_cgroup_per_zone *mz;
523
524 spin_lock(&mctz->lock);
525 mz = __mem_cgroup_largest_soft_limit_node(mctz);
526 spin_unlock(&mctz->lock);
527 return mz;
528}
529
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800530static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
531 enum mem_cgroup_stat_index idx)
532{
533 int cpu;
534 s64 val = 0;
535
536 for_each_possible_cpu(cpu)
537 val += per_cpu(mem->stat->count[idx], cpu);
538 return val;
539}
540
541static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
542{
543 s64 ret;
544
545 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
546 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
547 return ret;
548}
549
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700550static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
551 bool charge)
552{
553 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800554 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700555}
556
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700557static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
558 struct page_cgroup *pc,
559 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800560{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700561 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800562
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800563 preempt_disable();
564
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700565 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800566 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800567 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800568 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700569
570 if (charge)
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800571 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
Balaji Rao55e462b2008-05-01 04:35:12 -0700572 else
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800573 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800574 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800575
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -0800576 preempt_enable();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800577}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800578
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700579static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700580 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800581{
582 int nid, zid;
583 struct mem_cgroup_per_zone *mz;
584 u64 total = 0;
585
586 for_each_online_node(nid)
587 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
588 mz = mem_cgroup_zoneinfo(mem, nid, zid);
589 total += MEM_CGROUP_ZSTAT(mz, idx);
590 }
591 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800592}
593
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -0800594static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
595{
596 s64 val;
597
598 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
599
600 return !(val & ((1 << event_mask_shift) - 1));
601}
602
603/*
604 * Check events in order.
605 *
606 */
607static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
608{
609 /* threshold event is triggered in finer grain than soft limit */
610 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
611 mem_cgroup_threshold(mem);
612 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
613 mem_cgroup_update_tree(mem, page);
614 }
615}
616
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800617static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800618{
619 return container_of(cgroup_subsys_state(cont,
620 mem_cgroup_subsys_id), struct mem_cgroup,
621 css);
622}
623
Balbir Singhcf475ad2008-04-29 01:00:16 -0700624struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800625{
Balbir Singh31a78f22008-09-28 23:09:31 +0100626 /*
627 * mm_update_next_owner() may clear mm->owner to NULL
628 * if it races with swapoff, page migration, etc.
629 * So this can be called with p == NULL.
630 */
631 if (unlikely(!p))
632 return NULL;
633
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800634 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
635 struct mem_cgroup, css);
636}
637
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800638static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
639{
640 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700641
642 if (!mm)
643 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800644 /*
645 * Because we have no locks, mm->owner's may be being moved to other
646 * cgroup. We use css_tryget() here even if this looks
647 * pessimistic (rather than adding locks here).
648 */
649 rcu_read_lock();
650 do {
651 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
652 if (unlikely(!mem))
653 break;
654 } while (!css_tryget(&mem->css));
655 rcu_read_unlock();
656 return mem;
657}
658
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700659/*
660 * Call callback function against all cgroup under hierarchy tree.
661 */
662static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
663 int (*func)(struct mem_cgroup *, void *))
664{
665 int found, ret, nextid;
666 struct cgroup_subsys_state *css;
667 struct mem_cgroup *mem;
668
669 if (!root->use_hierarchy)
670 return (*func)(root, data);
671
672 nextid = 1;
673 do {
674 ret = 0;
675 mem = NULL;
676
677 rcu_read_lock();
678 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
679 &found);
680 if (css && css_tryget(css))
681 mem = container_of(css, struct mem_cgroup, css);
682 rcu_read_unlock();
683
684 if (mem) {
685 ret = (*func)(mem, data);
686 css_put(&mem->css);
687 }
688 nextid = found + 1;
689 } while (!ret && css);
690
691 return ret;
692}
693
Balbir Singh4b3bde42009-09-23 15:56:32 -0700694static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
695{
696 return (mem == root_mem_cgroup);
697}
698
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800699/*
700 * Following LRU functions are allowed to be used without PCG_LOCK.
701 * Operations are called by routine of global LRU independently from memcg.
702 * What we have to take care of here is validness of pc->mem_cgroup.
703 *
704 * Changes to pc->mem_cgroup happens when
705 * 1. charge
706 * 2. moving account
707 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
708 * It is added to LRU before charge.
709 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
710 * When moving account, the page is not on LRU. It's isolated.
711 */
712
713void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800714{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800715 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800716 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700717
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800718 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800719 return;
720 pc = lookup_page_cgroup(page);
721 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700722 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800723 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700724 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800725 /*
726 * We don't check PCG_USED bit. It's cleared when the "page" is finally
727 * removed from global LRU.
728 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800729 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700730 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700731 if (mem_cgroup_is_root(pc->mem_cgroup))
732 return;
733 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800734 list_del_init(&pc->lru);
735 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800736}
737
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800738void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800739{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800740 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800741}
742
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800743void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800744{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800745 struct mem_cgroup_per_zone *mz;
746 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800747
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800748 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700749 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700750
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800751 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800752 /*
753 * Used bit is set without atomic ops but after smp_wmb().
754 * For making pc->mem_cgroup visible, insert smp_rmb() here.
755 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800756 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700757 /* unused or root page is not rotated. */
758 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800759 return;
760 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700761 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800762}
763
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800764void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
765{
766 struct page_cgroup *pc;
767 struct mem_cgroup_per_zone *mz;
768
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800769 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800770 return;
771 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700772 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800773 /*
774 * Used bit is set without atomic ops but after smp_wmb().
775 * For making pc->mem_cgroup visible, insert smp_rmb() here.
776 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800777 smp_rmb();
778 if (!PageCgroupUsed(pc))
779 return;
780
781 mz = page_cgroup_zoneinfo(pc);
782 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700783 SetPageCgroupAcctLRU(pc);
784 if (mem_cgroup_is_root(pc->mem_cgroup))
785 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800786 list_add(&pc->lru, &mz->lists[lru]);
787}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800788
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800789/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800790 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
791 * lru because the page may.be reused after it's fully uncharged (because of
792 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
793 * it again. This function is only used to charge SwapCache. It's done under
794 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800795 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800796static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800797{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800798 unsigned long flags;
799 struct zone *zone = page_zone(page);
800 struct page_cgroup *pc = lookup_page_cgroup(page);
801
802 spin_lock_irqsave(&zone->lru_lock, flags);
803 /*
804 * Forget old LRU when this page_cgroup is *not* used. This Used bit
805 * is guarded by lock_page() because the page is SwapCache.
806 */
807 if (!PageCgroupUsed(pc))
808 mem_cgroup_del_lru_list(page, page_lru(page));
809 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800810}
811
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800812static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
813{
814 unsigned long flags;
815 struct zone *zone = page_zone(page);
816 struct page_cgroup *pc = lookup_page_cgroup(page);
817
818 spin_lock_irqsave(&zone->lru_lock, flags);
819 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700820 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800821 mem_cgroup_add_lru_list(page, page_lru(page));
822 spin_unlock_irqrestore(&zone->lru_lock, flags);
823}
824
825
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800826void mem_cgroup_move_lists(struct page *page,
827 enum lru_list from, enum lru_list to)
828{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800829 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800830 return;
831 mem_cgroup_del_lru_list(page, from);
832 mem_cgroup_add_lru_list(page, to);
833}
834
David Rientjes4c4a2212008-02-07 00:14:06 -0800835int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
836{
837 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700838 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800839
840 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700841 rcu_read_lock();
842 curr = try_get_mem_cgroup_from_mm(task->mm);
843 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800844 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700845 if (!curr)
846 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800847 /*
848 * We should check use_hierarchy of "mem" not "curr". Because checking
849 * use_hierarchy of "curr" here make this function true if hierarchy is
850 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
851 * hierarchy(even if use_hierarchy is disabled in "mem").
852 */
853 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700854 ret = css_is_ancestor(&curr->css, &mem->css);
855 else
856 ret = (curr == mem);
857 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800858 return ret;
859}
860
Balbir Singh66e17072008-02-07 00:13:56 -0800861/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800862 * prev_priority control...this will be used in memory reclaim path.
863 */
864int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
865{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800866 int prev_priority;
867
868 spin_lock(&mem->reclaim_param_lock);
869 prev_priority = mem->prev_priority;
870 spin_unlock(&mem->reclaim_param_lock);
871
872 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800873}
874
875void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
876{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800877 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800878 if (priority < mem->prev_priority)
879 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800880 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800881}
882
883void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
884{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800885 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800886 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800887 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800888}
889
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800890static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800891{
892 unsigned long active;
893 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800894 unsigned long gb;
895 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800896
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700897 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
898 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800899
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800900 gb = (inactive + active) >> (30 - PAGE_SHIFT);
901 if (gb)
902 inactive_ratio = int_sqrt(10 * gb);
903 else
904 inactive_ratio = 1;
905
906 if (present_pages) {
907 present_pages[0] = inactive;
908 present_pages[1] = active;
909 }
910
911 return inactive_ratio;
912}
913
914int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
915{
916 unsigned long active;
917 unsigned long inactive;
918 unsigned long present_pages[2];
919 unsigned long inactive_ratio;
920
921 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
922
923 inactive = present_pages[0];
924 active = present_pages[1];
925
926 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800927 return 1;
928
929 return 0;
930}
931
Rik van Riel56e49d22009-06-16 15:32:28 -0700932int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
933{
934 unsigned long active;
935 unsigned long inactive;
936
937 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
938 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
939
940 return (active > inactive);
941}
942
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800943unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
944 struct zone *zone,
945 enum lru_list lru)
946{
947 int nid = zone->zone_pgdat->node_id;
948 int zid = zone_idx(zone);
949 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
950
951 return MEM_CGROUP_ZSTAT(mz, lru);
952}
953
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800954struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
955 struct zone *zone)
956{
957 int nid = zone->zone_pgdat->node_id;
958 int zid = zone_idx(zone);
959 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
960
961 return &mz->reclaim_stat;
962}
963
964struct zone_reclaim_stat *
965mem_cgroup_get_reclaim_stat_from_page(struct page *page)
966{
967 struct page_cgroup *pc;
968 struct mem_cgroup_per_zone *mz;
969
970 if (mem_cgroup_disabled())
971 return NULL;
972
973 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800974 /*
975 * Used bit is set without atomic ops but after smp_wmb().
976 * For making pc->mem_cgroup visible, insert smp_rmb() here.
977 */
978 smp_rmb();
979 if (!PageCgroupUsed(pc))
980 return NULL;
981
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800982 mz = page_cgroup_zoneinfo(pc);
983 if (!mz)
984 return NULL;
985
986 return &mz->reclaim_stat;
987}
988
Balbir Singh66e17072008-02-07 00:13:56 -0800989unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
990 struct list_head *dst,
991 unsigned long *scanned, int order,
992 int mode, struct zone *z,
993 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700994 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800995{
996 unsigned long nr_taken = 0;
997 struct page *page;
998 unsigned long scan;
999 LIST_HEAD(pc_list);
1000 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001001 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001002 int nid = z->zone_pgdat->node_id;
1003 int zid = zone_idx(z);
1004 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -07001005 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001006 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -08001007
Balbir Singhcf475ad2008-04-29 01:00:16 -07001008 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001009 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001010 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001011
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001012 scan = 0;
1013 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001014 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001015 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001016
1017 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001018 if (unlikely(!PageCgroupUsed(pc)))
1019 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -08001020 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001021 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001022
Hugh Dickins436c65412008-02-07 00:14:12 -08001023 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001024 ret = __isolate_lru_page(page, mode, file);
1025 switch (ret) {
1026 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001027 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001028 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -08001029 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001030 break;
1031 case -EBUSY:
1032 /* we don't affect global LRU but rotate in our LRU */
1033 mem_cgroup_rotate_lru_list(page, page_lru(page));
1034 break;
1035 default:
1036 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001037 }
1038 }
1039
Balbir Singh66e17072008-02-07 00:13:56 -08001040 *scanned = scan;
1041 return nr_taken;
1042}
1043
Balbir Singh6d61ef42009-01-07 18:08:06 -08001044#define mem_cgroup_from_res_counter(counter, member) \
1045 container_of(counter, struct mem_cgroup, member)
1046
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001047static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1048{
1049 if (do_swap_account) {
1050 if (res_counter_check_under_limit(&mem->res) &&
1051 res_counter_check_under_limit(&mem->memsw))
1052 return true;
1053 } else
1054 if (res_counter_check_under_limit(&mem->res))
1055 return true;
1056 return false;
1057}
1058
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001059static unsigned int get_swappiness(struct mem_cgroup *memcg)
1060{
1061 struct cgroup *cgrp = memcg->css.cgroup;
1062 unsigned int swappiness;
1063
1064 /* root ? */
1065 if (cgrp->parent == NULL)
1066 return vm_swappiness;
1067
1068 spin_lock(&memcg->reclaim_param_lock);
1069 swappiness = memcg->swappiness;
1070 spin_unlock(&memcg->reclaim_param_lock);
1071
1072 return swappiness;
1073}
1074
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001075static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1076{
1077 int *val = data;
1078 (*val)++;
1079 return 0;
1080}
Balbir Singhe2224322009-04-02 16:57:39 -07001081
1082/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001083 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001084 * @memcg: The memory cgroup that went over limit
1085 * @p: Task that is going to be killed
1086 *
1087 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1088 * enabled
1089 */
1090void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1091{
1092 struct cgroup *task_cgrp;
1093 struct cgroup *mem_cgrp;
1094 /*
1095 * Need a buffer in BSS, can't rely on allocations. The code relies
1096 * on the assumption that OOM is serialized for memory controller.
1097 * If this assumption is broken, revisit this code.
1098 */
1099 static char memcg_name[PATH_MAX];
1100 int ret;
1101
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001102 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001103 return;
1104
1105
1106 rcu_read_lock();
1107
1108 mem_cgrp = memcg->css.cgroup;
1109 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1110
1111 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1112 if (ret < 0) {
1113 /*
1114 * Unfortunately, we are unable to convert to a useful name
1115 * But we'll still print out the usage information
1116 */
1117 rcu_read_unlock();
1118 goto done;
1119 }
1120 rcu_read_unlock();
1121
1122 printk(KERN_INFO "Task in %s killed", memcg_name);
1123
1124 rcu_read_lock();
1125 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1126 if (ret < 0) {
1127 rcu_read_unlock();
1128 goto done;
1129 }
1130 rcu_read_unlock();
1131
1132 /*
1133 * Continues from above, so we don't need an KERN_ level
1134 */
1135 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1136done:
1137
1138 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1139 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1140 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1141 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1142 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1143 "failcnt %llu\n",
1144 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1145 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1146 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1147}
1148
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001149/*
1150 * This function returns the number of memcg under hierarchy tree. Returns
1151 * 1(self count) if no children.
1152 */
1153static int mem_cgroup_count_children(struct mem_cgroup *mem)
1154{
1155 int num = 0;
1156 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1157 return num;
1158}
1159
Balbir Singh6d61ef42009-01-07 18:08:06 -08001160/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001161 * Visit the first child (need not be the first child as per the ordering
1162 * of the cgroup list, since we track last_scanned_child) of @mem and use
1163 * that to reclaim free pages from.
1164 */
1165static struct mem_cgroup *
1166mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1167{
1168 struct mem_cgroup *ret = NULL;
1169 struct cgroup_subsys_state *css;
1170 int nextid, found;
1171
1172 if (!root_mem->use_hierarchy) {
1173 css_get(&root_mem->css);
1174 ret = root_mem;
1175 }
1176
1177 while (!ret) {
1178 rcu_read_lock();
1179 nextid = root_mem->last_scanned_child + 1;
1180 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1181 &found);
1182 if (css && css_tryget(css))
1183 ret = container_of(css, struct mem_cgroup, css);
1184
1185 rcu_read_unlock();
1186 /* Updates scanning parameter */
1187 spin_lock(&root_mem->reclaim_param_lock);
1188 if (!css) {
1189 /* this means start scan from ID:1 */
1190 root_mem->last_scanned_child = 0;
1191 } else
1192 root_mem->last_scanned_child = found;
1193 spin_unlock(&root_mem->reclaim_param_lock);
1194 }
1195
1196 return ret;
1197}
1198
1199/*
1200 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1201 * we reclaimed from, so that we don't end up penalizing one child extensively
1202 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001203 *
1204 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001205 *
1206 * We give up and return to the caller when we visit root_mem twice.
1207 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001208 *
1209 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001210 */
1211static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001212 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001213 gfp_t gfp_mask,
1214 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001215{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001216 struct mem_cgroup *victim;
1217 int ret, total = 0;
1218 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001219 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1220 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001221 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1222 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001223
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001224 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1225 if (root_mem->memsw_is_minimum)
1226 noswap = true;
1227
Balbir Singh4e416952009-09-23 15:56:39 -07001228 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001229 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001230 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001231 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001232 if (loop >= 1)
1233 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001234 if (loop >= 2) {
1235 /*
1236 * If we have not been able to reclaim
1237 * anything, it might because there are
1238 * no reclaimable pages under this hierarchy
1239 */
1240 if (!check_soft || !total) {
1241 css_put(&victim->css);
1242 break;
1243 }
1244 /*
1245 * We want to do more targetted reclaim.
1246 * excess >> 2 is not to excessive so as to
1247 * reclaim too much, nor too less that we keep
1248 * coming back to reclaim from this cgroup
1249 */
1250 if (total >= (excess >> 2) ||
1251 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1252 css_put(&victim->css);
1253 break;
1254 }
1255 }
1256 }
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001257 if (!mem_cgroup_local_usage(victim)) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001258 /* this cgroup's local usage == 0 */
1259 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001260 continue;
1261 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001262 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001263 if (check_soft)
1264 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1265 noswap, get_swappiness(victim), zone,
1266 zone->zone_pgdat->node_id);
1267 else
1268 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1269 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001270 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001271 /*
1272 * At shrinking usage, we can't check we should stop here or
1273 * reclaim more. It's depends on callers. last_scanned_child
1274 * will work enough for keeping fairness under tree.
1275 */
1276 if (shrink)
1277 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001278 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001279 if (check_soft) {
1280 if (res_counter_check_under_soft_limit(&root_mem->res))
1281 return total;
1282 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001283 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001284 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001285 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001286}
1287
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001288static int mem_cgroup_oom_lock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001289{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001290 int *val = (int *)data;
1291 int x;
1292 /*
1293 * Logically, we can stop scanning immediately when we find
1294 * a memcg is already locked. But condidering unlock ops and
1295 * creation/removal of memcg, scan-all is simple operation.
1296 */
1297 x = atomic_inc_return(&mem->oom_lock);
1298 *val = max(x, *val);
1299 return 0;
1300}
1301/*
1302 * Check OOM-Killer is already running under our hierarchy.
1303 * If someone is running, return false.
1304 */
1305static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1306{
1307 int lock_count = 0;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001308
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001309 mem_cgroup_walk_tree(mem, &lock_count, mem_cgroup_oom_lock_cb);
1310
1311 if (lock_count == 1)
1312 return true;
1313 return false;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001314}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001315
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001316static int mem_cgroup_oom_unlock_cb(struct mem_cgroup *mem, void *data)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001317{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001318 /*
1319 * When a new child is created while the hierarchy is under oom,
1320 * mem_cgroup_oom_lock() may not be called. We have to use
1321 * atomic_add_unless() here.
1322 */
1323 atomic_add_unless(&mem->oom_lock, -1, 0);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001324 return 0;
1325}
1326
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001327static void mem_cgroup_oom_unlock(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001328{
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001329 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_unlock_cb);
1330}
1331
1332static DEFINE_MUTEX(memcg_oom_mutex);
1333static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1334
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001335struct oom_wait_info {
1336 struct mem_cgroup *mem;
1337 wait_queue_t wait;
1338};
1339
1340static int memcg_oom_wake_function(wait_queue_t *wait,
1341 unsigned mode, int sync, void *arg)
1342{
1343 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1344 struct oom_wait_info *oom_wait_info;
1345
1346 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1347
1348 if (oom_wait_info->mem == wake_mem)
1349 goto wakeup;
1350 /* if no hierarchy, no match */
1351 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1352 return 0;
1353 /*
1354 * Both of oom_wait_info->mem and wake_mem are stable under us.
1355 * Then we can use css_is_ancestor without taking care of RCU.
1356 */
1357 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1358 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1359 return 0;
1360
1361wakeup:
1362 return autoremove_wake_function(wait, mode, sync, arg);
1363}
1364
1365static void memcg_wakeup_oom(struct mem_cgroup *mem)
1366{
1367 /* for filtering, pass "mem" as argument. */
1368 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1369}
1370
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001371static void memcg_oom_recover(struct mem_cgroup *mem)
1372{
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07001373 if (atomic_read(&mem->oom_lock))
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001374 memcg_wakeup_oom(mem);
1375}
1376
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001377/*
1378 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1379 */
1380bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1381{
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001382 struct oom_wait_info owait;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001383 bool locked, need_to_kill;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001384
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001385 owait.mem = mem;
1386 owait.wait.flags = 0;
1387 owait.wait.func = memcg_oom_wake_function;
1388 owait.wait.private = current;
1389 INIT_LIST_HEAD(&owait.wait.task_list);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001390 need_to_kill = true;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001391 /* At first, try to OOM lock hierarchy under mem.*/
1392 mutex_lock(&memcg_oom_mutex);
1393 locked = mem_cgroup_oom_lock(mem);
1394 /*
1395 * Even if signal_pending(), we can't quit charge() loop without
1396 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1397 * under OOM is always welcomed, use TASK_KILLABLE here.
1398 */
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001399 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1400 if (!locked || mem->oom_kill_disable)
1401 need_to_kill = false;
1402 if (locked)
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07001403 mem_cgroup_oom_notify(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001404 mutex_unlock(&memcg_oom_mutex);
1405
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001406 if (need_to_kill) {
1407 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001408 mem_cgroup_out_of_memory(mem, mask);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07001409 } else {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001410 schedule();
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001411 finish_wait(&memcg_oom_waitq, &owait.wait);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001412 }
1413 mutex_lock(&memcg_oom_mutex);
1414 mem_cgroup_oom_unlock(mem);
KAMEZAWA Hiroyukidc98df52010-05-26 14:42:36 -07001415 memcg_wakeup_oom(mem);
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001416 mutex_unlock(&memcg_oom_mutex);
1417
1418 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1419 return false;
1420 /* Give chance to dying process */
1421 schedule_timeout(1);
1422 return true;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001423}
1424
Balbir Singhd69b0422009-06-17 16:26:34 -07001425/*
1426 * Currently used to update mapped file statistics, but the routine can be
1427 * generalized to update other statistics as well.
1428 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001429void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001430{
1431 struct mem_cgroup *mem;
Balbir Singhd69b0422009-06-17 16:26:34 -07001432 struct page_cgroup *pc;
1433
Balbir Singhd69b0422009-06-17 16:26:34 -07001434 pc = lookup_page_cgroup(page);
1435 if (unlikely(!pc))
1436 return;
1437
1438 lock_page_cgroup(pc);
1439 mem = pc->mem_cgroup;
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001440 if (!mem || !PageCgroupUsed(pc))
Balbir Singhd69b0422009-06-17 16:26:34 -07001441 goto done;
1442
1443 /*
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001444 * Preemption is already disabled. We can use __this_cpu_xxx
Balbir Singhd69b0422009-06-17 16:26:34 -07001445 */
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001446 if (val > 0) {
1447 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1448 SetPageCgroupFileMapped(pc);
1449 } else {
1450 __this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1451 ClearPageCgroupFileMapped(pc);
1452 }
Balbir Singhd69b0422009-06-17 16:26:34 -07001453
Balbir Singhd69b0422009-06-17 16:26:34 -07001454done:
1455 unlock_page_cgroup(pc);
1456}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001457
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001458/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001459 * size of first charge trial. "32" comes from vmscan.c's magic value.
1460 * TODO: maybe necessary to use big numbers in big irons.
1461 */
1462#define CHARGE_SIZE (32 * PAGE_SIZE)
1463struct memcg_stock_pcp {
1464 struct mem_cgroup *cached; /* this never be root cgroup */
1465 int charge;
1466 struct work_struct work;
1467};
1468static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1469static atomic_t memcg_drain_count;
1470
1471/*
1472 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1473 * from local stock and true is returned. If the stock is 0 or charges from a
1474 * cgroup which is not current target, returns false. This stock will be
1475 * refilled.
1476 */
1477static bool consume_stock(struct mem_cgroup *mem)
1478{
1479 struct memcg_stock_pcp *stock;
1480 bool ret = true;
1481
1482 stock = &get_cpu_var(memcg_stock);
1483 if (mem == stock->cached && stock->charge)
1484 stock->charge -= PAGE_SIZE;
1485 else /* need to call res_counter_charge */
1486 ret = false;
1487 put_cpu_var(memcg_stock);
1488 return ret;
1489}
1490
1491/*
1492 * Returns stocks cached in percpu to res_counter and reset cached information.
1493 */
1494static void drain_stock(struct memcg_stock_pcp *stock)
1495{
1496 struct mem_cgroup *old = stock->cached;
1497
1498 if (stock->charge) {
1499 res_counter_uncharge(&old->res, stock->charge);
1500 if (do_swap_account)
1501 res_counter_uncharge(&old->memsw, stock->charge);
1502 }
1503 stock->cached = NULL;
1504 stock->charge = 0;
1505}
1506
1507/*
1508 * This must be called under preempt disabled or must be called by
1509 * a thread which is pinned to local cpu.
1510 */
1511static void drain_local_stock(struct work_struct *dummy)
1512{
1513 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1514 drain_stock(stock);
1515}
1516
1517/*
1518 * Cache charges(val) which is from res_counter, to local per_cpu area.
Greg Thelen320cc512010-03-15 15:27:28 +01001519 * This will be consumed by consume_stock() function, later.
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001520 */
1521static void refill_stock(struct mem_cgroup *mem, int val)
1522{
1523 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1524
1525 if (stock->cached != mem) { /* reset if necessary */
1526 drain_stock(stock);
1527 stock->cached = mem;
1528 }
1529 stock->charge += val;
1530 put_cpu_var(memcg_stock);
1531}
1532
1533/*
1534 * Tries to drain stocked charges in other cpus. This function is asynchronous
1535 * and just put a work per cpu for draining localy on each cpu. Caller can
1536 * expects some charges will be back to res_counter later but cannot wait for
1537 * it.
1538 */
1539static void drain_all_stock_async(void)
1540{
1541 int cpu;
1542 /* This function is for scheduling "drain" in asynchronous way.
1543 * The result of "drain" is not directly handled by callers. Then,
1544 * if someone is calling drain, we don't have to call drain more.
1545 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1546 * there is a race. We just do loose check here.
1547 */
1548 if (atomic_read(&memcg_drain_count))
1549 return;
1550 /* Notify other cpus that system-wide "drain" is running */
1551 atomic_inc(&memcg_drain_count);
1552 get_online_cpus();
1553 for_each_online_cpu(cpu) {
1554 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1555 schedule_work_on(cpu, &stock->work);
1556 }
1557 put_online_cpus();
1558 atomic_dec(&memcg_drain_count);
1559 /* We don't wait for flush_work */
1560}
1561
1562/* This is a synchronous drain interface. */
1563static void drain_all_stock_sync(void)
1564{
1565 /* called when force_empty is called */
1566 atomic_inc(&memcg_drain_count);
1567 schedule_on_each_cpu(drain_local_stock);
1568 atomic_dec(&memcg_drain_count);
1569}
1570
1571static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1572 unsigned long action,
1573 void *hcpu)
1574{
1575 int cpu = (unsigned long)hcpu;
1576 struct memcg_stock_pcp *stock;
1577
1578 if (action != CPU_DEAD)
1579 return NOTIFY_OK;
1580 stock = &per_cpu(memcg_stock, cpu);
1581 drain_stock(stock);
1582 return NOTIFY_OK;
1583}
1584
1585/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001586 * Unlike exported interface, "oom" parameter is added. if oom==true,
1587 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001588 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001589static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001590 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001591{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001592 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001593 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001594 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001595 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001596
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001597 /*
1598 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1599 * in system level. So, allow to go ahead dying process in addition to
1600 * MEMDIE process.
1601 */
1602 if (unlikely(test_thread_flag(TIF_MEMDIE)
1603 || fatal_signal_pending(current)))
1604 goto bypass;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001605
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001606 /*
Hugh Dickins3be91272008-02-07 00:14:19 -08001607 * We always charge the cgroup the mm_struct belongs to.
1608 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001609 * thread group leader migrates. It's possible that mm is not
1610 * set, if so charge the init_mm (happens for pagecache usage).
1611 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001612 mem = *memcg;
1613 if (likely(!mem)) {
1614 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001615 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001616 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001617 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001618 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001619 if (unlikely(!mem))
1620 return 0;
1621
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001622 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001623 if (mem_cgroup_is_root(mem))
1624 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001625
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001626 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001627 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001628 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001629
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001630 if (consume_stock(mem))
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001631 goto done;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001632
1633 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001634 if (likely(!ret)) {
1635 if (!do_swap_account)
1636 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001637 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001638 if (likely(!ret))
1639 break;
1640 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001641 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001642 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001643 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1644 memsw);
1645 } else
1646 /* mem counter fails */
1647 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1648 res);
1649
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001650 /* reduce request size and retry */
1651 if (csize > PAGE_SIZE) {
1652 csize = PAGE_SIZE;
1653 continue;
1654 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001655 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001656 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001657
Balbir Singh4e416952009-09-23 15:56:39 -07001658 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1659 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001660 if (ret)
1661 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001662
1663 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001664 * try_to_free_mem_cgroup_pages() might not give us a full
1665 * picture of reclaim. Some pages are reclaimed and might be
1666 * moved to swap cache or just unmapped from the cgroup.
1667 * Check the limit again to see if the reclaim reduced the
1668 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001669 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001670 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001671 if (mem_cgroup_check_under_limit(mem_over_limit))
1672 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -08001673
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001674 /* try to avoid oom while someone is moving charge */
1675 if (mc.moving_task && current != mc.moving_task) {
1676 struct mem_cgroup *from, *to;
1677 bool do_continue = false;
1678 /*
1679 * There is a small race that "from" or "to" can be
1680 * freed by rmdir, so we use css_tryget().
1681 */
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001682 from = mc.from;
1683 to = mc.to;
1684 if (from && css_tryget(&from->css)) {
1685 if (mem_over_limit->use_hierarchy)
1686 do_continue = css_is_ancestor(
1687 &from->css,
1688 &mem_over_limit->css);
1689 else
1690 do_continue = (from == mem_over_limit);
1691 css_put(&from->css);
1692 }
1693 if (!do_continue && to && css_tryget(&to->css)) {
1694 if (mem_over_limit->use_hierarchy)
1695 do_continue = css_is_ancestor(
1696 &to->css,
1697 &mem_over_limit->css);
1698 else
1699 do_continue = (to == mem_over_limit);
1700 css_put(&to->css);
1701 }
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001702 if (do_continue) {
1703 DEFINE_WAIT(wait);
1704 prepare_to_wait(&mc.waitq, &wait,
1705 TASK_INTERRUPTIBLE);
1706 /* moving charge context might have finished. */
1707 if (mc.moving_task)
1708 schedule();
1709 finish_wait(&mc.waitq, &wait);
1710 continue;
1711 }
1712 }
1713
Hugh Dickins3be91272008-02-07 00:14:19 -08001714 if (!nr_retries--) {
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001715 if (!oom)
1716 goto nomem;
1717 if (mem_cgroup_handle_oom(mem_over_limit, gfp_mask)) {
1718 nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1719 continue;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001720 }
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001721 /* When we reach here, current task is dying .*/
1722 css_put(&mem->css);
1723 goto bypass;
Balbir Singh66e17072008-02-07 00:13:56 -08001724 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001725 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001726 if (csize > PAGE_SIZE)
1727 refill_stock(mem, csize - PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001728done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001729 return 0;
1730nomem:
1731 css_put(&mem->css);
1732 return -ENOMEM;
KAMEZAWA Hiroyuki867578c2010-03-10 15:22:39 -08001733bypass:
1734 *memcg = NULL;
1735 return 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001736}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001737
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001738/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001739 * Somemtimes we have to undo a charge we got by try_charge().
1740 * This function is for that and do uncharge, put css's refcnt.
1741 * gotten by try_charge().
1742 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001743static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1744 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001745{
1746 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001747 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001748 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001749 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1750 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
1751 WARN_ON_ONCE(count > INT_MAX);
1752 __css_put(&mem->css, (int)count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001753 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001754 /* we don't need css_put for root */
1755}
1756
1757static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1758{
1759 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001760}
1761
1762/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001763 * A helper function to get mem_cgroup from ID. must be called under
1764 * rcu_read_lock(). The caller must check css_is_removed() or some if
1765 * it's concern. (dropping refcnt from swap can be called against removed
1766 * memcg.)
1767 */
1768static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1769{
1770 struct cgroup_subsys_state *css;
1771
1772 /* ID 0 is unused ID */
1773 if (!id)
1774 return NULL;
1775 css = css_lookup(&mem_cgroup_subsys, id);
1776 if (!css)
1777 return NULL;
1778 return container_of(css, struct mem_cgroup, css);
1779}
1780
Wu Fengguange42d9d52009-12-16 12:19:59 +01001781struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001782{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001783 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001784 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001785 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001786 swp_entry_t ent;
1787
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001788 VM_BUG_ON(!PageLocked(page));
1789
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001790 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001791 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001792 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001793 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001794 if (mem && !css_tryget(&mem->css))
1795 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001796 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001797 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001798 id = lookup_swap_cgroup(ent);
1799 rcu_read_lock();
1800 mem = mem_cgroup_lookup(id);
1801 if (mem && !css_tryget(&mem->css))
1802 mem = NULL;
1803 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001804 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001805 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001806 return mem;
1807}
1808
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001809/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001810 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001811 * USED state. If already USED, uncharge and return.
1812 */
1813
1814static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1815 struct page_cgroup *pc,
1816 enum charge_type ctype)
1817{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001818 /* try_charge() can return NULL to *memcg, taking care of it. */
1819 if (!mem)
1820 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001821
1822 lock_page_cgroup(pc);
1823 if (unlikely(PageCgroupUsed(pc))) {
1824 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001825 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001826 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001827 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001828
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001829 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001830 /*
1831 * We access a page_cgroup asynchronously without lock_page_cgroup().
1832 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1833 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1834 * before USED bit, we need memory barrier here.
1835 * See mem_cgroup_add_lru_list(), etc.
1836 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001837 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001838 switch (ctype) {
1839 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1840 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1841 SetPageCgroupCache(pc);
1842 SetPageCgroupUsed(pc);
1843 break;
1844 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1845 ClearPageCgroupCache(pc);
1846 SetPageCgroupUsed(pc);
1847 break;
1848 default:
1849 break;
1850 }
Hugh Dickins3be91272008-02-07 00:14:19 -08001851
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001852 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001853
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001854 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001855 /*
1856 * "charge_statistics" updated event counter. Then, check it.
1857 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1858 * if they exceeds softlimit.
1859 */
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001860 memcg_check_events(mem, pc->page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001861}
1862
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001863/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001864 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001865 * @pc: page_cgroup of the page.
1866 * @from: mem_cgroup which the page is moved from.
1867 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001868 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001869 *
1870 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001871 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001872 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001873 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001874 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1875 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1876 * true, this function does "uncharge" from old cgroup, but it doesn't if
1877 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001878 */
1879
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001880static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001881 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001882{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001883 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001884 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001885 VM_BUG_ON(!PageCgroupLocked(pc));
1886 VM_BUG_ON(!PageCgroupUsed(pc));
1887 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001888
KAMEZAWA Hiroyuki8725d542010-04-06 14:35:05 -07001889 if (PageCgroupFileMapped(pc)) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08001890 /* Update mapped_file data for mem_cgroup */
1891 preempt_disable();
1892 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1893 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1894 preempt_enable();
Balbir Singhd69b0422009-06-17 16:26:34 -07001895 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001896 mem_cgroup_charge_statistics(from, pc, false);
1897 if (uncharge)
1898 /* This is not "cancel", but cancel_charge does all we need. */
1899 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07001900
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001901 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001902 pc->mem_cgroup = to;
1903 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001904 /*
1905 * We charges against "to" which may not have any tasks. Then, "to"
1906 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001907 * this function is just force_empty() and move charge, so it's
1908 * garanteed that "to" is never removed. So, we don't check rmdir
1909 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001910 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001911}
1912
1913/*
1914 * check whether the @pc is valid for moving account and call
1915 * __mem_cgroup_move_account()
1916 */
1917static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001918 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001919{
1920 int ret = -EINVAL;
1921 lock_page_cgroup(pc);
1922 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001923 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001924 ret = 0;
1925 }
1926 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08001927 /*
1928 * check events
1929 */
1930 memcg_check_events(to, pc->page);
1931 memcg_check_events(from, pc->page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001932 return ret;
1933}
1934
1935/*
1936 * move charges to its parent.
1937 */
1938
1939static int mem_cgroup_move_parent(struct page_cgroup *pc,
1940 struct mem_cgroup *child,
1941 gfp_t gfp_mask)
1942{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001943 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001944 struct cgroup *cg = child->css.cgroup;
1945 struct cgroup *pcg = cg->parent;
1946 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001947 int ret;
1948
1949 /* Is ROOT ? */
1950 if (!pcg)
1951 return -EINVAL;
1952
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001953 ret = -EBUSY;
1954 if (!get_page_unless_zero(page))
1955 goto out;
1956 if (isolate_lru_page(page))
1957 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001958
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001959 parent = mem_cgroup_from_cont(pcg);
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001960 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001961 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001962 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001963
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001964 ret = mem_cgroup_move_account(pc, child, parent, true);
1965 if (ret)
1966 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001967put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001968 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001969put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001970 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001971out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001972 return ret;
1973}
1974
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001975/*
1976 * Charge the memory controller for page usage.
1977 * Return
1978 * 0 if the charge was successful
1979 * < 0 if the cgroup is over its limit
1980 */
1981static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1982 gfp_t gfp_mask, enum charge_type ctype,
1983 struct mem_cgroup *memcg)
1984{
1985 struct mem_cgroup *mem;
1986 struct page_cgroup *pc;
1987 int ret;
1988
1989 pc = lookup_page_cgroup(page);
1990 /* can happen at boot */
1991 if (unlikely(!pc))
1992 return 0;
1993 prefetchw(pc);
1994
1995 mem = memcg;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08001996 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001997 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001998 return ret;
1999
2000 __mem_cgroup_commit_charge(mem, pc, ctype);
2001 return 0;
2002}
2003
2004int mem_cgroup_newpage_charge(struct page *page,
2005 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002006{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002007 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002008 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002009 if (PageCompound(page))
2010 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002011 /*
2012 * If already mapped, we don't have to account.
2013 * If page cache, page->mapping has address_space.
2014 * But page->mapping may have out-of-use anon_vma pointer,
2015 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2016 * is NULL.
2017 */
2018 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2019 return 0;
2020 if (unlikely(!mm))
2021 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002022 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002023 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08002024}
2025
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002026static void
2027__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2028 enum charge_type ctype);
2029
Balbir Singhe1a1cd52008-02-07 00:14:02 -08002030int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2031 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08002032{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002033 struct mem_cgroup *mem = NULL;
2034 int ret;
2035
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002036 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002037 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002038 if (PageCompound(page))
2039 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002040 /*
2041 * Corner case handling. This is called from add_to_page_cache()
2042 * in usual. But some FS (shmem) precharges this page before calling it
2043 * and call add_to_page_cache() with GFP_NOWAIT.
2044 *
2045 * For GFP_NOWAIT case, the page may be pre-charged before calling
2046 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2047 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002048 * And when the page is SwapCache, it should take swap information
2049 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002050 */
2051 if (!(gfp_mask & __GFP_WAIT)) {
2052 struct page_cgroup *pc;
2053
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002054
2055 pc = lookup_page_cgroup(page);
2056 if (!pc)
2057 return 0;
2058 lock_page_cgroup(pc);
2059 if (PageCgroupUsed(pc)) {
2060 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002061 return 0;
2062 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002063 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002064 }
2065
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002066 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08002067 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07002068
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07002069 if (page_is_file_cache(page))
2070 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002071 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002072
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002073 /* shmem */
2074 if (PageSwapCache(page)) {
2075 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2076 if (!ret)
2077 __mem_cgroup_commit_charge_swapin(page, mem,
2078 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2079 } else
2080 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
2081 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002082
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002083 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002084}
2085
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002086/*
2087 * While swap-in, try_charge -> commit or cancel, the page is locked.
2088 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02002089 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002090 * "commit()" or removed by "cancel()"
2091 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002092int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2093 struct page *page,
2094 gfp_t mask, struct mem_cgroup **ptr)
2095{
2096 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002097 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002098
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002099 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002100 return 0;
2101
2102 if (!do_swap_account)
2103 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002104 /*
2105 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08002106 * the pte, and even removed page from swap cache: in those cases
2107 * do_swap_page()'s pte_same() test will fail; but there's also a
2108 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002109 */
2110 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08002111 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002112 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002113 if (!mem)
2114 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002115 *ptr = mem;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002116 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002117 /* drop extra refcnt from tryget */
2118 css_put(&mem->css);
2119 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002120charge_cur_mm:
2121 if (unlikely(!mm))
2122 mm = &init_mm;
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08002123 return __mem_cgroup_try_charge(mm, mask, ptr, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002124}
2125
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002126static void
2127__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2128 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002129{
2130 struct page_cgroup *pc;
2131
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002132 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002133 return;
2134 if (!ptr)
2135 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002136 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002137 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002138 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002139 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002140 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002141 /*
2142 * Now swap is on-memory. This means this page may be
2143 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002144 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2145 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2146 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002147 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002148 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002149 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002150 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002151 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002152
2153 id = swap_cgroup_record(ent, 0);
2154 rcu_read_lock();
2155 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002156 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002157 /*
2158 * This recorded memcg can be obsolete one. So, avoid
2159 * calling css_tryget
2160 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002161 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002162 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002163 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002164 mem_cgroup_put(memcg);
2165 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002166 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002167 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002168 /*
2169 * At swapin, we may charge account against cgroup which has no tasks.
2170 * So, rmdir()->pre_destroy() can be called while we do this charge.
2171 * In that case, we need to call pre_destroy() again. check it here.
2172 */
2173 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002174}
2175
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002176void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2177{
2178 __mem_cgroup_commit_charge_swapin(page, ptr,
2179 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2180}
2181
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002182void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2183{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002184 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002185 return;
2186 if (!mem)
2187 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002188 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002189}
2190
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002191static void
2192__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2193{
2194 struct memcg_batch_info *batch = NULL;
2195 bool uncharge_memsw = true;
2196 /* If swapout, usage of swap doesn't decrease */
2197 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2198 uncharge_memsw = false;
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002199
2200 batch = &current->memcg_batch;
2201 /*
2202 * In usual, we do css_get() when we remember memcg pointer.
2203 * But in this case, we keep res->usage until end of a series of
2204 * uncharges. Then, it's ok to ignore memcg's refcnt.
2205 */
2206 if (!batch->memcg)
2207 batch->memcg = mem;
2208 /*
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002209 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2210 * In those cases, all pages freed continously can be expected to be in
2211 * the same cgroup and we have chance to coalesce uncharges.
2212 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2213 * because we want to do uncharge as soon as possible.
2214 */
2215
2216 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2217 goto direct_uncharge;
2218
2219 /*
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002220 * In typical case, batch->memcg == mem. This means we can
2221 * merge a series of uncharges to an uncharge of res_counter.
2222 * If not, we uncharge res_counter ony by one.
2223 */
2224 if (batch->memcg != mem)
2225 goto direct_uncharge;
2226 /* remember freed charge and uncharge it later */
2227 batch->bytes += PAGE_SIZE;
2228 if (uncharge_memsw)
2229 batch->memsw_bytes += PAGE_SIZE;
2230 return;
2231direct_uncharge:
2232 res_counter_uncharge(&mem->res, PAGE_SIZE);
2233 if (uncharge_memsw)
2234 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002235 if (unlikely(batch->memcg != mem))
2236 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002237 return;
2238}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002239
Balbir Singh8697d332008-02-07 00:13:59 -08002240/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002241 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002242 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002243static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002244__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002245{
Hugh Dickins82895462008-03-04 14:29:08 -08002246 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002247 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002248 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002249
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002250 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002251 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002252
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002253 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002254 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002255
Balbir Singh8697d332008-02-07 00:13:59 -08002256 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002257 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002258 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002259 pc = lookup_page_cgroup(page);
2260 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002261 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002262
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002263 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002264
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002265 mem = pc->mem_cgroup;
2266
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002267 if (!PageCgroupUsed(pc))
2268 goto unlock_out;
2269
2270 switch (ctype) {
2271 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002272 case MEM_CGROUP_CHARGE_TYPE_DROP:
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002273 /* See mem_cgroup_prepare_migration() */
2274 if (page_mapped(page) || PageCgroupMigration(pc))
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002275 goto unlock_out;
2276 break;
2277 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2278 if (!PageAnon(page)) { /* Shared memory */
2279 if (page->mapping && !page_is_file_cache(page))
2280 goto unlock_out;
2281 } else if (page_mapped(page)) /* Anon */
2282 goto unlock_out;
2283 break;
2284 default:
2285 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002286 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002287
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002288 if (!mem_cgroup_is_root(mem))
2289 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002290 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2291 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002292 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002293
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002294 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002295 /*
2296 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2297 * freed from LRU. This is safe because uncharged page is expected not
2298 * to be reused (freed soon). Exception is SwapCache, it's handled by
2299 * special functions.
2300 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002301
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002302 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002303 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002304
KAMEZAWA Hiroyukid2265e62010-03-10 15:22:31 -08002305 memcg_check_events(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002306 /* at swapout, this memcg will be accessed to record to swap */
2307 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2308 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002309
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002310 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002311
2312unlock_out:
2313 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002314 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002315}
2316
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002317void mem_cgroup_uncharge_page(struct page *page)
2318{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002319 /* early check. */
2320 if (page_mapped(page))
2321 return;
2322 if (page->mapping && !PageAnon(page))
2323 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002324 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2325}
2326
2327void mem_cgroup_uncharge_cache_page(struct page *page)
2328{
2329 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002330 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002331 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2332}
2333
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002334/*
2335 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2336 * In that cases, pages are freed continuously and we can expect pages
2337 * are in the same memcg. All these calls itself limits the number of
2338 * pages freed at once, then uncharge_start/end() is called properly.
2339 * This may be called prural(2) times in a context,
2340 */
2341
2342void mem_cgroup_uncharge_start(void)
2343{
2344 current->memcg_batch.do_batch++;
2345 /* We can do nest. */
2346 if (current->memcg_batch.do_batch == 1) {
2347 current->memcg_batch.memcg = NULL;
2348 current->memcg_batch.bytes = 0;
2349 current->memcg_batch.memsw_bytes = 0;
2350 }
2351}
2352
2353void mem_cgroup_uncharge_end(void)
2354{
2355 struct memcg_batch_info *batch = &current->memcg_batch;
2356
2357 if (!batch->do_batch)
2358 return;
2359
2360 batch->do_batch--;
2361 if (batch->do_batch) /* If stacked, do nothing. */
2362 return;
2363
2364 if (!batch->memcg)
2365 return;
2366 /*
2367 * This "batch->memcg" is valid without any css_get/put etc...
2368 * bacause we hide charges behind us.
2369 */
2370 if (batch->bytes)
2371 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2372 if (batch->memsw_bytes)
2373 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002374 memcg_oom_recover(batch->memcg);
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002375 /* forget this pointer (for sanity check) */
2376 batch->memcg = NULL;
2377}
2378
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002379#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002380/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002381 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002382 * memcg information is recorded to swap_cgroup of "ent"
2383 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002384void
2385mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002386{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002387 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002388 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002389
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002390 if (!swapout) /* this was a swap cache but the swap is unused ! */
2391 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2392
2393 memcg = __mem_cgroup_uncharge_common(page, ctype);
2394
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002395 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002396 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002397 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002398 mem_cgroup_get(memcg);
2399 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002400 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002401 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002402}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002403#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002404
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002405#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2406/*
2407 * called from swap_entry_free(). remove record in swap_cgroup and
2408 * uncharge "memsw" account.
2409 */
2410void mem_cgroup_uncharge_swap(swp_entry_t ent)
2411{
2412 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002413 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002414
2415 if (!do_swap_account)
2416 return;
2417
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002418 id = swap_cgroup_record(ent, 0);
2419 rcu_read_lock();
2420 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002421 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002422 /*
2423 * We uncharge this because swap is freed.
2424 * This memcg can be obsolete one. We avoid calling css_tryget
2425 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002426 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002427 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002428 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002429 mem_cgroup_put(memcg);
2430 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002431 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002432}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002433
2434/**
2435 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2436 * @entry: swap entry to be moved
2437 * @from: mem_cgroup which the entry is moved from
2438 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002439 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002440 *
2441 * It succeeds only when the swap_cgroup's record for this entry is the same
2442 * as the mem_cgroup's id of @from.
2443 *
2444 * Returns 0 on success, -EINVAL on failure.
2445 *
2446 * The caller must have charged to @to, IOW, called res_counter_charge() about
2447 * both res and memsw, and called css_get().
2448 */
2449static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002450 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002451{
2452 unsigned short old_id, new_id;
2453
2454 old_id = css_id(&from->css);
2455 new_id = css_id(&to->css);
2456
2457 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002458 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002459 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002460 /*
2461 * This function is only called from task migration context now.
2462 * It postpones res_counter and refcount handling till the end
2463 * of task migration(mem_cgroup_clear_mc()) for performance
2464 * improvement. But we cannot postpone mem_cgroup_get(to)
2465 * because if the process that has been moved to @to does
2466 * swap-in, the refcount of @to might be decreased to 0.
2467 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002468 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002469 if (need_fixup) {
2470 if (!mem_cgroup_is_root(from))
2471 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2472 mem_cgroup_put(from);
2473 /*
2474 * we charged both to->res and to->memsw, so we should
2475 * uncharge to->res.
2476 */
2477 if (!mem_cgroup_is_root(to))
2478 res_counter_uncharge(&to->res, PAGE_SIZE);
2479 css_put(&to->css);
2480 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002481 return 0;
2482 }
2483 return -EINVAL;
2484}
2485#else
2486static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002487 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002488{
2489 return -EINVAL;
2490}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002491#endif
2492
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002493/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002494 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2495 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002496 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002497int mem_cgroup_prepare_migration(struct page *page,
2498 struct page *newpage, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002499{
2500 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002501 struct mem_cgroup *mem = NULL;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002502 enum charge_type ctype;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002503 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002504
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002505 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002506 return 0;
2507
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002508 pc = lookup_page_cgroup(page);
2509 lock_page_cgroup(pc);
2510 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002511 mem = pc->mem_cgroup;
2512 css_get(&mem->css);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002513 /*
2514 * At migrating an anonymous page, its mapcount goes down
2515 * to 0 and uncharge() will be called. But, even if it's fully
2516 * unmapped, migration may fail and this page has to be
2517 * charged again. We set MIGRATION flag here and delay uncharge
2518 * until end_migration() is called
2519 *
2520 * Corner Case Thinking
2521 * A)
2522 * When the old page was mapped as Anon and it's unmap-and-freed
2523 * while migration was ongoing.
2524 * If unmap finds the old page, uncharge() of it will be delayed
2525 * until end_migration(). If unmap finds a new page, it's
2526 * uncharged when it make mapcount to be 1->0. If unmap code
2527 * finds swap_migration_entry, the new page will not be mapped
2528 * and end_migration() will find it(mapcount==0).
2529 *
2530 * B)
2531 * When the old page was mapped but migraion fails, the kernel
2532 * remaps it. A charge for it is kept by MIGRATION flag even
2533 * if mapcount goes down to 0. We can do remap successfully
2534 * without charging it again.
2535 *
2536 * C)
2537 * The "old" page is under lock_page() until the end of
2538 * migration, so, the old page itself will not be swapped-out.
2539 * If the new page is swapped out before end_migraton, our
2540 * hook to usual swap-out path will catch the event.
2541 */
2542 if (PageAnon(page))
2543 SetPageCgroupMigration(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002544 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002545 unlock_page_cgroup(pc);
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002546 /*
2547 * If the page is not charged at this point,
2548 * we return here.
2549 */
2550 if (!mem)
2551 return 0;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002552
Andrea Arcangeli93d5c9b2010-04-23 13:17:39 -04002553 *ptr = mem;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002554 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2555 css_put(&mem->css);/* drop extra refcnt */
2556 if (ret || *ptr == NULL) {
2557 if (PageAnon(page)) {
2558 lock_page_cgroup(pc);
2559 ClearPageCgroupMigration(pc);
2560 unlock_page_cgroup(pc);
2561 /*
2562 * The old page may be fully unmapped while we kept it.
2563 */
2564 mem_cgroup_uncharge_page(page);
2565 }
2566 return -ENOMEM;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002567 }
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002568 /*
2569 * We charge new page before it's used/mapped. So, even if unlock_page()
2570 * is called before end_migration, we can catch all events on this new
2571 * page. In the case new page is migrated but not remapped, new page's
2572 * mapcount will be finally 0 and we call uncharge in end_migration().
2573 */
2574 pc = lookup_page_cgroup(newpage);
2575 if (PageAnon(page))
2576 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2577 else if (page_is_file_cache(page))
2578 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2579 else
2580 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2581 __mem_cgroup_commit_charge(mem, pc, ctype);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002582 return ret;
2583}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002584
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002585/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002586void mem_cgroup_end_migration(struct mem_cgroup *mem,
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002587 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002588{
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002589 struct page *used, *unused;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002590 struct page_cgroup *pc;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002591
2592 if (!mem)
2593 return;
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002594 /* blocks rmdir() */
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002595 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002596 /* at migration success, oldpage->mapping is NULL. */
2597 if (oldpage->mapping) {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002598 used = oldpage;
2599 unused = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002600 } else {
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002601 used = newpage;
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002602 unused = oldpage;
2603 }
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002604 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002605 * We disallowed uncharge of pages under migration because mapcount
2606 * of the page goes down to zero, temporarly.
2607 * Clear the flag and check the page should be charged.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002608 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002609 pc = lookup_page_cgroup(oldpage);
2610 lock_page_cgroup(pc);
2611 ClearPageCgroupMigration(pc);
2612 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002613
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002614 if (unused != oldpage)
2615 pc = lookup_page_cgroup(unused);
2616 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2617
2618 pc = lookup_page_cgroup(used);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002619 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002620 * If a page is a file cache, radix-tree replacement is very atomic
2621 * and we can skip this check. When it was an Anon page, its mapcount
2622 * goes down to 0. But because we added MIGRATION flage, it's not
2623 * uncharged yet. There are several case but page->mapcount check
2624 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2625 * check. (see prepare_charge() also)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002626 */
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002627 if (PageAnon(used))
2628 mem_cgroup_uncharge_page(used);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002629 /*
akpm@linux-foundation.orgac39cf82010-05-26 14:42:46 -07002630 * At migration, we may charge account against cgroup which has no
2631 * tasks.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002632 * So, rmdir()->pre_destroy() can be called while we do this charge.
2633 * In that case, we need to call pre_destroy() again. check it here.
2634 */
2635 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002636}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002637
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002638/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002639 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2640 * Calling hierarchical_reclaim is not enough because we should update
2641 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2642 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2643 * not from the memcg which this page would be charged to.
2644 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002645 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002646int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002647 struct mm_struct *mm,
2648 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002649{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002650 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002651 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002652
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002653 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002654 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002655
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002656 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2657 if (!ret)
2658 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002659
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002660 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002661}
2662
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002663static DEFINE_MUTEX(set_limit_mutex);
2664
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002665static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002666 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002667{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002668 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002669 u64 memswlimit, memlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002670 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002671 int children = mem_cgroup_count_children(memcg);
2672 u64 curusage, oldusage;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002673 int enlarge;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002674
2675 /*
2676 * For keeping hierarchical_reclaim simple, how long we should retry
2677 * is depends on callers. We set our retry-count to be function
2678 * of # of children which we should visit in this loop.
2679 */
2680 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2681
2682 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002683
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002684 enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002685 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002686 if (signal_pending(current)) {
2687 ret = -EINTR;
2688 break;
2689 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002690 /*
2691 * Rather than hide all in some function, I do this in
2692 * open coded manner. You see what this really does.
2693 * We have to guarantee mem->res.limit < mem->memsw.limit.
2694 */
2695 mutex_lock(&set_limit_mutex);
2696 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2697 if (memswlimit < val) {
2698 ret = -EINVAL;
2699 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002700 break;
2701 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002702
2703 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2704 if (memlimit < val)
2705 enlarge = 1;
2706
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002707 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002708 if (!ret) {
2709 if (memswlimit == val)
2710 memcg->memsw_is_minimum = true;
2711 else
2712 memcg->memsw_is_minimum = false;
2713 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002714 mutex_unlock(&set_limit_mutex);
2715
2716 if (!ret)
2717 break;
2718
Bob Liuaa20d482009-12-15 16:47:14 -08002719 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002720 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002721 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2722 /* Usage is reduced ? */
2723 if (curusage >= oldusage)
2724 retry_count--;
2725 else
2726 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002727 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002728 if (!ret && enlarge)
2729 memcg_oom_recover(memcg);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002730
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002731 return ret;
2732}
2733
Li Zefan338c8432009-06-17 16:27:15 -07002734static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2735 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002736{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002737 int retry_count;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002738 u64 memlimit, memswlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002739 int children = mem_cgroup_count_children(memcg);
2740 int ret = -EBUSY;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002741 int enlarge = 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002742
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002743 /* see mem_cgroup_resize_res_limit */
2744 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2745 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002746 while (retry_count) {
2747 if (signal_pending(current)) {
2748 ret = -EINTR;
2749 break;
2750 }
2751 /*
2752 * Rather than hide all in some function, I do this in
2753 * open coded manner. You see what this really does.
2754 * We have to guarantee mem->res.limit < mem->memsw.limit.
2755 */
2756 mutex_lock(&set_limit_mutex);
2757 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2758 if (memlimit > val) {
2759 ret = -EINVAL;
2760 mutex_unlock(&set_limit_mutex);
2761 break;
2762 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002763 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2764 if (memswlimit < val)
2765 enlarge = 1;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002766 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002767 if (!ret) {
2768 if (memlimit == val)
2769 memcg->memsw_is_minimum = true;
2770 else
2771 memcg->memsw_is_minimum = false;
2772 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002773 mutex_unlock(&set_limit_mutex);
2774
2775 if (!ret)
2776 break;
2777
Balbir Singh4e416952009-09-23 15:56:39 -07002778 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002779 MEM_CGROUP_RECLAIM_NOSWAP |
2780 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002781 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002782 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002783 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002784 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002785 else
2786 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002787 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002788 if (!ret && enlarge)
2789 memcg_oom_recover(memcg);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002790 return ret;
2791}
2792
Balbir Singh4e416952009-09-23 15:56:39 -07002793unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2794 gfp_t gfp_mask, int nid,
2795 int zid)
2796{
2797 unsigned long nr_reclaimed = 0;
2798 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2799 unsigned long reclaimed;
2800 int loop = 0;
2801 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002802 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002803
2804 if (order > 0)
2805 return 0;
2806
2807 mctz = soft_limit_tree_node_zone(nid, zid);
2808 /*
2809 * This loop can run a while, specially if mem_cgroup's continuously
2810 * keep exceeding their soft limit and putting the system under
2811 * pressure
2812 */
2813 do {
2814 if (next_mz)
2815 mz = next_mz;
2816 else
2817 mz = mem_cgroup_largest_soft_limit_node(mctz);
2818 if (!mz)
2819 break;
2820
2821 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2822 gfp_mask,
2823 MEM_CGROUP_RECLAIM_SOFT);
2824 nr_reclaimed += reclaimed;
2825 spin_lock(&mctz->lock);
2826
2827 /*
2828 * If we failed to reclaim anything from this memory cgroup
2829 * it is time to move on to the next cgroup
2830 */
2831 next_mz = NULL;
2832 if (!reclaimed) {
2833 do {
2834 /*
2835 * Loop until we find yet another one.
2836 *
2837 * By the time we get the soft_limit lock
2838 * again, someone might have aded the
2839 * group back on the RB tree. Iterate to
2840 * make sure we get a different mem.
2841 * mem_cgroup_largest_soft_limit_node returns
2842 * NULL if no other cgroup is present on
2843 * the tree
2844 */
2845 next_mz =
2846 __mem_cgroup_largest_soft_limit_node(mctz);
2847 if (next_mz == mz) {
2848 css_put(&next_mz->mem->css);
2849 next_mz = NULL;
2850 } else /* next_mz == NULL or other memcg */
2851 break;
2852 } while (1);
2853 }
Balbir Singh4e416952009-09-23 15:56:39 -07002854 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002855 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002856 /*
2857 * One school of thought says that we should not add
2858 * back the node to the tree if reclaim returns 0.
2859 * But our reclaim could return 0, simply because due
2860 * to priority we are exposing a smaller subset of
2861 * memory to reclaim from. Consider this as a longer
2862 * term TODO.
2863 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002864 /* If excess == 0, no tree ops */
2865 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002866 spin_unlock(&mctz->lock);
2867 css_put(&mz->mem->css);
2868 loop++;
2869 /*
2870 * Could not reclaim anything and there are no more
2871 * mem cgroups to try or we seem to be looping without
2872 * reclaiming anything.
2873 */
2874 if (!nr_reclaimed &&
2875 (next_mz == NULL ||
2876 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2877 break;
2878 } while (!nr_reclaimed);
2879 if (next_mz)
2880 css_put(&next_mz->mem->css);
2881 return nr_reclaimed;
2882}
2883
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002884/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002885 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002886 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2887 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002888static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002889 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002890{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002891 struct zone *zone;
2892 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002893 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002894 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002895 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002896 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002897
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002898 zone = &NODE_DATA(node)->node_zones[zid];
2899 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002900 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002901
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002902 loop = MEM_CGROUP_ZSTAT(mz, lru);
2903 /* give some margin against EBUSY etc...*/
2904 loop += 256;
2905 busy = NULL;
2906 while (loop--) {
2907 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002908 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002909 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002910 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002911 break;
2912 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002913 pc = list_entry(list->prev, struct page_cgroup, lru);
2914 if (busy == pc) {
2915 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08002916 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002917 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002918 continue;
2919 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002920 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002921
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002922 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002923 if (ret == -ENOMEM)
2924 break;
2925
2926 if (ret == -EBUSY || ret == -EINVAL) {
2927 /* found lock contention or "pc" is obsolete. */
2928 busy = pc;
2929 cond_resched();
2930 } else
2931 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002932 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002933
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002934 if (!ret && !list_empty(list))
2935 return -EBUSY;
2936 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002937}
2938
2939/*
2940 * make mem_cgroup's charge to be 0 if there is no task.
2941 * This enables deleting this mem_cgroup.
2942 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002943static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002944{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002945 int ret;
2946 int node, zid, shrink;
2947 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002948 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002949
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002950 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002951
2952 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002953 /* should free all ? */
2954 if (free_all)
2955 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002956move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002957 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002958 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002959 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002960 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002961 ret = -EINTR;
2962 if (signal_pending(current))
2963 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002964 /* This is for making all *used* pages to be on LRU. */
2965 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002966 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002967 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002968 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002969 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002970 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002971 for_each_lru(l) {
2972 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002973 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002974 if (ret)
2975 break;
2976 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002977 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002978 if (ret)
2979 break;
2980 }
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07002981 memcg_oom_recover(mem);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002982 /* it seems parent cgroup doesn't have enough mem */
2983 if (ret == -ENOMEM)
2984 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002985 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002986 /* "ret" should also be checked to ensure all lists are empty. */
2987 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002988out:
2989 css_put(&mem->css);
2990 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002991
2992try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002993 /* returns EBUSY if there is a task or if we come here twice. */
2994 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002995 ret = -EBUSY;
2996 goto out;
2997 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002998 /* we call try-to-free pages for make this cgroup empty */
2999 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003000 /* try to free all pages in this cgroup */
3001 shrink = 1;
3002 while (nr_retries && mem->res.usage > 0) {
3003 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003004
3005 if (signal_pending(current)) {
3006 ret = -EINTR;
3007 goto out;
3008 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003009 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3010 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003011 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003012 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003013 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02003014 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003015 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003016
3017 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003018 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08003019 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08003020 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08003021}
3022
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003023int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3024{
3025 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3026}
3027
3028
Balbir Singh18f59ea2009-01-07 18:08:07 -08003029static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3030{
3031 return mem_cgroup_from_cont(cont)->use_hierarchy;
3032}
3033
3034static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3035 u64 val)
3036{
3037 int retval = 0;
3038 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3039 struct cgroup *parent = cont->parent;
3040 struct mem_cgroup *parent_mem = NULL;
3041
3042 if (parent)
3043 parent_mem = mem_cgroup_from_cont(parent);
3044
3045 cgroup_lock();
3046 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02003047 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08003048 * in the child subtrees. If it is unset, then the change can
3049 * occur, provided the current cgroup has no children.
3050 *
3051 * For the root cgroup, parent_mem is NULL, we allow value to be
3052 * set if there are no children.
3053 */
3054 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3055 (val == 1 || val == 0)) {
3056 if (list_empty(&cont->children))
3057 mem->use_hierarchy = val;
3058 else
3059 retval = -EBUSY;
3060 } else
3061 retval = -EINVAL;
3062 cgroup_unlock();
3063
3064 return retval;
3065}
3066
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003067struct mem_cgroup_idx_data {
3068 s64 val;
3069 enum mem_cgroup_stat_index idx;
3070};
3071
3072static int
3073mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
3074{
3075 struct mem_cgroup_idx_data *d = data;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003076 d->val += mem_cgroup_read_stat(mem, d->idx);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003077 return 0;
3078}
3079
3080static void
3081mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3082 enum mem_cgroup_stat_index idx, s64 *val)
3083{
3084 struct mem_cgroup_idx_data d;
3085 d.idx = idx;
3086 d.val = 0;
3087 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
3088 *val = d.val;
3089}
3090
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003091static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3092{
3093 u64 idx_val, val;
3094
3095 if (!mem_cgroup_is_root(mem)) {
3096 if (!swap)
3097 return res_counter_read_u64(&mem->res, RES_USAGE);
3098 else
3099 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3100 }
3101
3102 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
3103 val = idx_val;
3104 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
3105 val += idx_val;
3106
3107 if (swap) {
3108 mem_cgroup_get_recursive_idx_stat(mem,
3109 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
3110 val += idx_val;
3111 }
3112
3113 return val << PAGE_SHIFT;
3114}
3115
Paul Menage2c3daa72008-04-29 00:59:58 -07003116static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003117{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003118 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003119 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003120 int type, name;
3121
3122 type = MEMFILE_TYPE(cft->private);
3123 name = MEMFILE_ATTR(cft->private);
3124 switch (type) {
3125 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003126 if (name == RES_USAGE)
3127 val = mem_cgroup_usage(mem, false);
3128 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003129 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003130 break;
3131 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08003132 if (name == RES_USAGE)
3133 val = mem_cgroup_usage(mem, true);
3134 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07003135 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003136 break;
3137 default:
3138 BUG();
3139 break;
3140 }
3141 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003142}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003143/*
3144 * The user of this function is...
3145 * RES_LIMIT.
3146 */
Paul Menage856c13a2008-07-25 01:47:04 -07003147static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3148 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003149{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003150 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003151 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003152 unsigned long long val;
3153 int ret;
3154
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003155 type = MEMFILE_TYPE(cft->private);
3156 name = MEMFILE_ATTR(cft->private);
3157 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003158 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07003159 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3160 ret = -EINVAL;
3161 break;
3162 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003163 /* This function does all necessary parse...reuse it */
3164 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003165 if (ret)
3166 break;
3167 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003168 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003169 else
3170 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003171 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07003172 case RES_SOFT_LIMIT:
3173 ret = res_counter_memparse_write_strategy(buffer, &val);
3174 if (ret)
3175 break;
3176 /*
3177 * For memsw, soft limits are hard to implement in terms
3178 * of semantics, for now, we support soft limits for
3179 * control without swap
3180 */
3181 if (type == _MEM)
3182 ret = res_counter_set_soft_limit(&memcg->res, val);
3183 else
3184 ret = -EINVAL;
3185 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07003186 default:
3187 ret = -EINVAL; /* should be BUG() ? */
3188 break;
3189 }
3190 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003191}
3192
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003193static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3194 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3195{
3196 struct cgroup *cgroup;
3197 unsigned long long min_limit, min_memsw_limit, tmp;
3198
3199 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3200 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3201 cgroup = memcg->css.cgroup;
3202 if (!memcg->use_hierarchy)
3203 goto out;
3204
3205 while (cgroup->parent) {
3206 cgroup = cgroup->parent;
3207 memcg = mem_cgroup_from_cont(cgroup);
3208 if (!memcg->use_hierarchy)
3209 break;
3210 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3211 min_limit = min(min_limit, tmp);
3212 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3213 min_memsw_limit = min(min_memsw_limit, tmp);
3214 }
3215out:
3216 *mem_limit = min_limit;
3217 *memsw_limit = min_memsw_limit;
3218 return;
3219}
3220
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003221static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003222{
3223 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003224 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003225
3226 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003227 type = MEMFILE_TYPE(event);
3228 name = MEMFILE_ATTR(event);
3229 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003230 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003231 if (type == _MEM)
3232 res_counter_reset_max(&mem->res);
3233 else
3234 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003235 break;
3236 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003237 if (type == _MEM)
3238 res_counter_reset_failcnt(&mem->res);
3239 else
3240 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003241 break;
3242 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003243
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003244 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003245}
3246
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003247static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3248 struct cftype *cft)
3249{
3250 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3251}
3252
Daisuke Nishimura02491442010-03-10 15:22:17 -08003253#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003254static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3255 struct cftype *cft, u64 val)
3256{
3257 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3258
3259 if (val >= (1 << NR_MOVE_TYPE))
3260 return -EINVAL;
3261 /*
3262 * We check this value several times in both in can_attach() and
3263 * attach(), so we need cgroup lock to prevent this value from being
3264 * inconsistent.
3265 */
3266 cgroup_lock();
3267 mem->move_charge_at_immigrate = val;
3268 cgroup_unlock();
3269
3270 return 0;
3271}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003272#else
3273static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3274 struct cftype *cft, u64 val)
3275{
3276 return -ENOSYS;
3277}
3278#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003279
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003280
3281/* For read statistics */
3282enum {
3283 MCS_CACHE,
3284 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003285 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003286 MCS_PGPGIN,
3287 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003288 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003289 MCS_INACTIVE_ANON,
3290 MCS_ACTIVE_ANON,
3291 MCS_INACTIVE_FILE,
3292 MCS_ACTIVE_FILE,
3293 MCS_UNEVICTABLE,
3294 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003295};
3296
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003297struct mcs_total_stat {
3298 s64 stat[NR_MCS_STAT];
3299};
3300
3301struct {
3302 char *local_name;
3303 char *total_name;
3304} memcg_stat_strings[NR_MCS_STAT] = {
3305 {"cache", "total_cache"},
3306 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003307 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003308 {"pgpgin", "total_pgpgin"},
3309 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003310 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003311 {"inactive_anon", "total_inactive_anon"},
3312 {"active_anon", "total_active_anon"},
3313 {"inactive_file", "total_inactive_file"},
3314 {"active_file", "total_active_file"},
3315 {"unevictable", "total_unevictable"}
3316};
3317
3318
3319static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
3320{
3321 struct mcs_total_stat *s = data;
3322 s64 val;
3323
3324 /* per cpu stat */
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003325 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003326 s->stat[MCS_CACHE] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003327 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003328 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003329 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003330 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003331 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003332 s->stat[MCS_PGPGIN] += val;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003333 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003334 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003335 if (do_swap_account) {
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003336 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003337 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3338 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003339
3340 /* per zone stat */
3341 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3342 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3343 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3344 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3345 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3346 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3347 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3348 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3349 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3350 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3351 return 0;
3352}
3353
3354static void
3355mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3356{
3357 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3358}
3359
Paul Menagec64745c2008-04-29 01:00:02 -07003360static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3361 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003362{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003363 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003364 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003365 int i;
3366
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003367 memset(&mystat, 0, sizeof(mystat));
3368 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003369
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003370 for (i = 0; i < NR_MCS_STAT; i++) {
3371 if (i == MCS_SWAP && !do_swap_account)
3372 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003373 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003374 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003375
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003376 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003377 {
3378 unsigned long long limit, memsw_limit;
3379 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3380 cb->fill(cb, "hierarchical_memory_limit", limit);
3381 if (do_swap_account)
3382 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3383 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003384
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003385 memset(&mystat, 0, sizeof(mystat));
3386 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003387 for (i = 0; i < NR_MCS_STAT; i++) {
3388 if (i == MCS_SWAP && !do_swap_account)
3389 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003390 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003391 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003392
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003393#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003394 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003395
3396 {
3397 int nid, zid;
3398 struct mem_cgroup_per_zone *mz;
3399 unsigned long recent_rotated[2] = {0, 0};
3400 unsigned long recent_scanned[2] = {0, 0};
3401
3402 for_each_online_node(nid)
3403 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3404 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3405
3406 recent_rotated[0] +=
3407 mz->reclaim_stat.recent_rotated[0];
3408 recent_rotated[1] +=
3409 mz->reclaim_stat.recent_rotated[1];
3410 recent_scanned[0] +=
3411 mz->reclaim_stat.recent_scanned[0];
3412 recent_scanned[1] +=
3413 mz->reclaim_stat.recent_scanned[1];
3414 }
3415 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3416 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3417 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3418 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3419 }
3420#endif
3421
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003422 return 0;
3423}
3424
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003425static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3426{
3427 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3428
3429 return get_swappiness(memcg);
3430}
3431
3432static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3433 u64 val)
3434{
3435 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3436 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003437
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003438 if (val > 100)
3439 return -EINVAL;
3440
3441 if (cgrp->parent == NULL)
3442 return -EINVAL;
3443
3444 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003445
3446 cgroup_lock();
3447
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003448 /* If under hierarchy, only empty-root can set this value */
3449 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003450 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3451 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003452 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003453 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003454
3455 spin_lock(&memcg->reclaim_param_lock);
3456 memcg->swappiness = val;
3457 spin_unlock(&memcg->reclaim_param_lock);
3458
Li Zefan068b38c2009-01-15 13:51:26 -08003459 cgroup_unlock();
3460
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003461 return 0;
3462}
3463
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003464static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3465{
3466 struct mem_cgroup_threshold_ary *t;
3467 u64 usage;
3468 int i;
3469
3470 rcu_read_lock();
3471 if (!swap)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003472 t = rcu_dereference(memcg->thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003473 else
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003474 t = rcu_dereference(memcg->memsw_thresholds.primary);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003475
3476 if (!t)
3477 goto unlock;
3478
3479 usage = mem_cgroup_usage(memcg, swap);
3480
3481 /*
3482 * current_threshold points to threshold just below usage.
3483 * If it's not true, a threshold was crossed after last
3484 * call of __mem_cgroup_threshold().
3485 */
Phil Carmody5407a562010-05-26 14:42:42 -07003486 i = t->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003487
3488 /*
3489 * Iterate backward over array of thresholds starting from
3490 * current_threshold and check if a threshold is crossed.
3491 * If none of thresholds below usage is crossed, we read
3492 * only one element of the array here.
3493 */
3494 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3495 eventfd_signal(t->entries[i].eventfd, 1);
3496
3497 /* i = current_threshold + 1 */
3498 i++;
3499
3500 /*
3501 * Iterate forward over array of thresholds starting from
3502 * current_threshold+1 and check if a threshold is crossed.
3503 * If none of thresholds above usage is crossed, we read
3504 * only one element of the array here.
3505 */
3506 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3507 eventfd_signal(t->entries[i].eventfd, 1);
3508
3509 /* Update current_threshold */
Phil Carmody5407a562010-05-26 14:42:42 -07003510 t->current_threshold = i - 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003511unlock:
3512 rcu_read_unlock();
3513}
3514
3515static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3516{
3517 __mem_cgroup_threshold(memcg, false);
3518 if (do_swap_account)
3519 __mem_cgroup_threshold(memcg, true);
3520}
3521
3522static int compare_thresholds(const void *a, const void *b)
3523{
3524 const struct mem_cgroup_threshold *_a = a;
3525 const struct mem_cgroup_threshold *_b = b;
3526
3527 return _a->threshold - _b->threshold;
3528}
3529
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003530static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem, void *data)
3531{
3532 struct mem_cgroup_eventfd_list *ev;
3533
3534 list_for_each_entry(ev, &mem->oom_notify, list)
3535 eventfd_signal(ev->eventfd, 1);
3536 return 0;
3537}
3538
3539static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3540{
3541 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_notify_cb);
3542}
3543
3544static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3545 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003546{
3547 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003548 struct mem_cgroup_thresholds *thresholds;
3549 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003550 int type = MEMFILE_TYPE(cft->private);
3551 u64 threshold, usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003552 int i, size, ret;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003553
3554 ret = res_counter_memparse_write_strategy(args, &threshold);
3555 if (ret)
3556 return ret;
3557
3558 mutex_lock(&memcg->thresholds_lock);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003559
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003560 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003561 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003562 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003563 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003564 else
3565 BUG();
3566
3567 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3568
3569 /* Check if a threshold crossed before adding a new one */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003570 if (thresholds->primary)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003571 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3572
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003573 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003574
3575 /* Allocate memory for new array of thresholds */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003576 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003577 GFP_KERNEL);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003578 if (!new) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003579 ret = -ENOMEM;
3580 goto unlock;
3581 }
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003582 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003583
3584 /* Copy thresholds (if any) to new array */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003585 if (thresholds->primary) {
3586 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003587 sizeof(struct mem_cgroup_threshold));
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003588 }
3589
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003590 /* Add new threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003591 new->entries[size - 1].eventfd = eventfd;
3592 new->entries[size - 1].threshold = threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003593
3594 /* Sort thresholds. Registering of new threshold isn't time-critical */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003595 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003596 compare_thresholds, NULL);
3597
3598 /* Find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003599 new->current_threshold = -1;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003600 for (i = 0; i < size; i++) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003601 if (new->entries[i].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003602 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003603 * new->current_threshold will not be used until
3604 * rcu_assign_pointer(), so it's safe to increment
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003605 * it here.
3606 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003607 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003608 }
3609 }
3610
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003611 /* Free old spare buffer and save old primary buffer as spare */
3612 kfree(thresholds->spare);
3613 thresholds->spare = thresholds->primary;
3614
3615 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003616
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003617 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003618 synchronize_rcu();
3619
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003620unlock:
3621 mutex_unlock(&memcg->thresholds_lock);
3622
3623 return ret;
3624}
3625
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003626static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003627 struct cftype *cft, struct eventfd_ctx *eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003628{
3629 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003630 struct mem_cgroup_thresholds *thresholds;
3631 struct mem_cgroup_threshold_ary *new;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003632 int type = MEMFILE_TYPE(cft->private);
3633 u64 usage;
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003634 int i, j, size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003635
3636 mutex_lock(&memcg->thresholds_lock);
3637 if (type == _MEM)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003638 thresholds = &memcg->thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003639 else if (type == _MEMSWAP)
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003640 thresholds = &memcg->memsw_thresholds;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003641 else
3642 BUG();
3643
3644 /*
3645 * Something went wrong if we trying to unregister a threshold
3646 * if we don't have thresholds
3647 */
3648 BUG_ON(!thresholds);
3649
3650 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3651
3652 /* Check if a threshold crossed before removing */
3653 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3654
3655 /* Calculate new number of threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003656 size = 0;
3657 for (i = 0; i < thresholds->primary->size; i++) {
3658 if (thresholds->primary->entries[i].eventfd != eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003659 size++;
3660 }
3661
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003662 new = thresholds->spare;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003663
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003664 /* Set thresholds array to NULL if we don't have thresholds */
3665 if (!size) {
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003666 kfree(new);
3667 new = NULL;
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003668 goto swap_buffers;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003669 }
3670
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003671 new->size = size;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003672
3673 /* Copy thresholds and find current threshold */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003674 new->current_threshold = -1;
3675 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3676 if (thresholds->primary->entries[i].eventfd == eventfd)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003677 continue;
3678
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003679 new->entries[j] = thresholds->primary->entries[i];
3680 if (new->entries[j].threshold < usage) {
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003681 /*
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003682 * new->current_threshold will not be used
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003683 * until rcu_assign_pointer(), so it's safe to increment
3684 * it here.
3685 */
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003686 ++new->current_threshold;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003687 }
3688 j++;
3689 }
3690
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003691swap_buffers:
Kirill A. Shutemov2c488db2010-05-26 14:42:47 -07003692 /* Swap primary and spare array */
3693 thresholds->spare = thresholds->primary;
3694 rcu_assign_pointer(thresholds->primary, new);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003695
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003696 /* To be sure that nobody uses thresholds */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003697 synchronize_rcu();
3698
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003699 mutex_unlock(&memcg->thresholds_lock);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003700}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003701
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003702static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3703 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3704{
3705 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3706 struct mem_cgroup_eventfd_list *event;
3707 int type = MEMFILE_TYPE(cft->private);
3708
3709 BUG_ON(type != _OOM_TYPE);
3710 event = kmalloc(sizeof(*event), GFP_KERNEL);
3711 if (!event)
3712 return -ENOMEM;
3713
3714 mutex_lock(&memcg_oom_mutex);
3715
3716 event->eventfd = eventfd;
3717 list_add(&event->list, &memcg->oom_notify);
3718
3719 /* already in OOM ? */
3720 if (atomic_read(&memcg->oom_lock))
3721 eventfd_signal(eventfd, 1);
3722 mutex_unlock(&memcg_oom_mutex);
3723
3724 return 0;
3725}
3726
Kirill A. Shutemov907860e2010-05-26 14:42:46 -07003727static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003728 struct cftype *cft, struct eventfd_ctx *eventfd)
3729{
3730 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3731 struct mem_cgroup_eventfd_list *ev, *tmp;
3732 int type = MEMFILE_TYPE(cft->private);
3733
3734 BUG_ON(type != _OOM_TYPE);
3735
3736 mutex_lock(&memcg_oom_mutex);
3737
3738 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
3739 if (ev->eventfd == eventfd) {
3740 list_del(&ev->list);
3741 kfree(ev);
3742 }
3743 }
3744
3745 mutex_unlock(&memcg_oom_mutex);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003746}
3747
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003748static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3749 struct cftype *cft, struct cgroup_map_cb *cb)
3750{
3751 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3752
3753 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
3754
3755 if (atomic_read(&mem->oom_lock))
3756 cb->fill(cb, "under_oom", 1);
3757 else
3758 cb->fill(cb, "under_oom", 0);
3759 return 0;
3760}
3761
3762/*
3763 */
3764static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
3765 struct cftype *cft, u64 val)
3766{
3767 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3768 struct mem_cgroup *parent;
3769
3770 /* cannot set to root cgroup and only 0 and 1 are allowed */
3771 if (!cgrp->parent || !((val == 0) || (val == 1)))
3772 return -EINVAL;
3773
3774 parent = mem_cgroup_from_cont(cgrp->parent);
3775
3776 cgroup_lock();
3777 /* oom-kill-disable is a flag for subhierarchy. */
3778 if ((parent->use_hierarchy) ||
3779 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
3780 cgroup_unlock();
3781 return -EINVAL;
3782 }
3783 mem->oom_kill_disable = val;
KAMEZAWA Hiroyuki4d845eb2010-06-29 15:05:18 -07003784 if (!val)
3785 memcg_oom_recover(mem);
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003786 cgroup_unlock();
3787 return 0;
3788}
3789
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003790static struct cftype mem_cgroup_files[] = {
3791 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003792 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003793 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003794 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003795 .register_event = mem_cgroup_usage_register_event,
3796 .unregister_event = mem_cgroup_usage_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003797 },
3798 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003799 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003800 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003801 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003802 .read_u64 = mem_cgroup_read,
3803 },
3804 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003805 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003806 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003807 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003808 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003809 },
3810 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003811 .name = "soft_limit_in_bytes",
3812 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3813 .write_string = mem_cgroup_write,
3814 .read_u64 = mem_cgroup_read,
3815 },
3816 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003817 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003818 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003819 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003820 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003821 },
Balbir Singh8697d332008-02-07 00:13:59 -08003822 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003823 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003824 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003825 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003826 {
3827 .name = "force_empty",
3828 .trigger = mem_cgroup_force_empty_write,
3829 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003830 {
3831 .name = "use_hierarchy",
3832 .write_u64 = mem_cgroup_hierarchy_write,
3833 .read_u64 = mem_cgroup_hierarchy_read,
3834 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003835 {
3836 .name = "swappiness",
3837 .read_u64 = mem_cgroup_swappiness_read,
3838 .write_u64 = mem_cgroup_swappiness_write,
3839 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003840 {
3841 .name = "move_charge_at_immigrate",
3842 .read_u64 = mem_cgroup_move_charge_read,
3843 .write_u64 = mem_cgroup_move_charge_write,
3844 },
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003845 {
3846 .name = "oom_control",
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07003847 .read_map = mem_cgroup_oom_control_read,
3848 .write_u64 = mem_cgroup_oom_control_write,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003849 .register_event = mem_cgroup_oom_register_event,
3850 .unregister_event = mem_cgroup_oom_unregister_event,
3851 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
3852 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003853};
3854
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003855#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3856static struct cftype memsw_cgroup_files[] = {
3857 {
3858 .name = "memsw.usage_in_bytes",
3859 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3860 .read_u64 = mem_cgroup_read,
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07003861 .register_event = mem_cgroup_usage_register_event,
3862 .unregister_event = mem_cgroup_usage_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003863 },
3864 {
3865 .name = "memsw.max_usage_in_bytes",
3866 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3867 .trigger = mem_cgroup_reset,
3868 .read_u64 = mem_cgroup_read,
3869 },
3870 {
3871 .name = "memsw.limit_in_bytes",
3872 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3873 .write_string = mem_cgroup_write,
3874 .read_u64 = mem_cgroup_read,
3875 },
3876 {
3877 .name = "memsw.failcnt",
3878 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3879 .trigger = mem_cgroup_reset,
3880 .read_u64 = mem_cgroup_read,
3881 },
3882};
3883
3884static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3885{
3886 if (!do_swap_account)
3887 return 0;
3888 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3889 ARRAY_SIZE(memsw_cgroup_files));
3890};
3891#else
3892static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3893{
3894 return 0;
3895}
3896#endif
3897
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003898static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3899{
3900 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003901 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003902 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003903 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003904 /*
3905 * This routine is called against possible nodes.
3906 * But it's BUG to call kmalloc() against offline node.
3907 *
3908 * TODO: this routine can waste much memory for nodes which will
3909 * never be onlined. It's better to use memory hotplug callback
3910 * function.
3911 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003912 if (!node_state(node, N_NORMAL_MEMORY))
3913 tmp = -1;
3914 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003915 if (!pn)
3916 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003917
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003918 mem->info.nodeinfo[node] = pn;
3919 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003920
3921 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3922 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003923 for_each_lru(l)
3924 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003925 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003926 mz->on_tree = false;
3927 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003928 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003929 return 0;
3930}
3931
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003932static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3933{
3934 kfree(mem->info.nodeinfo[node]);
3935}
3936
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003937static struct mem_cgroup *mem_cgroup_alloc(void)
3938{
3939 struct mem_cgroup *mem;
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003940 int size = sizeof(struct mem_cgroup);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003941
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003942 /* Can be very big if MAX_NUMNODES is very big */
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003943 if (size < PAGE_SIZE)
3944 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003945 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003946 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003947
Dan Carpentere7bbcdf2010-03-23 13:35:12 -07003948 if (!mem)
3949 return NULL;
3950
3951 memset(mem, 0, size);
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003952 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
3953 if (!mem->stat) {
3954 if (size < PAGE_SIZE)
3955 kfree(mem);
3956 else
3957 vfree(mem);
3958 mem = NULL;
3959 }
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003960 return mem;
3961}
3962
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003963/*
3964 * At destroying mem_cgroup, references from swap_cgroup can remain.
3965 * (scanning all at force_empty is too costly...)
3966 *
3967 * Instead of clearing all references at force_empty, we remember
3968 * the number of reference from swap_cgroup and free mem_cgroup when
3969 * it goes down to 0.
3970 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003971 * Removal of cgroup itself succeeds regardless of refs from swap.
3972 */
3973
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003974static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003975{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003976 int node;
3977
Balbir Singhf64c3f52009-09-23 15:56:37 -07003978 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003979 free_css_id(&mem_cgroup_subsys, &mem->css);
3980
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003981 for_each_node_state(node, N_POSSIBLE)
3982 free_mem_cgroup_per_zone_info(mem, node);
3983
KAMEZAWA Hiroyukic62b1a32010-03-10 15:22:29 -08003984 free_percpu(mem->stat);
3985 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003986 kfree(mem);
3987 else
3988 vfree(mem);
3989}
3990
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003991static void mem_cgroup_get(struct mem_cgroup *mem)
3992{
3993 atomic_inc(&mem->refcnt);
3994}
3995
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003996static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003997{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003998 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003999 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004000 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004001 if (parent)
4002 mem_cgroup_put(parent);
4003 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004004}
4005
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004006static void mem_cgroup_put(struct mem_cgroup *mem)
4007{
4008 __mem_cgroup_put(mem, 1);
4009}
4010
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004011/*
4012 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4013 */
4014static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4015{
4016 if (!mem->res.parent)
4017 return NULL;
4018 return mem_cgroup_from_res_counter(mem->res.parent, res);
4019}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07004020
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004021#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4022static void __init enable_swap_cgroup(void)
4023{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08004024 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004025 do_swap_account = 1;
4026}
4027#else
4028static void __init enable_swap_cgroup(void)
4029{
4030}
4031#endif
4032
Balbir Singhf64c3f52009-09-23 15:56:37 -07004033static int mem_cgroup_soft_limit_tree_init(void)
4034{
4035 struct mem_cgroup_tree_per_node *rtpn;
4036 struct mem_cgroup_tree_per_zone *rtpz;
4037 int tmp, node, zone;
4038
4039 for_each_node_state(node, N_POSSIBLE) {
4040 tmp = node;
4041 if (!node_state(node, N_NORMAL_MEMORY))
4042 tmp = -1;
4043 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4044 if (!rtpn)
4045 return 1;
4046
4047 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4048
4049 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4050 rtpz = &rtpn->rb_tree_per_zone[zone];
4051 rtpz->rb_root = RB_ROOT;
4052 spin_lock_init(&rtpz->lock);
4053 }
4054 }
4055 return 0;
4056}
4057
Li Zefan0eb253e2009-01-15 13:51:25 -08004058static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004059mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4060{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004061 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004062 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004063 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004064
Jan Blunckc8dad2b2009-01-07 18:07:53 -08004065 mem = mem_cgroup_alloc();
4066 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004067 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08004068
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004069 for_each_node_state(node, N_POSSIBLE)
4070 if (alloc_mem_cgroup_per_zone_info(mem, node))
4071 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004072
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004073 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004074 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004075 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004076 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004077 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07004078 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07004079 if (mem_cgroup_soft_limit_tree_init())
4080 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08004081 for_each_possible_cpu(cpu) {
4082 struct memcg_stock_pcp *stock =
4083 &per_cpu(memcg_stock, cpu);
4084 INIT_WORK(&stock->work, drain_local_stock);
4085 }
4086 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004087 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004088 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004089 mem->use_hierarchy = parent->use_hierarchy;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004090 mem->oom_kill_disable = parent->oom_kill_disable;
Balbir Singh18f59ea2009-01-07 18:08:07 -08004091 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08004092
Balbir Singh18f59ea2009-01-07 18:08:07 -08004093 if (parent && parent->use_hierarchy) {
4094 res_counter_init(&mem->res, &parent->res);
4095 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08004096 /*
4097 * We increment refcnt of the parent to ensure that we can
4098 * safely access it on res_counter_charge/uncharge.
4099 * This refcnt will be decremented when freeing this
4100 * mem_cgroup(see mem_cgroup_put).
4101 */
4102 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08004103 } else {
4104 res_counter_init(&mem->res, NULL);
4105 res_counter_init(&mem->memsw, NULL);
4106 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004107 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08004108 spin_lock_init(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki9490ff22010-05-26 14:42:36 -07004109 INIT_LIST_HEAD(&mem->oom_notify);
Balbir Singh6d61ef42009-01-07 18:08:06 -08004110
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08004111 if (parent)
4112 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004113 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004114 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08004115 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004116 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004117free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08004118 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07004119 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004120 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004121}
4122
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004123static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004124 struct cgroup *cont)
4125{
4126 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07004127
4128 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004129}
4130
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004131static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4132 struct cgroup *cont)
4133{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004134 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004135
Daisuke Nishimurac268e992009-01-15 13:51:13 -08004136 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004137}
4138
4139static int mem_cgroup_populate(struct cgroup_subsys *ss,
4140 struct cgroup *cont)
4141{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08004142 int ret;
4143
4144 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4145 ARRAY_SIZE(mem_cgroup_files));
4146
4147 if (!ret)
4148 ret = register_memsw_files(cont, ss);
4149 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004150}
4151
Daisuke Nishimura02491442010-03-10 15:22:17 -08004152#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004153/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004154#define PRECHARGE_COUNT_AT_ONCE 256
4155static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004156{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004157 int ret = 0;
4158 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004159 struct mem_cgroup *mem = mc.to;
4160
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004161 if (mem_cgroup_is_root(mem)) {
4162 mc.precharge += count;
4163 /* we don't need css_get for root */
4164 return ret;
4165 }
4166 /* try to charge at once */
4167 if (count > 1) {
4168 struct res_counter *dummy;
4169 /*
4170 * "mem" cannot be under rmdir() because we've already checked
4171 * by cgroup_lock_live_cgroup() that it is not removed and we
4172 * are still under the same cgroup_mutex. So we can postpone
4173 * css_get().
4174 */
4175 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4176 goto one_by_one;
4177 if (do_swap_account && res_counter_charge(&mem->memsw,
4178 PAGE_SIZE * count, &dummy)) {
4179 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4180 goto one_by_one;
4181 }
4182 mc.precharge += count;
4183 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
4184 WARN_ON_ONCE(count > INT_MAX);
4185 __css_get(&mem->css, (int)count);
4186 return ret;
4187 }
4188one_by_one:
4189 /* fall back to one by one charge */
4190 while (count--) {
4191 if (signal_pending(current)) {
4192 ret = -EINTR;
4193 break;
4194 }
4195 if (!batch_count--) {
4196 batch_count = PRECHARGE_COUNT_AT_ONCE;
4197 cond_resched();
4198 }
KAMEZAWA Hiroyuki430e48632010-03-10 15:22:30 -08004199 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004200 if (ret || !mem)
4201 /* mem_cgroup_clear_mc() will do uncharge later */
4202 return -ENOMEM;
4203 mc.precharge++;
4204 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004205 return ret;
4206}
4207
4208/**
4209 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4210 * @vma: the vma the pte to be checked belongs
4211 * @addr: the address corresponding to the pte to be checked
4212 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08004213 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004214 *
4215 * Returns
4216 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4217 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4218 * move charge. if @target is not NULL, the page is stored in target->page
4219 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08004220 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4221 * target for charge migration. if @target is not NULL, the entry is stored
4222 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004223 *
4224 * Called with pte lock held.
4225 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004226union mc_target {
4227 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004228 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004229};
4230
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004231enum mc_target_type {
4232 MC_TARGET_NONE, /* not used */
4233 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08004234 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004235};
4236
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004237static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4238 unsigned long addr, pte_t ptent)
4239{
4240 struct page *page = vm_normal_page(vma, addr, ptent);
4241
4242 if (!page || !page_mapped(page))
4243 return NULL;
4244 if (PageAnon(page)) {
4245 /* we don't move shared anon */
4246 if (!move_anon() || page_mapcount(page) > 2)
4247 return NULL;
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004248 } else if (!move_file())
4249 /* we ignore mapcount for file pages */
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004250 return NULL;
4251 if (!get_page_unless_zero(page))
4252 return NULL;
4253
4254 return page;
4255}
4256
4257static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4258 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4259{
4260 int usage_count;
4261 struct page *page = NULL;
4262 swp_entry_t ent = pte_to_swp_entry(ptent);
4263
4264 if (!move_anon() || non_swap_entry(ent))
4265 return NULL;
4266 usage_count = mem_cgroup_count_swap_user(ent, &page);
4267 if (usage_count > 1) { /* we don't move shared anon */
4268 if (page)
4269 put_page(page);
4270 return NULL;
4271 }
4272 if (do_swap_account)
4273 entry->val = ent.val;
4274
4275 return page;
4276}
4277
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004278static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4279 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4280{
4281 struct page *page = NULL;
4282 struct inode *inode;
4283 struct address_space *mapping;
4284 pgoff_t pgoff;
4285
4286 if (!vma->vm_file) /* anonymous vma */
4287 return NULL;
4288 if (!move_file())
4289 return NULL;
4290
4291 inode = vma->vm_file->f_path.dentry->d_inode;
4292 mapping = vma->vm_file->f_mapping;
4293 if (pte_none(ptent))
4294 pgoff = linear_page_index(vma, addr);
4295 else /* pte_file(ptent) is true */
4296 pgoff = pte_to_pgoff(ptent);
4297
4298 /* page is moved even if it's not RSS of this task(page-faulted). */
4299 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4300 page = find_get_page(mapping, pgoff);
4301 } else { /* shmem/tmpfs file. we should take account of swap too. */
4302 swp_entry_t ent;
4303 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4304 if (do_swap_account)
4305 entry->val = ent.val;
4306 }
4307
4308 return page;
4309}
4310
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004311static int is_target_pte_for_mc(struct vm_area_struct *vma,
4312 unsigned long addr, pte_t ptent, union mc_target *target)
4313{
Daisuke Nishimura02491442010-03-10 15:22:17 -08004314 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004315 struct page_cgroup *pc;
4316 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004317 swp_entry_t ent = { .val = 0 };
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004318
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004319 if (pte_present(ptent))
4320 page = mc_handle_present_pte(vma, addr, ptent);
4321 else if (is_swap_pte(ptent))
4322 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
Daisuke Nishimura87946a72010-05-26 14:42:39 -07004323 else if (pte_none(ptent) || pte_file(ptent))
4324 page = mc_handle_file_pte(vma, addr, ptent, &ent);
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004325
4326 if (!page && !ent.val)
Daisuke Nishimura02491442010-03-10 15:22:17 -08004327 return 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004328 if (page) {
4329 pc = lookup_page_cgroup(page);
4330 /*
4331 * Do only loose check w/o page_cgroup lock.
4332 * mem_cgroup_move_account() checks the pc is valid or not under
4333 * the lock.
4334 */
4335 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4336 ret = MC_TARGET_PAGE;
4337 if (target)
4338 target->page = page;
4339 }
4340 if (!ret || !target)
4341 put_page(page);
4342 }
Daisuke Nishimura90254a62010-05-26 14:42:38 -07004343 /* There is a swap entry and a page doesn't exist or isn't charged */
4344 if (ent.val && !ret &&
KAMEZAWA Hiroyuki7f0f1542010-05-11 14:06:58 -07004345 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4346 ret = MC_TARGET_SWAP;
4347 if (target)
4348 target->ent = ent;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004349 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004350 return ret;
4351}
4352
4353static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4354 unsigned long addr, unsigned long end,
4355 struct mm_walk *walk)
4356{
4357 struct vm_area_struct *vma = walk->private;
4358 pte_t *pte;
4359 spinlock_t *ptl;
4360
4361 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4362 for (; addr != end; pte++, addr += PAGE_SIZE)
4363 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4364 mc.precharge++; /* increment precharge temporarily */
4365 pte_unmap_unlock(pte - 1, ptl);
4366 cond_resched();
4367
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004368 return 0;
4369}
4370
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004371static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4372{
4373 unsigned long precharge;
4374 struct vm_area_struct *vma;
4375
4376 down_read(&mm->mmap_sem);
4377 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4378 struct mm_walk mem_cgroup_count_precharge_walk = {
4379 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4380 .mm = mm,
4381 .private = vma,
4382 };
4383 if (is_vm_hugetlb_page(vma))
4384 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004385 walk_page_range(vma->vm_start, vma->vm_end,
4386 &mem_cgroup_count_precharge_walk);
4387 }
4388 up_read(&mm->mmap_sem);
4389
4390 precharge = mc.precharge;
4391 mc.precharge = 0;
4392
4393 return precharge;
4394}
4395
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004396static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4397{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004398 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004399}
4400
4401static void mem_cgroup_clear_mc(void)
4402{
4403 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004404 if (mc.precharge) {
4405 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4406 mc.precharge = 0;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004407 memcg_oom_recover(mc.to);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004408 }
4409 /*
4410 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4411 * we must uncharge here.
4412 */
4413 if (mc.moved_charge) {
4414 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4415 mc.moved_charge = 0;
KAMEZAWA Hiroyuki3c11ecf2010-05-26 14:42:37 -07004416 memcg_oom_recover(mc.from);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004417 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004418 /* we must fixup refcnts and charges */
4419 if (mc.moved_swap) {
4420 WARN_ON_ONCE(mc.moved_swap > INT_MAX);
4421 /* uncharge swap account from the old cgroup */
4422 if (!mem_cgroup_is_root(mc.from))
4423 res_counter_uncharge(&mc.from->memsw,
4424 PAGE_SIZE * mc.moved_swap);
4425 __mem_cgroup_put(mc.from, mc.moved_swap);
4426
4427 if (!mem_cgroup_is_root(mc.to)) {
4428 /*
4429 * we charged both to->res and to->memsw, so we should
4430 * uncharge to->res.
4431 */
4432 res_counter_uncharge(&mc.to->res,
4433 PAGE_SIZE * mc.moved_swap);
4434 VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags));
4435 __css_put(&mc.to->css, mc.moved_swap);
4436 }
4437 /* we've already done mem_cgroup_get(mc.to) */
4438
4439 mc.moved_swap = 0;
4440 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004441 mc.from = NULL;
4442 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004443 mc.moving_task = NULL;
4444 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004445}
4446
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004447static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4448 struct cgroup *cgroup,
4449 struct task_struct *p,
4450 bool threadgroup)
4451{
4452 int ret = 0;
4453 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4454
4455 if (mem->move_charge_at_immigrate) {
4456 struct mm_struct *mm;
4457 struct mem_cgroup *from = mem_cgroup_from_task(p);
4458
4459 VM_BUG_ON(from == mem);
4460
4461 mm = get_task_mm(p);
4462 if (!mm)
4463 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004464 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004465 if (mm->owner == p) {
4466 VM_BUG_ON(mc.from);
4467 VM_BUG_ON(mc.to);
4468 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004469 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004470 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004471 VM_BUG_ON(mc.moving_task);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004472 mc.from = from;
4473 mc.to = mem;
4474 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004475 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004476 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004477 mc.moving_task = current;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004478
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004479 ret = mem_cgroup_precharge_mc(mm);
4480 if (ret)
4481 mem_cgroup_clear_mc();
4482 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004483 mmput(mm);
4484 }
4485 return ret;
4486}
4487
4488static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4489 struct cgroup *cgroup,
4490 struct task_struct *p,
4491 bool threadgroup)
4492{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004493 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004494}
4495
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004496static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4497 unsigned long addr, unsigned long end,
4498 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004499{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004500 int ret = 0;
4501 struct vm_area_struct *vma = walk->private;
4502 pte_t *pte;
4503 spinlock_t *ptl;
4504
4505retry:
4506 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4507 for (; addr != end; addr += PAGE_SIZE) {
4508 pte_t ptent = *(pte++);
4509 union mc_target target;
4510 int type;
4511 struct page *page;
4512 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004513 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004514
4515 if (!mc.precharge)
4516 break;
4517
4518 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4519 switch (type) {
4520 case MC_TARGET_PAGE:
4521 page = target.page;
4522 if (isolate_lru_page(page))
4523 goto put;
4524 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004525 if (!mem_cgroup_move_account(pc,
4526 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004527 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004528 /* we uncharge from mc.from later. */
4529 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004530 }
4531 putback_lru_page(page);
4532put: /* is_target_pte_for_mc() gets the page */
4533 put_page(page);
4534 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004535 case MC_TARGET_SWAP:
4536 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004537 if (!mem_cgroup_move_swap_account(ent,
4538 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004539 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004540 /* we fixup refcnts and charges later. */
4541 mc.moved_swap++;
4542 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004543 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004544 default:
4545 break;
4546 }
4547 }
4548 pte_unmap_unlock(pte - 1, ptl);
4549 cond_resched();
4550
4551 if (addr != end) {
4552 /*
4553 * We have consumed all precharges we got in can_attach().
4554 * We try charge one by one, but don't do any additional
4555 * charges to mc.to if we have failed in charge once in attach()
4556 * phase.
4557 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004558 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004559 if (!ret)
4560 goto retry;
4561 }
4562
4563 return ret;
4564}
4565
4566static void mem_cgroup_move_charge(struct mm_struct *mm)
4567{
4568 struct vm_area_struct *vma;
4569
4570 lru_add_drain_all();
4571 down_read(&mm->mmap_sem);
4572 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4573 int ret;
4574 struct mm_walk mem_cgroup_move_charge_walk = {
4575 .pmd_entry = mem_cgroup_move_charge_pte_range,
4576 .mm = mm,
4577 .private = vma,
4578 };
4579 if (is_vm_hugetlb_page(vma))
4580 continue;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004581 ret = walk_page_range(vma->vm_start, vma->vm_end,
4582 &mem_cgroup_move_charge_walk);
4583 if (ret)
4584 /*
4585 * means we have consumed all precharges and failed in
4586 * doing additional charge. Just abandon here.
4587 */
4588 break;
4589 }
4590 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004591}
4592
Balbir Singh67e465a2008-02-07 00:13:54 -08004593static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4594 struct cgroup *cont,
4595 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004596 struct task_struct *p,
4597 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004598{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004599 struct mm_struct *mm;
4600
4601 if (!mc.to)
4602 /* no need to move charge */
4603 return;
4604
4605 mm = get_task_mm(p);
4606 if (mm) {
4607 mem_cgroup_move_charge(mm);
4608 mmput(mm);
4609 }
4610 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004611}
Daisuke Nishimura5cfb80a2010-03-23 13:35:11 -07004612#else /* !CONFIG_MMU */
4613static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4614 struct cgroup *cgroup,
4615 struct task_struct *p,
4616 bool threadgroup)
4617{
4618 return 0;
4619}
4620static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4621 struct cgroup *cgroup,
4622 struct task_struct *p,
4623 bool threadgroup)
4624{
4625}
4626static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4627 struct cgroup *cont,
4628 struct cgroup *old_cont,
4629 struct task_struct *p,
4630 bool threadgroup)
4631{
4632}
4633#endif
Balbir Singh67e465a2008-02-07 00:13:54 -08004634
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004635struct cgroup_subsys mem_cgroup_subsys = {
4636 .name = "memory",
4637 .subsys_id = mem_cgroup_subsys_id,
4638 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004639 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004640 .destroy = mem_cgroup_destroy,
4641 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004642 .can_attach = mem_cgroup_can_attach,
4643 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004644 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004645 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004646 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004647};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004648
4649#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4650
4651static int __init disable_swap_account(char *s)
4652{
4653 really_do_swap_account = 0;
4654 return 1;
4655}
4656__setup("noswapaccount", disable_swap_account);
4657#endif