blob: a82464b6e3d2fc4235f84c5a0fc512fe91936bfe [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
Balbir Singhf64c3f52009-09-23 15:56:37 -070066#define SOFTLIMIT_EVENTS_THRESH (1000)
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080067#define THRESHOLDS_EVENTS_THRESH (100)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080068
Balbir Singh8cdea7c2008-02-07 00:13:50 -080069/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080070 * Statistics for memory cgroup.
71 */
72enum mem_cgroup_stat_index {
73 /*
74 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
75 */
76 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070077 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080078 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070079 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
80 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070081 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
Kirill A. Shutemov378ce722010-03-10 15:22:23 -080082 MEM_CGROUP_STAT_SOFTLIMIT, /* decrements on each page in/out.
83 used by soft limit implementation */
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -080084 MEM_CGROUP_STAT_THRESHOLDS, /* decrements on each page in/out.
85 used by threshold implementation */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080086
87 MEM_CGROUP_STAT_NSTATS,
88};
89
90struct mem_cgroup_stat_cpu {
91 s64 count[MEM_CGROUP_STAT_NSTATS];
92} ____cacheline_aligned_in_smp;
93
94struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080095 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080096};
97
Balbir Singhf64c3f52009-09-23 15:56:37 -070098static inline void
Kirill A. Shutemov378ce722010-03-10 15:22:23 -080099__mem_cgroup_stat_set_safe(struct mem_cgroup_stat_cpu *stat,
100 enum mem_cgroup_stat_index idx, s64 val)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700101{
Kirill A. Shutemov378ce722010-03-10 15:22:23 -0800102 stat->count[idx] = val;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700103}
104
105static inline s64
106__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
107 enum mem_cgroup_stat_index idx)
108{
109 return stat->count[idx];
110}
111
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800112/*
113 * For accounting under irq disable, no need for increment preempt count.
114 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700115static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800116 enum mem_cgroup_stat_index idx, int val)
117{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700118 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800119}
120
121static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
122 enum mem_cgroup_stat_index idx)
123{
124 int cpu;
125 s64 ret = 0;
126 for_each_possible_cpu(cpu)
127 ret += stat->cpustat[cpu].count[idx];
128 return ret;
129}
130
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700131static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
132{
133 s64 ret;
134
135 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
136 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
137 return ret;
138}
139
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800140/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800141 * per-zone information in memory controller.
142 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800143struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800144 /*
145 * spin_lock to protect the per cgroup LRU
146 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700147 struct list_head lists[NR_LRU_LISTS];
148 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800149
150 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700151 struct rb_node tree_node; /* RB tree node */
152 unsigned long long usage_in_excess;/* Set to the value by which */
153 /* the soft limit is exceeded*/
154 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700155 struct mem_cgroup *mem; /* Back pointer, we cannot */
156 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800157};
158/* Macro for accessing counter */
159#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
160
161struct mem_cgroup_per_node {
162 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
163};
164
165struct mem_cgroup_lru_info {
166 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
167};
168
169/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700170 * Cgroups above their limits are maintained in a RB-Tree, independent of
171 * their hierarchy representation
172 */
173
174struct mem_cgroup_tree_per_zone {
175 struct rb_root rb_root;
176 spinlock_t lock;
177};
178
179struct mem_cgroup_tree_per_node {
180 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
181};
182
183struct mem_cgroup_tree {
184 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
185};
186
187static struct mem_cgroup_tree soft_limit_tree __read_mostly;
188
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800189struct mem_cgroup_threshold {
190 struct eventfd_ctx *eventfd;
191 u64 threshold;
192};
193
194struct mem_cgroup_threshold_ary {
195 /* An array index points to threshold just below usage. */
196 atomic_t current_threshold;
197 /* Size of entries[] */
198 unsigned int size;
199 /* Array of thresholds */
200 struct mem_cgroup_threshold entries[0];
201};
202
203static bool mem_cgroup_threshold_check(struct mem_cgroup *mem);
204static void mem_cgroup_threshold(struct mem_cgroup *mem);
205
Balbir Singhf64c3f52009-09-23 15:56:37 -0700206/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800207 * The memory controller data structure. The memory controller controls both
208 * page cache and RSS per cgroup. We would eventually like to provide
209 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
210 * to help the administrator determine what knobs to tune.
211 *
212 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800213 * we hit the water mark. May be even add a low water mark, such that
214 * no reclaim occurs from a cgroup at it's low water mark, this is
215 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800216 */
217struct mem_cgroup {
218 struct cgroup_subsys_state css;
219 /*
220 * the counter to account for memory usage
221 */
222 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800223 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800224 * the counter to account for mem+swap usage.
225 */
226 struct res_counter memsw;
227 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800228 * Per cgroup active and inactive list, similar to the
229 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800230 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800231 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800232
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800233 /*
234 protect against reclaim related member.
235 */
236 spinlock_t reclaim_param_lock;
237
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800238 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800239
240 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200241 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700242 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800243 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700244 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800245 /*
246 * Should the accounting and control be hierarchical, per subtree?
247 */
248 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800249 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800250 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800251
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800252 unsigned int swappiness;
253
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700254 /* set when res.limit == memsw.limit */
255 bool memsw_is_minimum;
256
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800257 /* protect arrays of thresholds */
258 struct mutex thresholds_lock;
259
260 /* thresholds for memory usage. RCU-protected */
261 struct mem_cgroup_threshold_ary *thresholds;
262
263 /* thresholds for mem+swap usage. RCU-protected */
264 struct mem_cgroup_threshold_ary *memsw_thresholds;
265
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800266 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800267 * Should we move charges of a task when a task is moved into this
268 * mem_cgroup ? And what type of charges should we move ?
269 */
270 unsigned long move_charge_at_immigrate;
271
272 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800273 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800274 */
275 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800276};
277
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800278/* Stuffs for move charges at task migration. */
279/*
280 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
281 * left-shifted bitmap of these types.
282 */
283enum move_type {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800284 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800285 NR_MOVE_TYPE,
286};
287
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800288/* "mc" and its members are protected by cgroup_mutex */
289static struct move_charge_struct {
290 struct mem_cgroup *from;
291 struct mem_cgroup *to;
292 unsigned long precharge;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -0800293 unsigned long moved_charge;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -0800294 unsigned long moved_swap;
Daisuke Nishimura8033b972010-03-10 15:22:16 -0800295 struct task_struct *moving_task; /* a task moving charges */
296 wait_queue_head_t waitq; /* a waitq for other context */
297} mc = {
298 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
299};
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -0800300
Balbir Singh4e416952009-09-23 15:56:39 -0700301/*
302 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
303 * limit reclaim to prevent infinite loops, if they ever occur.
304 */
305#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
306#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
307
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800308enum charge_type {
309 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
310 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700311 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700312 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800313 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700314 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700315 NR_CHARGE_TYPE,
316};
317
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700318/* only for here (for easy reading.) */
319#define PCGF_CACHE (1UL << PCG_CACHE)
320#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700321#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700322/* Not used, but added here for completeness */
323#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800324
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800325/* for encoding cft->private value on file */
326#define _MEM (0)
327#define _MEMSWAP (1)
328#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
329#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
330#define MEMFILE_ATTR(val) ((val) & 0xffff)
331
Balbir Singh75822b42009-09-23 15:56:38 -0700332/*
333 * Reclaim flags for mem_cgroup_hierarchical_reclaim
334 */
335#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
336#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
337#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
338#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700339#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
340#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700341
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800342static void mem_cgroup_get(struct mem_cgroup *mem);
343static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800344static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800345static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800346
Balbir Singhf64c3f52009-09-23 15:56:37 -0700347static struct mem_cgroup_per_zone *
348mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
349{
350 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
351}
352
Wu Fengguangd3242362009-12-16 12:19:59 +0100353struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
354{
355 return &mem->css;
356}
357
Balbir Singhf64c3f52009-09-23 15:56:37 -0700358static struct mem_cgroup_per_zone *
359page_cgroup_zoneinfo(struct page_cgroup *pc)
360{
361 struct mem_cgroup *mem = pc->mem_cgroup;
362 int nid = page_cgroup_nid(pc);
363 int zid = page_cgroup_zid(pc);
364
365 if (!mem)
366 return NULL;
367
368 return mem_cgroup_zoneinfo(mem, nid, zid);
369}
370
371static struct mem_cgroup_tree_per_zone *
372soft_limit_tree_node_zone(int nid, int zid)
373{
374 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
375}
376
377static struct mem_cgroup_tree_per_zone *
378soft_limit_tree_from_page(struct page *page)
379{
380 int nid = page_to_nid(page);
381 int zid = page_zonenum(page);
382
383 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
384}
385
386static void
Balbir Singh4e416952009-09-23 15:56:39 -0700387__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700388 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700389 struct mem_cgroup_tree_per_zone *mctz,
390 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700391{
392 struct rb_node **p = &mctz->rb_root.rb_node;
393 struct rb_node *parent = NULL;
394 struct mem_cgroup_per_zone *mz_node;
395
396 if (mz->on_tree)
397 return;
398
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700399 mz->usage_in_excess = new_usage_in_excess;
400 if (!mz->usage_in_excess)
401 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700402 while (*p) {
403 parent = *p;
404 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
405 tree_node);
406 if (mz->usage_in_excess < mz_node->usage_in_excess)
407 p = &(*p)->rb_left;
408 /*
409 * We can't avoid mem cgroups that are over their soft
410 * limit by the same amount
411 */
412 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
413 p = &(*p)->rb_right;
414 }
415 rb_link_node(&mz->tree_node, parent, p);
416 rb_insert_color(&mz->tree_node, &mctz->rb_root);
417 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700418}
419
420static void
421__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
422 struct mem_cgroup_per_zone *mz,
423 struct mem_cgroup_tree_per_zone *mctz)
424{
425 if (!mz->on_tree)
426 return;
427 rb_erase(&mz->tree_node, &mctz->rb_root);
428 mz->on_tree = false;
429}
430
431static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700432mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
433 struct mem_cgroup_per_zone *mz,
434 struct mem_cgroup_tree_per_zone *mctz)
435{
436 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700437 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700438 spin_unlock(&mctz->lock);
439}
440
441static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
442{
443 bool ret = false;
444 int cpu;
445 s64 val;
446 struct mem_cgroup_stat_cpu *cpustat;
447
448 cpu = get_cpu();
449 cpustat = &mem->stat.cpustat[cpu];
Kirill A. Shutemov378ce722010-03-10 15:22:23 -0800450 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_SOFTLIMIT);
451 if (unlikely(val < 0)) {
452 __mem_cgroup_stat_set_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT,
453 SOFTLIMIT_EVENTS_THRESH);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700454 ret = true;
455 }
456 put_cpu();
457 return ret;
458}
459
460static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
461{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700462 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700463 struct mem_cgroup_per_zone *mz;
464 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700465 int nid = page_to_nid(page);
466 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700467 mctz = soft_limit_tree_from_page(page);
468
469 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700470 * Necessary to update all ancestors when hierarchy is used.
471 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700472 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700473 for (; mem; mem = parent_mem_cgroup(mem)) {
474 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700475 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700476 /*
477 * We have to update the tree if mz is on RB-tree or
478 * mem is over its softlimit.
479 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700480 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700481 spin_lock(&mctz->lock);
482 /* if on-tree, remove it */
483 if (mz->on_tree)
484 __mem_cgroup_remove_exceeded(mem, mz, mctz);
485 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700486 * Insert again. mz->usage_in_excess will be updated.
487 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700488 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700489 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700490 spin_unlock(&mctz->lock);
491 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700492 }
493}
494
495static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
496{
497 int node, zone;
498 struct mem_cgroup_per_zone *mz;
499 struct mem_cgroup_tree_per_zone *mctz;
500
501 for_each_node_state(node, N_POSSIBLE) {
502 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
503 mz = mem_cgroup_zoneinfo(mem, node, zone);
504 mctz = soft_limit_tree_node_zone(node, zone);
505 mem_cgroup_remove_exceeded(mem, mz, mctz);
506 }
507 }
508}
509
Balbir Singh4e416952009-09-23 15:56:39 -0700510static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
511{
512 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
513}
514
515static struct mem_cgroup_per_zone *
516__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
517{
518 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700519 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700520
521retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700522 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700523 rightmost = rb_last(&mctz->rb_root);
524 if (!rightmost)
525 goto done; /* Nothing to reclaim from */
526
527 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
528 /*
529 * Remove the node now but someone else can add it back,
530 * we will to add it back at the end of reclaim to its correct
531 * position in the tree.
532 */
533 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
534 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
535 !css_tryget(&mz->mem->css))
536 goto retry;
537done:
538 return mz;
539}
540
541static struct mem_cgroup_per_zone *
542mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
543{
544 struct mem_cgroup_per_zone *mz;
545
546 spin_lock(&mctz->lock);
547 mz = __mem_cgroup_largest_soft_limit_node(mctz);
548 spin_unlock(&mctz->lock);
549 return mz;
550}
551
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700552static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
553 bool charge)
554{
555 int val = (charge) ? 1 : -1;
556 struct mem_cgroup_stat *stat = &mem->stat;
557 struct mem_cgroup_stat_cpu *cpustat;
558 int cpu = get_cpu();
559
560 cpustat = &stat->cpustat[cpu];
561 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
562 put_cpu();
563}
564
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700565static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
566 struct page_cgroup *pc,
567 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800568{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700569 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800570 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700571 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800572 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800573
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800574 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700575 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700576 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800577 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700578 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700579
580 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700581 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700582 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
583 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700584 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700585 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Kirill A. Shutemov378ce722010-03-10 15:22:23 -0800586 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SOFTLIMIT, -1);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -0800587 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS, -1);
588
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800589 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800590}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800591
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700592static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700593 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800594{
595 int nid, zid;
596 struct mem_cgroup_per_zone *mz;
597 u64 total = 0;
598
599 for_each_online_node(nid)
600 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
601 mz = mem_cgroup_zoneinfo(mem, nid, zid);
602 total += MEM_CGROUP_ZSTAT(mz, idx);
603 }
604 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800605}
606
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800607static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800608{
609 return container_of(cgroup_subsys_state(cont,
610 mem_cgroup_subsys_id), struct mem_cgroup,
611 css);
612}
613
Balbir Singhcf475ad2008-04-29 01:00:16 -0700614struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800615{
Balbir Singh31a78f22008-09-28 23:09:31 +0100616 /*
617 * mm_update_next_owner() may clear mm->owner to NULL
618 * if it races with swapoff, page migration, etc.
619 * So this can be called with p == NULL.
620 */
621 if (unlikely(!p))
622 return NULL;
623
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800624 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
625 struct mem_cgroup, css);
626}
627
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800628static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
629{
630 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700631
632 if (!mm)
633 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800634 /*
635 * Because we have no locks, mm->owner's may be being moved to other
636 * cgroup. We use css_tryget() here even if this looks
637 * pessimistic (rather than adding locks here).
638 */
639 rcu_read_lock();
640 do {
641 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
642 if (unlikely(!mem))
643 break;
644 } while (!css_tryget(&mem->css));
645 rcu_read_unlock();
646 return mem;
647}
648
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700649/*
650 * Call callback function against all cgroup under hierarchy tree.
651 */
652static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
653 int (*func)(struct mem_cgroup *, void *))
654{
655 int found, ret, nextid;
656 struct cgroup_subsys_state *css;
657 struct mem_cgroup *mem;
658
659 if (!root->use_hierarchy)
660 return (*func)(root, data);
661
662 nextid = 1;
663 do {
664 ret = 0;
665 mem = NULL;
666
667 rcu_read_lock();
668 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
669 &found);
670 if (css && css_tryget(css))
671 mem = container_of(css, struct mem_cgroup, css);
672 rcu_read_unlock();
673
674 if (mem) {
675 ret = (*func)(mem, data);
676 css_put(&mem->css);
677 }
678 nextid = found + 1;
679 } while (!ret && css);
680
681 return ret;
682}
683
Balbir Singh4b3bde42009-09-23 15:56:32 -0700684static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
685{
686 return (mem == root_mem_cgroup);
687}
688
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800689/*
690 * Following LRU functions are allowed to be used without PCG_LOCK.
691 * Operations are called by routine of global LRU independently from memcg.
692 * What we have to take care of here is validness of pc->mem_cgroup.
693 *
694 * Changes to pc->mem_cgroup happens when
695 * 1. charge
696 * 2. moving account
697 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
698 * It is added to LRU before charge.
699 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
700 * When moving account, the page is not on LRU. It's isolated.
701 */
702
703void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800704{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800705 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800706 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700707
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800708 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800709 return;
710 pc = lookup_page_cgroup(page);
711 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700712 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800713 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700714 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800715 /*
716 * We don't check PCG_USED bit. It's cleared when the "page" is finally
717 * removed from global LRU.
718 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800719 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700720 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700721 if (mem_cgroup_is_root(pc->mem_cgroup))
722 return;
723 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800724 list_del_init(&pc->lru);
725 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800726}
727
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800728void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800729{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800730 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800731}
732
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800733void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800734{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800735 struct mem_cgroup_per_zone *mz;
736 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800737
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800738 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700739 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700740
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800741 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800742 /*
743 * Used bit is set without atomic ops but after smp_wmb().
744 * For making pc->mem_cgroup visible, insert smp_rmb() here.
745 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800746 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700747 /* unused or root page is not rotated. */
748 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800749 return;
750 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700751 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800752}
753
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800754void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
755{
756 struct page_cgroup *pc;
757 struct mem_cgroup_per_zone *mz;
758
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800759 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800760 return;
761 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700762 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800763 /*
764 * Used bit is set without atomic ops but after smp_wmb().
765 * For making pc->mem_cgroup visible, insert smp_rmb() here.
766 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800767 smp_rmb();
768 if (!PageCgroupUsed(pc))
769 return;
770
771 mz = page_cgroup_zoneinfo(pc);
772 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700773 SetPageCgroupAcctLRU(pc);
774 if (mem_cgroup_is_root(pc->mem_cgroup))
775 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800776 list_add(&pc->lru, &mz->lists[lru]);
777}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800778
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800779/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800780 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
781 * lru because the page may.be reused after it's fully uncharged (because of
782 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
783 * it again. This function is only used to charge SwapCache. It's done under
784 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800785 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800786static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800787{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800788 unsigned long flags;
789 struct zone *zone = page_zone(page);
790 struct page_cgroup *pc = lookup_page_cgroup(page);
791
792 spin_lock_irqsave(&zone->lru_lock, flags);
793 /*
794 * Forget old LRU when this page_cgroup is *not* used. This Used bit
795 * is guarded by lock_page() because the page is SwapCache.
796 */
797 if (!PageCgroupUsed(pc))
798 mem_cgroup_del_lru_list(page, page_lru(page));
799 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800800}
801
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800802static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
803{
804 unsigned long flags;
805 struct zone *zone = page_zone(page);
806 struct page_cgroup *pc = lookup_page_cgroup(page);
807
808 spin_lock_irqsave(&zone->lru_lock, flags);
809 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700810 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800811 mem_cgroup_add_lru_list(page, page_lru(page));
812 spin_unlock_irqrestore(&zone->lru_lock, flags);
813}
814
815
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800816void mem_cgroup_move_lists(struct page *page,
817 enum lru_list from, enum lru_list to)
818{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800819 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800820 return;
821 mem_cgroup_del_lru_list(page, from);
822 mem_cgroup_add_lru_list(page, to);
823}
824
David Rientjes4c4a2212008-02-07 00:14:06 -0800825int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
826{
827 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700828 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800829
830 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700831 rcu_read_lock();
832 curr = try_get_mem_cgroup_from_mm(task->mm);
833 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800834 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700835 if (!curr)
836 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800837 /*
838 * We should check use_hierarchy of "mem" not "curr". Because checking
839 * use_hierarchy of "curr" here make this function true if hierarchy is
840 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
841 * hierarchy(even if use_hierarchy is disabled in "mem").
842 */
843 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700844 ret = css_is_ancestor(&curr->css, &mem->css);
845 else
846 ret = (curr == mem);
847 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800848 return ret;
849}
850
Balbir Singh66e17072008-02-07 00:13:56 -0800851/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800852 * prev_priority control...this will be used in memory reclaim path.
853 */
854int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
855{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800856 int prev_priority;
857
858 spin_lock(&mem->reclaim_param_lock);
859 prev_priority = mem->prev_priority;
860 spin_unlock(&mem->reclaim_param_lock);
861
862 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800863}
864
865void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
866{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800867 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800868 if (priority < mem->prev_priority)
869 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800870 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800871}
872
873void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
874{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800875 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800876 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800877 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800878}
879
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800880static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800881{
882 unsigned long active;
883 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800884 unsigned long gb;
885 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800886
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700887 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
888 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800889
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800890 gb = (inactive + active) >> (30 - PAGE_SHIFT);
891 if (gb)
892 inactive_ratio = int_sqrt(10 * gb);
893 else
894 inactive_ratio = 1;
895
896 if (present_pages) {
897 present_pages[0] = inactive;
898 present_pages[1] = active;
899 }
900
901 return inactive_ratio;
902}
903
904int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
905{
906 unsigned long active;
907 unsigned long inactive;
908 unsigned long present_pages[2];
909 unsigned long inactive_ratio;
910
911 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
912
913 inactive = present_pages[0];
914 active = present_pages[1];
915
916 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800917 return 1;
918
919 return 0;
920}
921
Rik van Riel56e49d22009-06-16 15:32:28 -0700922int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
923{
924 unsigned long active;
925 unsigned long inactive;
926
927 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
928 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
929
930 return (active > inactive);
931}
932
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800933unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
934 struct zone *zone,
935 enum lru_list lru)
936{
937 int nid = zone->zone_pgdat->node_id;
938 int zid = zone_idx(zone);
939 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
940
941 return MEM_CGROUP_ZSTAT(mz, lru);
942}
943
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800944struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
945 struct zone *zone)
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 &mz->reclaim_stat;
952}
953
954struct zone_reclaim_stat *
955mem_cgroup_get_reclaim_stat_from_page(struct page *page)
956{
957 struct page_cgroup *pc;
958 struct mem_cgroup_per_zone *mz;
959
960 if (mem_cgroup_disabled())
961 return NULL;
962
963 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800964 /*
965 * Used bit is set without atomic ops but after smp_wmb().
966 * For making pc->mem_cgroup visible, insert smp_rmb() here.
967 */
968 smp_rmb();
969 if (!PageCgroupUsed(pc))
970 return NULL;
971
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800972 mz = page_cgroup_zoneinfo(pc);
973 if (!mz)
974 return NULL;
975
976 return &mz->reclaim_stat;
977}
978
Balbir Singh66e17072008-02-07 00:13:56 -0800979unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
980 struct list_head *dst,
981 unsigned long *scanned, int order,
982 int mode, struct zone *z,
983 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700984 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800985{
986 unsigned long nr_taken = 0;
987 struct page *page;
988 unsigned long scan;
989 LIST_HEAD(pc_list);
990 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800991 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800992 int nid = z->zone_pgdat->node_id;
993 int zid = zone_idx(z);
994 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700995 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700996 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800997
Balbir Singhcf475ad2008-04-29 01:00:16 -0700998 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800999 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001000 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -08001001
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001002 scan = 0;
1003 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -08001004 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001005 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001006
1007 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001008 if (unlikely(!PageCgroupUsed(pc)))
1009 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -08001010 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001011 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -08001012
Hugh Dickins436c65412008-02-07 00:14:12 -08001013 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001014 ret = __isolate_lru_page(page, mode, file);
1015 switch (ret) {
1016 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -08001017 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001018 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -08001019 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -07001020 break;
1021 case -EBUSY:
1022 /* we don't affect global LRU but rotate in our LRU */
1023 mem_cgroup_rotate_lru_list(page, page_lru(page));
1024 break;
1025 default:
1026 break;
Balbir Singh66e17072008-02-07 00:13:56 -08001027 }
1028 }
1029
Balbir Singh66e17072008-02-07 00:13:56 -08001030 *scanned = scan;
1031 return nr_taken;
1032}
1033
Balbir Singh6d61ef42009-01-07 18:08:06 -08001034#define mem_cgroup_from_res_counter(counter, member) \
1035 container_of(counter, struct mem_cgroup, member)
1036
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001037static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1038{
1039 if (do_swap_account) {
1040 if (res_counter_check_under_limit(&mem->res) &&
1041 res_counter_check_under_limit(&mem->memsw))
1042 return true;
1043 } else
1044 if (res_counter_check_under_limit(&mem->res))
1045 return true;
1046 return false;
1047}
1048
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08001049static unsigned int get_swappiness(struct mem_cgroup *memcg)
1050{
1051 struct cgroup *cgrp = memcg->css.cgroup;
1052 unsigned int swappiness;
1053
1054 /* root ? */
1055 if (cgrp->parent == NULL)
1056 return vm_swappiness;
1057
1058 spin_lock(&memcg->reclaim_param_lock);
1059 swappiness = memcg->swappiness;
1060 spin_unlock(&memcg->reclaim_param_lock);
1061
1062 return swappiness;
1063}
1064
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001065static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1066{
1067 int *val = data;
1068 (*val)++;
1069 return 0;
1070}
Balbir Singhe2224322009-04-02 16:57:39 -07001071
1072/**
Kirill A. Shutemov6a6135b2010-03-10 15:22:25 -08001073 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
Balbir Singhe2224322009-04-02 16:57:39 -07001074 * @memcg: The memory cgroup that went over limit
1075 * @p: Task that is going to be killed
1076 *
1077 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1078 * enabled
1079 */
1080void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1081{
1082 struct cgroup *task_cgrp;
1083 struct cgroup *mem_cgrp;
1084 /*
1085 * Need a buffer in BSS, can't rely on allocations. The code relies
1086 * on the assumption that OOM is serialized for memory controller.
1087 * If this assumption is broken, revisit this code.
1088 */
1089 static char memcg_name[PATH_MAX];
1090 int ret;
1091
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001092 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001093 return;
1094
1095
1096 rcu_read_lock();
1097
1098 mem_cgrp = memcg->css.cgroup;
1099 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1100
1101 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1102 if (ret < 0) {
1103 /*
1104 * Unfortunately, we are unable to convert to a useful name
1105 * But we'll still print out the usage information
1106 */
1107 rcu_read_unlock();
1108 goto done;
1109 }
1110 rcu_read_unlock();
1111
1112 printk(KERN_INFO "Task in %s killed", memcg_name);
1113
1114 rcu_read_lock();
1115 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1116 if (ret < 0) {
1117 rcu_read_unlock();
1118 goto done;
1119 }
1120 rcu_read_unlock();
1121
1122 /*
1123 * Continues from above, so we don't need an KERN_ level
1124 */
1125 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1126done:
1127
1128 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1129 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1130 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1131 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1132 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1133 "failcnt %llu\n",
1134 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1135 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1136 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1137}
1138
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001139/*
1140 * This function returns the number of memcg under hierarchy tree. Returns
1141 * 1(self count) if no children.
1142 */
1143static int mem_cgroup_count_children(struct mem_cgroup *mem)
1144{
1145 int num = 0;
1146 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1147 return num;
1148}
1149
Balbir Singh6d61ef42009-01-07 18:08:06 -08001150/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001151 * Visit the first child (need not be the first child as per the ordering
1152 * of the cgroup list, since we track last_scanned_child) of @mem and use
1153 * that to reclaim free pages from.
1154 */
1155static struct mem_cgroup *
1156mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1157{
1158 struct mem_cgroup *ret = NULL;
1159 struct cgroup_subsys_state *css;
1160 int nextid, found;
1161
1162 if (!root_mem->use_hierarchy) {
1163 css_get(&root_mem->css);
1164 ret = root_mem;
1165 }
1166
1167 while (!ret) {
1168 rcu_read_lock();
1169 nextid = root_mem->last_scanned_child + 1;
1170 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1171 &found);
1172 if (css && css_tryget(css))
1173 ret = container_of(css, struct mem_cgroup, css);
1174
1175 rcu_read_unlock();
1176 /* Updates scanning parameter */
1177 spin_lock(&root_mem->reclaim_param_lock);
1178 if (!css) {
1179 /* this means start scan from ID:1 */
1180 root_mem->last_scanned_child = 0;
1181 } else
1182 root_mem->last_scanned_child = found;
1183 spin_unlock(&root_mem->reclaim_param_lock);
1184 }
1185
1186 return ret;
1187}
1188
1189/*
1190 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1191 * we reclaimed from, so that we don't end up penalizing one child extensively
1192 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001193 *
1194 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001195 *
1196 * We give up and return to the caller when we visit root_mem twice.
1197 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001198 *
1199 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001200 */
1201static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001202 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001203 gfp_t gfp_mask,
1204 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001205{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001206 struct mem_cgroup *victim;
1207 int ret, total = 0;
1208 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001209 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1210 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001211 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1212 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001213
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001214 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1215 if (root_mem->memsw_is_minimum)
1216 noswap = true;
1217
Balbir Singh4e416952009-09-23 15:56:39 -07001218 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001219 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001220 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001221 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001222 if (loop >= 1)
1223 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001224 if (loop >= 2) {
1225 /*
1226 * If we have not been able to reclaim
1227 * anything, it might because there are
1228 * no reclaimable pages under this hierarchy
1229 */
1230 if (!check_soft || !total) {
1231 css_put(&victim->css);
1232 break;
1233 }
1234 /*
1235 * We want to do more targetted reclaim.
1236 * excess >> 2 is not to excessive so as to
1237 * reclaim too much, nor too less that we keep
1238 * coming back to reclaim from this cgroup
1239 */
1240 if (total >= (excess >> 2) ||
1241 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1242 css_put(&victim->css);
1243 break;
1244 }
1245 }
1246 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001247 if (!mem_cgroup_local_usage(&victim->stat)) {
1248 /* this cgroup's local usage == 0 */
1249 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001250 continue;
1251 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001252 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001253 if (check_soft)
1254 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1255 noswap, get_swappiness(victim), zone,
1256 zone->zone_pgdat->node_id);
1257 else
1258 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1259 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001260 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001261 /*
1262 * At shrinking usage, we can't check we should stop here or
1263 * reclaim more. It's depends on callers. last_scanned_child
1264 * will work enough for keeping fairness under tree.
1265 */
1266 if (shrink)
1267 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001268 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001269 if (check_soft) {
1270 if (res_counter_check_under_soft_limit(&root_mem->res))
1271 return total;
1272 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001273 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001274 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001275 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001276}
1277
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001278bool mem_cgroup_oom_called(struct task_struct *task)
1279{
1280 bool ret = false;
1281 struct mem_cgroup *mem;
1282 struct mm_struct *mm;
1283
1284 rcu_read_lock();
1285 mm = task->mm;
1286 if (!mm)
1287 mm = &init_mm;
1288 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1289 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1290 ret = true;
1291 rcu_read_unlock();
1292 return ret;
1293}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001294
1295static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1296{
1297 mem->last_oom_jiffies = jiffies;
1298 return 0;
1299}
1300
1301static void record_last_oom(struct mem_cgroup *mem)
1302{
1303 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1304}
1305
Balbir Singhd69b0422009-06-17 16:26:34 -07001306/*
1307 * Currently used to update mapped file statistics, but the routine can be
1308 * generalized to update other statistics as well.
1309 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001310void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001311{
1312 struct mem_cgroup *mem;
1313 struct mem_cgroup_stat *stat;
1314 struct mem_cgroup_stat_cpu *cpustat;
1315 int cpu;
1316 struct page_cgroup *pc;
1317
Balbir Singhd69b0422009-06-17 16:26:34 -07001318 pc = lookup_page_cgroup(page);
1319 if (unlikely(!pc))
1320 return;
1321
1322 lock_page_cgroup(pc);
1323 mem = pc->mem_cgroup;
1324 if (!mem)
1325 goto done;
1326
1327 if (!PageCgroupUsed(pc))
1328 goto done;
1329
1330 /*
1331 * Preemption is already disabled, we don't need get_cpu()
1332 */
1333 cpu = smp_processor_id();
1334 stat = &mem->stat;
1335 cpustat = &stat->cpustat[cpu];
1336
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001337 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001338done:
1339 unlock_page_cgroup(pc);
1340}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001341
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001342/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001343 * size of first charge trial. "32" comes from vmscan.c's magic value.
1344 * TODO: maybe necessary to use big numbers in big irons.
1345 */
1346#define CHARGE_SIZE (32 * PAGE_SIZE)
1347struct memcg_stock_pcp {
1348 struct mem_cgroup *cached; /* this never be root cgroup */
1349 int charge;
1350 struct work_struct work;
1351};
1352static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1353static atomic_t memcg_drain_count;
1354
1355/*
1356 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1357 * from local stock and true is returned. If the stock is 0 or charges from a
1358 * cgroup which is not current target, returns false. This stock will be
1359 * refilled.
1360 */
1361static bool consume_stock(struct mem_cgroup *mem)
1362{
1363 struct memcg_stock_pcp *stock;
1364 bool ret = true;
1365
1366 stock = &get_cpu_var(memcg_stock);
1367 if (mem == stock->cached && stock->charge)
1368 stock->charge -= PAGE_SIZE;
1369 else /* need to call res_counter_charge */
1370 ret = false;
1371 put_cpu_var(memcg_stock);
1372 return ret;
1373}
1374
1375/*
1376 * Returns stocks cached in percpu to res_counter and reset cached information.
1377 */
1378static void drain_stock(struct memcg_stock_pcp *stock)
1379{
1380 struct mem_cgroup *old = stock->cached;
1381
1382 if (stock->charge) {
1383 res_counter_uncharge(&old->res, stock->charge);
1384 if (do_swap_account)
1385 res_counter_uncharge(&old->memsw, stock->charge);
1386 }
1387 stock->cached = NULL;
1388 stock->charge = 0;
1389}
1390
1391/*
1392 * This must be called under preempt disabled or must be called by
1393 * a thread which is pinned to local cpu.
1394 */
1395static void drain_local_stock(struct work_struct *dummy)
1396{
1397 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1398 drain_stock(stock);
1399}
1400
1401/*
1402 * Cache charges(val) which is from res_counter, to local per_cpu area.
1403 * This will be consumed by consumt_stock() function, later.
1404 */
1405static void refill_stock(struct mem_cgroup *mem, int val)
1406{
1407 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1408
1409 if (stock->cached != mem) { /* reset if necessary */
1410 drain_stock(stock);
1411 stock->cached = mem;
1412 }
1413 stock->charge += val;
1414 put_cpu_var(memcg_stock);
1415}
1416
1417/*
1418 * Tries to drain stocked charges in other cpus. This function is asynchronous
1419 * and just put a work per cpu for draining localy on each cpu. Caller can
1420 * expects some charges will be back to res_counter later but cannot wait for
1421 * it.
1422 */
1423static void drain_all_stock_async(void)
1424{
1425 int cpu;
1426 /* This function is for scheduling "drain" in asynchronous way.
1427 * The result of "drain" is not directly handled by callers. Then,
1428 * if someone is calling drain, we don't have to call drain more.
1429 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1430 * there is a race. We just do loose check here.
1431 */
1432 if (atomic_read(&memcg_drain_count))
1433 return;
1434 /* Notify other cpus that system-wide "drain" is running */
1435 atomic_inc(&memcg_drain_count);
1436 get_online_cpus();
1437 for_each_online_cpu(cpu) {
1438 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1439 schedule_work_on(cpu, &stock->work);
1440 }
1441 put_online_cpus();
1442 atomic_dec(&memcg_drain_count);
1443 /* We don't wait for flush_work */
1444}
1445
1446/* This is a synchronous drain interface. */
1447static void drain_all_stock_sync(void)
1448{
1449 /* called when force_empty is called */
1450 atomic_inc(&memcg_drain_count);
1451 schedule_on_each_cpu(drain_local_stock);
1452 atomic_dec(&memcg_drain_count);
1453}
1454
1455static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1456 unsigned long action,
1457 void *hcpu)
1458{
1459 int cpu = (unsigned long)hcpu;
1460 struct memcg_stock_pcp *stock;
1461
1462 if (action != CPU_DEAD)
1463 return NOTIFY_OK;
1464 stock = &per_cpu(memcg_stock, cpu);
1465 drain_stock(stock);
1466 return NOTIFY_OK;
1467}
1468
1469/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001470 * Unlike exported interface, "oom" parameter is added. if oom==true,
1471 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001472 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001473static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001474 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001475 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001476{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001477 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001478 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001479 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001480 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001481
1482 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1483 /* Don't account this! */
1484 *memcg = NULL;
1485 return 0;
1486 }
1487
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001488 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08001489 * We always charge the cgroup the mm_struct belongs to.
1490 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001491 * thread group leader migrates. It's possible that mm is not
1492 * set, if so charge the init_mm (happens for pagecache usage).
1493 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001494 mem = *memcg;
1495 if (likely(!mem)) {
1496 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001497 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001498 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001499 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001500 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001501 if (unlikely(!mem))
1502 return 0;
1503
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001504 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001505 if (mem_cgroup_is_root(mem))
1506 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001507
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001508 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001509 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001510 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001511
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001512 if (consume_stock(mem))
1513 goto charged;
1514
1515 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001516 if (likely(!ret)) {
1517 if (!do_swap_account)
1518 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001519 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001520 if (likely(!ret))
1521 break;
1522 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001523 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001524 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001525 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1526 memsw);
1527 } else
1528 /* mem counter fails */
1529 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1530 res);
1531
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001532 /* reduce request size and retry */
1533 if (csize > PAGE_SIZE) {
1534 csize = PAGE_SIZE;
1535 continue;
1536 }
Hugh Dickins3be912772008-02-07 00:14:19 -08001537 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001538 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001539
Balbir Singh4e416952009-09-23 15:56:39 -07001540 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1541 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001542 if (ret)
1543 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001544
1545 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001546 * try_to_free_mem_cgroup_pages() might not give us a full
1547 * picture of reclaim. Some pages are reclaimed and might be
1548 * moved to swap cache or just unmapped from the cgroup.
1549 * Check the limit again to see if the reclaim reduced the
1550 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001551 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001552 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001553 if (mem_cgroup_check_under_limit(mem_over_limit))
1554 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -08001555
Daisuke Nishimura8033b972010-03-10 15:22:16 -08001556 /* try to avoid oom while someone is moving charge */
1557 if (mc.moving_task && current != mc.moving_task) {
1558 struct mem_cgroup *from, *to;
1559 bool do_continue = false;
1560 /*
1561 * There is a small race that "from" or "to" can be
1562 * freed by rmdir, so we use css_tryget().
1563 */
1564 rcu_read_lock();
1565 from = mc.from;
1566 to = mc.to;
1567 if (from && css_tryget(&from->css)) {
1568 if (mem_over_limit->use_hierarchy)
1569 do_continue = css_is_ancestor(
1570 &from->css,
1571 &mem_over_limit->css);
1572 else
1573 do_continue = (from == mem_over_limit);
1574 css_put(&from->css);
1575 }
1576 if (!do_continue && to && css_tryget(&to->css)) {
1577 if (mem_over_limit->use_hierarchy)
1578 do_continue = css_is_ancestor(
1579 &to->css,
1580 &mem_over_limit->css);
1581 else
1582 do_continue = (to == mem_over_limit);
1583 css_put(&to->css);
1584 }
1585 rcu_read_unlock();
1586 if (do_continue) {
1587 DEFINE_WAIT(wait);
1588 prepare_to_wait(&mc.waitq, &wait,
1589 TASK_INTERRUPTIBLE);
1590 /* moving charge context might have finished. */
1591 if (mc.moving_task)
1592 schedule();
1593 finish_wait(&mc.waitq, &wait);
1594 continue;
1595 }
1596 }
1597
Hugh Dickins3be912772008-02-07 00:14:19 -08001598 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001599 if (oom) {
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001600 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001601 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001602 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001603 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001604 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001605 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001606 if (csize > PAGE_SIZE)
1607 refill_stock(mem, csize - PAGE_SIZE);
1608charged:
Balbir Singhf64c3f52009-09-23 15:56:37 -07001609 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001610 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1611 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001612 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001613 if (page && mem_cgroup_soft_limit_check(mem))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001614 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001615done:
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08001616 if (mem_cgroup_threshold_check(mem))
1617 mem_cgroup_threshold(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001618 return 0;
1619nomem:
1620 css_put(&mem->css);
1621 return -ENOMEM;
1622}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001623
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001624/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001625 * Somemtimes we have to undo a charge we got by try_charge().
1626 * This function is for that and do uncharge, put css's refcnt.
1627 * gotten by try_charge().
1628 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001629static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1630 unsigned long count)
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001631{
1632 if (!mem_cgroup_is_root(mem)) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001633 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001634 if (do_swap_account)
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001635 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1636 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
1637 WARN_ON_ONCE(count > INT_MAX);
1638 __css_put(&mem->css, (int)count);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001639 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001640 /* we don't need css_put for root */
1641}
1642
1643static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1644{
1645 __mem_cgroup_cancel_charge(mem, 1);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001646}
1647
1648/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001649 * A helper function to get mem_cgroup from ID. must be called under
1650 * rcu_read_lock(). The caller must check css_is_removed() or some if
1651 * it's concern. (dropping refcnt from swap can be called against removed
1652 * memcg.)
1653 */
1654static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1655{
1656 struct cgroup_subsys_state *css;
1657
1658 /* ID 0 is unused ID */
1659 if (!id)
1660 return NULL;
1661 css = css_lookup(&mem_cgroup_subsys, id);
1662 if (!css)
1663 return NULL;
1664 return container_of(css, struct mem_cgroup, css);
1665}
1666
Wu Fengguange42d9d52009-12-16 12:19:59 +01001667struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001668{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001669 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001670 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001671 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001672 swp_entry_t ent;
1673
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001674 VM_BUG_ON(!PageLocked(page));
1675
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001676 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001677 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001678 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001679 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001680 if (mem && !css_tryget(&mem->css))
1681 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001682 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001683 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001684 id = lookup_swap_cgroup(ent);
1685 rcu_read_lock();
1686 mem = mem_cgroup_lookup(id);
1687 if (mem && !css_tryget(&mem->css))
1688 mem = NULL;
1689 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001690 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001691 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001692 return mem;
1693}
1694
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001695/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001696 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001697 * USED state. If already USED, uncharge and return.
1698 */
1699
1700static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1701 struct page_cgroup *pc,
1702 enum charge_type ctype)
1703{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001704 /* try_charge() can return NULL to *memcg, taking care of it. */
1705 if (!mem)
1706 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001707
1708 lock_page_cgroup(pc);
1709 if (unlikely(PageCgroupUsed(pc))) {
1710 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001711 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001712 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001713 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001714
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001715 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001716 /*
1717 * We access a page_cgroup asynchronously without lock_page_cgroup().
1718 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1719 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1720 * before USED bit, we need memory barrier here.
1721 * See mem_cgroup_add_lru_list(), etc.
1722 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001723 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001724 switch (ctype) {
1725 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1726 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1727 SetPageCgroupCache(pc);
1728 SetPageCgroupUsed(pc);
1729 break;
1730 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1731 ClearPageCgroupCache(pc);
1732 SetPageCgroupUsed(pc);
1733 break;
1734 default:
1735 break;
1736 }
Hugh Dickins3be912772008-02-07 00:14:19 -08001737
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001738 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001739
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001740 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001741}
1742
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001743/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001744 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001745 * @pc: page_cgroup of the page.
1746 * @from: mem_cgroup which the page is moved from.
1747 * @to: mem_cgroup which the page is moved to. @from != @to.
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001748 * @uncharge: whether we should call uncharge and css_put against @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001749 *
1750 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001751 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001752 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001753 *
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001754 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1755 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1756 * true, this function does "uncharge" from old cgroup, but it doesn't if
1757 * @uncharge is false, so a caller should do "uncharge".
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001758 */
1759
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001760static void __mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001761 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001762{
Balbir Singhd69b0422009-06-17 16:26:34 -07001763 struct page *page;
1764 int cpu;
1765 struct mem_cgroup_stat *stat;
1766 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001767
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001768 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001769 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001770 VM_BUG_ON(!PageCgroupLocked(pc));
1771 VM_BUG_ON(!PageCgroupUsed(pc));
1772 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001773
Balbir Singhd69b0422009-06-17 16:26:34 -07001774 page = pc->page;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001775 if (page_mapped(page) && !PageAnon(page)) {
Balbir Singhd69b0422009-06-17 16:26:34 -07001776 cpu = smp_processor_id();
1777 /* Update mapped_file data for mem_cgroup "from" */
1778 stat = &from->stat;
1779 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001780 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001781 -1);
1782
1783 /* Update mapped_file data for mem_cgroup "to" */
1784 stat = &to->stat;
1785 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001786 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001787 1);
1788 }
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001789 mem_cgroup_charge_statistics(from, pc, false);
1790 if (uncharge)
1791 /* This is not "cancel", but cancel_charge does all we need. */
1792 mem_cgroup_cancel_charge(from);
Balbir Singhd69b0422009-06-17 16:26:34 -07001793
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001794 /* caller should have done css_get */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001795 pc->mem_cgroup = to;
1796 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001797 /*
1798 * We charges against "to" which may not have any tasks. Then, "to"
1799 * can be under rmdir(). But in current implementation, caller of
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08001800 * this function is just force_empty() and move charge, so it's
1801 * garanteed that "to" is never removed. So, we don't check rmdir
1802 * status here.
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001803 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001804}
1805
1806/*
1807 * check whether the @pc is valid for moving account and call
1808 * __mem_cgroup_move_account()
1809 */
1810static int mem_cgroup_move_account(struct page_cgroup *pc,
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001811 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001812{
1813 int ret = -EINVAL;
1814 lock_page_cgroup(pc);
1815 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001816 __mem_cgroup_move_account(pc, from, to, uncharge);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001817 ret = 0;
1818 }
1819 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001820 return ret;
1821}
1822
1823/*
1824 * move charges to its parent.
1825 */
1826
1827static int mem_cgroup_move_parent(struct page_cgroup *pc,
1828 struct mem_cgroup *child,
1829 gfp_t gfp_mask)
1830{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001831 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001832 struct cgroup *cg = child->css.cgroup;
1833 struct cgroup *pcg = cg->parent;
1834 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001835 int ret;
1836
1837 /* Is ROOT ? */
1838 if (!pcg)
1839 return -EINVAL;
1840
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001841 ret = -EBUSY;
1842 if (!get_page_unless_zero(page))
1843 goto out;
1844 if (isolate_lru_page(page))
1845 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001846
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001847 parent = mem_cgroup_from_cont(pcg);
Balbir Singhf64c3f52009-09-23 15:56:37 -07001848 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001849 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001850 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001851
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08001852 ret = mem_cgroup_move_account(pc, child, parent, true);
1853 if (ret)
1854 mem_cgroup_cancel_charge(parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001855put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001856 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001857put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001858 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001859out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001860 return ret;
1861}
1862
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001863/*
1864 * Charge the memory controller for page usage.
1865 * Return
1866 * 0 if the charge was successful
1867 * < 0 if the cgroup is over its limit
1868 */
1869static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1870 gfp_t gfp_mask, enum charge_type ctype,
1871 struct mem_cgroup *memcg)
1872{
1873 struct mem_cgroup *mem;
1874 struct page_cgroup *pc;
1875 int ret;
1876
1877 pc = lookup_page_cgroup(page);
1878 /* can happen at boot */
1879 if (unlikely(!pc))
1880 return 0;
1881 prefetchw(pc);
1882
1883 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001884 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001885 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001886 return ret;
1887
1888 __mem_cgroup_commit_charge(mem, pc, ctype);
1889 return 0;
1890}
1891
1892int mem_cgroup_newpage_charge(struct page *page,
1893 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001894{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001895 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001896 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001897 if (PageCompound(page))
1898 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001899 /*
1900 * If already mapped, we don't have to account.
1901 * If page cache, page->mapping has address_space.
1902 * But page->mapping may have out-of-use anon_vma pointer,
1903 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1904 * is NULL.
1905 */
1906 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1907 return 0;
1908 if (unlikely(!mm))
1909 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001910 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001911 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001912}
1913
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001914static void
1915__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1916 enum charge_type ctype);
1917
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001918int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1919 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001920{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001921 struct mem_cgroup *mem = NULL;
1922 int ret;
1923
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001924 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001925 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001926 if (PageCompound(page))
1927 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001928 /*
1929 * Corner case handling. This is called from add_to_page_cache()
1930 * in usual. But some FS (shmem) precharges this page before calling it
1931 * and call add_to_page_cache() with GFP_NOWAIT.
1932 *
1933 * For GFP_NOWAIT case, the page may be pre-charged before calling
1934 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1935 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001936 * And when the page is SwapCache, it should take swap information
1937 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001938 */
1939 if (!(gfp_mask & __GFP_WAIT)) {
1940 struct page_cgroup *pc;
1941
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001942
1943 pc = lookup_page_cgroup(page);
1944 if (!pc)
1945 return 0;
1946 lock_page_cgroup(pc);
1947 if (PageCgroupUsed(pc)) {
1948 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001949 return 0;
1950 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001951 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001952 }
1953
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001954 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001955 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001956
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001957 if (page_is_file_cache(page))
1958 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001959 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001960
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001961 /* shmem */
1962 if (PageSwapCache(page)) {
1963 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1964 if (!ret)
1965 __mem_cgroup_commit_charge_swapin(page, mem,
1966 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1967 } else
1968 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1969 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001970
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001971 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001972}
1973
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001974/*
1975 * While swap-in, try_charge -> commit or cancel, the page is locked.
1976 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001977 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001978 * "commit()" or removed by "cancel()"
1979 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001980int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1981 struct page *page,
1982 gfp_t mask, struct mem_cgroup **ptr)
1983{
1984 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001985 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001986
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001987 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001988 return 0;
1989
1990 if (!do_swap_account)
1991 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001992 /*
1993 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001994 * the pte, and even removed page from swap cache: in those cases
1995 * do_swap_page()'s pte_same() test will fail; but there's also a
1996 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001997 */
1998 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001999 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01002000 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002001 if (!mem)
2002 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002003 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07002004 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08002005 /* drop extra refcnt from tryget */
2006 css_put(&mem->css);
2007 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002008charge_cur_mm:
2009 if (unlikely(!mm))
2010 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07002011 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002012}
2013
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002014static void
2015__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2016 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002017{
2018 struct page_cgroup *pc;
2019
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002020 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002021 return;
2022 if (!ptr)
2023 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002024 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002025 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002026 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002027 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002028 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002029 /*
2030 * Now swap is on-memory. This means this page may be
2031 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002032 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2033 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2034 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002035 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08002036 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002037 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002038 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002039 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002040
2041 id = swap_cgroup_record(ent, 0);
2042 rcu_read_lock();
2043 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002044 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002045 /*
2046 * This recorded memcg can be obsolete one. So, avoid
2047 * calling css_tryget
2048 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002049 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002050 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002051 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002052 mem_cgroup_put(memcg);
2053 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002054 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002055 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002056 /*
2057 * At swapin, we may charge account against cgroup which has no tasks.
2058 * So, rmdir()->pre_destroy() can be called while we do this charge.
2059 * In that case, we need to call pre_destroy() again. check it here.
2060 */
2061 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002062}
2063
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07002064void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2065{
2066 __mem_cgroup_commit_charge_swapin(page, ptr,
2067 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2068}
2069
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002070void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2071{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002072 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002073 return;
2074 if (!mem)
2075 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08002076 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002077}
2078
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002079static void
2080__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2081{
2082 struct memcg_batch_info *batch = NULL;
2083 bool uncharge_memsw = true;
2084 /* If swapout, usage of swap doesn't decrease */
2085 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2086 uncharge_memsw = false;
2087 /*
2088 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2089 * In those cases, all pages freed continously can be expected to be in
2090 * the same cgroup and we have chance to coalesce uncharges.
2091 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2092 * because we want to do uncharge as soon as possible.
2093 */
2094 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
2095 goto direct_uncharge;
2096
2097 batch = &current->memcg_batch;
2098 /*
2099 * In usual, we do css_get() when we remember memcg pointer.
2100 * But in this case, we keep res->usage until end of a series of
2101 * uncharges. Then, it's ok to ignore memcg's refcnt.
2102 */
2103 if (!batch->memcg)
2104 batch->memcg = mem;
2105 /*
2106 * In typical case, batch->memcg == mem. This means we can
2107 * merge a series of uncharges to an uncharge of res_counter.
2108 * If not, we uncharge res_counter ony by one.
2109 */
2110 if (batch->memcg != mem)
2111 goto direct_uncharge;
2112 /* remember freed charge and uncharge it later */
2113 batch->bytes += PAGE_SIZE;
2114 if (uncharge_memsw)
2115 batch->memsw_bytes += PAGE_SIZE;
2116 return;
2117direct_uncharge:
2118 res_counter_uncharge(&mem->res, PAGE_SIZE);
2119 if (uncharge_memsw)
2120 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2121 return;
2122}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002123
Balbir Singh8697d332008-02-07 00:13:59 -08002124/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002125 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002126 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002127static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002128__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002129{
Hugh Dickins82895462008-03-04 14:29:08 -08002130 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002131 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002132 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002133
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002134 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002135 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002136
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002137 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002138 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002139
Balbir Singh8697d332008-02-07 00:13:59 -08002140 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002141 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002142 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002143 pc = lookup_page_cgroup(page);
2144 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002145 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002146
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002147 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002148
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002149 mem = pc->mem_cgroup;
2150
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002151 if (!PageCgroupUsed(pc))
2152 goto unlock_out;
2153
2154 switch (ctype) {
2155 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002156 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002157 if (page_mapped(page))
2158 goto unlock_out;
2159 break;
2160 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2161 if (!PageAnon(page)) { /* Shared memory */
2162 if (page->mapping && !page_is_file_cache(page))
2163 goto unlock_out;
2164 } else if (page_mapped(page)) /* Anon */
2165 goto unlock_out;
2166 break;
2167 default:
2168 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002169 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002170
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002171 if (!mem_cgroup_is_root(mem))
2172 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002173 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2174 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002175 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002176
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002177 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002178 /*
2179 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2180 * freed from LRU. This is safe because uncharged page is expected not
2181 * to be reused (freed soon). Exception is SwapCache, it's handled by
2182 * special functions.
2183 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002184
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002185 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002186 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002187
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002188 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07002189 mem_cgroup_update_tree(mem, page);
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08002190 if (mem_cgroup_threshold_check(mem))
2191 mem_cgroup_threshold(mem);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002192 /* at swapout, this memcg will be accessed to record to swap */
2193 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2194 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002195
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002196 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002197
2198unlock_out:
2199 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002200 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002201}
2202
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002203void mem_cgroup_uncharge_page(struct page *page)
2204{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002205 /* early check. */
2206 if (page_mapped(page))
2207 return;
2208 if (page->mapping && !PageAnon(page))
2209 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002210 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2211}
2212
2213void mem_cgroup_uncharge_cache_page(struct page *page)
2214{
2215 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002216 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002217 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2218}
2219
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002220/*
2221 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2222 * In that cases, pages are freed continuously and we can expect pages
2223 * are in the same memcg. All these calls itself limits the number of
2224 * pages freed at once, then uncharge_start/end() is called properly.
2225 * This may be called prural(2) times in a context,
2226 */
2227
2228void mem_cgroup_uncharge_start(void)
2229{
2230 current->memcg_batch.do_batch++;
2231 /* We can do nest. */
2232 if (current->memcg_batch.do_batch == 1) {
2233 current->memcg_batch.memcg = NULL;
2234 current->memcg_batch.bytes = 0;
2235 current->memcg_batch.memsw_bytes = 0;
2236 }
2237}
2238
2239void mem_cgroup_uncharge_end(void)
2240{
2241 struct memcg_batch_info *batch = &current->memcg_batch;
2242
2243 if (!batch->do_batch)
2244 return;
2245
2246 batch->do_batch--;
2247 if (batch->do_batch) /* If stacked, do nothing. */
2248 return;
2249
2250 if (!batch->memcg)
2251 return;
2252 /*
2253 * This "batch->memcg" is valid without any css_get/put etc...
2254 * bacause we hide charges behind us.
2255 */
2256 if (batch->bytes)
2257 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2258 if (batch->memsw_bytes)
2259 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2260 /* forget this pointer (for sanity check) */
2261 batch->memcg = NULL;
2262}
2263
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002264#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002265/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002266 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002267 * memcg information is recorded to swap_cgroup of "ent"
2268 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002269void
2270mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002271{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002272 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002273 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002274
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002275 if (!swapout) /* this was a swap cache but the swap is unused ! */
2276 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2277
2278 memcg = __mem_cgroup_uncharge_common(page, ctype);
2279
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002280 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002281 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002282 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002283 mem_cgroup_get(memcg);
2284 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002285 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002286 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002287}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002288#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002289
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002290#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2291/*
2292 * called from swap_entry_free(). remove record in swap_cgroup and
2293 * uncharge "memsw" account.
2294 */
2295void mem_cgroup_uncharge_swap(swp_entry_t ent)
2296{
2297 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002298 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002299
2300 if (!do_swap_account)
2301 return;
2302
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002303 id = swap_cgroup_record(ent, 0);
2304 rcu_read_lock();
2305 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002306 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002307 /*
2308 * We uncharge this because swap is freed.
2309 * This memcg can be obsolete one. We avoid calling css_tryget
2310 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002311 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002312 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002313 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002314 mem_cgroup_put(memcg);
2315 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002316 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002317}
Daisuke Nishimura02491442010-03-10 15:22:17 -08002318
2319/**
2320 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2321 * @entry: swap entry to be moved
2322 * @from: mem_cgroup which the entry is moved from
2323 * @to: mem_cgroup which the entry is moved to
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002324 * @need_fixup: whether we should fixup res_counters and refcounts.
Daisuke Nishimura02491442010-03-10 15:22:17 -08002325 *
2326 * It succeeds only when the swap_cgroup's record for this entry is the same
2327 * as the mem_cgroup's id of @from.
2328 *
2329 * Returns 0 on success, -EINVAL on failure.
2330 *
2331 * The caller must have charged to @to, IOW, called res_counter_charge() about
2332 * both res and memsw, and called css_get().
2333 */
2334static int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002335 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002336{
2337 unsigned short old_id, new_id;
2338
2339 old_id = css_id(&from->css);
2340 new_id = css_id(&to->css);
2341
2342 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08002343 mem_cgroup_swap_statistics(from, false);
Daisuke Nishimura02491442010-03-10 15:22:17 -08002344 mem_cgroup_swap_statistics(to, true);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002345 /*
2346 * This function is only called from task migration context now.
2347 * It postpones res_counter and refcount handling till the end
2348 * of task migration(mem_cgroup_clear_mc()) for performance
2349 * improvement. But we cannot postpone mem_cgroup_get(to)
2350 * because if the process that has been moved to @to does
2351 * swap-in, the refcount of @to might be decreased to 0.
2352 */
Daisuke Nishimura02491442010-03-10 15:22:17 -08002353 mem_cgroup_get(to);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002354 if (need_fixup) {
2355 if (!mem_cgroup_is_root(from))
2356 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2357 mem_cgroup_put(from);
2358 /*
2359 * we charged both to->res and to->memsw, so we should
2360 * uncharge to->res.
2361 */
2362 if (!mem_cgroup_is_root(to))
2363 res_counter_uncharge(&to->res, PAGE_SIZE);
2364 css_put(&to->css);
2365 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08002366 return 0;
2367 }
2368 return -EINVAL;
2369}
2370#else
2371static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08002372 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
Daisuke Nishimura02491442010-03-10 15:22:17 -08002373{
2374 return -EINVAL;
2375}
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002376#endif
2377
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002378/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002379 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2380 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002381 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002382int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002383{
2384 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002385 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002386 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002387
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002388 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002389 return 0;
2390
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002391 pc = lookup_page_cgroup(page);
2392 lock_page_cgroup(pc);
2393 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002394 mem = pc->mem_cgroup;
2395 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002396 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002397 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002398
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002399 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002400 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2401 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002402 css_put(&mem->css);
2403 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002404 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002405 return ret;
2406}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002407
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002408/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002409void mem_cgroup_end_migration(struct mem_cgroup *mem,
2410 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002411{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002412 struct page *target, *unused;
2413 struct page_cgroup *pc;
2414 enum charge_type ctype;
2415
2416 if (!mem)
2417 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002418 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002419 /* at migration success, oldpage->mapping is NULL. */
2420 if (oldpage->mapping) {
2421 target = oldpage;
2422 unused = NULL;
2423 } else {
2424 target = newpage;
2425 unused = oldpage;
2426 }
2427
2428 if (PageAnon(target))
2429 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2430 else if (page_is_file_cache(target))
2431 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2432 else
2433 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2434
2435 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002436 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002437 __mem_cgroup_uncharge_common(unused, ctype);
2438
2439 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002440 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002441 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2442 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002443 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002444 __mem_cgroup_commit_charge(mem, pc, ctype);
2445
2446 /*
2447 * Both of oldpage and newpage are still under lock_page().
2448 * Then, we don't have to care about race in radix-tree.
2449 * But we have to be careful that this page is unmapped or not.
2450 *
2451 * There is a case for !page_mapped(). At the start of
2452 * migration, oldpage was mapped. But now, it's zapped.
2453 * But we know *target* page is not freed/reused under us.
2454 * mem_cgroup_uncharge_page() does all necessary checks.
2455 */
2456 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2457 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002458 /*
2459 * At migration, we may charge account against cgroup which has no tasks
2460 * So, rmdir()->pre_destroy() can be called while we do this charge.
2461 * In that case, we need to call pre_destroy() again. check it here.
2462 */
2463 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002464}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002465
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002466/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002467 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2468 * Calling hierarchical_reclaim is not enough because we should update
2469 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2470 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2471 * not from the memcg which this page would be charged to.
2472 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002473 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002474int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002475 struct mm_struct *mm,
2476 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002477{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002478 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002479 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002480
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002481 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002482 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002483
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002484 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2485 if (!ret)
2486 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002487
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002488 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002489}
2490
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002491static DEFINE_MUTEX(set_limit_mutex);
2492
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002493static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002494 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002495{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002496 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002497 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002498 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002499 int children = mem_cgroup_count_children(memcg);
2500 u64 curusage, oldusage;
2501
2502 /*
2503 * For keeping hierarchical_reclaim simple, how long we should retry
2504 * is depends on callers. We set our retry-count to be function
2505 * of # of children which we should visit in this loop.
2506 */
2507 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2508
2509 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002510
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002511 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002512 if (signal_pending(current)) {
2513 ret = -EINTR;
2514 break;
2515 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002516 /*
2517 * Rather than hide all in some function, I do this in
2518 * open coded manner. You see what this really does.
2519 * We have to guarantee mem->res.limit < mem->memsw.limit.
2520 */
2521 mutex_lock(&set_limit_mutex);
2522 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2523 if (memswlimit < val) {
2524 ret = -EINVAL;
2525 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002526 break;
2527 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002528 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002529 if (!ret) {
2530 if (memswlimit == val)
2531 memcg->memsw_is_minimum = true;
2532 else
2533 memcg->memsw_is_minimum = false;
2534 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002535 mutex_unlock(&set_limit_mutex);
2536
2537 if (!ret)
2538 break;
2539
Bob Liuaa20d482009-12-15 16:47:14 -08002540 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002541 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002542 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2543 /* Usage is reduced ? */
2544 if (curusage >= oldusage)
2545 retry_count--;
2546 else
2547 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002548 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002549
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002550 return ret;
2551}
2552
Li Zefan338c8432009-06-17 16:27:15 -07002553static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2554 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002555{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002556 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002557 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002558 int children = mem_cgroup_count_children(memcg);
2559 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002560
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002561 /* see mem_cgroup_resize_res_limit */
2562 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2563 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002564 while (retry_count) {
2565 if (signal_pending(current)) {
2566 ret = -EINTR;
2567 break;
2568 }
2569 /*
2570 * Rather than hide all in some function, I do this in
2571 * open coded manner. You see what this really does.
2572 * We have to guarantee mem->res.limit < mem->memsw.limit.
2573 */
2574 mutex_lock(&set_limit_mutex);
2575 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2576 if (memlimit > val) {
2577 ret = -EINVAL;
2578 mutex_unlock(&set_limit_mutex);
2579 break;
2580 }
2581 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002582 if (!ret) {
2583 if (memlimit == val)
2584 memcg->memsw_is_minimum = true;
2585 else
2586 memcg->memsw_is_minimum = false;
2587 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002588 mutex_unlock(&set_limit_mutex);
2589
2590 if (!ret)
2591 break;
2592
Balbir Singh4e416952009-09-23 15:56:39 -07002593 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002594 MEM_CGROUP_RECLAIM_NOSWAP |
2595 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002596 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002597 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002598 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002599 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002600 else
2601 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002602 }
2603 return ret;
2604}
2605
Balbir Singh4e416952009-09-23 15:56:39 -07002606unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2607 gfp_t gfp_mask, int nid,
2608 int zid)
2609{
2610 unsigned long nr_reclaimed = 0;
2611 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2612 unsigned long reclaimed;
2613 int loop = 0;
2614 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002615 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002616
2617 if (order > 0)
2618 return 0;
2619
2620 mctz = soft_limit_tree_node_zone(nid, zid);
2621 /*
2622 * This loop can run a while, specially if mem_cgroup's continuously
2623 * keep exceeding their soft limit and putting the system under
2624 * pressure
2625 */
2626 do {
2627 if (next_mz)
2628 mz = next_mz;
2629 else
2630 mz = mem_cgroup_largest_soft_limit_node(mctz);
2631 if (!mz)
2632 break;
2633
2634 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2635 gfp_mask,
2636 MEM_CGROUP_RECLAIM_SOFT);
2637 nr_reclaimed += reclaimed;
2638 spin_lock(&mctz->lock);
2639
2640 /*
2641 * If we failed to reclaim anything from this memory cgroup
2642 * it is time to move on to the next cgroup
2643 */
2644 next_mz = NULL;
2645 if (!reclaimed) {
2646 do {
2647 /*
2648 * Loop until we find yet another one.
2649 *
2650 * By the time we get the soft_limit lock
2651 * again, someone might have aded the
2652 * group back on the RB tree. Iterate to
2653 * make sure we get a different mem.
2654 * mem_cgroup_largest_soft_limit_node returns
2655 * NULL if no other cgroup is present on
2656 * the tree
2657 */
2658 next_mz =
2659 __mem_cgroup_largest_soft_limit_node(mctz);
2660 if (next_mz == mz) {
2661 css_put(&next_mz->mem->css);
2662 next_mz = NULL;
2663 } else /* next_mz == NULL or other memcg */
2664 break;
2665 } while (1);
2666 }
Balbir Singh4e416952009-09-23 15:56:39 -07002667 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002668 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002669 /*
2670 * One school of thought says that we should not add
2671 * back the node to the tree if reclaim returns 0.
2672 * But our reclaim could return 0, simply because due
2673 * to priority we are exposing a smaller subset of
2674 * memory to reclaim from. Consider this as a longer
2675 * term TODO.
2676 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002677 /* If excess == 0, no tree ops */
2678 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002679 spin_unlock(&mctz->lock);
2680 css_put(&mz->mem->css);
2681 loop++;
2682 /*
2683 * Could not reclaim anything and there are no more
2684 * mem cgroups to try or we seem to be looping without
2685 * reclaiming anything.
2686 */
2687 if (!nr_reclaimed &&
2688 (next_mz == NULL ||
2689 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2690 break;
2691 } while (!nr_reclaimed);
2692 if (next_mz)
2693 css_put(&next_mz->mem->css);
2694 return nr_reclaimed;
2695}
2696
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002697/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002698 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002699 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2700 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002701static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002702 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002703{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002704 struct zone *zone;
2705 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002706 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002707 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002708 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002709 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002710
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002711 zone = &NODE_DATA(node)->node_zones[zid];
2712 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002713 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002714
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002715 loop = MEM_CGROUP_ZSTAT(mz, lru);
2716 /* give some margin against EBUSY etc...*/
2717 loop += 256;
2718 busy = NULL;
2719 while (loop--) {
2720 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002721 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002722 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002723 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002724 break;
2725 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002726 pc = list_entry(list->prev, struct page_cgroup, lru);
2727 if (busy == pc) {
2728 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08002729 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002730 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002731 continue;
2732 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002733 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002734
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002735 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002736 if (ret == -ENOMEM)
2737 break;
2738
2739 if (ret == -EBUSY || ret == -EINVAL) {
2740 /* found lock contention or "pc" is obsolete. */
2741 busy = pc;
2742 cond_resched();
2743 } else
2744 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002745 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002746
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002747 if (!ret && !list_empty(list))
2748 return -EBUSY;
2749 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002750}
2751
2752/*
2753 * make mem_cgroup's charge to be 0 if there is no task.
2754 * This enables deleting this mem_cgroup.
2755 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002756static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002757{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002758 int ret;
2759 int node, zid, shrink;
2760 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002761 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002762
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002763 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002764
2765 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002766 /* should free all ? */
2767 if (free_all)
2768 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002769move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002770 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002771 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002772 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002773 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002774 ret = -EINTR;
2775 if (signal_pending(current))
2776 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002777 /* This is for making all *used* pages to be on LRU. */
2778 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002779 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002780 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002781 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002782 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002783 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002784 for_each_lru(l) {
2785 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002786 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002787 if (ret)
2788 break;
2789 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002790 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002791 if (ret)
2792 break;
2793 }
2794 /* it seems parent cgroup doesn't have enough mem */
2795 if (ret == -ENOMEM)
2796 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002797 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002798 /* "ret" should also be checked to ensure all lists are empty. */
2799 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002800out:
2801 css_put(&mem->css);
2802 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002803
2804try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002805 /* returns EBUSY if there is a task or if we come here twice. */
2806 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002807 ret = -EBUSY;
2808 goto out;
2809 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002810 /* we call try-to-free pages for make this cgroup empty */
2811 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002812 /* try to free all pages in this cgroup */
2813 shrink = 1;
2814 while (nr_retries && mem->res.usage > 0) {
2815 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002816
2817 if (signal_pending(current)) {
2818 ret = -EINTR;
2819 goto out;
2820 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002821 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2822 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002823 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002824 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002825 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002826 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002827 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002828
2829 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002830 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002831 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002832 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002833}
2834
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002835int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2836{
2837 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2838}
2839
2840
Balbir Singh18f59ea2009-01-07 18:08:07 -08002841static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2842{
2843 return mem_cgroup_from_cont(cont)->use_hierarchy;
2844}
2845
2846static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2847 u64 val)
2848{
2849 int retval = 0;
2850 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2851 struct cgroup *parent = cont->parent;
2852 struct mem_cgroup *parent_mem = NULL;
2853
2854 if (parent)
2855 parent_mem = mem_cgroup_from_cont(parent);
2856
2857 cgroup_lock();
2858 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002859 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002860 * in the child subtrees. If it is unset, then the change can
2861 * occur, provided the current cgroup has no children.
2862 *
2863 * For the root cgroup, parent_mem is NULL, we allow value to be
2864 * set if there are no children.
2865 */
2866 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2867 (val == 1 || val == 0)) {
2868 if (list_empty(&cont->children))
2869 mem->use_hierarchy = val;
2870 else
2871 retval = -EBUSY;
2872 } else
2873 retval = -EINVAL;
2874 cgroup_unlock();
2875
2876 return retval;
2877}
2878
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002879struct mem_cgroup_idx_data {
2880 s64 val;
2881 enum mem_cgroup_stat_index idx;
2882};
2883
2884static int
2885mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2886{
2887 struct mem_cgroup_idx_data *d = data;
2888 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2889 return 0;
2890}
2891
2892static void
2893mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2894 enum mem_cgroup_stat_index idx, s64 *val)
2895{
2896 struct mem_cgroup_idx_data d;
2897 d.idx = idx;
2898 d.val = 0;
2899 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2900 *val = d.val;
2901}
2902
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002903static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
2904{
2905 u64 idx_val, val;
2906
2907 if (!mem_cgroup_is_root(mem)) {
2908 if (!swap)
2909 return res_counter_read_u64(&mem->res, RES_USAGE);
2910 else
2911 return res_counter_read_u64(&mem->memsw, RES_USAGE);
2912 }
2913
2914 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
2915 val = idx_val;
2916 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
2917 val += idx_val;
2918
2919 if (swap) {
2920 mem_cgroup_get_recursive_idx_stat(mem,
2921 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2922 val += idx_val;
2923 }
2924
2925 return val << PAGE_SHIFT;
2926}
2927
Paul Menage2c3daa72008-04-29 00:59:58 -07002928static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002929{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002930 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002931 u64 val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002932 int type, name;
2933
2934 type = MEMFILE_TYPE(cft->private);
2935 name = MEMFILE_ATTR(cft->private);
2936 switch (type) {
2937 case _MEM:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002938 if (name == RES_USAGE)
2939 val = mem_cgroup_usage(mem, false);
2940 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002941 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002942 break;
2943 case _MEMSWAP:
Kirill A. Shutemov104f3922010-03-10 15:22:21 -08002944 if (name == RES_USAGE)
2945 val = mem_cgroup_usage(mem, true);
2946 else
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002947 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002948 break;
2949 default:
2950 BUG();
2951 break;
2952 }
2953 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002954}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002955/*
2956 * The user of this function is...
2957 * RES_LIMIT.
2958 */
Paul Menage856c13a2008-07-25 01:47:04 -07002959static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2960 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002961{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002962 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002963 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002964 unsigned long long val;
2965 int ret;
2966
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002967 type = MEMFILE_TYPE(cft->private);
2968 name = MEMFILE_ATTR(cft->private);
2969 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002970 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002971 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2972 ret = -EINVAL;
2973 break;
2974 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002975 /* This function does all necessary parse...reuse it */
2976 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002977 if (ret)
2978 break;
2979 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002980 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002981 else
2982 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002983 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002984 case RES_SOFT_LIMIT:
2985 ret = res_counter_memparse_write_strategy(buffer, &val);
2986 if (ret)
2987 break;
2988 /*
2989 * For memsw, soft limits are hard to implement in terms
2990 * of semantics, for now, we support soft limits for
2991 * control without swap
2992 */
2993 if (type == _MEM)
2994 ret = res_counter_set_soft_limit(&memcg->res, val);
2995 else
2996 ret = -EINVAL;
2997 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002998 default:
2999 ret = -EINVAL; /* should be BUG() ? */
3000 break;
3001 }
3002 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003003}
3004
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003005static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3006 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3007{
3008 struct cgroup *cgroup;
3009 unsigned long long min_limit, min_memsw_limit, tmp;
3010
3011 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3012 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3013 cgroup = memcg->css.cgroup;
3014 if (!memcg->use_hierarchy)
3015 goto out;
3016
3017 while (cgroup->parent) {
3018 cgroup = cgroup->parent;
3019 memcg = mem_cgroup_from_cont(cgroup);
3020 if (!memcg->use_hierarchy)
3021 break;
3022 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3023 min_limit = min(min_limit, tmp);
3024 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3025 min_memsw_limit = min(min_memsw_limit, tmp);
3026 }
3027out:
3028 *mem_limit = min_limit;
3029 *memsw_limit = min_memsw_limit;
3030 return;
3031}
3032
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003033static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003034{
3035 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003036 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003037
3038 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003039 type = MEMFILE_TYPE(event);
3040 name = MEMFILE_ATTR(event);
3041 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003042 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003043 if (type == _MEM)
3044 res_counter_reset_max(&mem->res);
3045 else
3046 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003047 break;
3048 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003049 if (type == _MEM)
3050 res_counter_reset_failcnt(&mem->res);
3051 else
3052 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003053 break;
3054 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07003055
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07003056 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003057}
3058
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003059static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3060 struct cftype *cft)
3061{
3062 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3063}
3064
Daisuke Nishimura02491442010-03-10 15:22:17 -08003065#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003066static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3067 struct cftype *cft, u64 val)
3068{
3069 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3070
3071 if (val >= (1 << NR_MOVE_TYPE))
3072 return -EINVAL;
3073 /*
3074 * We check this value several times in both in can_attach() and
3075 * attach(), so we need cgroup lock to prevent this value from being
3076 * inconsistent.
3077 */
3078 cgroup_lock();
3079 mem->move_charge_at_immigrate = val;
3080 cgroup_unlock();
3081
3082 return 0;
3083}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003084#else
3085static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3086 struct cftype *cft, u64 val)
3087{
3088 return -ENOSYS;
3089}
3090#endif
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003091
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003092
3093/* For read statistics */
3094enum {
3095 MCS_CACHE,
3096 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003097 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003098 MCS_PGPGIN,
3099 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003100 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003101 MCS_INACTIVE_ANON,
3102 MCS_ACTIVE_ANON,
3103 MCS_INACTIVE_FILE,
3104 MCS_ACTIVE_FILE,
3105 MCS_UNEVICTABLE,
3106 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003107};
3108
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003109struct mcs_total_stat {
3110 s64 stat[NR_MCS_STAT];
3111};
3112
3113struct {
3114 char *local_name;
3115 char *total_name;
3116} memcg_stat_strings[NR_MCS_STAT] = {
3117 {"cache", "total_cache"},
3118 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07003119 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003120 {"pgpgin", "total_pgpgin"},
3121 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003122 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003123 {"inactive_anon", "total_inactive_anon"},
3124 {"active_anon", "total_active_anon"},
3125 {"inactive_file", "total_inactive_file"},
3126 {"active_file", "total_active_file"},
3127 {"unevictable", "total_unevictable"}
3128};
3129
3130
3131static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
3132{
3133 struct mcs_total_stat *s = data;
3134 s64 val;
3135
3136 /* per cpu stat */
3137 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
3138 s->stat[MCS_CACHE] += val * PAGE_SIZE;
3139 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
3140 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08003141 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
3142 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003143 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
3144 s->stat[MCS_PGPGIN] += val;
3145 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
3146 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003147 if (do_swap_account) {
3148 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
3149 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3150 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003151
3152 /* per zone stat */
3153 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3154 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3155 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3156 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3157 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3158 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3159 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3160 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3161 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3162 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3163 return 0;
3164}
3165
3166static void
3167mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3168{
3169 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3170}
3171
Paul Menagec64745c2008-04-29 01:00:02 -07003172static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3173 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003174{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003175 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003176 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003177 int i;
3178
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003179 memset(&mystat, 0, sizeof(mystat));
3180 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003181
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003182 for (i = 0; i < NR_MCS_STAT; i++) {
3183 if (i == MCS_SWAP && !do_swap_account)
3184 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003185 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003186 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003187
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003188 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003189 {
3190 unsigned long long limit, memsw_limit;
3191 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3192 cb->fill(cb, "hierarchical_memory_limit", limit);
3193 if (do_swap_account)
3194 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3195 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003196
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003197 memset(&mystat, 0, sizeof(mystat));
3198 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003199 for (i = 0; i < NR_MCS_STAT; i++) {
3200 if (i == MCS_SWAP && !do_swap_account)
3201 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003202 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003203 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003204
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003205#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003206 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003207
3208 {
3209 int nid, zid;
3210 struct mem_cgroup_per_zone *mz;
3211 unsigned long recent_rotated[2] = {0, 0};
3212 unsigned long recent_scanned[2] = {0, 0};
3213
3214 for_each_online_node(nid)
3215 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3216 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3217
3218 recent_rotated[0] +=
3219 mz->reclaim_stat.recent_rotated[0];
3220 recent_rotated[1] +=
3221 mz->reclaim_stat.recent_rotated[1];
3222 recent_scanned[0] +=
3223 mz->reclaim_stat.recent_scanned[0];
3224 recent_scanned[1] +=
3225 mz->reclaim_stat.recent_scanned[1];
3226 }
3227 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3228 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3229 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3230 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3231 }
3232#endif
3233
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003234 return 0;
3235}
3236
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003237static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3238{
3239 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3240
3241 return get_swappiness(memcg);
3242}
3243
3244static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3245 u64 val)
3246{
3247 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3248 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003249
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003250 if (val > 100)
3251 return -EINVAL;
3252
3253 if (cgrp->parent == NULL)
3254 return -EINVAL;
3255
3256 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003257
3258 cgroup_lock();
3259
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003260 /* If under hierarchy, only empty-root can set this value */
3261 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003262 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3263 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003264 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003265 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003266
3267 spin_lock(&memcg->reclaim_param_lock);
3268 memcg->swappiness = val;
3269 spin_unlock(&memcg->reclaim_param_lock);
3270
Li Zefan068b38c2009-01-15 13:51:26 -08003271 cgroup_unlock();
3272
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003273 return 0;
3274}
3275
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003276static bool mem_cgroup_threshold_check(struct mem_cgroup *mem)
3277{
3278 bool ret = false;
3279 int cpu;
3280 s64 val;
3281 struct mem_cgroup_stat_cpu *cpustat;
3282
3283 cpu = get_cpu();
3284 cpustat = &mem->stat.cpustat[cpu];
3285 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_THRESHOLDS);
3286 if (unlikely(val < 0)) {
3287 __mem_cgroup_stat_set_safe(cpustat, MEM_CGROUP_STAT_THRESHOLDS,
3288 THRESHOLDS_EVENTS_THRESH);
3289 ret = true;
3290 }
3291 put_cpu();
3292 return ret;
3293}
3294
3295static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3296{
3297 struct mem_cgroup_threshold_ary *t;
3298 u64 usage;
3299 int i;
3300
3301 rcu_read_lock();
3302 if (!swap)
3303 t = rcu_dereference(memcg->thresholds);
3304 else
3305 t = rcu_dereference(memcg->memsw_thresholds);
3306
3307 if (!t)
3308 goto unlock;
3309
3310 usage = mem_cgroup_usage(memcg, swap);
3311
3312 /*
3313 * current_threshold points to threshold just below usage.
3314 * If it's not true, a threshold was crossed after last
3315 * call of __mem_cgroup_threshold().
3316 */
3317 i = atomic_read(&t->current_threshold);
3318
3319 /*
3320 * Iterate backward over array of thresholds starting from
3321 * current_threshold and check if a threshold is crossed.
3322 * If none of thresholds below usage is crossed, we read
3323 * only one element of the array here.
3324 */
3325 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3326 eventfd_signal(t->entries[i].eventfd, 1);
3327
3328 /* i = current_threshold + 1 */
3329 i++;
3330
3331 /*
3332 * Iterate forward over array of thresholds starting from
3333 * current_threshold+1 and check if a threshold is crossed.
3334 * If none of thresholds above usage is crossed, we read
3335 * only one element of the array here.
3336 */
3337 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3338 eventfd_signal(t->entries[i].eventfd, 1);
3339
3340 /* Update current_threshold */
3341 atomic_set(&t->current_threshold, i - 1);
3342unlock:
3343 rcu_read_unlock();
3344}
3345
3346static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3347{
3348 __mem_cgroup_threshold(memcg, false);
3349 if (do_swap_account)
3350 __mem_cgroup_threshold(memcg, true);
3351}
3352
3353static int compare_thresholds(const void *a, const void *b)
3354{
3355 const struct mem_cgroup_threshold *_a = a;
3356 const struct mem_cgroup_threshold *_b = b;
3357
3358 return _a->threshold - _b->threshold;
3359}
3360
3361static int mem_cgroup_register_event(struct cgroup *cgrp, struct cftype *cft,
3362 struct eventfd_ctx *eventfd, const char *args)
3363{
3364 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3365 struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
3366 int type = MEMFILE_TYPE(cft->private);
3367 u64 threshold, usage;
3368 int size;
3369 int i, ret;
3370
3371 ret = res_counter_memparse_write_strategy(args, &threshold);
3372 if (ret)
3373 return ret;
3374
3375 mutex_lock(&memcg->thresholds_lock);
3376 if (type == _MEM)
3377 thresholds = memcg->thresholds;
3378 else if (type == _MEMSWAP)
3379 thresholds = memcg->memsw_thresholds;
3380 else
3381 BUG();
3382
3383 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3384
3385 /* Check if a threshold crossed before adding a new one */
3386 if (thresholds)
3387 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3388
3389 if (thresholds)
3390 size = thresholds->size + 1;
3391 else
3392 size = 1;
3393
3394 /* Allocate memory for new array of thresholds */
3395 thresholds_new = kmalloc(sizeof(*thresholds_new) +
3396 size * sizeof(struct mem_cgroup_threshold),
3397 GFP_KERNEL);
3398 if (!thresholds_new) {
3399 ret = -ENOMEM;
3400 goto unlock;
3401 }
3402 thresholds_new->size = size;
3403
3404 /* Copy thresholds (if any) to new array */
3405 if (thresholds)
3406 memcpy(thresholds_new->entries, thresholds->entries,
3407 thresholds->size *
3408 sizeof(struct mem_cgroup_threshold));
3409 /* Add new threshold */
3410 thresholds_new->entries[size - 1].eventfd = eventfd;
3411 thresholds_new->entries[size - 1].threshold = threshold;
3412
3413 /* Sort thresholds. Registering of new threshold isn't time-critical */
3414 sort(thresholds_new->entries, size,
3415 sizeof(struct mem_cgroup_threshold),
3416 compare_thresholds, NULL);
3417
3418 /* Find current threshold */
3419 atomic_set(&thresholds_new->current_threshold, -1);
3420 for (i = 0; i < size; i++) {
3421 if (thresholds_new->entries[i].threshold < usage) {
3422 /*
3423 * thresholds_new->current_threshold will not be used
3424 * until rcu_assign_pointer(), so it's safe to increment
3425 * it here.
3426 */
3427 atomic_inc(&thresholds_new->current_threshold);
3428 }
3429 }
3430
3431 /*
3432 * We need to increment refcnt to be sure that all thresholds
3433 * will be unregistered before calling __mem_cgroup_free()
3434 */
3435 mem_cgroup_get(memcg);
3436
3437 if (type == _MEM)
3438 rcu_assign_pointer(memcg->thresholds, thresholds_new);
3439 else
3440 rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
3441
3442 /* To be sure that nobody uses thresholds before freeing it */
3443 synchronize_rcu();
3444
3445 kfree(thresholds);
3446unlock:
3447 mutex_unlock(&memcg->thresholds_lock);
3448
3449 return ret;
3450}
3451
3452static int mem_cgroup_unregister_event(struct cgroup *cgrp, struct cftype *cft,
3453 struct eventfd_ctx *eventfd)
3454{
3455 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3456 struct mem_cgroup_threshold_ary *thresholds, *thresholds_new;
3457 int type = MEMFILE_TYPE(cft->private);
3458 u64 usage;
3459 int size = 0;
3460 int i, j, ret;
3461
3462 mutex_lock(&memcg->thresholds_lock);
3463 if (type == _MEM)
3464 thresholds = memcg->thresholds;
3465 else if (type == _MEMSWAP)
3466 thresholds = memcg->memsw_thresholds;
3467 else
3468 BUG();
3469
3470 /*
3471 * Something went wrong if we trying to unregister a threshold
3472 * if we don't have thresholds
3473 */
3474 BUG_ON(!thresholds);
3475
3476 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3477
3478 /* Check if a threshold crossed before removing */
3479 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3480
3481 /* Calculate new number of threshold */
3482 for (i = 0; i < thresholds->size; i++) {
3483 if (thresholds->entries[i].eventfd != eventfd)
3484 size++;
3485 }
3486
3487 /* Set thresholds array to NULL if we don't have thresholds */
3488 if (!size) {
3489 thresholds_new = NULL;
3490 goto assign;
3491 }
3492
3493 /* Allocate memory for new array of thresholds */
3494 thresholds_new = kmalloc(sizeof(*thresholds_new) +
3495 size * sizeof(struct mem_cgroup_threshold),
3496 GFP_KERNEL);
3497 if (!thresholds_new) {
3498 ret = -ENOMEM;
3499 goto unlock;
3500 }
3501 thresholds_new->size = size;
3502
3503 /* Copy thresholds and find current threshold */
3504 atomic_set(&thresholds_new->current_threshold, -1);
3505 for (i = 0, j = 0; i < thresholds->size; i++) {
3506 if (thresholds->entries[i].eventfd == eventfd)
3507 continue;
3508
3509 thresholds_new->entries[j] = thresholds->entries[i];
3510 if (thresholds_new->entries[j].threshold < usage) {
3511 /*
3512 * thresholds_new->current_threshold will not be used
3513 * until rcu_assign_pointer(), so it's safe to increment
3514 * it here.
3515 */
3516 atomic_inc(&thresholds_new->current_threshold);
3517 }
3518 j++;
3519 }
3520
3521assign:
3522 if (type == _MEM)
3523 rcu_assign_pointer(memcg->thresholds, thresholds_new);
3524 else
3525 rcu_assign_pointer(memcg->memsw_thresholds, thresholds_new);
3526
3527 /* To be sure that nobody uses thresholds before freeing it */
3528 synchronize_rcu();
3529
3530 for (i = 0; i < thresholds->size - size; i++)
3531 mem_cgroup_put(memcg);
3532
3533 kfree(thresholds);
3534unlock:
3535 mutex_unlock(&memcg->thresholds_lock);
3536
3537 return ret;
3538}
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003539
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003540static struct cftype mem_cgroup_files[] = {
3541 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003542 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003543 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003544 .read_u64 = mem_cgroup_read,
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003545 .register_event = mem_cgroup_register_event,
3546 .unregister_event = mem_cgroup_unregister_event,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003547 },
3548 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003549 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003550 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003551 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003552 .read_u64 = mem_cgroup_read,
3553 },
3554 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003555 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003556 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003557 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003558 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003559 },
3560 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003561 .name = "soft_limit_in_bytes",
3562 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3563 .write_string = mem_cgroup_write,
3564 .read_u64 = mem_cgroup_read,
3565 },
3566 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003567 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003568 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003569 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003570 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003571 },
Balbir Singh8697d332008-02-07 00:13:59 -08003572 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003573 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003574 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003575 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003576 {
3577 .name = "force_empty",
3578 .trigger = mem_cgroup_force_empty_write,
3579 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003580 {
3581 .name = "use_hierarchy",
3582 .write_u64 = mem_cgroup_hierarchy_write,
3583 .read_u64 = mem_cgroup_hierarchy_read,
3584 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003585 {
3586 .name = "swappiness",
3587 .read_u64 = mem_cgroup_swappiness_read,
3588 .write_u64 = mem_cgroup_swappiness_write,
3589 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003590 {
3591 .name = "move_charge_at_immigrate",
3592 .read_u64 = mem_cgroup_move_charge_read,
3593 .write_u64 = mem_cgroup_move_charge_write,
3594 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003595};
3596
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003597#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3598static struct cftype memsw_cgroup_files[] = {
3599 {
3600 .name = "memsw.usage_in_bytes",
3601 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3602 .read_u64 = mem_cgroup_read,
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003603 .register_event = mem_cgroup_register_event,
3604 .unregister_event = mem_cgroup_unregister_event,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003605 },
3606 {
3607 .name = "memsw.max_usage_in_bytes",
3608 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3609 .trigger = mem_cgroup_reset,
3610 .read_u64 = mem_cgroup_read,
3611 },
3612 {
3613 .name = "memsw.limit_in_bytes",
3614 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3615 .write_string = mem_cgroup_write,
3616 .read_u64 = mem_cgroup_read,
3617 },
3618 {
3619 .name = "memsw.failcnt",
3620 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3621 .trigger = mem_cgroup_reset,
3622 .read_u64 = mem_cgroup_read,
3623 },
3624};
3625
3626static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3627{
3628 if (!do_swap_account)
3629 return 0;
3630 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3631 ARRAY_SIZE(memsw_cgroup_files));
3632};
3633#else
3634static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3635{
3636 return 0;
3637}
3638#endif
3639
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003640static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3641{
3642 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003643 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003644 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003645 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003646 /*
3647 * This routine is called against possible nodes.
3648 * But it's BUG to call kmalloc() against offline node.
3649 *
3650 * TODO: this routine can waste much memory for nodes which will
3651 * never be onlined. It's better to use memory hotplug callback
3652 * function.
3653 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003654 if (!node_state(node, N_NORMAL_MEMORY))
3655 tmp = -1;
3656 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003657 if (!pn)
3658 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003659
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003660 mem->info.nodeinfo[node] = pn;
3661 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003662
3663 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3664 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003665 for_each_lru(l)
3666 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003667 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003668 mz->on_tree = false;
3669 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003670 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003671 return 0;
3672}
3673
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003674static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3675{
3676 kfree(mem->info.nodeinfo[node]);
3677}
3678
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003679static int mem_cgroup_size(void)
3680{
3681 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3682 return sizeof(struct mem_cgroup) + cpustat_size;
3683}
3684
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003685static struct mem_cgroup *mem_cgroup_alloc(void)
3686{
3687 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003688 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003689
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003690 if (size < PAGE_SIZE)
3691 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003692 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003693 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003694
3695 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003696 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003697 return mem;
3698}
3699
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003700/*
3701 * At destroying mem_cgroup, references from swap_cgroup can remain.
3702 * (scanning all at force_empty is too costly...)
3703 *
3704 * Instead of clearing all references at force_empty, we remember
3705 * the number of reference from swap_cgroup and free mem_cgroup when
3706 * it goes down to 0.
3707 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003708 * Removal of cgroup itself succeeds regardless of refs from swap.
3709 */
3710
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003711static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003712{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003713 int node;
3714
Balbir Singhf64c3f52009-09-23 15:56:37 -07003715 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003716 free_css_id(&mem_cgroup_subsys, &mem->css);
3717
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003718 for_each_node_state(node, N_POSSIBLE)
3719 free_mem_cgroup_per_zone_info(mem, node);
3720
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003721 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003722 kfree(mem);
3723 else
3724 vfree(mem);
3725}
3726
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003727static void mem_cgroup_get(struct mem_cgroup *mem)
3728{
3729 atomic_inc(&mem->refcnt);
3730}
3731
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003732static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003733{
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003734 if (atomic_sub_and_test(count, &mem->refcnt)) {
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003735 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003736 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003737 if (parent)
3738 mem_cgroup_put(parent);
3739 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003740}
3741
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08003742static void mem_cgroup_put(struct mem_cgroup *mem)
3743{
3744 __mem_cgroup_put(mem, 1);
3745}
3746
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003747/*
3748 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3749 */
3750static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3751{
3752 if (!mem->res.parent)
3753 return NULL;
3754 return mem_cgroup_from_res_counter(mem->res.parent, res);
3755}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003756
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003757#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3758static void __init enable_swap_cgroup(void)
3759{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003760 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003761 do_swap_account = 1;
3762}
3763#else
3764static void __init enable_swap_cgroup(void)
3765{
3766}
3767#endif
3768
Balbir Singhf64c3f52009-09-23 15:56:37 -07003769static int mem_cgroup_soft_limit_tree_init(void)
3770{
3771 struct mem_cgroup_tree_per_node *rtpn;
3772 struct mem_cgroup_tree_per_zone *rtpz;
3773 int tmp, node, zone;
3774
3775 for_each_node_state(node, N_POSSIBLE) {
3776 tmp = node;
3777 if (!node_state(node, N_NORMAL_MEMORY))
3778 tmp = -1;
3779 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3780 if (!rtpn)
3781 return 1;
3782
3783 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3784
3785 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3786 rtpz = &rtpn->rb_tree_per_zone[zone];
3787 rtpz->rb_root = RB_ROOT;
3788 spin_lock_init(&rtpz->lock);
3789 }
3790 }
3791 return 0;
3792}
3793
Li Zefan0eb253e2009-01-15 13:51:25 -08003794static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003795mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3796{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003797 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003798 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003799 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003800
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003801 mem = mem_cgroup_alloc();
3802 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003803 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003804
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003805 for_each_node_state(node, N_POSSIBLE)
3806 if (alloc_mem_cgroup_per_zone_info(mem, node))
3807 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003808
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003809 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003810 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003811 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003812 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003813 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003814 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003815 if (mem_cgroup_soft_limit_tree_init())
3816 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003817 for_each_possible_cpu(cpu) {
3818 struct memcg_stock_pcp *stock =
3819 &per_cpu(memcg_stock, cpu);
3820 INIT_WORK(&stock->work, drain_local_stock);
3821 }
3822 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003823 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003824 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003825 mem->use_hierarchy = parent->use_hierarchy;
3826 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003827
Balbir Singh18f59ea2009-01-07 18:08:07 -08003828 if (parent && parent->use_hierarchy) {
3829 res_counter_init(&mem->res, &parent->res);
3830 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003831 /*
3832 * We increment refcnt of the parent to ensure that we can
3833 * safely access it on res_counter_charge/uncharge.
3834 * This refcnt will be decremented when freeing this
3835 * mem_cgroup(see mem_cgroup_put).
3836 */
3837 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003838 } else {
3839 res_counter_init(&mem->res, NULL);
3840 res_counter_init(&mem->memsw, NULL);
3841 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003842 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003843 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003844
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003845 if (parent)
3846 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003847 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003848 mem->move_charge_at_immigrate = 0;
Kirill A. Shutemov2e72b632010-03-10 15:22:24 -08003849 mutex_init(&mem->thresholds_lock);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003850 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003851free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003852 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003853 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003854 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003855}
3856
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003857static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003858 struct cgroup *cont)
3859{
3860 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003861
3862 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003863}
3864
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003865static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3866 struct cgroup *cont)
3867{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003868 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003869
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003870 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003871}
3872
3873static int mem_cgroup_populate(struct cgroup_subsys *ss,
3874 struct cgroup *cont)
3875{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003876 int ret;
3877
3878 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3879 ARRAY_SIZE(mem_cgroup_files));
3880
3881 if (!ret)
3882 ret = register_memsw_files(cont, ss);
3883 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003884}
3885
Daisuke Nishimura02491442010-03-10 15:22:17 -08003886#ifdef CONFIG_MMU
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003887/* Handlers for move charge at task migration. */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003888#define PRECHARGE_COUNT_AT_ONCE 256
3889static int mem_cgroup_do_precharge(unsigned long count)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003890{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003891 int ret = 0;
3892 int batch_count = PRECHARGE_COUNT_AT_ONCE;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003893 struct mem_cgroup *mem = mc.to;
3894
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08003895 if (mem_cgroup_is_root(mem)) {
3896 mc.precharge += count;
3897 /* we don't need css_get for root */
3898 return ret;
3899 }
3900 /* try to charge at once */
3901 if (count > 1) {
3902 struct res_counter *dummy;
3903 /*
3904 * "mem" cannot be under rmdir() because we've already checked
3905 * by cgroup_lock_live_cgroup() that it is not removed and we
3906 * are still under the same cgroup_mutex. So we can postpone
3907 * css_get().
3908 */
3909 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
3910 goto one_by_one;
3911 if (do_swap_account && res_counter_charge(&mem->memsw,
3912 PAGE_SIZE * count, &dummy)) {
3913 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
3914 goto one_by_one;
3915 }
3916 mc.precharge += count;
3917 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
3918 WARN_ON_ONCE(count > INT_MAX);
3919 __css_get(&mem->css, (int)count);
3920 return ret;
3921 }
3922one_by_one:
3923 /* fall back to one by one charge */
3924 while (count--) {
3925 if (signal_pending(current)) {
3926 ret = -EINTR;
3927 break;
3928 }
3929 if (!batch_count--) {
3930 batch_count = PRECHARGE_COUNT_AT_ONCE;
3931 cond_resched();
3932 }
3933 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem,
3934 false, NULL);
3935 if (ret || !mem)
3936 /* mem_cgroup_clear_mc() will do uncharge later */
3937 return -ENOMEM;
3938 mc.precharge++;
3939 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003940 return ret;
3941}
Daisuke Nishimura02491442010-03-10 15:22:17 -08003942#else /* !CONFIG_MMU */
3943static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
3944 struct cgroup *cgroup,
3945 struct task_struct *p,
3946 bool threadgroup)
3947{
3948 return 0;
3949}
3950static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
3951 struct cgroup *cgroup,
3952 struct task_struct *p,
3953 bool threadgroup)
3954{
3955}
3956static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3957 struct cgroup *cont,
3958 struct cgroup *old_cont,
3959 struct task_struct *p,
3960 bool threadgroup)
3961{
3962}
3963#endif
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003964
3965/**
3966 * is_target_pte_for_mc - check a pte whether it is valid for move charge
3967 * @vma: the vma the pte to be checked belongs
3968 * @addr: the address corresponding to the pte to be checked
3969 * @ptent: the pte to be checked
Daisuke Nishimura02491442010-03-10 15:22:17 -08003970 * @target: the pointer the target page or swap ent will be stored(can be NULL)
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003971 *
3972 * Returns
3973 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
3974 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
3975 * move charge. if @target is not NULL, the page is stored in target->page
3976 * with extra refcnt got(Callers should handle it).
Daisuke Nishimura02491442010-03-10 15:22:17 -08003977 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
3978 * target for charge migration. if @target is not NULL, the entry is stored
3979 * in target->ent.
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003980 *
3981 * Called with pte lock held.
3982 */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003983union mc_target {
3984 struct page *page;
Daisuke Nishimura02491442010-03-10 15:22:17 -08003985 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003986};
3987
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003988enum mc_target_type {
3989 MC_TARGET_NONE, /* not used */
3990 MC_TARGET_PAGE,
Daisuke Nishimura02491442010-03-10 15:22:17 -08003991 MC_TARGET_SWAP,
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003992};
3993
3994static int is_target_pte_for_mc(struct vm_area_struct *vma,
3995 unsigned long addr, pte_t ptent, union mc_target *target)
3996{
Daisuke Nishimura02491442010-03-10 15:22:17 -08003997 struct page *page = NULL;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08003998 struct page_cgroup *pc;
3999 int ret = 0;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004000 swp_entry_t ent = { .val = 0 };
4001 int usage_count = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004002 bool move_anon = test_bit(MOVE_CHARGE_TYPE_ANON,
4003 &mc.to->move_charge_at_immigrate);
4004
Daisuke Nishimura02491442010-03-10 15:22:17 -08004005 if (!pte_present(ptent)) {
4006 /* TODO: handle swap of shmes/tmpfs */
4007 if (pte_none(ptent) || pte_file(ptent))
4008 return 0;
4009 else if (is_swap_pte(ptent)) {
4010 ent = pte_to_swp_entry(ptent);
4011 if (!move_anon || non_swap_entry(ent))
4012 return 0;
4013 usage_count = mem_cgroup_count_swap_user(ent, &page);
4014 }
4015 } else {
4016 page = vm_normal_page(vma, addr, ptent);
4017 if (!page || !page_mapped(page))
4018 return 0;
4019 /*
4020 * TODO: We don't move charges of file(including shmem/tmpfs)
4021 * pages for now.
4022 */
4023 if (!move_anon || !PageAnon(page))
4024 return 0;
4025 if (!get_page_unless_zero(page))
4026 return 0;
4027 usage_count = page_mapcount(page);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004028 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004029 if (usage_count > 1) {
4030 /*
4031 * TODO: We don't move charges of shared(used by multiple
4032 * processes) pages for now.
4033 */
4034 if (page)
4035 put_page(page);
4036 return 0;
4037 }
4038 if (page) {
4039 pc = lookup_page_cgroup(page);
4040 /*
4041 * Do only loose check w/o page_cgroup lock.
4042 * mem_cgroup_move_account() checks the pc is valid or not under
4043 * the lock.
4044 */
4045 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4046 ret = MC_TARGET_PAGE;
4047 if (target)
4048 target->page = page;
4049 }
4050 if (!ret || !target)
4051 put_page(page);
4052 }
4053 /* throught */
4054 if (ent.val && do_swap_account && !ret &&
4055 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4056 ret = MC_TARGET_SWAP;
4057 if (target)
4058 target->ent = ent;
4059 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004060 return ret;
4061}
4062
4063static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4064 unsigned long addr, unsigned long end,
4065 struct mm_walk *walk)
4066{
4067 struct vm_area_struct *vma = walk->private;
4068 pte_t *pte;
4069 spinlock_t *ptl;
4070
4071 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4072 for (; addr != end; pte++, addr += PAGE_SIZE)
4073 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4074 mc.precharge++; /* increment precharge temporarily */
4075 pte_unmap_unlock(pte - 1, ptl);
4076 cond_resched();
4077
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004078 return 0;
4079}
4080
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004081static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4082{
4083 unsigned long precharge;
4084 struct vm_area_struct *vma;
4085
4086 down_read(&mm->mmap_sem);
4087 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4088 struct mm_walk mem_cgroup_count_precharge_walk = {
4089 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4090 .mm = mm,
4091 .private = vma,
4092 };
4093 if (is_vm_hugetlb_page(vma))
4094 continue;
4095 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
4096 if (vma->vm_flags & VM_SHARED)
4097 continue;
4098 walk_page_range(vma->vm_start, vma->vm_end,
4099 &mem_cgroup_count_precharge_walk);
4100 }
4101 up_read(&mm->mmap_sem);
4102
4103 precharge = mc.precharge;
4104 mc.precharge = 0;
4105
4106 return precharge;
4107}
4108
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004109static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4110{
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004111 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004112}
4113
4114static void mem_cgroup_clear_mc(void)
4115{
4116 /* we must uncharge all the leftover precharges from mc.to */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004117 if (mc.precharge) {
4118 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4119 mc.precharge = 0;
4120 }
4121 /*
4122 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4123 * we must uncharge here.
4124 */
4125 if (mc.moved_charge) {
4126 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4127 mc.moved_charge = 0;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004128 }
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004129 /* we must fixup refcnts and charges */
4130 if (mc.moved_swap) {
4131 WARN_ON_ONCE(mc.moved_swap > INT_MAX);
4132 /* uncharge swap account from the old cgroup */
4133 if (!mem_cgroup_is_root(mc.from))
4134 res_counter_uncharge(&mc.from->memsw,
4135 PAGE_SIZE * mc.moved_swap);
4136 __mem_cgroup_put(mc.from, mc.moved_swap);
4137
4138 if (!mem_cgroup_is_root(mc.to)) {
4139 /*
4140 * we charged both to->res and to->memsw, so we should
4141 * uncharge to->res.
4142 */
4143 res_counter_uncharge(&mc.to->res,
4144 PAGE_SIZE * mc.moved_swap);
4145 VM_BUG_ON(test_bit(CSS_ROOT, &mc.to->css.flags));
4146 __css_put(&mc.to->css, mc.moved_swap);
4147 }
4148 /* we've already done mem_cgroup_get(mc.to) */
4149
4150 mc.moved_swap = 0;
4151 }
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004152 mc.from = NULL;
4153 mc.to = NULL;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004154 mc.moving_task = NULL;
4155 wake_up_all(&mc.waitq);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004156}
4157
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004158static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4159 struct cgroup *cgroup,
4160 struct task_struct *p,
4161 bool threadgroup)
4162{
4163 int ret = 0;
4164 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4165
4166 if (mem->move_charge_at_immigrate) {
4167 struct mm_struct *mm;
4168 struct mem_cgroup *from = mem_cgroup_from_task(p);
4169
4170 VM_BUG_ON(from == mem);
4171
4172 mm = get_task_mm(p);
4173 if (!mm)
4174 return 0;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004175 /* We move charges only when we move a owner of the mm */
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004176 if (mm->owner == p) {
4177 VM_BUG_ON(mc.from);
4178 VM_BUG_ON(mc.to);
4179 VM_BUG_ON(mc.precharge);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004180 VM_BUG_ON(mc.moved_charge);
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004181 VM_BUG_ON(mc.moved_swap);
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004182 VM_BUG_ON(mc.moving_task);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004183 mc.from = from;
4184 mc.to = mem;
4185 mc.precharge = 0;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004186 mc.moved_charge = 0;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004187 mc.moved_swap = 0;
Daisuke Nishimura8033b972010-03-10 15:22:16 -08004188 mc.moving_task = current;
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004189
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004190 ret = mem_cgroup_precharge_mc(mm);
4191 if (ret)
4192 mem_cgroup_clear_mc();
4193 }
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004194 mmput(mm);
4195 }
4196 return ret;
4197}
4198
4199static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4200 struct cgroup *cgroup,
4201 struct task_struct *p,
4202 bool threadgroup)
4203{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004204 mem_cgroup_clear_mc();
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004205}
4206
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004207static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4208 unsigned long addr, unsigned long end,
4209 struct mm_walk *walk)
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004210{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004211 int ret = 0;
4212 struct vm_area_struct *vma = walk->private;
4213 pte_t *pte;
4214 spinlock_t *ptl;
4215
4216retry:
4217 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4218 for (; addr != end; addr += PAGE_SIZE) {
4219 pte_t ptent = *(pte++);
4220 union mc_target target;
4221 int type;
4222 struct page *page;
4223 struct page_cgroup *pc;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004224 swp_entry_t ent;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004225
4226 if (!mc.precharge)
4227 break;
4228
4229 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4230 switch (type) {
4231 case MC_TARGET_PAGE:
4232 page = target.page;
4233 if (isolate_lru_page(page))
4234 goto put;
4235 pc = lookup_page_cgroup(page);
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004236 if (!mem_cgroup_move_account(pc,
4237 mc.from, mc.to, false)) {
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004238 mc.precharge--;
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004239 /* we uncharge from mc.from later. */
4240 mc.moved_charge++;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004241 }
4242 putback_lru_page(page);
4243put: /* is_target_pte_for_mc() gets the page */
4244 put_page(page);
4245 break;
Daisuke Nishimura02491442010-03-10 15:22:17 -08004246 case MC_TARGET_SWAP:
4247 ent = target.ent;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004248 if (!mem_cgroup_move_swap_account(ent,
4249 mc.from, mc.to, false)) {
Daisuke Nishimura02491442010-03-10 15:22:17 -08004250 mc.precharge--;
Daisuke Nishimura483c30b2010-03-10 15:22:18 -08004251 /* we fixup refcnts and charges later. */
4252 mc.moved_swap++;
4253 }
Daisuke Nishimura02491442010-03-10 15:22:17 -08004254 break;
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004255 default:
4256 break;
4257 }
4258 }
4259 pte_unmap_unlock(pte - 1, ptl);
4260 cond_resched();
4261
4262 if (addr != end) {
4263 /*
4264 * We have consumed all precharges we got in can_attach().
4265 * We try charge one by one, but don't do any additional
4266 * charges to mc.to if we have failed in charge once in attach()
4267 * phase.
4268 */
Daisuke Nishimura854ffa82010-03-10 15:22:15 -08004269 ret = mem_cgroup_do_precharge(1);
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004270 if (!ret)
4271 goto retry;
4272 }
4273
4274 return ret;
4275}
4276
4277static void mem_cgroup_move_charge(struct mm_struct *mm)
4278{
4279 struct vm_area_struct *vma;
4280
4281 lru_add_drain_all();
4282 down_read(&mm->mmap_sem);
4283 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4284 int ret;
4285 struct mm_walk mem_cgroup_move_charge_walk = {
4286 .pmd_entry = mem_cgroup_move_charge_pte_range,
4287 .mm = mm,
4288 .private = vma,
4289 };
4290 if (is_vm_hugetlb_page(vma))
4291 continue;
4292 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
4293 if (vma->vm_flags & VM_SHARED)
4294 continue;
4295 ret = walk_page_range(vma->vm_start, vma->vm_end,
4296 &mem_cgroup_move_charge_walk);
4297 if (ret)
4298 /*
4299 * means we have consumed all precharges and failed in
4300 * doing additional charge. Just abandon here.
4301 */
4302 break;
4303 }
4304 up_read(&mm->mmap_sem);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004305}
4306
Balbir Singh67e465a2008-02-07 00:13:54 -08004307static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4308 struct cgroup *cont,
4309 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07004310 struct task_struct *p,
4311 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08004312{
Daisuke Nishimura4ffef5f2010-03-10 15:22:14 -08004313 struct mm_struct *mm;
4314
4315 if (!mc.to)
4316 /* no need to move charge */
4317 return;
4318
4319 mm = get_task_mm(p);
4320 if (mm) {
4321 mem_cgroup_move_charge(mm);
4322 mmput(mm);
4323 }
4324 mem_cgroup_clear_mc();
Balbir Singh67e465a2008-02-07 00:13:54 -08004325}
4326
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004327struct cgroup_subsys mem_cgroup_subsys = {
4328 .name = "memory",
4329 .subsys_id = mem_cgroup_subsys_id,
4330 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08004331 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004332 .destroy = mem_cgroup_destroy,
4333 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08004334 .can_attach = mem_cgroup_can_attach,
4335 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08004336 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08004337 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07004338 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08004339};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08004340
4341#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4342
4343static int __init disable_swap_account(char *s)
4344{
4345 really_do_swap_account = 0;
4346 return 1;
4347}
4348__setup("noswapaccount", disable_swap_account);
4349#endif