blob: 59ffaf511d77fce9c7f8f6fe7cc2e02cf456bc85 [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 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhe2224322009-04-02 16:57:39 -070030#include <linux/limits.h>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080031#include <linux/mutex.h>
Balbir Singhf64c3f52009-09-23 15:56:37 -070032#include <linux/rbtree.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070033#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080034#include <linux/swap.h>
35#include <linux/spinlock.h>
36#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080037#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070038#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070039#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070040#include <linux/page_cgroup.h>
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -080041#include <linux/cpu.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080042#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080043
Balbir Singh8697d332008-02-07 00:13:59 -080044#include <asm/uaccess.h>
45
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070046struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070047#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh4b3bde42009-09-23 15:56:32 -070048struct mem_cgroup *root_mem_cgroup __read_mostly;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080049
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080050#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
Li Zefan338c8432009-06-17 16:27:15 -070051/* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080052int do_swap_account __read_mostly;
53static int really_do_swap_account __initdata = 1; /* for remember boot option*/
54#else
55#define do_swap_account (0)
56#endif
57
Balbir Singhf64c3f52009-09-23 15:56:37 -070058#define SOFTLIMIT_EVENTS_THRESH (1000)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080059
Balbir Singh8cdea7c2008-02-07 00:13:50 -080060/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080061 * Statistics for memory cgroup.
62 */
63enum mem_cgroup_stat_index {
64 /*
65 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
66 */
67 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
Balbir Singhd69b0422009-06-17 16:26:34 -070068 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -080069 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070070 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
Balbir Singhf64c3f52009-09-23 15:56:37 -070072 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
Balbir Singh0c3e73e2009-09-23 15:56:42 -070073 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080074
75 MEM_CGROUP_STAT_NSTATS,
76};
77
78struct mem_cgroup_stat_cpu {
79 s64 count[MEM_CGROUP_STAT_NSTATS];
80} ____cacheline_aligned_in_smp;
81
82struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080083 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080084};
85
Balbir Singhf64c3f52009-09-23 15:56:37 -070086static inline void
87__mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
88 enum mem_cgroup_stat_index idx)
89{
90 stat->count[idx] = 0;
91}
92
93static inline s64
94__mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
95 enum mem_cgroup_stat_index idx)
96{
97 return stat->count[idx];
98}
99
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800100/*
101 * For accounting under irq disable, no need for increment preempt count.
102 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700103static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800104 enum mem_cgroup_stat_index idx, int val)
105{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700106 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800107}
108
109static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
110 enum mem_cgroup_stat_index idx)
111{
112 int cpu;
113 s64 ret = 0;
114 for_each_possible_cpu(cpu)
115 ret += stat->cpustat[cpu].count[idx];
116 return ret;
117}
118
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700119static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
120{
121 s64 ret;
122
123 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
124 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
125 return ret;
126}
127
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800128/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800129 * per-zone information in memory controller.
130 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800131struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800132 /*
133 * spin_lock to protect the per cgroup LRU
134 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700135 struct list_head lists[NR_LRU_LISTS];
136 unsigned long count[NR_LRU_LISTS];
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800137
138 struct zone_reclaim_stat reclaim_stat;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700139 struct rb_node tree_node; /* RB tree node */
140 unsigned long long usage_in_excess;/* Set to the value by which */
141 /* the soft limit is exceeded*/
142 bool on_tree;
Balbir Singh4e416952009-09-23 15:56:39 -0700143 struct mem_cgroup *mem; /* Back pointer, we cannot */
144 /* use container_of */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800145};
146/* Macro for accessing counter */
147#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
148
149struct mem_cgroup_per_node {
150 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
151};
152
153struct mem_cgroup_lru_info {
154 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
155};
156
157/*
Balbir Singhf64c3f52009-09-23 15:56:37 -0700158 * Cgroups above their limits are maintained in a RB-Tree, independent of
159 * their hierarchy representation
160 */
161
162struct mem_cgroup_tree_per_zone {
163 struct rb_root rb_root;
164 spinlock_t lock;
165};
166
167struct mem_cgroup_tree_per_node {
168 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
169};
170
171struct mem_cgroup_tree {
172 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
173};
174
175static struct mem_cgroup_tree soft_limit_tree __read_mostly;
176
177/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800178 * The memory controller data structure. The memory controller controls both
179 * page cache and RSS per cgroup. We would eventually like to provide
180 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
181 * to help the administrator determine what knobs to tune.
182 *
183 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800184 * we hit the water mark. May be even add a low water mark, such that
185 * no reclaim occurs from a cgroup at it's low water mark, this is
186 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800187 */
188struct mem_cgroup {
189 struct cgroup_subsys_state css;
190 /*
191 * the counter to account for memory usage
192 */
193 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800194 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800195 * the counter to account for mem+swap usage.
196 */
197 struct res_counter memsw;
198 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800199 * Per cgroup active and inactive list, similar to the
200 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800201 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800202 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800203
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800204 /*
205 protect against reclaim related member.
206 */
207 spinlock_t reclaim_param_lock;
208
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800209 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800210
211 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -0200212 * While reclaiming in a hierarchy, we cache the last child we
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700213 * reclaimed from.
Balbir Singh6d61ef42009-01-07 18:08:06 -0800214 */
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -0700215 int last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800216 /*
217 * Should the accounting and control be hierarchical, per subtree?
218 */
219 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800220 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800221 atomic_t refcnt;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800222
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800223 unsigned int swappiness;
224
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -0700225 /* set when res.limit == memsw.limit */
226 bool memsw_is_minimum;
227
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800228 /*
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800229 * Should we move charges of a task when a task is moved into this
230 * mem_cgroup ? And what type of charges should we move ?
231 */
232 unsigned long move_charge_at_immigrate;
233
234 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800235 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800236 */
237 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800238};
239
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -0800240/* Stuffs for move charges at task migration. */
241/*
242 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
243 * left-shifted bitmap of these types.
244 */
245enum move_type {
246 NR_MOVE_TYPE,
247};
248
Balbir Singh4e416952009-09-23 15:56:39 -0700249/*
250 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
251 * limit reclaim to prevent infinite loops, if they ever occur.
252 */
253#define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
254#define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
255
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800256enum charge_type {
257 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
258 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700259 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700260 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800261 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -0700262 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700263 NR_CHARGE_TYPE,
264};
265
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700266/* only for here (for easy reading.) */
267#define PCGF_CACHE (1UL << PCG_CACHE)
268#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700269#define PCGF_LOCK (1UL << PCG_LOCK)
Balbir Singh4b3bde42009-09-23 15:56:32 -0700270/* Not used, but added here for completeness */
271#define PCGF_ACCT (1UL << PCG_ACCT)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800272
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800273/* for encoding cft->private value on file */
274#define _MEM (0)
275#define _MEMSWAP (1)
276#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
277#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
278#define MEMFILE_ATTR(val) ((val) & 0xffff)
279
Balbir Singh75822b42009-09-23 15:56:38 -0700280/*
281 * Reclaim flags for mem_cgroup_hierarchical_reclaim
282 */
283#define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
284#define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
285#define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
286#define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
Balbir Singh4e416952009-09-23 15:56:39 -0700287#define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
288#define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
Balbir Singh75822b42009-09-23 15:56:38 -0700289
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800290static void mem_cgroup_get(struct mem_cgroup *mem);
291static void mem_cgroup_put(struct mem_cgroup *mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -0800292static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -0800293static void drain_all_stock_async(void);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800294
Balbir Singhf64c3f52009-09-23 15:56:37 -0700295static struct mem_cgroup_per_zone *
296mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
297{
298 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
299}
300
Wu Fengguangd3242362009-12-16 12:19:59 +0100301struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
302{
303 return &mem->css;
304}
305
Balbir Singhf64c3f52009-09-23 15:56:37 -0700306static struct mem_cgroup_per_zone *
307page_cgroup_zoneinfo(struct page_cgroup *pc)
308{
309 struct mem_cgroup *mem = pc->mem_cgroup;
310 int nid = page_cgroup_nid(pc);
311 int zid = page_cgroup_zid(pc);
312
313 if (!mem)
314 return NULL;
315
316 return mem_cgroup_zoneinfo(mem, nid, zid);
317}
318
319static struct mem_cgroup_tree_per_zone *
320soft_limit_tree_node_zone(int nid, int zid)
321{
322 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
323}
324
325static struct mem_cgroup_tree_per_zone *
326soft_limit_tree_from_page(struct page *page)
327{
328 int nid = page_to_nid(page);
329 int zid = page_zonenum(page);
330
331 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
332}
333
334static void
Balbir Singh4e416952009-09-23 15:56:39 -0700335__mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
Balbir Singhf64c3f52009-09-23 15:56:37 -0700336 struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700337 struct mem_cgroup_tree_per_zone *mctz,
338 unsigned long long new_usage_in_excess)
Balbir Singhf64c3f52009-09-23 15:56:37 -0700339{
340 struct rb_node **p = &mctz->rb_root.rb_node;
341 struct rb_node *parent = NULL;
342 struct mem_cgroup_per_zone *mz_node;
343
344 if (mz->on_tree)
345 return;
346
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700347 mz->usage_in_excess = new_usage_in_excess;
348 if (!mz->usage_in_excess)
349 return;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700350 while (*p) {
351 parent = *p;
352 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
353 tree_node);
354 if (mz->usage_in_excess < mz_node->usage_in_excess)
355 p = &(*p)->rb_left;
356 /*
357 * We can't avoid mem cgroups that are over their soft
358 * limit by the same amount
359 */
360 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
361 p = &(*p)->rb_right;
362 }
363 rb_link_node(&mz->tree_node, parent, p);
364 rb_insert_color(&mz->tree_node, &mctz->rb_root);
365 mz->on_tree = true;
Balbir Singh4e416952009-09-23 15:56:39 -0700366}
367
368static void
369__mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
370 struct mem_cgroup_per_zone *mz,
371 struct mem_cgroup_tree_per_zone *mctz)
372{
373 if (!mz->on_tree)
374 return;
375 rb_erase(&mz->tree_node, &mctz->rb_root);
376 mz->on_tree = false;
377}
378
379static void
Balbir Singhf64c3f52009-09-23 15:56:37 -0700380mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
381 struct mem_cgroup_per_zone *mz,
382 struct mem_cgroup_tree_per_zone *mctz)
383{
384 spin_lock(&mctz->lock);
Balbir Singh4e416952009-09-23 15:56:39 -0700385 __mem_cgroup_remove_exceeded(mem, mz, mctz);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700386 spin_unlock(&mctz->lock);
387}
388
389static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
390{
391 bool ret = false;
392 int cpu;
393 s64 val;
394 struct mem_cgroup_stat_cpu *cpustat;
395
396 cpu = get_cpu();
397 cpustat = &mem->stat.cpustat[cpu];
398 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
399 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
400 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
401 ret = true;
402 }
403 put_cpu();
404 return ret;
405}
406
407static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
408{
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700409 unsigned long long excess;
Balbir Singhf64c3f52009-09-23 15:56:37 -0700410 struct mem_cgroup_per_zone *mz;
411 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700412 int nid = page_to_nid(page);
413 int zid = page_zonenum(page);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700414 mctz = soft_limit_tree_from_page(page);
415
416 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700417 * Necessary to update all ancestors when hierarchy is used.
418 * because their event counter is not touched.
Balbir Singhf64c3f52009-09-23 15:56:37 -0700419 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700420 for (; mem; mem = parent_mem_cgroup(mem)) {
421 mz = mem_cgroup_zoneinfo(mem, nid, zid);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700422 excess = res_counter_soft_limit_excess(&mem->res);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700423 /*
424 * We have to update the tree if mz is on RB-tree or
425 * mem is over its softlimit.
426 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700427 if (excess || mz->on_tree) {
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700428 spin_lock(&mctz->lock);
429 /* if on-tree, remove it */
430 if (mz->on_tree)
431 __mem_cgroup_remove_exceeded(mem, mz, mctz);
432 /*
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700433 * Insert again. mz->usage_in_excess will be updated.
434 * If excess is 0, no tree ops.
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700435 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -0700436 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -0700437 spin_unlock(&mctz->lock);
438 }
Balbir Singhf64c3f52009-09-23 15:56:37 -0700439 }
440}
441
442static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
443{
444 int node, zone;
445 struct mem_cgroup_per_zone *mz;
446 struct mem_cgroup_tree_per_zone *mctz;
447
448 for_each_node_state(node, N_POSSIBLE) {
449 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
450 mz = mem_cgroup_zoneinfo(mem, node, zone);
451 mctz = soft_limit_tree_node_zone(node, zone);
452 mem_cgroup_remove_exceeded(mem, mz, mctz);
453 }
454 }
455}
456
Balbir Singh4e416952009-09-23 15:56:39 -0700457static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
458{
459 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
460}
461
462static struct mem_cgroup_per_zone *
463__mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
464{
465 struct rb_node *rightmost = NULL;
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700466 struct mem_cgroup_per_zone *mz;
Balbir Singh4e416952009-09-23 15:56:39 -0700467
468retry:
KAMEZAWA Hiroyuki26251ea2009-10-01 15:44:08 -0700469 mz = NULL;
Balbir Singh4e416952009-09-23 15:56:39 -0700470 rightmost = rb_last(&mctz->rb_root);
471 if (!rightmost)
472 goto done; /* Nothing to reclaim from */
473
474 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
475 /*
476 * Remove the node now but someone else can add it back,
477 * we will to add it back at the end of reclaim to its correct
478 * position in the tree.
479 */
480 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
481 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
482 !css_tryget(&mz->mem->css))
483 goto retry;
484done:
485 return mz;
486}
487
488static struct mem_cgroup_per_zone *
489mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
490{
491 struct mem_cgroup_per_zone *mz;
492
493 spin_lock(&mctz->lock);
494 mz = __mem_cgroup_largest_soft_limit_node(mctz);
495 spin_unlock(&mctz->lock);
496 return mz;
497}
498
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700499static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
500 bool charge)
501{
502 int val = (charge) ? 1 : -1;
503 struct mem_cgroup_stat *stat = &mem->stat;
504 struct mem_cgroup_stat_cpu *cpustat;
505 int cpu = get_cpu();
506
507 cpustat = &stat->cpustat[cpu];
508 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
509 put_cpu();
510}
511
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700512static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
513 struct page_cgroup *pc,
514 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800515{
Balbir Singh0c3e73e2009-09-23 15:56:42 -0700516 int val = (charge) ? 1 : -1;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800517 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700518 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800519 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800520
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800521 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700522 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700523 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800524 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700525 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700526
527 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700528 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700529 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
530 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700531 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700532 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
Balbir Singhf64c3f52009-09-23 15:56:37 -0700533 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800534 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800535}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800536
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700537static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700538 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800539{
540 int nid, zid;
541 struct mem_cgroup_per_zone *mz;
542 u64 total = 0;
543
544 for_each_online_node(nid)
545 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
546 mz = mem_cgroup_zoneinfo(mem, nid, zid);
547 total += MEM_CGROUP_ZSTAT(mz, idx);
548 }
549 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800550}
551
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800552static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800553{
554 return container_of(cgroup_subsys_state(cont,
555 mem_cgroup_subsys_id), struct mem_cgroup,
556 css);
557}
558
Balbir Singhcf475ad2008-04-29 01:00:16 -0700559struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800560{
Balbir Singh31a78f22008-09-28 23:09:31 +0100561 /*
562 * mm_update_next_owner() may clear mm->owner to NULL
563 * if it races with swapoff, page migration, etc.
564 * So this can be called with p == NULL.
565 */
566 if (unlikely(!p))
567 return NULL;
568
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800569 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
570 struct mem_cgroup, css);
571}
572
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800573static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
574{
575 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700576
577 if (!mm)
578 return NULL;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -0800579 /*
580 * Because we have no locks, mm->owner's may be being moved to other
581 * cgroup. We use css_tryget() here even if this looks
582 * pessimistic (rather than adding locks here).
583 */
584 rcu_read_lock();
585 do {
586 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
587 if (unlikely(!mem))
588 break;
589 } while (!css_tryget(&mem->css));
590 rcu_read_unlock();
591 return mem;
592}
593
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700594/*
595 * Call callback function against all cgroup under hierarchy tree.
596 */
597static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
598 int (*func)(struct mem_cgroup *, void *))
599{
600 int found, ret, nextid;
601 struct cgroup_subsys_state *css;
602 struct mem_cgroup *mem;
603
604 if (!root->use_hierarchy)
605 return (*func)(root, data);
606
607 nextid = 1;
608 do {
609 ret = 0;
610 mem = NULL;
611
612 rcu_read_lock();
613 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
614 &found);
615 if (css && css_tryget(css))
616 mem = container_of(css, struct mem_cgroup, css);
617 rcu_read_unlock();
618
619 if (mem) {
620 ret = (*func)(mem, data);
621 css_put(&mem->css);
622 }
623 nextid = found + 1;
624 } while (!ret && css);
625
626 return ret;
627}
628
Balbir Singh4b3bde42009-09-23 15:56:32 -0700629static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
630{
631 return (mem == root_mem_cgroup);
632}
633
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800634/*
635 * Following LRU functions are allowed to be used without PCG_LOCK.
636 * Operations are called by routine of global LRU independently from memcg.
637 * What we have to take care of here is validness of pc->mem_cgroup.
638 *
639 * Changes to pc->mem_cgroup happens when
640 * 1. charge
641 * 2. moving account
642 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
643 * It is added to LRU before charge.
644 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
645 * When moving account, the page is not on LRU. It's isolated.
646 */
647
648void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800649{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800650 struct page_cgroup *pc;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800651 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700652
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800653 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800654 return;
655 pc = lookup_page_cgroup(page);
656 /* can happen while we handle swapcache. */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700657 if (!TestClearPageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800658 return;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700659 VM_BUG_ON(!pc->mem_cgroup);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800660 /*
661 * We don't check PCG_USED bit. It's cleared when the "page" is finally
662 * removed from global LRU.
663 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800664 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700665 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700666 if (mem_cgroup_is_root(pc->mem_cgroup))
667 return;
668 VM_BUG_ON(list_empty(&pc->lru));
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800669 list_del_init(&pc->lru);
670 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800671}
672
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800673void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800674{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800675 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800676}
677
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800678void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800679{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800680 struct mem_cgroup_per_zone *mz;
681 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800682
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800683 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700684 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700685
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800686 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800687 /*
688 * Used bit is set without atomic ops but after smp_wmb().
689 * For making pc->mem_cgroup visible, insert smp_rmb() here.
690 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800691 smp_rmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -0700692 /* unused or root page is not rotated. */
693 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800694 return;
695 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700696 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800697}
698
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800699void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
700{
701 struct page_cgroup *pc;
702 struct mem_cgroup_per_zone *mz;
703
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800704 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800705 return;
706 pc = lookup_page_cgroup(page);
Balbir Singh4b3bde42009-09-23 15:56:32 -0700707 VM_BUG_ON(PageCgroupAcctLRU(pc));
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800708 /*
709 * Used bit is set without atomic ops but after smp_wmb().
710 * For making pc->mem_cgroup visible, insert smp_rmb() here.
711 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800712 smp_rmb();
713 if (!PageCgroupUsed(pc))
714 return;
715
716 mz = page_cgroup_zoneinfo(pc);
717 MEM_CGROUP_ZSTAT(mz, lru) += 1;
Balbir Singh4b3bde42009-09-23 15:56:32 -0700718 SetPageCgroupAcctLRU(pc);
719 if (mem_cgroup_is_root(pc->mem_cgroup))
720 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800721 list_add(&pc->lru, &mz->lists[lru]);
722}
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800723
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800724/*
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800725 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
726 * lru because the page may.be reused after it's fully uncharged (because of
727 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
728 * it again. This function is only used to charge SwapCache. It's done under
729 * lock_page and expected that zone->lru_lock is never held.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800730 */
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800731static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800732{
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800733 unsigned long flags;
734 struct zone *zone = page_zone(page);
735 struct page_cgroup *pc = lookup_page_cgroup(page);
736
737 spin_lock_irqsave(&zone->lru_lock, flags);
738 /*
739 * Forget old LRU when this page_cgroup is *not* used. This Used bit
740 * is guarded by lock_page() because the page is SwapCache.
741 */
742 if (!PageCgroupUsed(pc))
743 mem_cgroup_del_lru_list(page, page_lru(page));
744 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800745}
746
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800747static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
748{
749 unsigned long flags;
750 struct zone *zone = page_zone(page);
751 struct page_cgroup *pc = lookup_page_cgroup(page);
752
753 spin_lock_irqsave(&zone->lru_lock, flags);
754 /* link when the page is linked to LRU but page_cgroup isn't */
Balbir Singh4b3bde42009-09-23 15:56:32 -0700755 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -0800756 mem_cgroup_add_lru_list(page, page_lru(page));
757 spin_unlock_irqrestore(&zone->lru_lock, flags);
758}
759
760
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800761void mem_cgroup_move_lists(struct page *page,
762 enum lru_list from, enum lru_list to)
763{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800764 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800765 return;
766 mem_cgroup_del_lru_list(page, from);
767 mem_cgroup_add_lru_list(page, to);
768}
769
David Rientjes4c4a2212008-02-07 00:14:06 -0800770int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
771{
772 int ret;
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700773 struct mem_cgroup *curr = NULL;
David Rientjes4c4a2212008-02-07 00:14:06 -0800774
775 task_lock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700776 rcu_read_lock();
777 curr = try_get_mem_cgroup_from_mm(task->mm);
778 rcu_read_unlock();
David Rientjes4c4a2212008-02-07 00:14:06 -0800779 task_unlock(task);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700780 if (!curr)
781 return 0;
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -0800782 /*
783 * We should check use_hierarchy of "mem" not "curr". Because checking
784 * use_hierarchy of "curr" here make this function true if hierarchy is
785 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
786 * hierarchy(even if use_hierarchy is disabled in "mem").
787 */
788 if (mem->use_hierarchy)
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -0700789 ret = css_is_ancestor(&curr->css, &mem->css);
790 else
791 ret = (curr == mem);
792 css_put(&curr->css);
David Rientjes4c4a2212008-02-07 00:14:06 -0800793 return ret;
794}
795
Balbir Singh66e17072008-02-07 00:13:56 -0800796/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800797 * prev_priority control...this will be used in memory reclaim path.
798 */
799int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
800{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800801 int prev_priority;
802
803 spin_lock(&mem->reclaim_param_lock);
804 prev_priority = mem->prev_priority;
805 spin_unlock(&mem->reclaim_param_lock);
806
807 return prev_priority;
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800808}
809
810void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
811{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800812 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800813 if (priority < mem->prev_priority)
814 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800815 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800816}
817
818void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
819{
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800820 spin_lock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800821 mem->prev_priority = priority;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -0800822 spin_unlock(&mem->reclaim_param_lock);
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800823}
824
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800825static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800826{
827 unsigned long active;
828 unsigned long inactive;
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800829 unsigned long gb;
830 unsigned long inactive_ratio;
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800831
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -0700832 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
833 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800834
KOSAKI Motohiroc772be92009-01-07 18:08:25 -0800835 gb = (inactive + active) >> (30 - PAGE_SHIFT);
836 if (gb)
837 inactive_ratio = int_sqrt(10 * gb);
838 else
839 inactive_ratio = 1;
840
841 if (present_pages) {
842 present_pages[0] = inactive;
843 present_pages[1] = active;
844 }
845
846 return inactive_ratio;
847}
848
849int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
850{
851 unsigned long active;
852 unsigned long inactive;
853 unsigned long present_pages[2];
854 unsigned long inactive_ratio;
855
856 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
857
858 inactive = present_pages[0];
859 active = present_pages[1];
860
861 if (inactive * inactive_ratio < active)
KOSAKI Motohiro14797e22009-01-07 18:08:18 -0800862 return 1;
863
864 return 0;
865}
866
Rik van Riel56e49d22009-06-16 15:32:28 -0700867int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
868{
869 unsigned long active;
870 unsigned long inactive;
871
872 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
873 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
874
875 return (active > inactive);
876}
877
KOSAKI Motohiroa3d8e052009-01-07 18:08:19 -0800878unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
879 struct zone *zone,
880 enum lru_list lru)
881{
882 int nid = zone->zone_pgdat->node_id;
883 int zid = zone_idx(zone);
884 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
885
886 return MEM_CGROUP_ZSTAT(mz, lru);
887}
888
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800889struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
890 struct zone *zone)
891{
892 int nid = zone->zone_pgdat->node_id;
893 int zid = zone_idx(zone);
894 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
895
896 return &mz->reclaim_stat;
897}
898
899struct zone_reclaim_stat *
900mem_cgroup_get_reclaim_stat_from_page(struct page *page)
901{
902 struct page_cgroup *pc;
903 struct mem_cgroup_per_zone *mz;
904
905 if (mem_cgroup_disabled())
906 return NULL;
907
908 pc = lookup_page_cgroup(page);
Daisuke Nishimurabd112db2009-01-15 13:51:11 -0800909 /*
910 * Used bit is set without atomic ops but after smp_wmb().
911 * For making pc->mem_cgroup visible, insert smp_rmb() here.
912 */
913 smp_rmb();
914 if (!PageCgroupUsed(pc))
915 return NULL;
916
KOSAKI Motohiro3e2f41f2009-01-07 18:08:20 -0800917 mz = page_cgroup_zoneinfo(pc);
918 if (!mz)
919 return NULL;
920
921 return &mz->reclaim_stat;
922}
923
Balbir Singh66e17072008-02-07 00:13:56 -0800924unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
925 struct list_head *dst,
926 unsigned long *scanned, int order,
927 int mode, struct zone *z,
928 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700929 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800930{
931 unsigned long nr_taken = 0;
932 struct page *page;
933 unsigned long scan;
934 LIST_HEAD(pc_list);
935 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800936 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800937 int nid = z->zone_pgdat->node_id;
938 int zid = zone_idx(z);
939 struct mem_cgroup_per_zone *mz;
Johannes Weinerb7c46d12009-09-21 17:02:56 -0700940 int lru = LRU_FILE * file + active;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700941 int ret;
Balbir Singh66e17072008-02-07 00:13:56 -0800942
Balbir Singhcf475ad2008-04-29 01:00:16 -0700943 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800944 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700945 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800946
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800947 scan = 0;
948 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800949 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800950 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800951
952 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700953 if (unlikely(!PageCgroupUsed(pc)))
954 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800955 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800956 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800957
Hugh Dickins436c65412008-02-07 00:14:12 -0800958 scan++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700959 ret = __isolate_lru_page(page, mode, file);
960 switch (ret) {
961 case 0:
Balbir Singh66e17072008-02-07 00:13:56 -0800962 list_move(&page->lru, dst);
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700963 mem_cgroup_del_lru(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800964 nr_taken++;
KAMEZAWA Hiroyuki2ffebca2009-06-17 16:27:21 -0700965 break;
966 case -EBUSY:
967 /* we don't affect global LRU but rotate in our LRU */
968 mem_cgroup_rotate_lru_list(page, page_lru(page));
969 break;
970 default:
971 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800972 }
973 }
974
Balbir Singh66e17072008-02-07 00:13:56 -0800975 *scanned = scan;
976 return nr_taken;
977}
978
Balbir Singh6d61ef42009-01-07 18:08:06 -0800979#define mem_cgroup_from_res_counter(counter, member) \
980 container_of(counter, struct mem_cgroup, member)
981
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -0800982static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
983{
984 if (do_swap_account) {
985 if (res_counter_check_under_limit(&mem->res) &&
986 res_counter_check_under_limit(&mem->memsw))
987 return true;
988 } else
989 if (res_counter_check_under_limit(&mem->res))
990 return true;
991 return false;
992}
993
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -0800994static unsigned int get_swappiness(struct mem_cgroup *memcg)
995{
996 struct cgroup *cgrp = memcg->css.cgroup;
997 unsigned int swappiness;
998
999 /* root ? */
1000 if (cgrp->parent == NULL)
1001 return vm_swappiness;
1002
1003 spin_lock(&memcg->reclaim_param_lock);
1004 swappiness = memcg->swappiness;
1005 spin_unlock(&memcg->reclaim_param_lock);
1006
1007 return swappiness;
1008}
1009
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001010static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1011{
1012 int *val = data;
1013 (*val)++;
1014 return 0;
1015}
Balbir Singhe2224322009-04-02 16:57:39 -07001016
1017/**
1018 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
1019 * @memcg: The memory cgroup that went over limit
1020 * @p: Task that is going to be killed
1021 *
1022 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1023 * enabled
1024 */
1025void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1026{
1027 struct cgroup *task_cgrp;
1028 struct cgroup *mem_cgrp;
1029 /*
1030 * Need a buffer in BSS, can't rely on allocations. The code relies
1031 * on the assumption that OOM is serialized for memory controller.
1032 * If this assumption is broken, revisit this code.
1033 */
1034 static char memcg_name[PATH_MAX];
1035 int ret;
1036
Daisuke Nishimurad31f56d2009-12-15 16:47:12 -08001037 if (!memcg || !p)
Balbir Singhe2224322009-04-02 16:57:39 -07001038 return;
1039
1040
1041 rcu_read_lock();
1042
1043 mem_cgrp = memcg->css.cgroup;
1044 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1045
1046 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1047 if (ret < 0) {
1048 /*
1049 * Unfortunately, we are unable to convert to a useful name
1050 * But we'll still print out the usage information
1051 */
1052 rcu_read_unlock();
1053 goto done;
1054 }
1055 rcu_read_unlock();
1056
1057 printk(KERN_INFO "Task in %s killed", memcg_name);
1058
1059 rcu_read_lock();
1060 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1061 if (ret < 0) {
1062 rcu_read_unlock();
1063 goto done;
1064 }
1065 rcu_read_unlock();
1066
1067 /*
1068 * Continues from above, so we don't need an KERN_ level
1069 */
1070 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1071done:
1072
1073 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1074 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1075 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1076 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1077 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1078 "failcnt %llu\n",
1079 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1080 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1081 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1082}
1083
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001084/*
1085 * This function returns the number of memcg under hierarchy tree. Returns
1086 * 1(self count) if no children.
1087 */
1088static int mem_cgroup_count_children(struct mem_cgroup *mem)
1089{
1090 int num = 0;
1091 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1092 return num;
1093}
1094
Balbir Singh6d61ef42009-01-07 18:08:06 -08001095/*
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001096 * Visit the first child (need not be the first child as per the ordering
1097 * of the cgroup list, since we track last_scanned_child) of @mem and use
1098 * that to reclaim free pages from.
1099 */
1100static struct mem_cgroup *
1101mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1102{
1103 struct mem_cgroup *ret = NULL;
1104 struct cgroup_subsys_state *css;
1105 int nextid, found;
1106
1107 if (!root_mem->use_hierarchy) {
1108 css_get(&root_mem->css);
1109 ret = root_mem;
1110 }
1111
1112 while (!ret) {
1113 rcu_read_lock();
1114 nextid = root_mem->last_scanned_child + 1;
1115 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1116 &found);
1117 if (css && css_tryget(css))
1118 ret = container_of(css, struct mem_cgroup, css);
1119
1120 rcu_read_unlock();
1121 /* Updates scanning parameter */
1122 spin_lock(&root_mem->reclaim_param_lock);
1123 if (!css) {
1124 /* this means start scan from ID:1 */
1125 root_mem->last_scanned_child = 0;
1126 } else
1127 root_mem->last_scanned_child = found;
1128 spin_unlock(&root_mem->reclaim_param_lock);
1129 }
1130
1131 return ret;
1132}
1133
1134/*
1135 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1136 * we reclaimed from, so that we don't end up penalizing one child extensively
1137 * based on its position in the children list.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001138 *
1139 * root_mem is the original ancestor that we've been reclaim from.
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001140 *
1141 * We give up and return to the caller when we visit root_mem twice.
1142 * (other groups can be removed while we're walking....)
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001143 *
1144 * If shrink==true, for avoiding to free too much, this returns immedieately.
Balbir Singh6d61ef42009-01-07 18:08:06 -08001145 */
1146static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
Balbir Singh4e416952009-09-23 15:56:39 -07001147 struct zone *zone,
Balbir Singh75822b42009-09-23 15:56:38 -07001148 gfp_t gfp_mask,
1149 unsigned long reclaim_options)
Balbir Singh6d61ef42009-01-07 18:08:06 -08001150{
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001151 struct mem_cgroup *victim;
1152 int ret, total = 0;
1153 int loop = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001154 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1155 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
Balbir Singh4e416952009-09-23 15:56:39 -07001156 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1157 unsigned long excess = mem_cgroup_get_excess(root_mem);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001158
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07001159 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1160 if (root_mem->memsw_is_minimum)
1161 noswap = true;
1162
Balbir Singh4e416952009-09-23 15:56:39 -07001163 while (1) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001164 victim = mem_cgroup_select_victim(root_mem);
Balbir Singh4e416952009-09-23 15:56:39 -07001165 if (victim == root_mem) {
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001166 loop++;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001167 if (loop >= 1)
1168 drain_all_stock_async();
Balbir Singh4e416952009-09-23 15:56:39 -07001169 if (loop >= 2) {
1170 /*
1171 * If we have not been able to reclaim
1172 * anything, it might because there are
1173 * no reclaimable pages under this hierarchy
1174 */
1175 if (!check_soft || !total) {
1176 css_put(&victim->css);
1177 break;
1178 }
1179 /*
1180 * We want to do more targetted reclaim.
1181 * excess >> 2 is not to excessive so as to
1182 * reclaim too much, nor too less that we keep
1183 * coming back to reclaim from this cgroup
1184 */
1185 if (total >= (excess >> 2) ||
1186 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1187 css_put(&victim->css);
1188 break;
1189 }
1190 }
1191 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001192 if (!mem_cgroup_local_usage(&victim->stat)) {
1193 /* this cgroup's local usage == 0 */
1194 css_put(&victim->css);
Balbir Singh6d61ef42009-01-07 18:08:06 -08001195 continue;
1196 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001197 /* we use swappiness of local cgroup */
Balbir Singh4e416952009-09-23 15:56:39 -07001198 if (check_soft)
1199 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1200 noswap, get_swappiness(victim), zone,
1201 zone->zone_pgdat->node_id);
1202 else
1203 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1204 noswap, get_swappiness(victim));
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001205 css_put(&victim->css);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07001206 /*
1207 * At shrinking usage, we can't check we should stop here or
1208 * reclaim more. It's depends on callers. last_scanned_child
1209 * will work enough for keeping fairness under tree.
1210 */
1211 if (shrink)
1212 return ret;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001213 total += ret;
Balbir Singh4e416952009-09-23 15:56:39 -07001214 if (check_soft) {
1215 if (res_counter_check_under_soft_limit(&root_mem->res))
1216 return total;
1217 } else if (mem_cgroup_check_under_limit(root_mem))
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001218 return 1 + total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001219 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07001220 return total;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001221}
1222
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001223bool mem_cgroup_oom_called(struct task_struct *task)
1224{
1225 bool ret = false;
1226 struct mem_cgroup *mem;
1227 struct mm_struct *mm;
1228
1229 rcu_read_lock();
1230 mm = task->mm;
1231 if (!mm)
1232 mm = &init_mm;
1233 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1234 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1235 ret = true;
1236 rcu_read_unlock();
1237 return ret;
1238}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001239
1240static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1241{
1242 mem->last_oom_jiffies = jiffies;
1243 return 0;
1244}
1245
1246static void record_last_oom(struct mem_cgroup *mem)
1247{
1248 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1249}
1250
Balbir Singhd69b0422009-06-17 16:26:34 -07001251/*
1252 * Currently used to update mapped file statistics, but the routine can be
1253 * generalized to update other statistics as well.
1254 */
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001255void mem_cgroup_update_file_mapped(struct page *page, int val)
Balbir Singhd69b0422009-06-17 16:26:34 -07001256{
1257 struct mem_cgroup *mem;
1258 struct mem_cgroup_stat *stat;
1259 struct mem_cgroup_stat_cpu *cpustat;
1260 int cpu;
1261 struct page_cgroup *pc;
1262
Balbir Singhd69b0422009-06-17 16:26:34 -07001263 pc = lookup_page_cgroup(page);
1264 if (unlikely(!pc))
1265 return;
1266
1267 lock_page_cgroup(pc);
1268 mem = pc->mem_cgroup;
1269 if (!mem)
1270 goto done;
1271
1272 if (!PageCgroupUsed(pc))
1273 goto done;
1274
1275 /*
1276 * Preemption is already disabled, we don't need get_cpu()
1277 */
1278 cpu = smp_processor_id();
1279 stat = &mem->stat;
1280 cpustat = &stat->cpustat[cpu];
1281
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001282 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
Balbir Singhd69b0422009-06-17 16:26:34 -07001283done:
1284 unlock_page_cgroup(pc);
1285}
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001286
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001287/*
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001288 * size of first charge trial. "32" comes from vmscan.c's magic value.
1289 * TODO: maybe necessary to use big numbers in big irons.
1290 */
1291#define CHARGE_SIZE (32 * PAGE_SIZE)
1292struct memcg_stock_pcp {
1293 struct mem_cgroup *cached; /* this never be root cgroup */
1294 int charge;
1295 struct work_struct work;
1296};
1297static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1298static atomic_t memcg_drain_count;
1299
1300/*
1301 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1302 * from local stock and true is returned. If the stock is 0 or charges from a
1303 * cgroup which is not current target, returns false. This stock will be
1304 * refilled.
1305 */
1306static bool consume_stock(struct mem_cgroup *mem)
1307{
1308 struct memcg_stock_pcp *stock;
1309 bool ret = true;
1310
1311 stock = &get_cpu_var(memcg_stock);
1312 if (mem == stock->cached && stock->charge)
1313 stock->charge -= PAGE_SIZE;
1314 else /* need to call res_counter_charge */
1315 ret = false;
1316 put_cpu_var(memcg_stock);
1317 return ret;
1318}
1319
1320/*
1321 * Returns stocks cached in percpu to res_counter and reset cached information.
1322 */
1323static void drain_stock(struct memcg_stock_pcp *stock)
1324{
1325 struct mem_cgroup *old = stock->cached;
1326
1327 if (stock->charge) {
1328 res_counter_uncharge(&old->res, stock->charge);
1329 if (do_swap_account)
1330 res_counter_uncharge(&old->memsw, stock->charge);
1331 }
1332 stock->cached = NULL;
1333 stock->charge = 0;
1334}
1335
1336/*
1337 * This must be called under preempt disabled or must be called by
1338 * a thread which is pinned to local cpu.
1339 */
1340static void drain_local_stock(struct work_struct *dummy)
1341{
1342 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1343 drain_stock(stock);
1344}
1345
1346/*
1347 * Cache charges(val) which is from res_counter, to local per_cpu area.
1348 * This will be consumed by consumt_stock() function, later.
1349 */
1350static void refill_stock(struct mem_cgroup *mem, int val)
1351{
1352 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1353
1354 if (stock->cached != mem) { /* reset if necessary */
1355 drain_stock(stock);
1356 stock->cached = mem;
1357 }
1358 stock->charge += val;
1359 put_cpu_var(memcg_stock);
1360}
1361
1362/*
1363 * Tries to drain stocked charges in other cpus. This function is asynchronous
1364 * and just put a work per cpu for draining localy on each cpu. Caller can
1365 * expects some charges will be back to res_counter later but cannot wait for
1366 * it.
1367 */
1368static void drain_all_stock_async(void)
1369{
1370 int cpu;
1371 /* This function is for scheduling "drain" in asynchronous way.
1372 * The result of "drain" is not directly handled by callers. Then,
1373 * if someone is calling drain, we don't have to call drain more.
1374 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1375 * there is a race. We just do loose check here.
1376 */
1377 if (atomic_read(&memcg_drain_count))
1378 return;
1379 /* Notify other cpus that system-wide "drain" is running */
1380 atomic_inc(&memcg_drain_count);
1381 get_online_cpus();
1382 for_each_online_cpu(cpu) {
1383 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1384 schedule_work_on(cpu, &stock->work);
1385 }
1386 put_online_cpus();
1387 atomic_dec(&memcg_drain_count);
1388 /* We don't wait for flush_work */
1389}
1390
1391/* This is a synchronous drain interface. */
1392static void drain_all_stock_sync(void)
1393{
1394 /* called when force_empty is called */
1395 atomic_inc(&memcg_drain_count);
1396 schedule_on_each_cpu(drain_local_stock);
1397 atomic_dec(&memcg_drain_count);
1398}
1399
1400static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1401 unsigned long action,
1402 void *hcpu)
1403{
1404 int cpu = (unsigned long)hcpu;
1405 struct memcg_stock_pcp *stock;
1406
1407 if (action != CPU_DEAD)
1408 return NOTIFY_OK;
1409 stock = &per_cpu(memcg_stock, cpu);
1410 drain_stock(stock);
1411 return NOTIFY_OK;
1412}
1413
1414/*
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001415 * Unlike exported interface, "oom" parameter is added. if oom==true,
1416 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001417 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001418static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001419 gfp_t gfp_mask, struct mem_cgroup **memcg,
Balbir Singhf64c3f52009-09-23 15:56:37 -07001420 bool oom, struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001421{
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001422 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001423 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001424 struct res_counter *fail_res;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001425 int csize = CHARGE_SIZE;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001426
1427 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1428 /* Don't account this! */
1429 *memcg = NULL;
1430 return 0;
1431 }
1432
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001433 /*
Hugh Dickins3be912772008-02-07 00:14:19 -08001434 * We always charge the cgroup the mm_struct belongs to.
1435 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001436 * thread group leader migrates. It's possible that mm is not
1437 * set, if so charge the init_mm (happens for pagecache usage).
1438 */
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001439 mem = *memcg;
1440 if (likely(!mem)) {
1441 mem = try_get_mem_cgroup_from_mm(mm);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001442 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001443 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001444 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001445 }
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001446 if (unlikely(!mem))
1447 return 0;
1448
Nikanth Karthikesan46f7e602009-05-28 14:34:41 -07001449 VM_BUG_ON(css_is_removed(&mem->css));
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001450 if (mem_cgroup_is_root(mem))
1451 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001452
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001453 while (1) {
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001454 int ret = 0;
Balbir Singh75822b42009-09-23 15:56:38 -07001455 unsigned long flags = 0;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001456
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001457 if (consume_stock(mem))
1458 goto charged;
1459
1460 ret = res_counter_charge(&mem->res, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001461 if (likely(!ret)) {
1462 if (!do_swap_account)
1463 break;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001464 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001465 if (likely(!ret))
1466 break;
1467 /* mem+swap counter fails */
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001468 res_counter_uncharge(&mem->res, csize);
Balbir Singh75822b42009-09-23 15:56:38 -07001469 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
Balbir Singh6d61ef42009-01-07 18:08:06 -08001470 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1471 memsw);
1472 } else
1473 /* mem counter fails */
1474 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1475 res);
1476
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001477 /* reduce request size and retry */
1478 if (csize > PAGE_SIZE) {
1479 csize = PAGE_SIZE;
1480 continue;
1481 }
Hugh Dickins3be912772008-02-07 00:14:19 -08001482 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001483 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001484
Balbir Singh4e416952009-09-23 15:56:39 -07001485 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1486 gfp_mask, flags);
Daisuke Nishimura4d1c6272009-01-15 13:51:14 -08001487 if (ret)
1488 continue;
Balbir Singh66e17072008-02-07 00:13:56 -08001489
1490 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001491 * try_to_free_mem_cgroup_pages() might not give us a full
1492 * picture of reclaim. Some pages are reclaimed and might be
1493 * moved to swap cache or just unmapped from the cgroup.
1494 * Check the limit again to see if the reclaim reduced the
1495 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001496 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001497 */
Daisuke Nishimurab85a96c2009-01-07 18:08:12 -08001498 if (mem_cgroup_check_under_limit(mem_over_limit))
1499 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -08001500
1501 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001502 if (oom) {
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -08001503 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
KAMEZAWA Hiroyuki0b7f5692009-04-02 16:57:38 -07001504 record_last_oom(mem_over_limit);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001505 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001506 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -08001507 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001508 }
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08001509 if (csize > PAGE_SIZE)
1510 refill_stock(mem, csize - PAGE_SIZE);
1511charged:
Balbir Singhf64c3f52009-09-23 15:56:37 -07001512 /*
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001513 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1514 * if they exceeds softlimit.
Balbir Singhf64c3f52009-09-23 15:56:37 -07001515 */
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001516 if (mem_cgroup_soft_limit_check(mem))
1517 mem_cgroup_update_tree(mem, page);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001518done:
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001519 return 0;
1520nomem:
1521 css_put(&mem->css);
1522 return -ENOMEM;
1523}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001524
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001525/*
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001526 * Somemtimes we have to undo a charge we got by try_charge().
1527 * This function is for that and do uncharge, put css's refcnt.
1528 * gotten by try_charge().
1529 */
1530static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1531{
1532 if (!mem_cgroup_is_root(mem)) {
1533 res_counter_uncharge(&mem->res, PAGE_SIZE);
1534 if (do_swap_account)
1535 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1536 }
1537 css_put(&mem->css);
1538}
1539
1540/*
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001541 * A helper function to get mem_cgroup from ID. must be called under
1542 * rcu_read_lock(). The caller must check css_is_removed() or some if
1543 * it's concern. (dropping refcnt from swap can be called against removed
1544 * memcg.)
1545 */
1546static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1547{
1548 struct cgroup_subsys_state *css;
1549
1550 /* ID 0 is unused ID */
1551 if (!id)
1552 return NULL;
1553 css = css_lookup(&mem_cgroup_subsys, id);
1554 if (!css)
1555 return NULL;
1556 return container_of(css, struct mem_cgroup, css);
1557}
1558
Wu Fengguange42d9d52009-12-16 12:19:59 +01001559struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001560{
Wu Fengguange42d9d52009-12-16 12:19:59 +01001561 struct mem_cgroup *mem = NULL;
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001562 struct page_cgroup *pc;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001563 unsigned short id;
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001564 swp_entry_t ent;
1565
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001566 VM_BUG_ON(!PageLocked(page));
1567
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001568 pc = lookup_page_cgroup(page);
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001569 lock_page_cgroup(pc);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001570 if (PageCgroupUsed(pc)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001571 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001572 if (mem && !css_tryget(&mem->css))
1573 mem = NULL;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001574 } else if (PageSwapCache(page)) {
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001575 ent.val = page_private(page);
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001576 id = lookup_swap_cgroup(ent);
1577 rcu_read_lock();
1578 mem = mem_cgroup_lookup(id);
1579 if (mem && !css_tryget(&mem->css))
1580 mem = NULL;
1581 rcu_read_unlock();
Daisuke Nishimura3c776e62009-04-02 16:57:43 -07001582 }
Daisuke Nishimurac0bd3f62009-04-30 15:08:11 -07001583 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001584 return mem;
1585}
1586
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001587/*
Daisuke Nishimuraa5e924f2009-01-07 18:08:28 -08001588 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001589 * USED state. If already USED, uncharge and return.
1590 */
1591
1592static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1593 struct page_cgroup *pc,
1594 enum charge_type ctype)
1595{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001596 /* try_charge() can return NULL to *memcg, taking care of it. */
1597 if (!mem)
1598 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001599
1600 lock_page_cgroup(pc);
1601 if (unlikely(PageCgroupUsed(pc))) {
1602 unlock_page_cgroup(pc);
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001603 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001604 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001605 }
Balbir Singh4b3bde42009-09-23 15:56:32 -07001606
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001607 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki261fb612009-09-23 15:56:33 -07001608 /*
1609 * We access a page_cgroup asynchronously without lock_page_cgroup().
1610 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1611 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1612 * before USED bit, we need memory barrier here.
1613 * See mem_cgroup_add_lru_list(), etc.
1614 */
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001615 smp_wmb();
Balbir Singh4b3bde42009-09-23 15:56:32 -07001616 switch (ctype) {
1617 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1618 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1619 SetPageCgroupCache(pc);
1620 SetPageCgroupUsed(pc);
1621 break;
1622 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1623 ClearPageCgroupCache(pc);
1624 SetPageCgroupUsed(pc);
1625 break;
1626 default:
1627 break;
1628 }
Hugh Dickins3be912772008-02-07 00:14:19 -08001629
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001630 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001631
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001632 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001633}
1634
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001635/**
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001636 * __mem_cgroup_move_account - move account of the page
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001637 * @pc: page_cgroup of the page.
1638 * @from: mem_cgroup which the page is moved from.
1639 * @to: mem_cgroup which the page is moved to. @from != @to.
1640 *
1641 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001642 * - page is not on LRU (isolate_page() is useful.)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001643 * - the pc is locked, used, and ->mem_cgroup points to @from.
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001644 *
1645 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1646 * new cgroup. It should be done by a caller.
1647 */
1648
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001649static void __mem_cgroup_move_account(struct page_cgroup *pc,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001650 struct mem_cgroup *from, struct mem_cgroup *to)
1651{
Balbir Singhd69b0422009-06-17 16:26:34 -07001652 struct page *page;
1653 int cpu;
1654 struct mem_cgroup_stat *stat;
1655 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001656
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001657 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001658 VM_BUG_ON(PageLRU(pc->page));
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001659 VM_BUG_ON(!PageCgroupLocked(pc));
1660 VM_BUG_ON(!PageCgroupUsed(pc));
1661 VM_BUG_ON(pc->mem_cgroup != from);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001662
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001663 if (!mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001664 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001665 mem_cgroup_charge_statistics(from, pc, false);
Balbir Singhd69b0422009-06-17 16:26:34 -07001666
1667 page = pc->page;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001668 if (page_mapped(page) && !PageAnon(page)) {
Balbir Singhd69b0422009-06-17 16:26:34 -07001669 cpu = smp_processor_id();
1670 /* Update mapped_file data for mem_cgroup "from" */
1671 stat = &from->stat;
1672 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001673 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001674 -1);
1675
1676 /* Update mapped_file data for mem_cgroup "to" */
1677 stat = &to->stat;
1678 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08001679 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
Balbir Singhd69b0422009-06-17 16:26:34 -07001680 1);
1681 }
1682
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001683 if (do_swap_account && !mem_cgroup_is_root(from))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001684 res_counter_uncharge(&from->memsw, PAGE_SIZE);
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001685 css_put(&from->css);
1686
1687 css_get(&to->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001688 pc->mem_cgroup = to;
1689 mem_cgroup_charge_statistics(to, pc, true);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001690 /*
1691 * We charges against "to" which may not have any tasks. Then, "to"
1692 * can be under rmdir(). But in current implementation, caller of
1693 * this function is just force_empty() and it's garanteed that
1694 * "to" is never removed. So, we don't check rmdir status here.
1695 */
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001696}
1697
1698/*
1699 * check whether the @pc is valid for moving account and call
1700 * __mem_cgroup_move_account()
1701 */
1702static int mem_cgroup_move_account(struct page_cgroup *pc,
1703 struct mem_cgroup *from, struct mem_cgroup *to)
1704{
1705 int ret = -EINVAL;
1706 lock_page_cgroup(pc);
1707 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
1708 __mem_cgroup_move_account(pc, from, to);
1709 ret = 0;
1710 }
1711 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001712 return ret;
1713}
1714
1715/*
1716 * move charges to its parent.
1717 */
1718
1719static int mem_cgroup_move_parent(struct page_cgroup *pc,
1720 struct mem_cgroup *child,
1721 gfp_t gfp_mask)
1722{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001723 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001724 struct cgroup *cg = child->css.cgroup;
1725 struct cgroup *pcg = cg->parent;
1726 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001727 int ret;
1728
1729 /* Is ROOT ? */
1730 if (!pcg)
1731 return -EINVAL;
1732
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001733 ret = -EBUSY;
1734 if (!get_page_unless_zero(page))
1735 goto out;
1736 if (isolate_lru_page(page))
1737 goto put;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001738
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001739 parent = mem_cgroup_from_cont(pcg);
Balbir Singhf64c3f52009-09-23 15:56:37 -07001740 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001741 if (ret || !parent)
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001742 goto put_back;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001743
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001744 ret = mem_cgroup_move_account(pc, child, parent);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001745 if (!ret)
1746 css_put(&parent->css); /* drop extra refcnt by try_charge() */
1747 else
1748 mem_cgroup_cancel_charge(parent); /* does css_put */
1749put_back:
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001750 putback_lru_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001751put:
Daisuke Nishimura40d58132009-01-15 13:51:12 -08001752 put_page(page);
Daisuke Nishimura57f9fd7d2009-12-15 16:47:11 -08001753out:
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001754 return ret;
1755}
1756
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001757/*
1758 * Charge the memory controller for page usage.
1759 * Return
1760 * 0 if the charge was successful
1761 * < 0 if the cgroup is over its limit
1762 */
1763static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1764 gfp_t gfp_mask, enum charge_type ctype,
1765 struct mem_cgroup *memcg)
1766{
1767 struct mem_cgroup *mem;
1768 struct page_cgroup *pc;
1769 int ret;
1770
1771 pc = lookup_page_cgroup(page);
1772 /* can happen at boot */
1773 if (unlikely(!pc))
1774 return 0;
1775 prefetchw(pc);
1776
1777 mem = memcg;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001778 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -08001779 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001780 return ret;
1781
1782 __mem_cgroup_commit_charge(mem, pc, ctype);
1783 return 0;
1784}
1785
1786int mem_cgroup_newpage_charge(struct page *page,
1787 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001788{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001789 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001790 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001791 if (PageCompound(page))
1792 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001793 /*
1794 * If already mapped, we don't have to account.
1795 * If page cache, page->mapping has address_space.
1796 * But page->mapping may have out-of-use anon_vma pointer,
1797 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1798 * is NULL.
1799 */
1800 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1801 return 0;
1802 if (unlikely(!mm))
1803 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001804 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001805 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -08001806}
1807
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001808static void
1809__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1810 enum charge_type ctype);
1811
Balbir Singhe1a1cd52008-02-07 00:14:02 -08001812int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1813 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -08001814{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001815 struct mem_cgroup *mem = NULL;
1816 int ret;
1817
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001818 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001819 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001820 if (PageCompound(page))
1821 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001822 /*
1823 * Corner case handling. This is called from add_to_page_cache()
1824 * in usual. But some FS (shmem) precharges this page before calling it
1825 * and call add_to_page_cache() with GFP_NOWAIT.
1826 *
1827 * For GFP_NOWAIT case, the page may be pre-charged before calling
1828 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1829 * charge twice. (It works but has to pay a bit larger cost.)
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001830 * And when the page is SwapCache, it should take swap information
1831 * into account. This is under lock_page() now.
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001832 */
1833 if (!(gfp_mask & __GFP_WAIT)) {
1834 struct page_cgroup *pc;
1835
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001836
1837 pc = lookup_page_cgroup(page);
1838 if (!pc)
1839 return 0;
1840 lock_page_cgroup(pc);
1841 if (PageCgroupUsed(pc)) {
1842 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001843 return 0;
1844 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001845 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001846 }
1847
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001848 if (unlikely(!mm && !mem))
Balbir Singh8697d332008-02-07 00:13:59 -08001849 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -07001850
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -07001851 if (page_is_file_cache(page))
1852 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001853 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001854
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001855 /* shmem */
1856 if (PageSwapCache(page)) {
1857 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1858 if (!ret)
1859 __mem_cgroup_commit_charge_swapin(page, mem,
1860 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1861 } else
1862 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1863 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001864
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08001865 return ret;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001866}
1867
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001868/*
1869 * While swap-in, try_charge -> commit or cancel, the page is locked.
1870 * And when try_charge() successfully returns, one refcnt to memcg without
Uwe Kleine-König21ae2952009-10-07 15:21:09 +02001871 * struct page_cgroup is acquired. This refcnt will be consumed by
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001872 * "commit()" or removed by "cancel()"
1873 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001874int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1875 struct page *page,
1876 gfp_t mask, struct mem_cgroup **ptr)
1877{
1878 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001879 int ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001880
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001881 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001882 return 0;
1883
1884 if (!do_swap_account)
1885 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001886 /*
1887 * A racing thread's fault, or swapoff, may have already updated
Hugh Dickins407f9c82009-12-14 17:59:30 -08001888 * the pte, and even removed page from swap cache: in those cases
1889 * do_swap_page()'s pte_same() test will fail; but there's also a
1890 * KSM case which does need to charge the page.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001891 */
1892 if (!PageSwapCache(page))
Hugh Dickins407f9c82009-12-14 17:59:30 -08001893 goto charge_cur_mm;
Wu Fengguange42d9d52009-12-16 12:19:59 +01001894 mem = try_get_mem_cgroup_from_page(page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001895 if (!mem)
1896 goto charge_cur_mm;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001897 *ptr = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001898 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
KAMEZAWA Hiroyuki54595fe2009-01-07 18:08:33 -08001899 /* drop extra refcnt from tryget */
1900 css_put(&mem->css);
1901 return ret;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001902charge_cur_mm:
1903 if (unlikely(!mm))
1904 mm = &init_mm;
Balbir Singhf64c3f52009-09-23 15:56:37 -07001905 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001906}
1907
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001908static void
1909__mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1910 enum charge_type ctype)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001911{
1912 struct page_cgroup *pc;
1913
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001914 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001915 return;
1916 if (!ptr)
1917 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001918 cgroup_exclude_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001919 pc = lookup_page_cgroup(page);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001920 mem_cgroup_lru_del_before_commit_swapcache(page);
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001921 __mem_cgroup_commit_charge(ptr, pc, ctype);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08001922 mem_cgroup_lru_add_after_commit_swapcache(page);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001923 /*
1924 * Now swap is on-memory. This means this page may be
1925 * counted both as mem and swap....double count.
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001926 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1927 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1928 * may call delete_from_swap_cache() before reach here.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001929 */
KAMEZAWA Hiroyuki03f3c432009-01-07 18:08:31 -08001930 if (do_swap_account && PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001931 swp_entry_t ent = {.val = page_private(page)};
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001932 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001933 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001934
1935 id = swap_cgroup_record(ent, 0);
1936 rcu_read_lock();
1937 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001938 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001939 /*
1940 * This recorded memcg can be obsolete one. So, avoid
1941 * calling css_tryget
1942 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001943 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07001944 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07001945 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001946 mem_cgroup_put(memcg);
1947 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07001948 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001949 }
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07001950 /*
1951 * At swapin, we may charge account against cgroup which has no tasks.
1952 * So, rmdir()->pre_destroy() can be called while we do this charge.
1953 * In that case, we need to call pre_destroy() again. check it here.
1954 */
1955 cgroup_release_and_wakeup_rmdir(&ptr->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001956}
1957
Daisuke Nishimura83aae4c2009-04-02 16:57:48 -07001958void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1959{
1960 __mem_cgroup_commit_charge_swapin(page, ptr,
1961 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1962}
1963
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001964void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1965{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001966 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001967 return;
1968 if (!mem)
1969 return;
Daisuke Nishimuraa3032a22009-12-15 16:47:10 -08001970 mem_cgroup_cancel_charge(mem);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001971}
1972
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08001973static void
1974__do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
1975{
1976 struct memcg_batch_info *batch = NULL;
1977 bool uncharge_memsw = true;
1978 /* If swapout, usage of swap doesn't decrease */
1979 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1980 uncharge_memsw = false;
1981 /*
1982 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
1983 * In those cases, all pages freed continously can be expected to be in
1984 * the same cgroup and we have chance to coalesce uncharges.
1985 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
1986 * because we want to do uncharge as soon as possible.
1987 */
1988 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
1989 goto direct_uncharge;
1990
1991 batch = &current->memcg_batch;
1992 /*
1993 * In usual, we do css_get() when we remember memcg pointer.
1994 * But in this case, we keep res->usage until end of a series of
1995 * uncharges. Then, it's ok to ignore memcg's refcnt.
1996 */
1997 if (!batch->memcg)
1998 batch->memcg = mem;
1999 /*
2000 * In typical case, batch->memcg == mem. This means we can
2001 * merge a series of uncharges to an uncharge of res_counter.
2002 * If not, we uncharge res_counter ony by one.
2003 */
2004 if (batch->memcg != mem)
2005 goto direct_uncharge;
2006 /* remember freed charge and uncharge it later */
2007 batch->bytes += PAGE_SIZE;
2008 if (uncharge_memsw)
2009 batch->memsw_bytes += PAGE_SIZE;
2010 return;
2011direct_uncharge:
2012 res_counter_uncharge(&mem->res, PAGE_SIZE);
2013 if (uncharge_memsw)
2014 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2015 return;
2016}
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08002017
Balbir Singh8697d332008-02-07 00:13:59 -08002018/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002019 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002020 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002021static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002022__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002023{
Hugh Dickins82895462008-03-04 14:29:08 -08002024 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002025 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002026 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002027
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002028 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002029 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07002030
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002031 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002032 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002033
Balbir Singh8697d332008-02-07 00:13:59 -08002034 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08002035 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08002036 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002037 pc = lookup_page_cgroup(page);
2038 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002039 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08002040
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002041 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002042
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002043 mem = pc->mem_cgroup;
2044
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002045 if (!PageCgroupUsed(pc))
2046 goto unlock_out;
2047
2048 switch (ctype) {
2049 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002050 case MEM_CGROUP_CHARGE_TYPE_DROP:
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002051 if (page_mapped(page))
2052 goto unlock_out;
2053 break;
2054 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2055 if (!PageAnon(page)) { /* Shared memory */
2056 if (page->mapping && !page_is_file_cache(page))
2057 goto unlock_out;
2058 } else if (page_mapped(page)) /* Anon */
2059 goto unlock_out;
2060 break;
2061 default:
2062 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002063 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002064
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002065 if (!mem_cgroup_is_root(mem))
2066 __do_uncharge(mem, ctype);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002067 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2068 mem_cgroup_swap_statistics(mem, true);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002069 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07002070
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002071 ClearPageCgroupUsed(pc);
KAMEZAWA Hiroyuki544122e2009-01-07 18:08:34 -08002072 /*
2073 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2074 * freed from LRU. This is safe because uncharged page is expected not
2075 * to be reused (freed soon). Exception is SwapCache, it's handled by
2076 * special functions.
2077 */
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002078
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002079 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002080 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002081
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002082 if (mem_cgroup_soft_limit_check(mem))
Balbir Singhf64c3f52009-09-23 15:56:37 -07002083 mem_cgroup_update_tree(mem, page);
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002084 /* at swapout, this memcg will be accessed to record to swap */
2085 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2086 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002087
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002088 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002089
2090unlock_out:
2091 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002092 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08002093}
2094
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002095void mem_cgroup_uncharge_page(struct page *page)
2096{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002097 /* early check. */
2098 if (page_mapped(page))
2099 return;
2100 if (page->mapping && !PageAnon(page))
2101 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002102 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2103}
2104
2105void mem_cgroup_uncharge_cache_page(struct page *page)
2106{
2107 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07002108 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002109 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2110}
2111
KAMEZAWA Hiroyuki569b8462009-12-15 16:47:03 -08002112/*
2113 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2114 * In that cases, pages are freed continuously and we can expect pages
2115 * are in the same memcg. All these calls itself limits the number of
2116 * pages freed at once, then uncharge_start/end() is called properly.
2117 * This may be called prural(2) times in a context,
2118 */
2119
2120void mem_cgroup_uncharge_start(void)
2121{
2122 current->memcg_batch.do_batch++;
2123 /* We can do nest. */
2124 if (current->memcg_batch.do_batch == 1) {
2125 current->memcg_batch.memcg = NULL;
2126 current->memcg_batch.bytes = 0;
2127 current->memcg_batch.memsw_bytes = 0;
2128 }
2129}
2130
2131void mem_cgroup_uncharge_end(void)
2132{
2133 struct memcg_batch_info *batch = &current->memcg_batch;
2134
2135 if (!batch->do_batch)
2136 return;
2137
2138 batch->do_batch--;
2139 if (batch->do_batch) /* If stacked, do nothing. */
2140 return;
2141
2142 if (!batch->memcg)
2143 return;
2144 /*
2145 * This "batch->memcg" is valid without any css_get/put etc...
2146 * bacause we hide charges behind us.
2147 */
2148 if (batch->bytes)
2149 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2150 if (batch->memsw_bytes)
2151 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2152 /* forget this pointer (for sanity check) */
2153 batch->memcg = NULL;
2154}
2155
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002156#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002157/*
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002158 * called after __delete_from_swap_cache() and drop "page" account.
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002159 * memcg information is recorded to swap_cgroup of "ent"
2160 */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002161void
2162mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002163{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002164 struct mem_cgroup *memcg;
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002165 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002166
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002167 if (!swapout) /* this was a swap cache but the swap is unused ! */
2168 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2169
2170 memcg = __mem_cgroup_uncharge_common(page, ctype);
2171
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002172 /* record memcg information */
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002173 if (do_swap_account && swapout && memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002174 swap_cgroup_record(ent, css_id(&memcg->css));
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002175 mem_cgroup_get(memcg);
2176 }
KAMEZAWA Hiroyuki8a9478c2009-06-17 16:27:17 -07002177 if (swapout && memcg)
KAMEZAWA Hiroyukia7fe9422009-01-07 18:08:13 -08002178 css_put(&memcg->css);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002179}
Daisuke Nishimurae767e052009-05-28 14:34:28 -07002180#endif
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002181
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002182#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2183/*
2184 * called from swap_entry_free(). remove record in swap_cgroup and
2185 * uncharge "memsw" account.
2186 */
2187void mem_cgroup_uncharge_swap(swp_entry_t ent)
2188{
2189 struct mem_cgroup *memcg;
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002190 unsigned short id;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002191
2192 if (!do_swap_account)
2193 return;
2194
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002195 id = swap_cgroup_record(ent, 0);
2196 rcu_read_lock();
2197 memcg = mem_cgroup_lookup(id);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002198 if (memcg) {
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002199 /*
2200 * We uncharge this because swap is freed.
2201 * This memcg can be obsolete one. We avoid calling css_tryget
2202 */
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002203 if (!mem_cgroup_is_root(memcg))
KAMEZAWA Hiroyuki4e649152009-10-01 15:44:11 -07002204 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002205 mem_cgroup_swap_statistics(memcg, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002206 mem_cgroup_put(memcg);
2207 }
KAMEZAWA Hiroyukia3b2d692009-04-02 16:57:45 -07002208 rcu_read_unlock();
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002209}
2210#endif
2211
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002212/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002213 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2214 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002215 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002216int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002217{
2218 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002219 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002220 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002221
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002222 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07002223 return 0;
2224
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002225 pc = lookup_page_cgroup(page);
2226 lock_page_cgroup(pc);
2227 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002228 mem = pc->mem_cgroup;
2229 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08002230 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002231 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002232
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002233 if (mem) {
Balbir Singhf64c3f52009-09-23 15:56:37 -07002234 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2235 page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002236 css_put(&mem->css);
2237 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002238 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002239 return ret;
2240}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08002241
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002242/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002243void mem_cgroup_end_migration(struct mem_cgroup *mem,
2244 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07002245{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002246 struct page *target, *unused;
2247 struct page_cgroup *pc;
2248 enum charge_type ctype;
2249
2250 if (!mem)
2251 return;
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002252 cgroup_exclude_rmdir(&mem->css);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002253 /* at migration success, oldpage->mapping is NULL. */
2254 if (oldpage->mapping) {
2255 target = oldpage;
2256 unused = NULL;
2257 } else {
2258 target = newpage;
2259 unused = oldpage;
2260 }
2261
2262 if (PageAnon(target))
2263 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2264 else if (page_is_file_cache(target))
2265 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2266 else
2267 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2268
2269 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08002270 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002271 __mem_cgroup_uncharge_common(unused, ctype);
2272
2273 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002274 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002275 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2276 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07002277 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08002278 __mem_cgroup_commit_charge(mem, pc, ctype);
2279
2280 /*
2281 * Both of oldpage and newpage are still under lock_page().
2282 * Then, we don't have to care about race in radix-tree.
2283 * But we have to be careful that this page is unmapped or not.
2284 *
2285 * There is a case for !page_mapped(). At the start of
2286 * migration, oldpage was mapped. But now, it's zapped.
2287 * But we know *target* page is not freed/reused under us.
2288 * mem_cgroup_uncharge_page() does all necessary checks.
2289 */
2290 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2291 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyuki88703262009-07-29 15:04:06 -07002292 /*
2293 * At migration, we may charge account against cgroup which has no tasks
2294 * So, rmdir()->pre_destroy() can be called while we do this charge.
2295 * In that case, we need to call pre_destroy() again. check it here.
2296 */
2297 cgroup_release_and_wakeup_rmdir(&mem->css);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08002298}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08002299
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002300/*
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002301 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2302 * Calling hierarchical_reclaim is not enough because we should update
2303 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2304 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2305 * not from the memcg which this page would be charged to.
2306 * try_charge_swapin does all of these works properly.
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002307 */
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002308int mem_cgroup_shmem_charge_fallback(struct page *page,
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002309 struct mm_struct *mm,
2310 gfp_t gfp_mask)
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002311{
KAMEZAWA Hiroyukib5a84312009-01-07 18:08:35 -08002312 struct mem_cgroup *mem = NULL;
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002313 int ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002314
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08002315 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07002316 return 0;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002317
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002318 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2319 if (!ret)
2320 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002321
Daisuke Nishimuraae3abae2009-04-30 15:08:19 -07002322 return ret;
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002323}
2324
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002325static DEFINE_MUTEX(set_limit_mutex);
2326
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08002327static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002328 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002329{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002330 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002331 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002332 int ret = 0;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002333 int children = mem_cgroup_count_children(memcg);
2334 u64 curusage, oldusage;
2335
2336 /*
2337 * For keeping hierarchical_reclaim simple, how long we should retry
2338 * is depends on callers. We set our retry-count to be function
2339 * of # of children which we should visit in this loop.
2340 */
2341 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2342
2343 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002344
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002345 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002346 if (signal_pending(current)) {
2347 ret = -EINTR;
2348 break;
2349 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002350 /*
2351 * Rather than hide all in some function, I do this in
2352 * open coded manner. You see what this really does.
2353 * We have to guarantee mem->res.limit < mem->memsw.limit.
2354 */
2355 mutex_lock(&set_limit_mutex);
2356 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2357 if (memswlimit < val) {
2358 ret = -EINVAL;
2359 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002360 break;
2361 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002362 ret = res_counter_set_limit(&memcg->res, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002363 if (!ret) {
2364 if (memswlimit == val)
2365 memcg->memsw_is_minimum = true;
2366 else
2367 memcg->memsw_is_minimum = false;
2368 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002369 mutex_unlock(&set_limit_mutex);
2370
2371 if (!ret)
2372 break;
2373
Bob Liuaa20d482009-12-15 16:47:14 -08002374 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh4e416952009-09-23 15:56:39 -07002375 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002376 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2377 /* Usage is reduced ? */
2378 if (curusage >= oldusage)
2379 retry_count--;
2380 else
2381 oldusage = curusage;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002382 }
KOSAKI Motohiro14797e22009-01-07 18:08:18 -08002383
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002384 return ret;
2385}
2386
Li Zefan338c8432009-06-17 16:27:15 -07002387static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2388 unsigned long long val)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002389{
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002390 int retry_count;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002391 u64 memlimit, oldusage, curusage;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002392 int children = mem_cgroup_count_children(memcg);
2393 int ret = -EBUSY;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002394
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002395 /* see mem_cgroup_resize_res_limit */
2396 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2397 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002398 while (retry_count) {
2399 if (signal_pending(current)) {
2400 ret = -EINTR;
2401 break;
2402 }
2403 /*
2404 * Rather than hide all in some function, I do this in
2405 * open coded manner. You see what this really does.
2406 * We have to guarantee mem->res.limit < mem->memsw.limit.
2407 */
2408 mutex_lock(&set_limit_mutex);
2409 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2410 if (memlimit > val) {
2411 ret = -EINVAL;
2412 mutex_unlock(&set_limit_mutex);
2413 break;
2414 }
2415 ret = res_counter_set_limit(&memcg->memsw, val);
KAMEZAWA Hiroyuki22a668d2009-06-17 16:27:19 -07002416 if (!ret) {
2417 if (memlimit == val)
2418 memcg->memsw_is_minimum = true;
2419 else
2420 memcg->memsw_is_minimum = false;
2421 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002422 mutex_unlock(&set_limit_mutex);
2423
2424 if (!ret)
2425 break;
2426
Balbir Singh4e416952009-09-23 15:56:39 -07002427 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
Balbir Singh75822b42009-09-23 15:56:38 -07002428 MEM_CGROUP_RECLAIM_NOSWAP |
2429 MEM_CGROUP_RECLAIM_SHRINK);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002430 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002431 /* Usage is reduced ? */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002432 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002433 retry_count--;
KAMEZAWA Hiroyuki81d39c22009-04-02 16:57:36 -07002434 else
2435 oldusage = curusage;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002436 }
2437 return ret;
2438}
2439
Balbir Singh4e416952009-09-23 15:56:39 -07002440unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2441 gfp_t gfp_mask, int nid,
2442 int zid)
2443{
2444 unsigned long nr_reclaimed = 0;
2445 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2446 unsigned long reclaimed;
2447 int loop = 0;
2448 struct mem_cgroup_tree_per_zone *mctz;
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002449 unsigned long long excess;
Balbir Singh4e416952009-09-23 15:56:39 -07002450
2451 if (order > 0)
2452 return 0;
2453
2454 mctz = soft_limit_tree_node_zone(nid, zid);
2455 /*
2456 * This loop can run a while, specially if mem_cgroup's continuously
2457 * keep exceeding their soft limit and putting the system under
2458 * pressure
2459 */
2460 do {
2461 if (next_mz)
2462 mz = next_mz;
2463 else
2464 mz = mem_cgroup_largest_soft_limit_node(mctz);
2465 if (!mz)
2466 break;
2467
2468 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2469 gfp_mask,
2470 MEM_CGROUP_RECLAIM_SOFT);
2471 nr_reclaimed += reclaimed;
2472 spin_lock(&mctz->lock);
2473
2474 /*
2475 * If we failed to reclaim anything from this memory cgroup
2476 * it is time to move on to the next cgroup
2477 */
2478 next_mz = NULL;
2479 if (!reclaimed) {
2480 do {
2481 /*
2482 * Loop until we find yet another one.
2483 *
2484 * By the time we get the soft_limit lock
2485 * again, someone might have aded the
2486 * group back on the RB tree. Iterate to
2487 * make sure we get a different mem.
2488 * mem_cgroup_largest_soft_limit_node returns
2489 * NULL if no other cgroup is present on
2490 * the tree
2491 */
2492 next_mz =
2493 __mem_cgroup_largest_soft_limit_node(mctz);
2494 if (next_mz == mz) {
2495 css_put(&next_mz->mem->css);
2496 next_mz = NULL;
2497 } else /* next_mz == NULL or other memcg */
2498 break;
2499 } while (1);
2500 }
Balbir Singh4e416952009-09-23 15:56:39 -07002501 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002502 excess = res_counter_soft_limit_excess(&mz->mem->res);
Balbir Singh4e416952009-09-23 15:56:39 -07002503 /*
2504 * One school of thought says that we should not add
2505 * back the node to the tree if reclaim returns 0.
2506 * But our reclaim could return 0, simply because due
2507 * to priority we are exposing a smaller subset of
2508 * memory to reclaim from. Consider this as a longer
2509 * term TODO.
2510 */
KAMEZAWA Hiroyukief8745c2009-10-01 15:44:12 -07002511 /* If excess == 0, no tree ops */
2512 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
Balbir Singh4e416952009-09-23 15:56:39 -07002513 spin_unlock(&mctz->lock);
2514 css_put(&mz->mem->css);
2515 loop++;
2516 /*
2517 * Could not reclaim anything and there are no more
2518 * mem cgroups to try or we seem to be looping without
2519 * reclaiming anything.
2520 */
2521 if (!nr_reclaimed &&
2522 (next_mz == NULL ||
2523 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2524 break;
2525 } while (!nr_reclaimed);
2526 if (next_mz)
2527 css_put(&next_mz->mem->css);
2528 return nr_reclaimed;
2529}
2530
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07002531/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002532 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002533 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2534 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002535static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002536 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002537{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002538 struct zone *zone;
2539 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002540 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002541 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002542 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002543 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08002544
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002545 zone = &NODE_DATA(node)->node_zones[zid];
2546 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07002547 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002548
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002549 loop = MEM_CGROUP_ZSTAT(mz, lru);
2550 /* give some margin against EBUSY etc...*/
2551 loop += 256;
2552 busy = NULL;
2553 while (loop--) {
2554 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002555 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002556 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002557 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002558 break;
2559 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002560 pc = list_entry(list->prev, struct page_cgroup, lru);
2561 if (busy == pc) {
2562 list_move(&pc->lru, list);
Thiago Farina648bcc72010-03-05 13:42:04 -08002563 busy = NULL;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002564 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002565 continue;
2566 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002567 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002568
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08002569 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002570 if (ret == -ENOMEM)
2571 break;
2572
2573 if (ret == -EBUSY || ret == -EINVAL) {
2574 /* found lock contention or "pc" is obsolete. */
2575 busy = pc;
2576 cond_resched();
2577 } else
2578 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002579 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002580
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002581 if (!ret && !list_empty(list))
2582 return -EBUSY;
2583 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002584}
2585
2586/*
2587 * make mem_cgroup's charge to be 0 if there is no task.
2588 * This enables deleting this mem_cgroup.
2589 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002590static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002591{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002592 int ret;
2593 int node, zid, shrink;
2594 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002595 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08002596
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002597 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002598
2599 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002600 /* should free all ? */
2601 if (free_all)
2602 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002603move_account:
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002604 do {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002605 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002606 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002607 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002608 ret = -EINTR;
2609 if (signal_pending(current))
2610 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002611 /* This is for making all *used* pages to be on LRU. */
2612 lru_add_drain_all();
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08002613 drain_all_stock_sync();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002614 ret = 0;
KAMEZAWA Hiroyuki299b4ea2009-01-29 14:25:17 -08002615 for_each_node_state(node, N_HIGH_MEMORY) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002616 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07002617 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002618 for_each_lru(l) {
2619 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002620 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002621 if (ret)
2622 break;
2623 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08002624 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002625 if (ret)
2626 break;
2627 }
2628 /* it seems parent cgroup doesn't have enough mem */
2629 if (ret == -ENOMEM)
2630 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07002631 cond_resched();
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002632 /* "ret" should also be checked to ensure all lists are empty. */
2633 } while (mem->res.usage > 0 || ret);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002634out:
2635 css_put(&mem->css);
2636 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002637
2638try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002639 /* returns EBUSY if there is a task or if we come here twice. */
2640 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002641 ret = -EBUSY;
2642 goto out;
2643 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002644 /* we call try-to-free pages for make this cgroup empty */
2645 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002646 /* try to free all pages in this cgroup */
2647 shrink = 1;
2648 while (nr_retries && mem->res.usage > 0) {
2649 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002650
2651 if (signal_pending(current)) {
2652 ret = -EINTR;
2653 goto out;
2654 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08002655 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2656 false, get_swappiness(mem));
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002657 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002658 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002659 /* maybe some writeback is necessary */
Jens Axboe8aa7e842009-07-09 14:52:32 +02002660 congestion_wait(BLK_RW_ASYNC, HZ/10);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002661 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002662
2663 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08002664 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08002665 /* try move_account...there may be some *locked* pages. */
Daisuke Nishimurafce66472010-01-15 17:01:30 -08002666 goto move_account;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08002667}
2668
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08002669int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2670{
2671 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2672}
2673
2674
Balbir Singh18f59ea2009-01-07 18:08:07 -08002675static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2676{
2677 return mem_cgroup_from_cont(cont)->use_hierarchy;
2678}
2679
2680static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2681 u64 val)
2682{
2683 int retval = 0;
2684 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2685 struct cgroup *parent = cont->parent;
2686 struct mem_cgroup *parent_mem = NULL;
2687
2688 if (parent)
2689 parent_mem = mem_cgroup_from_cont(parent);
2690
2691 cgroup_lock();
2692 /*
André Goddard Rosaaf901ca2009-11-14 13:09:05 -02002693 * If parent's use_hierarchy is set, we can't make any modifications
Balbir Singh18f59ea2009-01-07 18:08:07 -08002694 * in the child subtrees. If it is unset, then the change can
2695 * occur, provided the current cgroup has no children.
2696 *
2697 * For the root cgroup, parent_mem is NULL, we allow value to be
2698 * set if there are no children.
2699 */
2700 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2701 (val == 1 || val == 0)) {
2702 if (list_empty(&cont->children))
2703 mem->use_hierarchy = val;
2704 else
2705 retval = -EBUSY;
2706 } else
2707 retval = -EINVAL;
2708 cgroup_unlock();
2709
2710 return retval;
2711}
2712
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002713struct mem_cgroup_idx_data {
2714 s64 val;
2715 enum mem_cgroup_stat_index idx;
2716};
2717
2718static int
2719mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2720{
2721 struct mem_cgroup_idx_data *d = data;
2722 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2723 return 0;
2724}
2725
2726static void
2727mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2728 enum mem_cgroup_stat_index idx, s64 *val)
2729{
2730 struct mem_cgroup_idx_data d;
2731 d.idx = idx;
2732 d.val = 0;
2733 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2734 *val = d.val;
2735}
2736
Paul Menage2c3daa72008-04-29 00:59:58 -07002737static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002738{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002739 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002740 u64 idx_val, val;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002741 int type, name;
2742
2743 type = MEMFILE_TYPE(cft->private);
2744 name = MEMFILE_ATTR(cft->private);
2745 switch (type) {
2746 case _MEM:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002747 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2748 mem_cgroup_get_recursive_idx_stat(mem,
2749 MEM_CGROUP_STAT_CACHE, &idx_val);
2750 val = idx_val;
2751 mem_cgroup_get_recursive_idx_stat(mem,
2752 MEM_CGROUP_STAT_RSS, &idx_val);
2753 val += idx_val;
2754 val <<= PAGE_SHIFT;
2755 } else
2756 val = res_counter_read_u64(&mem->res, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002757 break;
2758 case _MEMSWAP:
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002759 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2760 mem_cgroup_get_recursive_idx_stat(mem,
2761 MEM_CGROUP_STAT_CACHE, &idx_val);
2762 val = idx_val;
2763 mem_cgroup_get_recursive_idx_stat(mem,
2764 MEM_CGROUP_STAT_RSS, &idx_val);
2765 val += idx_val;
2766 mem_cgroup_get_recursive_idx_stat(mem,
2767 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
Kirill A. Shutemovcd9b45b2009-12-15 16:47:01 -08002768 val += idx_val;
Balbir Singh0c3e73e2009-09-23 15:56:42 -07002769 val <<= PAGE_SHIFT;
2770 } else
2771 val = res_counter_read_u64(&mem->memsw, name);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002772 break;
2773 default:
2774 BUG();
2775 break;
2776 }
2777 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002778}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002779/*
2780 * The user of this function is...
2781 * RES_LIMIT.
2782 */
Paul Menage856c13a2008-07-25 01:47:04 -07002783static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2784 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002785{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002786 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002787 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002788 unsigned long long val;
2789 int ret;
2790
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002791 type = MEMFILE_TYPE(cft->private);
2792 name = MEMFILE_ATTR(cft->private);
2793 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002794 case RES_LIMIT:
Balbir Singh4b3bde42009-09-23 15:56:32 -07002795 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2796 ret = -EINVAL;
2797 break;
2798 }
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002799 /* This function does all necessary parse...reuse it */
2800 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002801 if (ret)
2802 break;
2803 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002804 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002805 else
2806 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002807 break;
Balbir Singh296c81d2009-09-23 15:56:36 -07002808 case RES_SOFT_LIMIT:
2809 ret = res_counter_memparse_write_strategy(buffer, &val);
2810 if (ret)
2811 break;
2812 /*
2813 * For memsw, soft limits are hard to implement in terms
2814 * of semantics, for now, we support soft limits for
2815 * control without swap
2816 */
2817 if (type == _MEM)
2818 ret = res_counter_set_soft_limit(&memcg->res, val);
2819 else
2820 ret = -EINVAL;
2821 break;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07002822 default:
2823 ret = -EINVAL; /* should be BUG() ? */
2824 break;
2825 }
2826 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002827}
2828
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08002829static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2830 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2831{
2832 struct cgroup *cgroup;
2833 unsigned long long min_limit, min_memsw_limit, tmp;
2834
2835 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2836 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2837 cgroup = memcg->css.cgroup;
2838 if (!memcg->use_hierarchy)
2839 goto out;
2840
2841 while (cgroup->parent) {
2842 cgroup = cgroup->parent;
2843 memcg = mem_cgroup_from_cont(cgroup);
2844 if (!memcg->use_hierarchy)
2845 break;
2846 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2847 min_limit = min(min_limit, tmp);
2848 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2849 min_memsw_limit = min(min_memsw_limit, tmp);
2850 }
2851out:
2852 *mem_limit = min_limit;
2853 *memsw_limit = min_memsw_limit;
2854 return;
2855}
2856
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002857static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002858{
2859 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002860 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002861
2862 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002863 type = MEMFILE_TYPE(event);
2864 name = MEMFILE_ATTR(event);
2865 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002866 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002867 if (type == _MEM)
2868 res_counter_reset_max(&mem->res);
2869 else
2870 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002871 break;
2872 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08002873 if (type == _MEM)
2874 res_counter_reset_failcnt(&mem->res);
2875 else
2876 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07002877 break;
2878 }
Balbir Singhf64c3f52009-09-23 15:56:37 -07002879
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07002880 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07002881}
2882
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08002883static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
2884 struct cftype *cft)
2885{
2886 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
2887}
2888
2889static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
2890 struct cftype *cft, u64 val)
2891{
2892 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
2893
2894 if (val >= (1 << NR_MOVE_TYPE))
2895 return -EINVAL;
2896 /*
2897 * We check this value several times in both in can_attach() and
2898 * attach(), so we need cgroup lock to prevent this value from being
2899 * inconsistent.
2900 */
2901 cgroup_lock();
2902 mem->move_charge_at_immigrate = val;
2903 cgroup_unlock();
2904
2905 return 0;
2906}
2907
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002908
2909/* For read statistics */
2910enum {
2911 MCS_CACHE,
2912 MCS_RSS,
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002913 MCS_FILE_MAPPED,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002914 MCS_PGPGIN,
2915 MCS_PGPGOUT,
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002916 MCS_SWAP,
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002917 MCS_INACTIVE_ANON,
2918 MCS_ACTIVE_ANON,
2919 MCS_INACTIVE_FILE,
2920 MCS_ACTIVE_FILE,
2921 MCS_UNEVICTABLE,
2922 NR_MCS_STAT,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002923};
2924
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002925struct mcs_total_stat {
2926 s64 stat[NR_MCS_STAT];
2927};
2928
2929struct {
2930 char *local_name;
2931 char *total_name;
2932} memcg_stat_strings[NR_MCS_STAT] = {
2933 {"cache", "total_cache"},
2934 {"rss", "total_rss"},
Balbir Singhd69b0422009-06-17 16:26:34 -07002935 {"mapped_file", "total_mapped_file"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002936 {"pgpgin", "total_pgpgin"},
2937 {"pgpgout", "total_pgpgout"},
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002938 {"swap", "total_swap"},
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002939 {"inactive_anon", "total_inactive_anon"},
2940 {"active_anon", "total_active_anon"},
2941 {"inactive_file", "total_inactive_file"},
2942 {"active_file", "total_active_file"},
2943 {"unevictable", "total_unevictable"}
2944};
2945
2946
2947static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2948{
2949 struct mcs_total_stat *s = data;
2950 s64 val;
2951
2952 /* per cpu stat */
2953 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2954 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2955 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2956 s->stat[MCS_RSS] += val * PAGE_SIZE;
KAMEZAWA Hiroyukid8046582009-12-15 16:47:09 -08002957 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
2958 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002959 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2960 s->stat[MCS_PGPGIN] += val;
2961 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2962 s->stat[MCS_PGPGOUT] += val;
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002963 if (do_swap_account) {
2964 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2965 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2966 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002967
2968 /* per zone stat */
2969 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2970 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2971 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2972 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2973 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2974 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2975 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2976 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2977 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2978 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2979 return 0;
2980}
2981
2982static void
2983mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2984{
2985 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2986}
2987
Paul Menagec64745c2008-04-29 01:00:02 -07002988static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2989 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002990{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002991 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002992 struct mcs_total_stat mystat;
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002993 int i;
2994
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07002995 memset(&mystat, 0, sizeof(mystat));
2996 mem_cgroup_get_local_stat(mem_cont, &mystat);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08002997
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07002998 for (i = 0; i < NR_MCS_STAT; i++) {
2999 if (i == MCS_SWAP && !do_swap_account)
3000 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003001 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003002 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003003
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003004 /* Hierarchical information */
KAMEZAWA Hiroyukifee7b542009-01-07 18:08:26 -08003005 {
3006 unsigned long long limit, memsw_limit;
3007 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3008 cb->fill(cb, "hierarchical_memory_limit", limit);
3009 if (do_swap_account)
3010 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3011 }
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003012
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003013 memset(&mystat, 0, sizeof(mystat));
3014 mem_cgroup_get_total_stat(mem_cont, &mystat);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003015 for (i = 0; i < NR_MCS_STAT; i++) {
3016 if (i == MCS_SWAP && !do_swap_account)
3017 continue;
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003018 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
Daisuke Nishimura1dd3a272009-09-23 15:56:43 -07003019 }
KAMEZAWA Hiroyuki14067bb2009-04-02 16:57:35 -07003020
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003021#ifdef CONFIG_DEBUG_VM
KOSAKI Motohiroc772be92009-01-07 18:08:25 -08003022 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
KOSAKI Motohiro7f016ee2009-01-07 18:08:22 -08003023
3024 {
3025 int nid, zid;
3026 struct mem_cgroup_per_zone *mz;
3027 unsigned long recent_rotated[2] = {0, 0};
3028 unsigned long recent_scanned[2] = {0, 0};
3029
3030 for_each_online_node(nid)
3031 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3032 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3033
3034 recent_rotated[0] +=
3035 mz->reclaim_stat.recent_rotated[0];
3036 recent_rotated[1] +=
3037 mz->reclaim_stat.recent_rotated[1];
3038 recent_scanned[0] +=
3039 mz->reclaim_stat.recent_scanned[0];
3040 recent_scanned[1] +=
3041 mz->reclaim_stat.recent_scanned[1];
3042 }
3043 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3044 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3045 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3046 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3047 }
3048#endif
3049
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003050 return 0;
3051}
3052
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003053static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3054{
3055 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3056
3057 return get_swappiness(memcg);
3058}
3059
3060static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3061 u64 val)
3062{
3063 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3064 struct mem_cgroup *parent;
Li Zefan068b38c2009-01-15 13:51:26 -08003065
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003066 if (val > 100)
3067 return -EINVAL;
3068
3069 if (cgrp->parent == NULL)
3070 return -EINVAL;
3071
3072 parent = mem_cgroup_from_cont(cgrp->parent);
Li Zefan068b38c2009-01-15 13:51:26 -08003073
3074 cgroup_lock();
3075
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003076 /* If under hierarchy, only empty-root can set this value */
3077 if ((parent->use_hierarchy) ||
Li Zefan068b38c2009-01-15 13:51:26 -08003078 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3079 cgroup_unlock();
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003080 return -EINVAL;
Li Zefan068b38c2009-01-15 13:51:26 -08003081 }
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003082
3083 spin_lock(&memcg->reclaim_param_lock);
3084 memcg->swappiness = val;
3085 spin_unlock(&memcg->reclaim_param_lock);
3086
Li Zefan068b38c2009-01-15 13:51:26 -08003087 cgroup_unlock();
3088
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003089 return 0;
3090}
3091
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003092
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003093static struct cftype mem_cgroup_files[] = {
3094 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003095 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003096 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07003097 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003098 },
3099 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003100 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003101 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003102 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07003103 .read_u64 = mem_cgroup_read,
3104 },
3105 {
Balbir Singh0eea1032008-02-07 00:13:57 -08003106 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003107 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07003108 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07003109 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003110 },
3111 {
Balbir Singh296c81d2009-09-23 15:56:36 -07003112 .name = "soft_limit_in_bytes",
3113 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3114 .write_string = mem_cgroup_write,
3115 .read_u64 = mem_cgroup_read,
3116 },
3117 {
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003118 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003119 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07003120 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07003121 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003122 },
Balbir Singh8697d332008-02-07 00:13:59 -08003123 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003124 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07003125 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08003126 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08003127 {
3128 .name = "force_empty",
3129 .trigger = mem_cgroup_force_empty_write,
3130 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08003131 {
3132 .name = "use_hierarchy",
3133 .write_u64 = mem_cgroup_hierarchy_write,
3134 .read_u64 = mem_cgroup_hierarchy_read,
3135 },
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003136 {
3137 .name = "swappiness",
3138 .read_u64 = mem_cgroup_swappiness_read,
3139 .write_u64 = mem_cgroup_swappiness_write,
3140 },
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003141 {
3142 .name = "move_charge_at_immigrate",
3143 .read_u64 = mem_cgroup_move_charge_read,
3144 .write_u64 = mem_cgroup_move_charge_write,
3145 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003146};
3147
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003148#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3149static struct cftype memsw_cgroup_files[] = {
3150 {
3151 .name = "memsw.usage_in_bytes",
3152 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3153 .read_u64 = mem_cgroup_read,
3154 },
3155 {
3156 .name = "memsw.max_usage_in_bytes",
3157 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3158 .trigger = mem_cgroup_reset,
3159 .read_u64 = mem_cgroup_read,
3160 },
3161 {
3162 .name = "memsw.limit_in_bytes",
3163 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3164 .write_string = mem_cgroup_write,
3165 .read_u64 = mem_cgroup_read,
3166 },
3167 {
3168 .name = "memsw.failcnt",
3169 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3170 .trigger = mem_cgroup_reset,
3171 .read_u64 = mem_cgroup_read,
3172 },
3173};
3174
3175static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3176{
3177 if (!do_swap_account)
3178 return 0;
3179 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3180 ARRAY_SIZE(memsw_cgroup_files));
3181};
3182#else
3183static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3184{
3185 return 0;
3186}
3187#endif
3188
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003189static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3190{
3191 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003192 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07003193 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003194 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003195 /*
3196 * This routine is called against possible nodes.
3197 * But it's BUG to call kmalloc() against offline node.
3198 *
3199 * TODO: this routine can waste much memory for nodes which will
3200 * never be onlined. It's better to use memory hotplug callback
3201 * function.
3202 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07003203 if (!node_state(node, N_NORMAL_MEMORY))
3204 tmp = -1;
3205 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003206 if (!pn)
3207 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003208
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003209 mem->info.nodeinfo[node] = pn;
3210 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003211
3212 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3213 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07003214 for_each_lru(l)
3215 INIT_LIST_HEAD(&mz->lists[l]);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003216 mz->usage_in_excess = 0;
Balbir Singh4e416952009-09-23 15:56:39 -07003217 mz->on_tree = false;
3218 mz->mem = mem;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003219 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003220 return 0;
3221}
3222
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08003223static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3224{
3225 kfree(mem->info.nodeinfo[node]);
3226}
3227
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003228static int mem_cgroup_size(void)
3229{
3230 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3231 return sizeof(struct mem_cgroup) + cpustat_size;
3232}
3233
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003234static struct mem_cgroup *mem_cgroup_alloc(void)
3235{
3236 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003237 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003238
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003239 if (size < PAGE_SIZE)
3240 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003241 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003242 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003243
3244 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003245 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003246 return mem;
3247}
3248
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003249/*
3250 * At destroying mem_cgroup, references from swap_cgroup can remain.
3251 * (scanning all at force_empty is too costly...)
3252 *
3253 * Instead of clearing all references at force_empty, we remember
3254 * the number of reference from swap_cgroup and free mem_cgroup when
3255 * it goes down to 0.
3256 *
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003257 * Removal of cgroup itself succeeds regardless of refs from swap.
3258 */
3259
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003260static void __mem_cgroup_free(struct mem_cgroup *mem)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003261{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003262 int node;
3263
Balbir Singhf64c3f52009-09-23 15:56:37 -07003264 mem_cgroup_remove_from_trees(mem);
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003265 free_css_id(&mem_cgroup_subsys, &mem->css);
3266
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08003267 for_each_node_state(node, N_POSSIBLE)
3268 free_mem_cgroup_per_zone_info(mem, node);
3269
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003270 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003271 kfree(mem);
3272 else
3273 vfree(mem);
3274}
3275
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003276static void mem_cgroup_get(struct mem_cgroup *mem)
3277{
3278 atomic_inc(&mem->refcnt);
3279}
3280
3281static void mem_cgroup_put(struct mem_cgroup *mem)
3282{
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003283 if (atomic_dec_and_test(&mem->refcnt)) {
3284 struct mem_cgroup *parent = parent_mem_cgroup(mem);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003285 __mem_cgroup_free(mem);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003286 if (parent)
3287 mem_cgroup_put(parent);
3288 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003289}
3290
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003291/*
3292 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3293 */
3294static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3295{
3296 if (!mem->res.parent)
3297 return NULL;
3298 return mem_cgroup_from_res_counter(mem->res.parent, res);
3299}
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07003300
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003301#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3302static void __init enable_swap_cgroup(void)
3303{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08003304 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003305 do_swap_account = 1;
3306}
3307#else
3308static void __init enable_swap_cgroup(void)
3309{
3310}
3311#endif
3312
Balbir Singhf64c3f52009-09-23 15:56:37 -07003313static int mem_cgroup_soft_limit_tree_init(void)
3314{
3315 struct mem_cgroup_tree_per_node *rtpn;
3316 struct mem_cgroup_tree_per_zone *rtpz;
3317 int tmp, node, zone;
3318
3319 for_each_node_state(node, N_POSSIBLE) {
3320 tmp = node;
3321 if (!node_state(node, N_NORMAL_MEMORY))
3322 tmp = -1;
3323 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3324 if (!rtpn)
3325 return 1;
3326
3327 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3328
3329 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3330 rtpz = &rtpn->rb_tree_per_zone[zone];
3331 rtpz->rb_root = RB_ROOT;
3332 spin_lock_init(&rtpz->lock);
3333 }
3334 }
3335 return 0;
3336}
3337
Li Zefan0eb253e2009-01-15 13:51:25 -08003338static struct cgroup_subsys_state * __ref
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003339mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3340{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003341 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003342 long error = -ENOMEM;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003343 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003344
Jan Blunckc8dad2b2009-01-07 18:07:53 -08003345 mem = mem_cgroup_alloc();
3346 if (!mem)
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003347 return ERR_PTR(error);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08003348
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003349 for_each_node_state(node, N_POSSIBLE)
3350 if (alloc_mem_cgroup_per_zone_info(mem, node))
3351 goto free_out;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003352
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003353 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003354 if (cont->parent == NULL) {
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003355 int cpu;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003356 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003357 parent = NULL;
Balbir Singh4b3bde42009-09-23 15:56:32 -07003358 root_mem_cgroup = mem;
Balbir Singhf64c3f52009-09-23 15:56:37 -07003359 if (mem_cgroup_soft_limit_tree_init())
3360 goto free_out;
KAMEZAWA Hiroyukicdec2e42009-12-15 16:47:08 -08003361 for_each_possible_cpu(cpu) {
3362 struct memcg_stock_pcp *stock =
3363 &per_cpu(memcg_stock, cpu);
3364 INIT_WORK(&stock->work, drain_local_stock);
3365 }
3366 hotcpu_notifier(memcg_stock_cpu_callback, 0);
Balbir Singhf64c3f52009-09-23 15:56:37 -07003367
Balbir Singh18f59ea2009-01-07 18:08:07 -08003368 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003369 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003370 mem->use_hierarchy = parent->use_hierarchy;
3371 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08003372
Balbir Singh18f59ea2009-01-07 18:08:07 -08003373 if (parent && parent->use_hierarchy) {
3374 res_counter_init(&mem->res, &parent->res);
3375 res_counter_init(&mem->memsw, &parent->memsw);
Daisuke Nishimura7bcc1bb2009-01-29 14:25:11 -08003376 /*
3377 * We increment refcnt of the parent to ensure that we can
3378 * safely access it on res_counter_charge/uncharge.
3379 * This refcnt will be decremented when freeing this
3380 * mem_cgroup(see mem_cgroup_put).
3381 */
3382 mem_cgroup_get(parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08003383 } else {
3384 res_counter_init(&mem->res, NULL);
3385 res_counter_init(&mem->memsw, NULL);
3386 }
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003387 mem->last_scanned_child = 0;
KOSAKI Motohiro2733c062009-01-07 18:08:23 -08003388 spin_lock_init(&mem->reclaim_param_lock);
Balbir Singh6d61ef42009-01-07 18:08:06 -08003389
KOSAKI Motohiroa7885eb2009-01-07 18:08:24 -08003390 if (parent)
3391 mem->swappiness = get_swappiness(parent);
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003392 atomic_set(&mem->refcnt, 1);
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003393 mem->move_charge_at_immigrate = 0;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003394 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003395free_out:
KAMEZAWA Hiroyukia7ba0ee2009-01-07 18:08:32 -08003396 __mem_cgroup_free(mem);
Balbir Singh4b3bde42009-09-23 15:56:32 -07003397 root_mem_cgroup = NULL;
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003398 return ERR_PTR(error);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003399}
3400
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003401static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003402 struct cgroup *cont)
3403{
3404 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukiec64f512009-04-02 16:57:26 -07003405
3406 return mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003407}
3408
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003409static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3410 struct cgroup *cont)
3411{
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003412 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003413
Daisuke Nishimurac268e992009-01-15 13:51:13 -08003414 mem_cgroup_put(mem);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003415}
3416
3417static int mem_cgroup_populate(struct cgroup_subsys *ss,
3418 struct cgroup *cont)
3419{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08003420 int ret;
3421
3422 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3423 ARRAY_SIZE(mem_cgroup_files));
3424
3425 if (!ret)
3426 ret = register_memsw_files(cont, ss);
3427 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003428}
3429
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003430/* Handlers for move charge at task migration. */
3431static int mem_cgroup_can_move_charge(void)
3432{
3433 return 0;
3434}
3435
3436static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
3437 struct cgroup *cgroup,
3438 struct task_struct *p,
3439 bool threadgroup)
3440{
3441 int ret = 0;
3442 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
3443
3444 if (mem->move_charge_at_immigrate) {
3445 struct mm_struct *mm;
3446 struct mem_cgroup *from = mem_cgroup_from_task(p);
3447
3448 VM_BUG_ON(from == mem);
3449
3450 mm = get_task_mm(p);
3451 if (!mm)
3452 return 0;
3453
3454 /* We move charges only when we move a owner of the mm */
3455 if (mm->owner == p)
3456 ret = mem_cgroup_can_move_charge();
3457
3458 mmput(mm);
3459 }
3460 return ret;
3461}
3462
3463static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
3464 struct cgroup *cgroup,
3465 struct task_struct *p,
3466 bool threadgroup)
3467{
3468}
3469
3470static void mem_cgroup_move_charge(void)
3471{
3472}
3473
Balbir Singh67e465a2008-02-07 00:13:54 -08003474static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3475 struct cgroup *cont,
3476 struct cgroup *old_cont,
Ben Blumbe367d02009-09-23 15:56:31 -07003477 struct task_struct *p,
3478 bool threadgroup)
Balbir Singh67e465a2008-02-07 00:13:54 -08003479{
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003480 mem_cgroup_move_charge();
Balbir Singh67e465a2008-02-07 00:13:54 -08003481}
3482
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003483struct cgroup_subsys mem_cgroup_subsys = {
3484 .name = "memory",
3485 .subsys_id = mem_cgroup_subsys_id,
3486 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08003487 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003488 .destroy = mem_cgroup_destroy,
3489 .populate = mem_cgroup_populate,
Daisuke Nishimura7dc74be2010-03-10 15:22:13 -08003490 .can_attach = mem_cgroup_can_attach,
3491 .cancel_attach = mem_cgroup_cancel_attach,
Balbir Singh67e465a2008-02-07 00:13:54 -08003492 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08003493 .early_init = 0,
KAMEZAWA Hiroyuki04046e12009-04-02 16:57:33 -07003494 .use_id = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08003495};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08003496
3497#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3498
3499static int __init disable_swap_account(char *s)
3500{
3501 really_do_swap_account = 0;
3502 return 1;
3503}
3504__setup("noswapaccount", disable_swap_account);
3505#endif