blob: b837900830871ad19ccf714c62b6f49756c36a85 [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>
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -080030#include <linux/mutex.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070031#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080032#include <linux/swap.h>
33#include <linux/spinlock.h>
34#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080035#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070036#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070037#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070038#include <linux/page_cgroup.h>
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -080039#include "internal.h"
Balbir Singh8cdea7c2008-02-07 00:13:50 -080040
Balbir Singh8697d332008-02-07 00:13:59 -080041#include <asm/uaccess.h>
42
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070043struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070044#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080045
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080046#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
47/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
48int do_swap_account __read_mostly;
49static int really_do_swap_account __initdata = 1; /* for remember boot option*/
50#else
51#define do_swap_account (0)
52#endif
53
54
Balbir Singh8cdea7c2008-02-07 00:13:50 -080055/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080056 * Statistics for memory cgroup.
57 */
58enum mem_cgroup_stat_index {
59 /*
60 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
61 */
62 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
63 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070064 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
65 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080066
67 MEM_CGROUP_STAT_NSTATS,
68};
69
70struct mem_cgroup_stat_cpu {
71 s64 count[MEM_CGROUP_STAT_NSTATS];
72} ____cacheline_aligned_in_smp;
73
74struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080075 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080076};
77
78/*
79 * For accounting under irq disable, no need for increment preempt count.
80 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070081static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080082 enum mem_cgroup_stat_index idx, int val)
83{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070084 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080085}
86
87static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
88 enum mem_cgroup_stat_index idx)
89{
90 int cpu;
91 s64 ret = 0;
92 for_each_possible_cpu(cpu)
93 ret += stat->cpustat[cpu].count[idx];
94 return ret;
95}
96
97/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080098 * per-zone information in memory controller.
99 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800100struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800101 /*
102 * spin_lock to protect the per cgroup LRU
103 */
Christoph Lameterb69408e2008-10-18 20:26:14 -0700104 struct list_head lists[NR_LRU_LISTS];
105 unsigned long count[NR_LRU_LISTS];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800106};
107/* Macro for accessing counter */
108#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
109
110struct mem_cgroup_per_node {
111 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
112};
113
114struct mem_cgroup_lru_info {
115 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
116};
117
118/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800119 * The memory controller data structure. The memory controller controls both
120 * page cache and RSS per cgroup. We would eventually like to provide
121 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
122 * to help the administrator determine what knobs to tune.
123 *
124 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800125 * we hit the water mark. May be even add a low water mark, such that
126 * no reclaim occurs from a cgroup at it's low water mark, this is
127 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800128 */
129struct mem_cgroup {
130 struct cgroup_subsys_state css;
131 /*
132 * the counter to account for memory usage
133 */
134 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800135 /*
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800136 * the counter to account for mem+swap usage.
137 */
138 struct res_counter memsw;
139 /*
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800140 * Per cgroup active and inactive list, similar to the
141 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800142 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800143 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800144
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800145 int prev_priority; /* for recording reclaim priority */
Balbir Singh6d61ef42009-01-07 18:08:06 -0800146
147 /*
148 * While reclaiming in a hiearchy, we cache the last child we
149 * reclaimed from. Protected by cgroup_lock()
150 */
151 struct mem_cgroup *last_scanned_child;
Balbir Singh18f59ea2009-01-07 18:08:07 -0800152 /*
153 * Should the accounting and control be hierarchical, per subtree?
154 */
155 bool use_hierarchy;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800156 unsigned long last_oom_jiffies;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800157 int obsolete;
158 atomic_t refcnt;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800159 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800160 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800161 */
162 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800163};
164
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800165enum charge_type {
166 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
167 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700168 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700169 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800170 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700171 NR_CHARGE_TYPE,
172};
173
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700174/* only for here (for easy reading.) */
175#define PCGF_CACHE (1UL << PCG_CACHE)
176#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700177#define PCGF_LOCK (1UL << PCG_LOCK)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700178static const unsigned long
179pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800180 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
181 PCGF_USED | PCGF_LOCK, /* Anon */
182 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700183 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800184};
185
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800186
187/* for encoding cft->private value on file */
188#define _MEM (0)
189#define _MEMSWAP (1)
190#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
191#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
192#define MEMFILE_ATTR(val) ((val) & 0xffff)
193
194static void mem_cgroup_get(struct mem_cgroup *mem);
195static void mem_cgroup_put(struct mem_cgroup *mem);
196
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700197static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
198 struct page_cgroup *pc,
199 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800200{
201 int val = (charge)? 1 : -1;
202 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700203 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800204 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800205
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800206 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700207 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700208 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800209 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700210 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700211
212 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700213 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700214 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
215 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700216 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700217 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800218 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800219}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800220
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800221static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800222mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
223{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800224 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
225}
226
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800227static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800228page_cgroup_zoneinfo(struct page_cgroup *pc)
229{
230 struct mem_cgroup *mem = pc->mem_cgroup;
231 int nid = page_cgroup_nid(pc);
232 int zid = page_cgroup_zid(pc);
233
234 return mem_cgroup_zoneinfo(mem, nid, zid);
235}
236
237static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700238 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800239{
240 int nid, zid;
241 struct mem_cgroup_per_zone *mz;
242 u64 total = 0;
243
244 for_each_online_node(nid)
245 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
246 mz = mem_cgroup_zoneinfo(mem, nid, zid);
247 total += MEM_CGROUP_ZSTAT(mz, idx);
248 }
249 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800250}
251
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800252static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800253{
254 return container_of(cgroup_subsys_state(cont,
255 mem_cgroup_subsys_id), struct mem_cgroup,
256 css);
257}
258
Balbir Singhcf475ad2008-04-29 01:00:16 -0700259struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800260{
Balbir Singh31a78f22008-09-28 23:09:31 +0100261 /*
262 * mm_update_next_owner() may clear mm->owner to NULL
263 * if it races with swapoff, page migration, etc.
264 * So this can be called with p == NULL.
265 */
266 if (unlikely(!p))
267 return NULL;
268
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800269 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
270 struct mem_cgroup, css);
271}
272
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800273/*
274 * Following LRU functions are allowed to be used without PCG_LOCK.
275 * Operations are called by routine of global LRU independently from memcg.
276 * What we have to take care of here is validness of pc->mem_cgroup.
277 *
278 * Changes to pc->mem_cgroup happens when
279 * 1. charge
280 * 2. moving account
281 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
282 * It is added to LRU before charge.
283 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
284 * When moving account, the page is not on LRU. It's isolated.
285 */
286
287void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800288{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800289 struct page_cgroup *pc;
290 struct mem_cgroup *mem;
291 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700292
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800293 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800294 return;
295 pc = lookup_page_cgroup(page);
296 /* can happen while we handle swapcache. */
297 if (list_empty(&pc->lru))
298 return;
299 mz = page_cgroup_zoneinfo(pc);
300 mem = pc->mem_cgroup;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700301 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800302 list_del_init(&pc->lru);
303 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800304}
305
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800306void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800307{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800308 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800309}
310
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800311void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800312{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800313 struct mem_cgroup_per_zone *mz;
314 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800315
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800316 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700317 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700318
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800319 pc = lookup_page_cgroup(page);
320 smp_rmb();
321 /* unused page is not rotated. */
322 if (!PageCgroupUsed(pc))
323 return;
324 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700325 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800326}
327
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800328void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
329{
330 struct page_cgroup *pc;
331 struct mem_cgroup_per_zone *mz;
332
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800333 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800334 return;
335 pc = lookup_page_cgroup(page);
336 /* barrier to sync with "charge" */
337 smp_rmb();
338 if (!PageCgroupUsed(pc))
339 return;
340
341 mz = page_cgroup_zoneinfo(pc);
342 MEM_CGROUP_ZSTAT(mz, lru) += 1;
343 list_add(&pc->lru, &mz->lists[lru]);
344}
345/*
346 * To add swapcache into LRU. Be careful to all this function.
347 * zone->lru_lock shouldn't be held and irq must not be disabled.
348 */
349static void mem_cgroup_lru_fixup(struct page *page)
350{
351 if (!isolate_lru_page(page))
352 putback_lru_page(page);
353}
354
355void mem_cgroup_move_lists(struct page *page,
356 enum lru_list from, enum lru_list to)
357{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800358 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800359 return;
360 mem_cgroup_del_lru_list(page, from);
361 mem_cgroup_add_lru_list(page, to);
362}
363
David Rientjes4c4a2212008-02-07 00:14:06 -0800364int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
365{
366 int ret;
367
368 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800369 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800370 task_unlock(task);
371 return ret;
372}
373
Balbir Singh66e17072008-02-07 00:13:56 -0800374/*
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800375 * Calculate mapped_ratio under memory controller. This will be used in
376 * vmscan.c for deteremining we have to reclaim mapped pages.
377 */
378int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
379{
380 long total, rss;
381
382 /*
383 * usage is recorded in bytes. But, here, we assume the number of
384 * physical pages can be represented by "long" on any arch.
385 */
386 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
387 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
388 return (int)((rss * 100L) / total);
389}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800390
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800391/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800392 * prev_priority control...this will be used in memory reclaim path.
393 */
394int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
395{
396 return mem->prev_priority;
397}
398
399void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
400{
401 if (priority < mem->prev_priority)
402 mem->prev_priority = priority;
403}
404
405void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
406{
407 mem->prev_priority = priority;
408}
409
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800410/*
411 * Calculate # of pages to be scanned in this priority/zone.
412 * See also vmscan.c
413 *
414 * priority starts from "DEF_PRIORITY" and decremented in each loop.
415 * (see include/linux/mmzone.h)
416 */
417
Christoph Lameterb69408e2008-10-18 20:26:14 -0700418long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
419 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800420{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700421 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800422 int nid = zone->zone_pgdat->node_id;
423 int zid = zone_idx(zone);
424 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
425
Christoph Lameterb69408e2008-10-18 20:26:14 -0700426 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800427
Christoph Lameterb69408e2008-10-18 20:26:14 -0700428 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800429}
430
Balbir Singh66e17072008-02-07 00:13:56 -0800431unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
432 struct list_head *dst,
433 unsigned long *scanned, int order,
434 int mode, struct zone *z,
435 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700436 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800437{
438 unsigned long nr_taken = 0;
439 struct page *page;
440 unsigned long scan;
441 LIST_HEAD(pc_list);
442 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800443 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800444 int nid = z->zone_pgdat->node_id;
445 int zid = zone_idx(z);
446 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700447 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800448
Balbir Singhcf475ad2008-04-29 01:00:16 -0700449 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800450 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700451 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800452
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800453 scan = 0;
454 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800455 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800456 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800457
458 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700459 if (unlikely(!PageCgroupUsed(pc)))
460 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800461 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800462 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800463
Hugh Dickins436c65412008-02-07 00:14:12 -0800464 scan++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700465 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800466 list_move(&page->lru, dst);
467 nr_taken++;
468 }
469 }
470
Balbir Singh66e17072008-02-07 00:13:56 -0800471 *scanned = scan;
472 return nr_taken;
473}
474
Balbir Singh6d61ef42009-01-07 18:08:06 -0800475#define mem_cgroup_from_res_counter(counter, member) \
476 container_of(counter, struct mem_cgroup, member)
477
478/*
479 * This routine finds the DFS walk successor. This routine should be
480 * called with cgroup_mutex held
481 */
482static struct mem_cgroup *
483mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
484{
485 struct cgroup *cgroup, *curr_cgroup, *root_cgroup;
486
487 curr_cgroup = curr->css.cgroup;
488 root_cgroup = root_mem->css.cgroup;
489
490 if (!list_empty(&curr_cgroup->children)) {
491 /*
492 * Walk down to children
493 */
494 mem_cgroup_put(curr);
495 cgroup = list_entry(curr_cgroup->children.next,
496 struct cgroup, sibling);
497 curr = mem_cgroup_from_cont(cgroup);
498 mem_cgroup_get(curr);
499 goto done;
500 }
501
502visit_parent:
503 if (curr_cgroup == root_cgroup) {
504 mem_cgroup_put(curr);
505 curr = root_mem;
506 mem_cgroup_get(curr);
507 goto done;
508 }
509
510 /*
511 * Goto next sibling
512 */
513 if (curr_cgroup->sibling.next != &curr_cgroup->parent->children) {
514 mem_cgroup_put(curr);
515 cgroup = list_entry(curr_cgroup->sibling.next, struct cgroup,
516 sibling);
517 curr = mem_cgroup_from_cont(cgroup);
518 mem_cgroup_get(curr);
519 goto done;
520 }
521
522 /*
523 * Go up to next parent and next parent's sibling if need be
524 */
525 curr_cgroup = curr_cgroup->parent;
526 goto visit_parent;
527
528done:
529 root_mem->last_scanned_child = curr;
530 return curr;
531}
532
533/*
534 * Visit the first child (need not be the first child as per the ordering
535 * of the cgroup list, since we track last_scanned_child) of @mem and use
536 * that to reclaim free pages from.
537 */
538static struct mem_cgroup *
539mem_cgroup_get_first_node(struct mem_cgroup *root_mem)
540{
541 struct cgroup *cgroup;
542 struct mem_cgroup *ret;
543 bool obsolete = (root_mem->last_scanned_child &&
544 root_mem->last_scanned_child->obsolete);
545
546 /*
547 * Scan all children under the mem_cgroup mem
548 */
549 cgroup_lock();
550 if (list_empty(&root_mem->css.cgroup->children)) {
551 ret = root_mem;
552 goto done;
553 }
554
555 if (!root_mem->last_scanned_child || obsolete) {
556
557 if (obsolete)
558 mem_cgroup_put(root_mem->last_scanned_child);
559
560 cgroup = list_first_entry(&root_mem->css.cgroup->children,
561 struct cgroup, sibling);
562 ret = mem_cgroup_from_cont(cgroup);
563 mem_cgroup_get(ret);
564 } else
565 ret = mem_cgroup_get_next_node(root_mem->last_scanned_child,
566 root_mem);
567
568done:
569 root_mem->last_scanned_child = ret;
570 cgroup_unlock();
571 return ret;
572}
573
574/*
575 * Dance down the hierarchy if needed to reclaim memory. We remember the
576 * last child we reclaimed from, so that we don't end up penalizing
577 * one child extensively based on its position in the children list.
578 *
579 * root_mem is the original ancestor that we've been reclaim from.
580 */
581static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
582 gfp_t gfp_mask, bool noswap)
583{
584 struct mem_cgroup *next_mem;
585 int ret = 0;
586
587 /*
588 * Reclaim unconditionally and don't check for return value.
589 * We need to reclaim in the current group and down the tree.
590 * One might think about checking for children before reclaiming,
591 * but there might be left over accounting, even after children
592 * have left.
593 */
594 ret = try_to_free_mem_cgroup_pages(root_mem, gfp_mask, noswap);
595 if (res_counter_check_under_limit(&root_mem->res))
596 return 0;
597
598 next_mem = mem_cgroup_get_first_node(root_mem);
599
600 while (next_mem != root_mem) {
601 if (next_mem->obsolete) {
602 mem_cgroup_put(next_mem);
603 cgroup_lock();
604 next_mem = mem_cgroup_get_first_node(root_mem);
605 cgroup_unlock();
606 continue;
607 }
608 ret = try_to_free_mem_cgroup_pages(next_mem, gfp_mask, noswap);
609 if (res_counter_check_under_limit(&root_mem->res))
610 return 0;
611 cgroup_lock();
612 next_mem = mem_cgroup_get_next_node(next_mem, root_mem);
613 cgroup_unlock();
614 }
615 return ret;
616}
617
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800618bool mem_cgroup_oom_called(struct task_struct *task)
619{
620 bool ret = false;
621 struct mem_cgroup *mem;
622 struct mm_struct *mm;
623
624 rcu_read_lock();
625 mm = task->mm;
626 if (!mm)
627 mm = &init_mm;
628 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
629 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
630 ret = true;
631 rcu_read_unlock();
632 return ret;
633}
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800634/*
635 * Unlike exported interface, "oom" parameter is added. if oom==true,
636 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800637 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800638static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800639 gfp_t gfp_mask, struct mem_cgroup **memcg,
640 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800641{
Balbir Singh6d61ef42009-01-07 18:08:06 -0800642 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800643 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800644 struct res_counter *fail_res;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800645
646 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
647 /* Don't account this! */
648 *memcg = NULL;
649 return 0;
650 }
651
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800652 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800653 * We always charge the cgroup the mm_struct belongs to.
654 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800655 * thread group leader migrates. It's possible that mm is not
656 * set, if so charge the init_mm (happens for pagecache usage).
657 */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800658 if (likely(!*memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700659 rcu_read_lock();
660 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100661 if (unlikely(!mem)) {
662 rcu_read_unlock();
Balbir Singh31a78f22008-09-28 23:09:31 +0100663 return 0;
664 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700665 /*
666 * For every charge from the cgroup, increment reference count
667 */
668 css_get(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800669 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700670 rcu_read_unlock();
671 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800672 mem = *memcg;
673 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700674 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800675
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800676 while (1) {
677 int ret;
678 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800679
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800680 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800681 if (likely(!ret)) {
682 if (!do_swap_account)
683 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800684 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
685 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800686 if (likely(!ret))
687 break;
688 /* mem+swap counter fails */
689 res_counter_uncharge(&mem->res, PAGE_SIZE);
690 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800691 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
692 memsw);
693 } else
694 /* mem counter fails */
695 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
696 res);
697
Hugh Dickins3be91272008-02-07 00:14:19 -0800698 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800699 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800700
Balbir Singh6d61ef42009-01-07 18:08:06 -0800701 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
702 noswap);
Balbir Singh66e17072008-02-07 00:13:56 -0800703
704 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800705 * try_to_free_mem_cgroup_pages() might not give us a full
706 * picture of reclaim. Some pages are reclaimed and might be
707 * moved to swap cache or just unmapped from the cgroup.
708 * Check the limit again to see if the reclaim reduced the
709 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800710 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800711 */
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -0800712 if (do_swap_account) {
713 if (res_counter_check_under_limit(&mem_over_limit->res) &&
714 res_counter_check_under_limit(&mem_over_limit->memsw))
715 continue;
716 } else if (res_counter_check_under_limit(&mem_over_limit->res))
717 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800718
719 if (!nr_retries--) {
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800720 if (oom) {
KAMEZAWA Hiroyuki88700752009-01-07 18:08:09 -0800721 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
722 mem_over_limit->last_oom_jiffies = jiffies;
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800723 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800724 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800725 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800726 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800727 return 0;
728nomem:
729 css_put(&mem->css);
730 return -ENOMEM;
731}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800732
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800733/**
734 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
735 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
736 * @gfp_mask: gfp_mask for reclaim.
737 * @memcg: a pointer to memory cgroup which is charged against.
738 *
739 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
740 * memory cgroup from @mm is got and stored in *memcg.
741 *
742 * Returns 0 if success. -ENOMEM at failure.
743 * This call can invoke OOM-Killer.
744 */
745
746int mem_cgroup_try_charge(struct mm_struct *mm,
747 gfp_t mask, struct mem_cgroup **memcg)
748{
749 return __mem_cgroup_try_charge(mm, mask, memcg, true);
750}
751
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800752/*
753 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
754 * USED state. If already USED, uncharge and return.
755 */
756
757static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
758 struct page_cgroup *pc,
759 enum charge_type ctype)
760{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800761 /* try_charge() can return NULL to *memcg, taking care of it. */
762 if (!mem)
763 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700764
765 lock_page_cgroup(pc);
766 if (unlikely(PageCgroupUsed(pc))) {
767 unlock_page_cgroup(pc);
768 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800769 if (do_swap_account)
770 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700771 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800772 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700773 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800774 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800775 smp_wmb();
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700776 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be91272008-02-07 00:14:19 -0800777
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800778 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700779
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700780 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800781}
782
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800783/**
784 * mem_cgroup_move_account - move account of the page
785 * @pc: page_cgroup of the page.
786 * @from: mem_cgroup which the page is moved from.
787 * @to: mem_cgroup which the page is moved to. @from != @to.
788 *
789 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800790 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800791 *
792 * returns 0 at success,
793 * returns -EBUSY when lock is busy or "pc" is unstable.
794 *
795 * This function does "uncharge" from old cgroup but doesn't do "charge" to
796 * new cgroup. It should be done by a caller.
797 */
798
799static int mem_cgroup_move_account(struct page_cgroup *pc,
800 struct mem_cgroup *from, struct mem_cgroup *to)
801{
802 struct mem_cgroup_per_zone *from_mz, *to_mz;
803 int nid, zid;
804 int ret = -EBUSY;
805
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800806 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800807 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800808
809 nid = page_cgroup_nid(pc);
810 zid = page_cgroup_zid(pc);
811 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
812 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
813
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800814 if (!trylock_page_cgroup(pc))
815 return ret;
816
817 if (!PageCgroupUsed(pc))
818 goto out;
819
820 if (pc->mem_cgroup != from)
821 goto out;
822
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800823 css_put(&from->css);
824 res_counter_uncharge(&from->res, PAGE_SIZE);
825 mem_cgroup_charge_statistics(from, pc, false);
826 if (do_swap_account)
827 res_counter_uncharge(&from->memsw, PAGE_SIZE);
828 pc->mem_cgroup = to;
829 mem_cgroup_charge_statistics(to, pc, true);
830 css_get(&to->css);
831 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800832out:
833 unlock_page_cgroup(pc);
834 return ret;
835}
836
837/*
838 * move charges to its parent.
839 */
840
841static int mem_cgroup_move_parent(struct page_cgroup *pc,
842 struct mem_cgroup *child,
843 gfp_t gfp_mask)
844{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800845 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800846 struct cgroup *cg = child->css.cgroup;
847 struct cgroup *pcg = cg->parent;
848 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800849 int ret;
850
851 /* Is ROOT ? */
852 if (!pcg)
853 return -EINVAL;
854
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800855
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800856 parent = mem_cgroup_from_cont(pcg);
857
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800858
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800859 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800860 if (ret || !parent)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800861 return ret;
862
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800863 if (!get_page_unless_zero(page))
864 return -EBUSY;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800865
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800866 ret = isolate_lru_page(page);
867
868 if (ret)
869 goto cancel;
870
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800871 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800872
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800873 /* drop extra refcnt by try_charge() (move_account increment one) */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800874 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800875 putback_lru_page(page);
876 if (!ret) {
877 put_page(page);
878 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800879 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800880 /* uncharge if move fails */
881cancel:
882 res_counter_uncharge(&parent->res, PAGE_SIZE);
883 if (do_swap_account)
884 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
885 put_page(page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800886 return ret;
887}
888
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800889/*
890 * Charge the memory controller for page usage.
891 * Return
892 * 0 if the charge was successful
893 * < 0 if the cgroup is over its limit
894 */
895static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
896 gfp_t gfp_mask, enum charge_type ctype,
897 struct mem_cgroup *memcg)
898{
899 struct mem_cgroup *mem;
900 struct page_cgroup *pc;
901 int ret;
902
903 pc = lookup_page_cgroup(page);
904 /* can happen at boot */
905 if (unlikely(!pc))
906 return 0;
907 prefetchw(pc);
908
909 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800910 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyukia636b322009-01-07 18:08:08 -0800911 if (ret || !mem)
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800912 return ret;
913
914 __mem_cgroup_commit_charge(mem, pc, ctype);
915 return 0;
916}
917
918int mem_cgroup_newpage_charge(struct page *page,
919 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800920{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800921 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -0700922 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700923 if (PageCompound(page))
924 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700925 /*
926 * If already mapped, we don't have to account.
927 * If page cache, page->mapping has address_space.
928 * But page->mapping may have out-of-use anon_vma pointer,
929 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
930 * is NULL.
931 */
932 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
933 return 0;
934 if (unlikely(!mm))
935 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800936 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700937 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800938}
939
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800940int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
941 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800942{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800943 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -0700944 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700945 if (PageCompound(page))
946 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700947 /*
948 * Corner case handling. This is called from add_to_page_cache()
949 * in usual. But some FS (shmem) precharges this page before calling it
950 * and call add_to_page_cache() with GFP_NOWAIT.
951 *
952 * For GFP_NOWAIT case, the page may be pre-charged before calling
953 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
954 * charge twice. (It works but has to pay a bit larger cost.)
955 */
956 if (!(gfp_mask & __GFP_WAIT)) {
957 struct page_cgroup *pc;
958
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700959
960 pc = lookup_page_cgroup(page);
961 if (!pc)
962 return 0;
963 lock_page_cgroup(pc);
964 if (PageCgroupUsed(pc)) {
965 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700966 return 0;
967 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700968 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700969 }
970
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700971 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800972 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700973
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700974 if (page_is_file_cache(page))
975 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700976 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700977 else
978 return mem_cgroup_charge_common(page, mm, gfp_mask,
979 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700980}
981
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800982int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
983 struct page *page,
984 gfp_t mask, struct mem_cgroup **ptr)
985{
986 struct mem_cgroup *mem;
987 swp_entry_t ent;
988
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800989 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800990 return 0;
991
992 if (!do_swap_account)
993 goto charge_cur_mm;
994
995 /*
996 * A racing thread's fault, or swapoff, may have already updated
997 * the pte, and even removed page from swap cache: return success
998 * to go on to do_swap_page()'s pte_same() test, which should fail.
999 */
1000 if (!PageSwapCache(page))
1001 return 0;
1002
1003 ent.val = page_private(page);
1004
1005 mem = lookup_swap_cgroup(ent);
1006 if (!mem || mem->obsolete)
1007 goto charge_cur_mm;
1008 *ptr = mem;
1009 return __mem_cgroup_try_charge(NULL, mask, ptr, true);
1010charge_cur_mm:
1011 if (unlikely(!mm))
1012 mm = &init_mm;
1013 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1014}
1015
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001016#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001017
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001018int mem_cgroup_cache_charge_swapin(struct page *page,
1019 struct mm_struct *mm, gfp_t mask, bool locked)
1020{
1021 int ret = 0;
1022
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001023 if (mem_cgroup_disabled())
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001024 return 0;
1025 if (unlikely(!mm))
1026 mm = &init_mm;
1027 if (!locked)
1028 lock_page(page);
1029 /*
1030 * If not locked, the page can be dropped from SwapCache until
1031 * we reach here.
1032 */
1033 if (PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001034 struct mem_cgroup *mem = NULL;
1035 swp_entry_t ent;
1036
1037 ent.val = page_private(page);
1038 if (do_swap_account) {
1039 mem = lookup_swap_cgroup(ent);
1040 if (mem && mem->obsolete)
1041 mem = NULL;
1042 if (mem)
1043 mm = NULL;
1044 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001045 ret = mem_cgroup_charge_common(page, mm, mask,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001046 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1047
1048 if (!ret && do_swap_account) {
1049 /* avoid double counting */
1050 mem = swap_cgroup_record(ent, NULL);
1051 if (mem) {
1052 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1053 mem_cgroup_put(mem);
1054 }
1055 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001056 }
1057 if (!locked)
1058 unlock_page(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001059 /* add this page(page_cgroup) to the LRU we want. */
1060 mem_cgroup_lru_fixup(page);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001061
1062 return ret;
1063}
1064#endif
1065
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001066void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1067{
1068 struct page_cgroup *pc;
1069
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001070 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001071 return;
1072 if (!ptr)
1073 return;
1074 pc = lookup_page_cgroup(page);
1075 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001076 /*
1077 * Now swap is on-memory. This means this page may be
1078 * counted both as mem and swap....double count.
1079 * Fix it by uncharging from memsw. This SwapCache is stable
1080 * because we're still under lock_page().
1081 */
1082 if (do_swap_account) {
1083 swp_entry_t ent = {.val = page_private(page)};
1084 struct mem_cgroup *memcg;
1085 memcg = swap_cgroup_record(ent, NULL);
1086 if (memcg) {
1087 /* If memcg is obsolete, memcg can be != ptr */
1088 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1089 mem_cgroup_put(memcg);
1090 }
1091
1092 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001093 /* add this page(page_cgroup) to the LRU we want. */
1094 mem_cgroup_lru_fixup(page);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001095}
1096
1097void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1098{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001099 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001100 return;
1101 if (!mem)
1102 return;
1103 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001104 if (do_swap_account)
1105 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001106 css_put(&mem->css);
1107}
1108
1109
Balbir Singh8697d332008-02-07 00:13:59 -08001110/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001111 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001112 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001113static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001114__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001115{
Hugh Dickins82895462008-03-04 14:29:08 -08001116 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001117 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001118 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001119
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001120 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001121 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001122
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001123 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001124 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001125
Balbir Singh8697d332008-02-07 00:13:59 -08001126 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001127 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001128 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001129 pc = lookup_page_cgroup(page);
1130 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001131 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001132
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001133 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001134
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001135 mem = pc->mem_cgroup;
1136
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001137 if (!PageCgroupUsed(pc))
1138 goto unlock_out;
1139
1140 switch (ctype) {
1141 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1142 if (page_mapped(page))
1143 goto unlock_out;
1144 break;
1145 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1146 if (!PageAnon(page)) { /* Shared memory */
1147 if (page->mapping && !page_is_file_cache(page))
1148 goto unlock_out;
1149 } else if (page_mapped(page)) /* Anon */
1150 goto unlock_out;
1151 break;
1152 default:
1153 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001154 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001155
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001156 res_counter_uncharge(&mem->res, PAGE_SIZE);
1157 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1158 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1159
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001160 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001161 ClearPageCgroupUsed(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001162
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001163 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001164 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001165
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001166 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001167
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001168 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001169
1170unlock_out:
1171 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001172 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001173}
1174
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001175void mem_cgroup_uncharge_page(struct page *page)
1176{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001177 /* early check. */
1178 if (page_mapped(page))
1179 return;
1180 if (page->mapping && !PageAnon(page))
1181 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001182 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1183}
1184
1185void mem_cgroup_uncharge_cache_page(struct page *page)
1186{
1187 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001188 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001189 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1190}
1191
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001192/*
1193 * called from __delete_from_swap_cache() and drop "page" account.
1194 * memcg information is recorded to swap_cgroup of "ent"
1195 */
1196void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001197{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001198 struct mem_cgroup *memcg;
1199
1200 memcg = __mem_cgroup_uncharge_common(page,
1201 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1202 /* record memcg information */
1203 if (do_swap_account && memcg) {
1204 swap_cgroup_record(ent, memcg);
1205 mem_cgroup_get(memcg);
1206 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001207}
1208
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001209#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1210/*
1211 * called from swap_entry_free(). remove record in swap_cgroup and
1212 * uncharge "memsw" account.
1213 */
1214void mem_cgroup_uncharge_swap(swp_entry_t ent)
1215{
1216 struct mem_cgroup *memcg;
1217
1218 if (!do_swap_account)
1219 return;
1220
1221 memcg = swap_cgroup_record(ent, NULL);
1222 if (memcg) {
1223 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1224 mem_cgroup_put(memcg);
1225 }
1226}
1227#endif
1228
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001229/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001230 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1231 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001232 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001233int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001234{
1235 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001236 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001237 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001238
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001239 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001240 return 0;
1241
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001242 pc = lookup_page_cgroup(page);
1243 lock_page_cgroup(pc);
1244 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001245 mem = pc->mem_cgroup;
1246 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001247 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001248 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001249
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001250 if (mem) {
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001251 ret = mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001252 css_put(&mem->css);
1253 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001254 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001255 return ret;
1256}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001257
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001258/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001259void mem_cgroup_end_migration(struct mem_cgroup *mem,
1260 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001261{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001262 struct page *target, *unused;
1263 struct page_cgroup *pc;
1264 enum charge_type ctype;
1265
1266 if (!mem)
1267 return;
1268
1269 /* at migration success, oldpage->mapping is NULL. */
1270 if (oldpage->mapping) {
1271 target = oldpage;
1272 unused = NULL;
1273 } else {
1274 target = newpage;
1275 unused = oldpage;
1276 }
1277
1278 if (PageAnon(target))
1279 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1280 else if (page_is_file_cache(target))
1281 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1282 else
1283 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1284
1285 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001286 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001287 __mem_cgroup_uncharge_common(unused, ctype);
1288
1289 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001290 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001291 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1292 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001293 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001294 __mem_cgroup_commit_charge(mem, pc, ctype);
1295
1296 /*
1297 * Both of oldpage and newpage are still under lock_page().
1298 * Then, we don't have to care about race in radix-tree.
1299 * But we have to be careful that this page is unmapped or not.
1300 *
1301 * There is a case for !page_mapped(). At the start of
1302 * migration, oldpage was mapped. But now, it's zapped.
1303 * But we know *target* page is not freed/reused under us.
1304 * mem_cgroup_uncharge_page() does all necessary checks.
1305 */
1306 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1307 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001308}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001309
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001310/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001311 * A call to try to shrink memory usage under specified resource controller.
1312 * This is typically used for page reclaiming for shmem for reducing side
1313 * effect of page allocation from shmem, which is used by some mem_cgroup.
1314 */
1315int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1316{
1317 struct mem_cgroup *mem;
1318 int progress = 0;
1319 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1320
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001321 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001322 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -07001323 if (!mm)
1324 return 0;
Li Zefancede86a2008-07-25 01:47:18 -07001325
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001326 rcu_read_lock();
1327 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +01001328 if (unlikely(!mem)) {
1329 rcu_read_unlock();
1330 return 0;
1331 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001332 css_get(&mem->css);
1333 rcu_read_unlock();
1334
1335 do {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001336 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -07001337 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001338 } while (!progress && --retry);
1339
1340 css_put(&mem->css);
1341 if (!retry)
1342 return -ENOMEM;
1343 return 0;
1344}
1345
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001346static DEFINE_MUTEX(set_limit_mutex);
1347
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001348static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001349 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001350{
1351
1352 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1353 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001354 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001355 int ret = 0;
1356
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001357 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001358 if (signal_pending(current)) {
1359 ret = -EINTR;
1360 break;
1361 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001362 /*
1363 * Rather than hide all in some function, I do this in
1364 * open coded manner. You see what this really does.
1365 * We have to guarantee mem->res.limit < mem->memsw.limit.
1366 */
1367 mutex_lock(&set_limit_mutex);
1368 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1369 if (memswlimit < val) {
1370 ret = -EINVAL;
1371 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001372 break;
1373 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001374 ret = res_counter_set_limit(&memcg->res, val);
1375 mutex_unlock(&set_limit_mutex);
1376
1377 if (!ret)
1378 break;
1379
KAMEZAWA Hiroyukibced0522009-01-07 18:07:49 -08001380 progress = try_to_free_mem_cgroup_pages(memcg,
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001381 GFP_KERNEL, false);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001382 if (!progress) retry_count--;
1383 }
1384 return ret;
1385}
1386
1387int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1388 unsigned long long val)
1389{
1390 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1391 u64 memlimit, oldusage, curusage;
1392 int ret;
1393
1394 if (!do_swap_account)
1395 return -EINVAL;
1396
1397 while (retry_count) {
1398 if (signal_pending(current)) {
1399 ret = -EINTR;
1400 break;
1401 }
1402 /*
1403 * Rather than hide all in some function, I do this in
1404 * open coded manner. You see what this really does.
1405 * We have to guarantee mem->res.limit < mem->memsw.limit.
1406 */
1407 mutex_lock(&set_limit_mutex);
1408 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1409 if (memlimit > val) {
1410 ret = -EINVAL;
1411 mutex_unlock(&set_limit_mutex);
1412 break;
1413 }
1414 ret = res_counter_set_limit(&memcg->memsw, val);
1415 mutex_unlock(&set_limit_mutex);
1416
1417 if (!ret)
1418 break;
1419
1420 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001421 try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, true);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001422 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1423 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001424 retry_count--;
1425 }
1426 return ret;
1427}
1428
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001429/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001430 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001431 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1432 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001433static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001434 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001435{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001436 struct zone *zone;
1437 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001438 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001439 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001440 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001441 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001442
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001443 zone = &NODE_DATA(node)->node_zones[zid];
1444 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001445 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001446
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001447 loop = MEM_CGROUP_ZSTAT(mz, lru);
1448 /* give some margin against EBUSY etc...*/
1449 loop += 256;
1450 busy = NULL;
1451 while (loop--) {
1452 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001453 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001454 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001455 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001456 break;
1457 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001458 pc = list_entry(list->prev, struct page_cgroup, lru);
1459 if (busy == pc) {
1460 list_move(&pc->lru, list);
1461 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001462 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001463 continue;
1464 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001465 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001466
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001467 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001468 if (ret == -ENOMEM)
1469 break;
1470
1471 if (ret == -EBUSY || ret == -EINVAL) {
1472 /* found lock contention or "pc" is obsolete. */
1473 busy = pc;
1474 cond_resched();
1475 } else
1476 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001477 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001478
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001479 if (!ret && !list_empty(list))
1480 return -EBUSY;
1481 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001482}
1483
1484/*
1485 * make mem_cgroup's charge to be 0 if there is no task.
1486 * This enables deleting this mem_cgroup.
1487 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001488static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001489{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001490 int ret;
1491 int node, zid, shrink;
1492 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001493 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001494
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001495 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001496
1497 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001498 /* should free all ? */
1499 if (free_all)
1500 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001501move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001502 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001503 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001504 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001505 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001506 ret = -EINTR;
1507 if (signal_pending(current))
1508 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001509 /* This is for making all *used* pages to be on LRU. */
1510 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001511 ret = 0;
1512 for_each_node_state(node, N_POSSIBLE) {
1513 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001514 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001515 for_each_lru(l) {
1516 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001517 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001518 if (ret)
1519 break;
1520 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001521 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001522 if (ret)
1523 break;
1524 }
1525 /* it seems parent cgroup doesn't have enough mem */
1526 if (ret == -ENOMEM)
1527 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001528 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001529 }
1530 ret = 0;
1531out:
1532 css_put(&mem->css);
1533 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001534
1535try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001536 /* returns EBUSY if there is a task or if we come here twice. */
1537 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001538 ret = -EBUSY;
1539 goto out;
1540 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001541 /* we call try-to-free pages for make this cgroup empty */
1542 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001543 /* try to free all pages in this cgroup */
1544 shrink = 1;
1545 while (nr_retries && mem->res.usage > 0) {
1546 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001547
1548 if (signal_pending(current)) {
1549 ret = -EINTR;
1550 goto out;
1551 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001552 progress = try_to_free_mem_cgroup_pages(mem,
KAMEZAWA Hiroyuki2c26fdd2009-01-07 18:08:10 -08001553 GFP_KERNEL, false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001554 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001555 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001556 /* maybe some writeback is necessary */
1557 congestion_wait(WRITE, HZ/10);
1558 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001559
1560 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001561 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001562 /* try move_account...there may be some *locked* pages. */
1563 if (mem->res.usage)
1564 goto move_account;
1565 ret = 0;
1566 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001567}
1568
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001569int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1570{
1571 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1572}
1573
1574
Balbir Singh18f59ea2009-01-07 18:08:07 -08001575static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1576{
1577 return mem_cgroup_from_cont(cont)->use_hierarchy;
1578}
1579
1580static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1581 u64 val)
1582{
1583 int retval = 0;
1584 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1585 struct cgroup *parent = cont->parent;
1586 struct mem_cgroup *parent_mem = NULL;
1587
1588 if (parent)
1589 parent_mem = mem_cgroup_from_cont(parent);
1590
1591 cgroup_lock();
1592 /*
1593 * If parent's use_hiearchy is set, we can't make any modifications
1594 * in the child subtrees. If it is unset, then the change can
1595 * occur, provided the current cgroup has no children.
1596 *
1597 * For the root cgroup, parent_mem is NULL, we allow value to be
1598 * set if there are no children.
1599 */
1600 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1601 (val == 1 || val == 0)) {
1602 if (list_empty(&cont->children))
1603 mem->use_hierarchy = val;
1604 else
1605 retval = -EBUSY;
1606 } else
1607 retval = -EINVAL;
1608 cgroup_unlock();
1609
1610 return retval;
1611}
1612
Paul Menage2c3daa72008-04-29 00:59:58 -07001613static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001614{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001615 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1616 u64 val = 0;
1617 int type, name;
1618
1619 type = MEMFILE_TYPE(cft->private);
1620 name = MEMFILE_ATTR(cft->private);
1621 switch (type) {
1622 case _MEM:
1623 val = res_counter_read_u64(&mem->res, name);
1624 break;
1625 case _MEMSWAP:
1626 if (do_swap_account)
1627 val = res_counter_read_u64(&mem->memsw, name);
1628 break;
1629 default:
1630 BUG();
1631 break;
1632 }
1633 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001634}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001635/*
1636 * The user of this function is...
1637 * RES_LIMIT.
1638 */
Paul Menage856c13a2008-07-25 01:47:04 -07001639static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1640 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001641{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001642 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001643 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001644 unsigned long long val;
1645 int ret;
1646
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001647 type = MEMFILE_TYPE(cft->private);
1648 name = MEMFILE_ATTR(cft->private);
1649 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001650 case RES_LIMIT:
1651 /* This function does all necessary parse...reuse it */
1652 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001653 if (ret)
1654 break;
1655 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001656 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001657 else
1658 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001659 break;
1660 default:
1661 ret = -EINVAL; /* should be BUG() ? */
1662 break;
1663 }
1664 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001665}
1666
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001667static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001668{
1669 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001670 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001671
1672 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001673 type = MEMFILE_TYPE(event);
1674 name = MEMFILE_ATTR(event);
1675 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001676 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001677 if (type == _MEM)
1678 res_counter_reset_max(&mem->res);
1679 else
1680 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001681 break;
1682 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001683 if (type == _MEM)
1684 res_counter_reset_failcnt(&mem->res);
1685 else
1686 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001687 break;
1688 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001689 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001690}
1691
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001692static const struct mem_cgroup_stat_desc {
1693 const char *msg;
1694 u64 unit;
1695} mem_cgroup_stat_desc[] = {
1696 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1697 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -07001698 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1699 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001700};
1701
Paul Menagec64745c2008-04-29 01:00:02 -07001702static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1703 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001704{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001705 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1706 struct mem_cgroup_stat *stat = &mem_cont->stat;
1707 int i;
1708
1709 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1710 s64 val;
1711
1712 val = mem_cgroup_read_stat(stat, i);
1713 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001714 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001715 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001716 /* showing # of active pages */
1717 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001718 unsigned long active_anon, inactive_anon;
1719 unsigned long active_file, inactive_file;
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001720 unsigned long unevictable;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001721
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001722 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1723 LRU_INACTIVE_ANON);
1724 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1725 LRU_ACTIVE_ANON);
1726 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1727 LRU_INACTIVE_FILE);
1728 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1729 LRU_ACTIVE_FILE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001730 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1731 LRU_UNEVICTABLE);
1732
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001733 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1734 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1735 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1736 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001737 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1738
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001739 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001740 return 0;
1741}
1742
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001743
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001744static struct cftype mem_cgroup_files[] = {
1745 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001746 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001747 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07001748 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001749 },
1750 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001751 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001752 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001753 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001754 .read_u64 = mem_cgroup_read,
1755 },
1756 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001757 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001758 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07001759 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001760 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001761 },
1762 {
1763 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001764 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001765 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001766 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001767 },
Balbir Singh8697d332008-02-07 00:13:59 -08001768 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001769 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001770 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001771 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001772 {
1773 .name = "force_empty",
1774 .trigger = mem_cgroup_force_empty_write,
1775 },
Balbir Singh18f59ea2009-01-07 18:08:07 -08001776 {
1777 .name = "use_hierarchy",
1778 .write_u64 = mem_cgroup_hierarchy_write,
1779 .read_u64 = mem_cgroup_hierarchy_read,
1780 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001781};
1782
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001783#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1784static struct cftype memsw_cgroup_files[] = {
1785 {
1786 .name = "memsw.usage_in_bytes",
1787 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1788 .read_u64 = mem_cgroup_read,
1789 },
1790 {
1791 .name = "memsw.max_usage_in_bytes",
1792 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1793 .trigger = mem_cgroup_reset,
1794 .read_u64 = mem_cgroup_read,
1795 },
1796 {
1797 .name = "memsw.limit_in_bytes",
1798 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1799 .write_string = mem_cgroup_write,
1800 .read_u64 = mem_cgroup_read,
1801 },
1802 {
1803 .name = "memsw.failcnt",
1804 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1805 .trigger = mem_cgroup_reset,
1806 .read_u64 = mem_cgroup_read,
1807 },
1808};
1809
1810static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1811{
1812 if (!do_swap_account)
1813 return 0;
1814 return cgroup_add_files(cont, ss, memsw_cgroup_files,
1815 ARRAY_SIZE(memsw_cgroup_files));
1816};
1817#else
1818static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1819{
1820 return 0;
1821}
1822#endif
1823
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001824static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1825{
1826 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001827 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001828 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001829 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001830 /*
1831 * This routine is called against possible nodes.
1832 * But it's BUG to call kmalloc() against offline node.
1833 *
1834 * TODO: this routine can waste much memory for nodes which will
1835 * never be onlined. It's better to use memory hotplug callback
1836 * function.
1837 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001838 if (!node_state(node, N_NORMAL_MEMORY))
1839 tmp = -1;
1840 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001841 if (!pn)
1842 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001843
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001844 mem->info.nodeinfo[node] = pn;
1845 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001846
1847 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1848 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07001849 for_each_lru(l)
1850 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001851 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001852 return 0;
1853}
1854
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001855static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1856{
1857 kfree(mem->info.nodeinfo[node]);
1858}
1859
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001860static int mem_cgroup_size(void)
1861{
1862 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1863 return sizeof(struct mem_cgroup) + cpustat_size;
1864}
1865
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001866static struct mem_cgroup *mem_cgroup_alloc(void)
1867{
1868 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001869 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001870
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001871 if (size < PAGE_SIZE)
1872 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001873 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001874 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001875
1876 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001877 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001878 return mem;
1879}
1880
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001881/*
1882 * At destroying mem_cgroup, references from swap_cgroup can remain.
1883 * (scanning all at force_empty is too costly...)
1884 *
1885 * Instead of clearing all references at force_empty, we remember
1886 * the number of reference from swap_cgroup and free mem_cgroup when
1887 * it goes down to 0.
1888 *
1889 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1890 * entry which points to this memcg will be ignore at swapin.
1891 *
1892 * Removal of cgroup itself succeeds regardless of refs from swap.
1893 */
1894
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001895static void mem_cgroup_free(struct mem_cgroup *mem)
1896{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001897 int node;
1898
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001899 if (atomic_read(&mem->refcnt) > 0)
1900 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001901
1902
1903 for_each_node_state(node, N_POSSIBLE)
1904 free_mem_cgroup_per_zone_info(mem, node);
1905
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001906 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001907 kfree(mem);
1908 else
1909 vfree(mem);
1910}
1911
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001912static void mem_cgroup_get(struct mem_cgroup *mem)
1913{
1914 atomic_inc(&mem->refcnt);
1915}
1916
1917static void mem_cgroup_put(struct mem_cgroup *mem)
1918{
1919 if (atomic_dec_and_test(&mem->refcnt)) {
1920 if (!mem->obsolete)
1921 return;
1922 mem_cgroup_free(mem);
1923 }
1924}
1925
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001926
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001927#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1928static void __init enable_swap_cgroup(void)
1929{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001930 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001931 do_swap_account = 1;
1932}
1933#else
1934static void __init enable_swap_cgroup(void)
1935{
1936}
1937#endif
1938
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001939static struct cgroup_subsys_state *
1940mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1941{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001942 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001943 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001944
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001945 mem = mem_cgroup_alloc();
1946 if (!mem)
1947 return ERR_PTR(-ENOMEM);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001948
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001949 for_each_node_state(node, N_POSSIBLE)
1950 if (alloc_mem_cgroup_per_zone_info(mem, node))
1951 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001952 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001953 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001954 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001955 parent = NULL;
Balbir Singh18f59ea2009-01-07 18:08:07 -08001956 } else {
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001957 parent = mem_cgroup_from_cont(cont->parent);
Balbir Singh18f59ea2009-01-07 18:08:07 -08001958 mem->use_hierarchy = parent->use_hierarchy;
1959 }
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001960
Balbir Singh18f59ea2009-01-07 18:08:07 -08001961 if (parent && parent->use_hierarchy) {
1962 res_counter_init(&mem->res, &parent->res);
1963 res_counter_init(&mem->memsw, &parent->memsw);
1964 } else {
1965 res_counter_init(&mem->res, NULL);
1966 res_counter_init(&mem->memsw, NULL);
1967 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001968
Balbir Singh6d61ef42009-01-07 18:08:06 -08001969 mem->last_scanned_child = NULL;
1970
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001971 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001972free_out:
1973 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001974 free_mem_cgroup_per_zone_info(mem, node);
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001975 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001976 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001977}
1978
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001979static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1980 struct cgroup *cont)
1981{
1982 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001983 mem->obsolete = 1;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001984 mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001985}
1986
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001987static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1988 struct cgroup *cont)
1989{
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001990 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001991}
1992
1993static int mem_cgroup_populate(struct cgroup_subsys *ss,
1994 struct cgroup *cont)
1995{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001996 int ret;
1997
1998 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
1999 ARRAY_SIZE(mem_cgroup_files));
2000
2001 if (!ret)
2002 ret = register_memsw_files(cont, ss);
2003 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002004}
2005
Balbir Singh67e465a2008-02-07 00:13:54 -08002006static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2007 struct cgroup *cont,
2008 struct cgroup *old_cont,
2009 struct task_struct *p)
2010{
Balbir Singh67e465a2008-02-07 00:13:54 -08002011 /*
Nikanth Karthikesanf9717d22009-01-07 18:08:11 -08002012 * FIXME: It's better to move charges of this process from old
2013 * memcg to new memcg. But it's just on TODO-List now.
Balbir Singh67e465a2008-02-07 00:13:54 -08002014 */
Balbir Singh67e465a2008-02-07 00:13:54 -08002015}
2016
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002017struct cgroup_subsys mem_cgroup_subsys = {
2018 .name = "memory",
2019 .subsys_id = mem_cgroup_subsys_id,
2020 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08002021 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002022 .destroy = mem_cgroup_destroy,
2023 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08002024 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08002025 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08002026};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08002027
2028#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2029
2030static int __init disable_swap_account(char *s)
2031{
2032 really_do_swap_account = 0;
2033 return 1;
2034}
2035__setup("noswapaccount", disable_swap_account);
2036#endif