blob: 20e1d90b3363a6e59191e48fe1660af166732723 [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;
152
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800153 int obsolete;
154 atomic_t refcnt;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800155 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800156 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800157 */
158 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800159};
160
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800161enum charge_type {
162 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
163 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700164 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700165 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800166 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700167 NR_CHARGE_TYPE,
168};
169
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700170/* only for here (for easy reading.) */
171#define PCGF_CACHE (1UL << PCG_CACHE)
172#define PCGF_USED (1UL << PCG_USED)
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700173#define PCGF_LOCK (1UL << PCG_LOCK)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700174static const unsigned long
175pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800176 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
177 PCGF_USED | PCGF_LOCK, /* Anon */
178 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700179 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800180};
181
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800182
183/* for encoding cft->private value on file */
184#define _MEM (0)
185#define _MEMSWAP (1)
186#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
187#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
188#define MEMFILE_ATTR(val) ((val) & 0xffff)
189
190static void mem_cgroup_get(struct mem_cgroup *mem);
191static void mem_cgroup_put(struct mem_cgroup *mem);
192
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700193static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
194 struct page_cgroup *pc,
195 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800196{
197 int val = (charge)? 1 : -1;
198 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700199 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800200 int cpu = get_cpu();
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800201
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800202 cpustat = &stat->cpustat[cpu];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700203 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700204 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800205 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700206 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700207
208 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700209 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700210 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
211 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700212 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700213 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800214 put_cpu();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800215}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800216
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800217static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800218mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
219{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800220 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
221}
222
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800223static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800224page_cgroup_zoneinfo(struct page_cgroup *pc)
225{
226 struct mem_cgroup *mem = pc->mem_cgroup;
227 int nid = page_cgroup_nid(pc);
228 int zid = page_cgroup_zid(pc);
229
230 return mem_cgroup_zoneinfo(mem, nid, zid);
231}
232
233static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700234 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800235{
236 int nid, zid;
237 struct mem_cgroup_per_zone *mz;
238 u64 total = 0;
239
240 for_each_online_node(nid)
241 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
242 mz = mem_cgroup_zoneinfo(mem, nid, zid);
243 total += MEM_CGROUP_ZSTAT(mz, idx);
244 }
245 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800246}
247
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800248static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800249{
250 return container_of(cgroup_subsys_state(cont,
251 mem_cgroup_subsys_id), struct mem_cgroup,
252 css);
253}
254
Balbir Singhcf475ad2008-04-29 01:00:16 -0700255struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800256{
Balbir Singh31a78f22008-09-28 23:09:31 +0100257 /*
258 * mm_update_next_owner() may clear mm->owner to NULL
259 * if it races with swapoff, page migration, etc.
260 * So this can be called with p == NULL.
261 */
262 if (unlikely(!p))
263 return NULL;
264
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800265 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
266 struct mem_cgroup, css);
267}
268
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800269/*
270 * Following LRU functions are allowed to be used without PCG_LOCK.
271 * Operations are called by routine of global LRU independently from memcg.
272 * What we have to take care of here is validness of pc->mem_cgroup.
273 *
274 * Changes to pc->mem_cgroup happens when
275 * 1. charge
276 * 2. moving account
277 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
278 * It is added to LRU before charge.
279 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
280 * When moving account, the page is not on LRU. It's isolated.
281 */
282
283void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800284{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800285 struct page_cgroup *pc;
286 struct mem_cgroup *mem;
287 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700288
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800289 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800290 return;
291 pc = lookup_page_cgroup(page);
292 /* can happen while we handle swapcache. */
293 if (list_empty(&pc->lru))
294 return;
295 mz = page_cgroup_zoneinfo(pc);
296 mem = pc->mem_cgroup;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700297 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800298 list_del_init(&pc->lru);
299 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800300}
301
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800302void mem_cgroup_del_lru(struct page *page)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800303{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800304 mem_cgroup_del_lru_list(page, page_lru(page));
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800305}
306
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800307void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800308{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800309 struct mem_cgroup_per_zone *mz;
310 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800311
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800312 if (mem_cgroup_disabled())
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700313 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700314
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800315 pc = lookup_page_cgroup(page);
316 smp_rmb();
317 /* unused page is not rotated. */
318 if (!PageCgroupUsed(pc))
319 return;
320 mz = page_cgroup_zoneinfo(pc);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700321 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800322}
323
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800324void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
325{
326 struct page_cgroup *pc;
327 struct mem_cgroup_per_zone *mz;
328
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800329 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800330 return;
331 pc = lookup_page_cgroup(page);
332 /* barrier to sync with "charge" */
333 smp_rmb();
334 if (!PageCgroupUsed(pc))
335 return;
336
337 mz = page_cgroup_zoneinfo(pc);
338 MEM_CGROUP_ZSTAT(mz, lru) += 1;
339 list_add(&pc->lru, &mz->lists[lru]);
340}
341/*
342 * To add swapcache into LRU. Be careful to all this function.
343 * zone->lru_lock shouldn't be held and irq must not be disabled.
344 */
345static void mem_cgroup_lru_fixup(struct page *page)
346{
347 if (!isolate_lru_page(page))
348 putback_lru_page(page);
349}
350
351void mem_cgroup_move_lists(struct page *page,
352 enum lru_list from, enum lru_list to)
353{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800354 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800355 return;
356 mem_cgroup_del_lru_list(page, from);
357 mem_cgroup_add_lru_list(page, to);
358}
359
David Rientjes4c4a2212008-02-07 00:14:06 -0800360int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
361{
362 int ret;
363
364 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800365 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800366 task_unlock(task);
367 return ret;
368}
369
Balbir Singh66e17072008-02-07 00:13:56 -0800370/*
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800371 * Calculate mapped_ratio under memory controller. This will be used in
372 * vmscan.c for deteremining we have to reclaim mapped pages.
373 */
374int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
375{
376 long total, rss;
377
378 /*
379 * usage is recorded in bytes. But, here, we assume the number of
380 * physical pages can be represented by "long" on any arch.
381 */
382 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
383 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
384 return (int)((rss * 100L) / total);
385}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800386
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800387/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800388 * prev_priority control...this will be used in memory reclaim path.
389 */
390int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
391{
392 return mem->prev_priority;
393}
394
395void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
396{
397 if (priority < mem->prev_priority)
398 mem->prev_priority = priority;
399}
400
401void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
402{
403 mem->prev_priority = priority;
404}
405
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800406/*
407 * Calculate # of pages to be scanned in this priority/zone.
408 * See also vmscan.c
409 *
410 * priority starts from "DEF_PRIORITY" and decremented in each loop.
411 * (see include/linux/mmzone.h)
412 */
413
Christoph Lameterb69408e2008-10-18 20:26:14 -0700414long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
415 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800416{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700417 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800418 int nid = zone->zone_pgdat->node_id;
419 int zid = zone_idx(zone);
420 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
421
Christoph Lameterb69408e2008-10-18 20:26:14 -0700422 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800423
Christoph Lameterb69408e2008-10-18 20:26:14 -0700424 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800425}
426
Balbir Singh66e17072008-02-07 00:13:56 -0800427unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
428 struct list_head *dst,
429 unsigned long *scanned, int order,
430 int mode, struct zone *z,
431 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700432 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800433{
434 unsigned long nr_taken = 0;
435 struct page *page;
436 unsigned long scan;
437 LIST_HEAD(pc_list);
438 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800439 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800440 int nid = z->zone_pgdat->node_id;
441 int zid = zone_idx(z);
442 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700443 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800444
Balbir Singhcf475ad2008-04-29 01:00:16 -0700445 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800446 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700447 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800448
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800449 scan = 0;
450 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800451 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800452 break;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800453
454 page = pc->page;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700455 if (unlikely(!PageCgroupUsed(pc)))
456 continue;
Hugh Dickins436c65412008-02-07 00:14:12 -0800457 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800458 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800459
Hugh Dickins436c65412008-02-07 00:14:12 -0800460 scan++;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700461 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800462 list_move(&page->lru, dst);
463 nr_taken++;
464 }
465 }
466
Balbir Singh66e17072008-02-07 00:13:56 -0800467 *scanned = scan;
468 return nr_taken;
469}
470
Balbir Singh6d61ef42009-01-07 18:08:06 -0800471#define mem_cgroup_from_res_counter(counter, member) \
472 container_of(counter, struct mem_cgroup, member)
473
474/*
475 * This routine finds the DFS walk successor. This routine should be
476 * called with cgroup_mutex held
477 */
478static struct mem_cgroup *
479mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
480{
481 struct cgroup *cgroup, *curr_cgroup, *root_cgroup;
482
483 curr_cgroup = curr->css.cgroup;
484 root_cgroup = root_mem->css.cgroup;
485
486 if (!list_empty(&curr_cgroup->children)) {
487 /*
488 * Walk down to children
489 */
490 mem_cgroup_put(curr);
491 cgroup = list_entry(curr_cgroup->children.next,
492 struct cgroup, sibling);
493 curr = mem_cgroup_from_cont(cgroup);
494 mem_cgroup_get(curr);
495 goto done;
496 }
497
498visit_parent:
499 if (curr_cgroup == root_cgroup) {
500 mem_cgroup_put(curr);
501 curr = root_mem;
502 mem_cgroup_get(curr);
503 goto done;
504 }
505
506 /*
507 * Goto next sibling
508 */
509 if (curr_cgroup->sibling.next != &curr_cgroup->parent->children) {
510 mem_cgroup_put(curr);
511 cgroup = list_entry(curr_cgroup->sibling.next, struct cgroup,
512 sibling);
513 curr = mem_cgroup_from_cont(cgroup);
514 mem_cgroup_get(curr);
515 goto done;
516 }
517
518 /*
519 * Go up to next parent and next parent's sibling if need be
520 */
521 curr_cgroup = curr_cgroup->parent;
522 goto visit_parent;
523
524done:
525 root_mem->last_scanned_child = curr;
526 return curr;
527}
528
529/*
530 * Visit the first child (need not be the first child as per the ordering
531 * of the cgroup list, since we track last_scanned_child) of @mem and use
532 * that to reclaim free pages from.
533 */
534static struct mem_cgroup *
535mem_cgroup_get_first_node(struct mem_cgroup *root_mem)
536{
537 struct cgroup *cgroup;
538 struct mem_cgroup *ret;
539 bool obsolete = (root_mem->last_scanned_child &&
540 root_mem->last_scanned_child->obsolete);
541
542 /*
543 * Scan all children under the mem_cgroup mem
544 */
545 cgroup_lock();
546 if (list_empty(&root_mem->css.cgroup->children)) {
547 ret = root_mem;
548 goto done;
549 }
550
551 if (!root_mem->last_scanned_child || obsolete) {
552
553 if (obsolete)
554 mem_cgroup_put(root_mem->last_scanned_child);
555
556 cgroup = list_first_entry(&root_mem->css.cgroup->children,
557 struct cgroup, sibling);
558 ret = mem_cgroup_from_cont(cgroup);
559 mem_cgroup_get(ret);
560 } else
561 ret = mem_cgroup_get_next_node(root_mem->last_scanned_child,
562 root_mem);
563
564done:
565 root_mem->last_scanned_child = ret;
566 cgroup_unlock();
567 return ret;
568}
569
570/*
571 * Dance down the hierarchy if needed to reclaim memory. We remember the
572 * last child we reclaimed from, so that we don't end up penalizing
573 * one child extensively based on its position in the children list.
574 *
575 * root_mem is the original ancestor that we've been reclaim from.
576 */
577static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
578 gfp_t gfp_mask, bool noswap)
579{
580 struct mem_cgroup *next_mem;
581 int ret = 0;
582
583 /*
584 * Reclaim unconditionally and don't check for return value.
585 * We need to reclaim in the current group and down the tree.
586 * One might think about checking for children before reclaiming,
587 * but there might be left over accounting, even after children
588 * have left.
589 */
590 ret = try_to_free_mem_cgroup_pages(root_mem, gfp_mask, noswap);
591 if (res_counter_check_under_limit(&root_mem->res))
592 return 0;
593
594 next_mem = mem_cgroup_get_first_node(root_mem);
595
596 while (next_mem != root_mem) {
597 if (next_mem->obsolete) {
598 mem_cgroup_put(next_mem);
599 cgroup_lock();
600 next_mem = mem_cgroup_get_first_node(root_mem);
601 cgroup_unlock();
602 continue;
603 }
604 ret = try_to_free_mem_cgroup_pages(next_mem, gfp_mask, noswap);
605 if (res_counter_check_under_limit(&root_mem->res))
606 return 0;
607 cgroup_lock();
608 next_mem = mem_cgroup_get_next_node(next_mem, root_mem);
609 cgroup_unlock();
610 }
611 return ret;
612}
613
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800614/*
615 * Unlike exported interface, "oom" parameter is added. if oom==true,
616 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800617 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800618static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800619 gfp_t gfp_mask, struct mem_cgroup **memcg,
620 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800621{
Balbir Singh6d61ef42009-01-07 18:08:06 -0800622 struct mem_cgroup *mem, *mem_over_limit;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800623 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800624 struct res_counter *fail_res;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800625 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800626 * We always charge the cgroup the mm_struct belongs to.
627 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800628 * thread group leader migrates. It's possible that mm is not
629 * set, if so charge the init_mm (happens for pagecache usage).
630 */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800631 if (likely(!*memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700632 rcu_read_lock();
633 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100634 if (unlikely(!mem)) {
635 rcu_read_unlock();
Balbir Singh31a78f22008-09-28 23:09:31 +0100636 return 0;
637 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700638 /*
639 * For every charge from the cgroup, increment reference count
640 */
641 css_get(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800642 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700643 rcu_read_unlock();
644 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800645 mem = *memcg;
646 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700647 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800648
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800649 while (1) {
650 int ret;
651 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800652
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800653 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800654 if (likely(!ret)) {
655 if (!do_swap_account)
656 break;
Balbir Singh28dbc4b2009-01-07 18:08:05 -0800657 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
658 &fail_res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800659 if (likely(!ret))
660 break;
661 /* mem+swap counter fails */
662 res_counter_uncharge(&mem->res, PAGE_SIZE);
663 noswap = true;
Balbir Singh6d61ef42009-01-07 18:08:06 -0800664 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
665 memsw);
666 } else
667 /* mem counter fails */
668 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
669 res);
670
Hugh Dickins3be912772008-02-07 00:14:19 -0800671 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800672 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800673
Balbir Singh6d61ef42009-01-07 18:08:06 -0800674 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
675 noswap);
Balbir Singh66e17072008-02-07 00:13:56 -0800676
677 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800678 * try_to_free_mem_cgroup_pages() might not give us a full
679 * picture of reclaim. Some pages are reclaimed and might be
680 * moved to swap cache or just unmapped from the cgroup.
681 * Check the limit again to see if the reclaim reduced the
682 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800683 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800684 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800685 if (!do_swap_account &&
686 res_counter_check_under_limit(&mem->res))
687 continue;
688 if (do_swap_account &&
689 res_counter_check_under_limit(&mem->memsw))
Balbir Singh66e17072008-02-07 00:13:56 -0800690 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -0800691
692 if (!nr_retries--) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800693 if (oom)
694 mem_cgroup_out_of_memory(mem, gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800695 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800696 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800697 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800698 return 0;
699nomem:
700 css_put(&mem->css);
701 return -ENOMEM;
702}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800703
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800704/**
705 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
706 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
707 * @gfp_mask: gfp_mask for reclaim.
708 * @memcg: a pointer to memory cgroup which is charged against.
709 *
710 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
711 * memory cgroup from @mm is got and stored in *memcg.
712 *
713 * Returns 0 if success. -ENOMEM at failure.
714 * This call can invoke OOM-Killer.
715 */
716
717int mem_cgroup_try_charge(struct mm_struct *mm,
718 gfp_t mask, struct mem_cgroup **memcg)
719{
720 return __mem_cgroup_try_charge(mm, mask, memcg, true);
721}
722
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800723/*
724 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
725 * USED state. If already USED, uncharge and return.
726 */
727
728static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
729 struct page_cgroup *pc,
730 enum charge_type ctype)
731{
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800732 /* try_charge() can return NULL to *memcg, taking care of it. */
733 if (!mem)
734 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700735
736 lock_page_cgroup(pc);
737 if (unlikely(PageCgroupUsed(pc))) {
738 unlock_page_cgroup(pc);
739 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800740 if (do_swap_account)
741 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700742 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800743 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700744 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800745 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800746 smp_wmb();
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700747 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be912772008-02-07 00:14:19 -0800748
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800749 mem_cgroup_charge_statistics(mem, pc, true);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700750
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700751 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800752}
753
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800754/**
755 * mem_cgroup_move_account - move account of the page
756 * @pc: page_cgroup of the page.
757 * @from: mem_cgroup which the page is moved from.
758 * @to: mem_cgroup which the page is moved to. @from != @to.
759 *
760 * The caller must confirm following.
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800761 * - page is not on LRU (isolate_page() is useful.)
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800762 *
763 * returns 0 at success,
764 * returns -EBUSY when lock is busy or "pc" is unstable.
765 *
766 * This function does "uncharge" from old cgroup but doesn't do "charge" to
767 * new cgroup. It should be done by a caller.
768 */
769
770static int mem_cgroup_move_account(struct page_cgroup *pc,
771 struct mem_cgroup *from, struct mem_cgroup *to)
772{
773 struct mem_cgroup_per_zone *from_mz, *to_mz;
774 int nid, zid;
775 int ret = -EBUSY;
776
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800777 VM_BUG_ON(from == to);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800778 VM_BUG_ON(PageLRU(pc->page));
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800779
780 nid = page_cgroup_nid(pc);
781 zid = page_cgroup_zid(pc);
782 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
783 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
784
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800785 if (!trylock_page_cgroup(pc))
786 return ret;
787
788 if (!PageCgroupUsed(pc))
789 goto out;
790
791 if (pc->mem_cgroup != from)
792 goto out;
793
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800794 css_put(&from->css);
795 res_counter_uncharge(&from->res, PAGE_SIZE);
796 mem_cgroup_charge_statistics(from, pc, false);
797 if (do_swap_account)
798 res_counter_uncharge(&from->memsw, PAGE_SIZE);
799 pc->mem_cgroup = to;
800 mem_cgroup_charge_statistics(to, pc, true);
801 css_get(&to->css);
802 ret = 0;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800803out:
804 unlock_page_cgroup(pc);
805 return ret;
806}
807
808/*
809 * move charges to its parent.
810 */
811
812static int mem_cgroup_move_parent(struct page_cgroup *pc,
813 struct mem_cgroup *child,
814 gfp_t gfp_mask)
815{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800816 struct page *page = pc->page;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800817 struct cgroup *cg = child->css.cgroup;
818 struct cgroup *pcg = cg->parent;
819 struct mem_cgroup *parent;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800820 int ret;
821
822 /* Is ROOT ? */
823 if (!pcg)
824 return -EINVAL;
825
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800826
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800827 parent = mem_cgroup_from_cont(pcg);
828
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800829
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800830 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
831 if (ret)
832 return ret;
833
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800834 if (!get_page_unless_zero(page))
835 return -EBUSY;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800836
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800837 ret = isolate_lru_page(page);
838
839 if (ret)
840 goto cancel;
841
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800842 ret = mem_cgroup_move_account(pc, child, parent);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800843
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800844 /* drop extra refcnt by try_charge() (move_account increment one) */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800845 css_put(&parent->css);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800846 putback_lru_page(page);
847 if (!ret) {
848 put_page(page);
849 return 0;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800850 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -0800851 /* uncharge if move fails */
852cancel:
853 res_counter_uncharge(&parent->res, PAGE_SIZE);
854 if (do_swap_account)
855 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
856 put_page(page);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800857 return ret;
858}
859
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800860/*
861 * Charge the memory controller for page usage.
862 * Return
863 * 0 if the charge was successful
864 * < 0 if the cgroup is over its limit
865 */
866static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
867 gfp_t gfp_mask, enum charge_type ctype,
868 struct mem_cgroup *memcg)
869{
870 struct mem_cgroup *mem;
871 struct page_cgroup *pc;
872 int ret;
873
874 pc = lookup_page_cgroup(page);
875 /* can happen at boot */
876 if (unlikely(!pc))
877 return 0;
878 prefetchw(pc);
879
880 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800881 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800882 if (ret)
883 return ret;
884
885 __mem_cgroup_commit_charge(mem, pc, ctype);
886 return 0;
887}
888
889int mem_cgroup_newpage_charge(struct page *page,
890 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800891{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800892 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -0700893 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700894 if (PageCompound(page))
895 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700896 /*
897 * If already mapped, we don't have to account.
898 * If page cache, page->mapping has address_space.
899 * But page->mapping may have out-of-use anon_vma pointer,
900 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
901 * is NULL.
902 */
903 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
904 return 0;
905 if (unlikely(!mm))
906 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800907 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700908 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800909}
910
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800911int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
912 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800913{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800914 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -0700915 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700916 if (PageCompound(page))
917 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700918 /*
919 * Corner case handling. This is called from add_to_page_cache()
920 * in usual. But some FS (shmem) precharges this page before calling it
921 * and call add_to_page_cache() with GFP_NOWAIT.
922 *
923 * For GFP_NOWAIT case, the page may be pre-charged before calling
924 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
925 * charge twice. (It works but has to pay a bit larger cost.)
926 */
927 if (!(gfp_mask & __GFP_WAIT)) {
928 struct page_cgroup *pc;
929
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700930
931 pc = lookup_page_cgroup(page);
932 if (!pc)
933 return 0;
934 lock_page_cgroup(pc);
935 if (PageCgroupUsed(pc)) {
936 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700937 return 0;
938 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700939 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700940 }
941
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700942 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800943 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700944
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700945 if (page_is_file_cache(page))
946 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700947 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700948 else
949 return mem_cgroup_charge_common(page, mm, gfp_mask,
950 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700951}
952
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800953int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
954 struct page *page,
955 gfp_t mask, struct mem_cgroup **ptr)
956{
957 struct mem_cgroup *mem;
958 swp_entry_t ent;
959
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800960 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800961 return 0;
962
963 if (!do_swap_account)
964 goto charge_cur_mm;
965
966 /*
967 * A racing thread's fault, or swapoff, may have already updated
968 * the pte, and even removed page from swap cache: return success
969 * to go on to do_swap_page()'s pte_same() test, which should fail.
970 */
971 if (!PageSwapCache(page))
972 return 0;
973
974 ent.val = page_private(page);
975
976 mem = lookup_swap_cgroup(ent);
977 if (!mem || mem->obsolete)
978 goto charge_cur_mm;
979 *ptr = mem;
980 return __mem_cgroup_try_charge(NULL, mask, ptr, true);
981charge_cur_mm:
982 if (unlikely(!mm))
983 mm = &init_mm;
984 return __mem_cgroup_try_charge(mm, mask, ptr, true);
985}
986
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800987#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800988
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800989int mem_cgroup_cache_charge_swapin(struct page *page,
990 struct mm_struct *mm, gfp_t mask, bool locked)
991{
992 int ret = 0;
993
Hirokazu Takahashif8d665422009-01-07 18:08:02 -0800994 if (mem_cgroup_disabled())
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800995 return 0;
996 if (unlikely(!mm))
997 mm = &init_mm;
998 if (!locked)
999 lock_page(page);
1000 /*
1001 * If not locked, the page can be dropped from SwapCache until
1002 * we reach here.
1003 */
1004 if (PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001005 struct mem_cgroup *mem = NULL;
1006 swp_entry_t ent;
1007
1008 ent.val = page_private(page);
1009 if (do_swap_account) {
1010 mem = lookup_swap_cgroup(ent);
1011 if (mem && mem->obsolete)
1012 mem = NULL;
1013 if (mem)
1014 mm = NULL;
1015 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001016 ret = mem_cgroup_charge_common(page, mm, mask,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001017 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1018
1019 if (!ret && do_swap_account) {
1020 /* avoid double counting */
1021 mem = swap_cgroup_record(ent, NULL);
1022 if (mem) {
1023 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1024 mem_cgroup_put(mem);
1025 }
1026 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001027 }
1028 if (!locked)
1029 unlock_page(page);
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001030 /* add this page(page_cgroup) to the LRU we want. */
1031 mem_cgroup_lru_fixup(page);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001032
1033 return ret;
1034}
1035#endif
1036
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001037void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1038{
1039 struct page_cgroup *pc;
1040
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001041 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001042 return;
1043 if (!ptr)
1044 return;
1045 pc = lookup_page_cgroup(page);
1046 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001047 /*
1048 * Now swap is on-memory. This means this page may be
1049 * counted both as mem and swap....double count.
1050 * Fix it by uncharging from memsw. This SwapCache is stable
1051 * because we're still under lock_page().
1052 */
1053 if (do_swap_account) {
1054 swp_entry_t ent = {.val = page_private(page)};
1055 struct mem_cgroup *memcg;
1056 memcg = swap_cgroup_record(ent, NULL);
1057 if (memcg) {
1058 /* If memcg is obsolete, memcg can be != ptr */
1059 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1060 mem_cgroup_put(memcg);
1061 }
1062
1063 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001064 /* add this page(page_cgroup) to the LRU we want. */
1065 mem_cgroup_lru_fixup(page);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001066}
1067
1068void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1069{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001070 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001071 return;
1072 if (!mem)
1073 return;
1074 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001075 if (do_swap_account)
1076 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -08001077 css_put(&mem->css);
1078}
1079
1080
Balbir Singh8697d332008-02-07 00:13:59 -08001081/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001082 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001083 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001084static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001085__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001086{
Hugh Dickins82895462008-03-04 14:29:08 -08001087 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001088 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001089 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001090
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001091 if (mem_cgroup_disabled())
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001092 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -07001093
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001094 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001095 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001096
Balbir Singh8697d332008-02-07 00:13:59 -08001097 /*
Balbir Singh3c541e12008-02-07 00:14:41 -08001098 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -08001099 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001100 pc = lookup_page_cgroup(page);
1101 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001102 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001103
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001104 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001105
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001106 mem = pc->mem_cgroup;
1107
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001108 if (!PageCgroupUsed(pc))
1109 goto unlock_out;
1110
1111 switch (ctype) {
1112 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1113 if (page_mapped(page))
1114 goto unlock_out;
1115 break;
1116 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1117 if (!PageAnon(page)) { /* Shared memory */
1118 if (page->mapping && !page_is_file_cache(page))
1119 goto unlock_out;
1120 } else if (page_mapped(page)) /* Anon */
1121 goto unlock_out;
1122 break;
1123 default:
1124 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001125 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001126
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001127 res_counter_uncharge(&mem->res, PAGE_SIZE);
1128 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1129 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1130
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001131 mem_cgroup_charge_statistics(mem, pc, false);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001132 ClearPageCgroupUsed(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001133
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001134 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001135 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001136
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001137 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001138
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001139 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001140
1141unlock_out:
1142 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001143 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001144}
1145
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001146void mem_cgroup_uncharge_page(struct page *page)
1147{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001148 /* early check. */
1149 if (page_mapped(page))
1150 return;
1151 if (page->mapping && !PageAnon(page))
1152 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001153 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1154}
1155
1156void mem_cgroup_uncharge_cache_page(struct page *page)
1157{
1158 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001159 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001160 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1161}
1162
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001163/*
1164 * called from __delete_from_swap_cache() and drop "page" account.
1165 * memcg information is recorded to swap_cgroup of "ent"
1166 */
1167void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001168{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001169 struct mem_cgroup *memcg;
1170
1171 memcg = __mem_cgroup_uncharge_common(page,
1172 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1173 /* record memcg information */
1174 if (do_swap_account && memcg) {
1175 swap_cgroup_record(ent, memcg);
1176 mem_cgroup_get(memcg);
1177 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001178}
1179
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001180#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1181/*
1182 * called from swap_entry_free(). remove record in swap_cgroup and
1183 * uncharge "memsw" account.
1184 */
1185void mem_cgroup_uncharge_swap(swp_entry_t ent)
1186{
1187 struct mem_cgroup *memcg;
1188
1189 if (!do_swap_account)
1190 return;
1191
1192 memcg = swap_cgroup_record(ent, NULL);
1193 if (memcg) {
1194 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1195 mem_cgroup_put(memcg);
1196 }
1197}
1198#endif
1199
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001200/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001201 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1202 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001203 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001204int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001205{
1206 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001207 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001208 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001209
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001210 if (mem_cgroup_disabled())
Balbir Singh40779602008-04-04 14:29:59 -07001211 return 0;
1212
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001213 pc = lookup_page_cgroup(page);
1214 lock_page_cgroup(pc);
1215 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001216 mem = pc->mem_cgroup;
1217 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001218 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001219 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001220
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001221 if (mem) {
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001222 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001223 css_put(&mem->css);
1224 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001225 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001226 return ret;
1227}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001228
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001229/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001230void mem_cgroup_end_migration(struct mem_cgroup *mem,
1231 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001232{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001233 struct page *target, *unused;
1234 struct page_cgroup *pc;
1235 enum charge_type ctype;
1236
1237 if (!mem)
1238 return;
1239
1240 /* at migration success, oldpage->mapping is NULL. */
1241 if (oldpage->mapping) {
1242 target = oldpage;
1243 unused = NULL;
1244 } else {
1245 target = newpage;
1246 unused = oldpage;
1247 }
1248
1249 if (PageAnon(target))
1250 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1251 else if (page_is_file_cache(target))
1252 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1253 else
1254 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1255
1256 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001257 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001258 __mem_cgroup_uncharge_common(unused, ctype);
1259
1260 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001261 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001262 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1263 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001264 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001265 __mem_cgroup_commit_charge(mem, pc, ctype);
1266
1267 /*
1268 * Both of oldpage and newpage are still under lock_page().
1269 * Then, we don't have to care about race in radix-tree.
1270 * But we have to be careful that this page is unmapped or not.
1271 *
1272 * There is a case for !page_mapped(). At the start of
1273 * migration, oldpage was mapped. But now, it's zapped.
1274 * But we know *target* page is not freed/reused under us.
1275 * mem_cgroup_uncharge_page() does all necessary checks.
1276 */
1277 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1278 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001279}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001280
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001281/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001282 * A call to try to shrink memory usage under specified resource controller.
1283 * This is typically used for page reclaiming for shmem for reducing side
1284 * effect of page allocation from shmem, which is used by some mem_cgroup.
1285 */
1286int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1287{
1288 struct mem_cgroup *mem;
1289 int progress = 0;
1290 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1291
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001292 if (mem_cgroup_disabled())
Li Zefancede86a2008-07-25 01:47:18 -07001293 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -07001294 if (!mm)
1295 return 0;
Li Zefancede86a2008-07-25 01:47:18 -07001296
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001297 rcu_read_lock();
1298 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +01001299 if (unlikely(!mem)) {
1300 rcu_read_unlock();
1301 return 0;
1302 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001303 css_get(&mem->css);
1304 rcu_read_unlock();
1305
1306 do {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001307 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -07001308 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001309 } while (!progress && --retry);
1310
1311 css_put(&mem->css);
1312 if (!retry)
1313 return -ENOMEM;
1314 return 0;
1315}
1316
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001317static DEFINE_MUTEX(set_limit_mutex);
1318
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001319static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001320 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001321{
1322
1323 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1324 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001325 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001326 int ret = 0;
1327
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001328 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001329 if (signal_pending(current)) {
1330 ret = -EINTR;
1331 break;
1332 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001333 /*
1334 * Rather than hide all in some function, I do this in
1335 * open coded manner. You see what this really does.
1336 * We have to guarantee mem->res.limit < mem->memsw.limit.
1337 */
1338 mutex_lock(&set_limit_mutex);
1339 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1340 if (memswlimit < val) {
1341 ret = -EINVAL;
1342 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001343 break;
1344 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001345 ret = res_counter_set_limit(&memcg->res, val);
1346 mutex_unlock(&set_limit_mutex);
1347
1348 if (!ret)
1349 break;
1350
KAMEZAWA Hiroyukibced0522009-01-07 18:07:49 -08001351 progress = try_to_free_mem_cgroup_pages(memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001352 GFP_HIGHUSER_MOVABLE, false);
1353 if (!progress) retry_count--;
1354 }
1355 return ret;
1356}
1357
1358int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1359 unsigned long long val)
1360{
1361 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1362 u64 memlimit, oldusage, curusage;
1363 int ret;
1364
1365 if (!do_swap_account)
1366 return -EINVAL;
1367
1368 while (retry_count) {
1369 if (signal_pending(current)) {
1370 ret = -EINTR;
1371 break;
1372 }
1373 /*
1374 * Rather than hide all in some function, I do this in
1375 * open coded manner. You see what this really does.
1376 * We have to guarantee mem->res.limit < mem->memsw.limit.
1377 */
1378 mutex_lock(&set_limit_mutex);
1379 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1380 if (memlimit > val) {
1381 ret = -EINVAL;
1382 mutex_unlock(&set_limit_mutex);
1383 break;
1384 }
1385 ret = res_counter_set_limit(&memcg->memsw, val);
1386 mutex_unlock(&set_limit_mutex);
1387
1388 if (!ret)
1389 break;
1390
1391 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1392 try_to_free_mem_cgroup_pages(memcg, GFP_HIGHUSER_MOVABLE, true);
1393 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1394 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001395 retry_count--;
1396 }
1397 return ret;
1398}
1399
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001400/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001401 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001402 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1403 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001404static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001405 int node, int zid, enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001406{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001407 struct zone *zone;
1408 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001409 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001410 unsigned long flags, loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001411 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001412 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001413
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001414 zone = &NODE_DATA(node)->node_zones[zid];
1415 mz = mem_cgroup_zoneinfo(mem, node, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001416 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001417
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001418 loop = MEM_CGROUP_ZSTAT(mz, lru);
1419 /* give some margin against EBUSY etc...*/
1420 loop += 256;
1421 busy = NULL;
1422 while (loop--) {
1423 ret = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001424 spin_lock_irqsave(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001425 if (list_empty(list)) {
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001426 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001427 break;
1428 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001429 pc = list_entry(list->prev, struct page_cgroup, lru);
1430 if (busy == pc) {
1431 list_move(&pc->lru, list);
1432 busy = 0;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001433 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001434 continue;
1435 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001436 spin_unlock_irqrestore(&zone->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001437
1438 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1439 if (ret == -ENOMEM)
1440 break;
1441
1442 if (ret == -EBUSY || ret == -EINVAL) {
1443 /* found lock contention or "pc" is obsolete. */
1444 busy = pc;
1445 cond_resched();
1446 } else
1447 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001448 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001449
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001450 if (!ret && !list_empty(list))
1451 return -EBUSY;
1452 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001453}
1454
1455/*
1456 * make mem_cgroup's charge to be 0 if there is no task.
1457 * This enables deleting this mem_cgroup.
1458 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001459static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001460{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001461 int ret;
1462 int node, zid, shrink;
1463 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001464 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001465
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001466 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001467
1468 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001469 /* should free all ? */
1470 if (free_all)
1471 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001472move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001473 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001474 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001475 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001476 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001477 ret = -EINTR;
1478 if (signal_pending(current))
1479 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001480 /* This is for making all *used* pages to be on LRU. */
1481 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001482 ret = 0;
1483 for_each_node_state(node, N_POSSIBLE) {
1484 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
Christoph Lameterb69408e2008-10-18 20:26:14 -07001485 enum lru_list l;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001486 for_each_lru(l) {
1487 ret = mem_cgroup_force_empty_list(mem,
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001488 node, zid, l);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001489 if (ret)
1490 break;
1491 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001492 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001493 if (ret)
1494 break;
1495 }
1496 /* it seems parent cgroup doesn't have enough mem */
1497 if (ret == -ENOMEM)
1498 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001499 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001500 }
1501 ret = 0;
1502out:
1503 css_put(&mem->css);
1504 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001505
1506try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001507 /* returns EBUSY if there is a task or if we come here twice. */
1508 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001509 ret = -EBUSY;
1510 goto out;
1511 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001512 /* we call try-to-free pages for make this cgroup empty */
1513 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001514 /* try to free all pages in this cgroup */
1515 shrink = 1;
1516 while (nr_retries && mem->res.usage > 0) {
1517 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001518
1519 if (signal_pending(current)) {
1520 ret = -EINTR;
1521 goto out;
1522 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001523 progress = try_to_free_mem_cgroup_pages(mem,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001524 GFP_HIGHUSER_MOVABLE, false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001525 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001526 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001527 /* maybe some writeback is necessary */
1528 congestion_wait(WRITE, HZ/10);
1529 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001530
1531 }
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001532 lru_add_drain();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001533 /* try move_account...there may be some *locked* pages. */
1534 if (mem->res.usage)
1535 goto move_account;
1536 ret = 0;
1537 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001538}
1539
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001540int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1541{
1542 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1543}
1544
1545
Paul Menage2c3daa72008-04-29 00:59:58 -07001546static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001547{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001548 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1549 u64 val = 0;
1550 int type, name;
1551
1552 type = MEMFILE_TYPE(cft->private);
1553 name = MEMFILE_ATTR(cft->private);
1554 switch (type) {
1555 case _MEM:
1556 val = res_counter_read_u64(&mem->res, name);
1557 break;
1558 case _MEMSWAP:
1559 if (do_swap_account)
1560 val = res_counter_read_u64(&mem->memsw, name);
1561 break;
1562 default:
1563 BUG();
1564 break;
1565 }
1566 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001567}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001568/*
1569 * The user of this function is...
1570 * RES_LIMIT.
1571 */
Paul Menage856c13a2008-07-25 01:47:04 -07001572static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1573 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001574{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001575 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001576 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001577 unsigned long long val;
1578 int ret;
1579
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001580 type = MEMFILE_TYPE(cft->private);
1581 name = MEMFILE_ATTR(cft->private);
1582 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001583 case RES_LIMIT:
1584 /* This function does all necessary parse...reuse it */
1585 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001586 if (ret)
1587 break;
1588 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001589 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001590 else
1591 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001592 break;
1593 default:
1594 ret = -EINVAL; /* should be BUG() ? */
1595 break;
1596 }
1597 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001598}
1599
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001600static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001601{
1602 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001603 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001604
1605 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001606 type = MEMFILE_TYPE(event);
1607 name = MEMFILE_ATTR(event);
1608 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001609 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001610 if (type == _MEM)
1611 res_counter_reset_max(&mem->res);
1612 else
1613 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001614 break;
1615 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001616 if (type == _MEM)
1617 res_counter_reset_failcnt(&mem->res);
1618 else
1619 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001620 break;
1621 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001622 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001623}
1624
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001625static const struct mem_cgroup_stat_desc {
1626 const char *msg;
1627 u64 unit;
1628} mem_cgroup_stat_desc[] = {
1629 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1630 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -07001631 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1632 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001633};
1634
Paul Menagec64745c2008-04-29 01:00:02 -07001635static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1636 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001637{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001638 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1639 struct mem_cgroup_stat *stat = &mem_cont->stat;
1640 int i;
1641
1642 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1643 s64 val;
1644
1645 val = mem_cgroup_read_stat(stat, i);
1646 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001647 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001648 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001649 /* showing # of active pages */
1650 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001651 unsigned long active_anon, inactive_anon;
1652 unsigned long active_file, inactive_file;
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001653 unsigned long unevictable;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001654
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001655 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1656 LRU_INACTIVE_ANON);
1657 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1658 LRU_ACTIVE_ANON);
1659 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1660 LRU_INACTIVE_FILE);
1661 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1662 LRU_ACTIVE_FILE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001663 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1664 LRU_UNEVICTABLE);
1665
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001666 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1667 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1668 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1669 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001670 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1671
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001672 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001673 return 0;
1674}
1675
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001676
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001677static struct cftype mem_cgroup_files[] = {
1678 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001679 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001680 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07001681 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001682 },
1683 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001684 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001685 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001686 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001687 .read_u64 = mem_cgroup_read,
1688 },
1689 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001690 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001691 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07001692 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001693 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001694 },
1695 {
1696 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001697 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001698 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001699 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001700 },
Balbir Singh8697d332008-02-07 00:13:59 -08001701 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001702 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001703 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001704 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001705 {
1706 .name = "force_empty",
1707 .trigger = mem_cgroup_force_empty_write,
1708 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001709};
1710
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001711#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1712static struct cftype memsw_cgroup_files[] = {
1713 {
1714 .name = "memsw.usage_in_bytes",
1715 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1716 .read_u64 = mem_cgroup_read,
1717 },
1718 {
1719 .name = "memsw.max_usage_in_bytes",
1720 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1721 .trigger = mem_cgroup_reset,
1722 .read_u64 = mem_cgroup_read,
1723 },
1724 {
1725 .name = "memsw.limit_in_bytes",
1726 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1727 .write_string = mem_cgroup_write,
1728 .read_u64 = mem_cgroup_read,
1729 },
1730 {
1731 .name = "memsw.failcnt",
1732 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1733 .trigger = mem_cgroup_reset,
1734 .read_u64 = mem_cgroup_read,
1735 },
1736};
1737
1738static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1739{
1740 if (!do_swap_account)
1741 return 0;
1742 return cgroup_add_files(cont, ss, memsw_cgroup_files,
1743 ARRAY_SIZE(memsw_cgroup_files));
1744};
1745#else
1746static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1747{
1748 return 0;
1749}
1750#endif
1751
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001752static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1753{
1754 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001755 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001756 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001757 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001758 /*
1759 * This routine is called against possible nodes.
1760 * But it's BUG to call kmalloc() against offline node.
1761 *
1762 * TODO: this routine can waste much memory for nodes which will
1763 * never be onlined. It's better to use memory hotplug callback
1764 * function.
1765 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001766 if (!node_state(node, N_NORMAL_MEMORY))
1767 tmp = -1;
1768 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001769 if (!pn)
1770 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001771
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001772 mem->info.nodeinfo[node] = pn;
1773 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001774
1775 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1776 mz = &pn->zoneinfo[zone];
Christoph Lameterb69408e2008-10-18 20:26:14 -07001777 for_each_lru(l)
1778 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001779 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001780 return 0;
1781}
1782
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001783static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1784{
1785 kfree(mem->info.nodeinfo[node]);
1786}
1787
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001788static int mem_cgroup_size(void)
1789{
1790 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1791 return sizeof(struct mem_cgroup) + cpustat_size;
1792}
1793
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001794static struct mem_cgroup *mem_cgroup_alloc(void)
1795{
1796 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001797 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001798
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001799 if (size < PAGE_SIZE)
1800 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001801 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001802 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001803
1804 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001805 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001806 return mem;
1807}
1808
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001809/*
1810 * At destroying mem_cgroup, references from swap_cgroup can remain.
1811 * (scanning all at force_empty is too costly...)
1812 *
1813 * Instead of clearing all references at force_empty, we remember
1814 * the number of reference from swap_cgroup and free mem_cgroup when
1815 * it goes down to 0.
1816 *
1817 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1818 * entry which points to this memcg will be ignore at swapin.
1819 *
1820 * Removal of cgroup itself succeeds regardless of refs from swap.
1821 */
1822
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001823static void mem_cgroup_free(struct mem_cgroup *mem)
1824{
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001825 int node;
1826
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001827 if (atomic_read(&mem->refcnt) > 0)
1828 return;
KAMEZAWA Hiroyuki08e552c2009-01-07 18:08:01 -08001829
1830
1831 for_each_node_state(node, N_POSSIBLE)
1832 free_mem_cgroup_per_zone_info(mem, node);
1833
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001834 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001835 kfree(mem);
1836 else
1837 vfree(mem);
1838}
1839
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001840static void mem_cgroup_get(struct mem_cgroup *mem)
1841{
1842 atomic_inc(&mem->refcnt);
1843}
1844
1845static void mem_cgroup_put(struct mem_cgroup *mem)
1846{
1847 if (atomic_dec_and_test(&mem->refcnt)) {
1848 if (!mem->obsolete)
1849 return;
1850 mem_cgroup_free(mem);
1851 }
1852}
1853
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001854
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001855#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1856static void __init enable_swap_cgroup(void)
1857{
Hirokazu Takahashif8d665422009-01-07 18:08:02 -08001858 if (!mem_cgroup_disabled() && really_do_swap_account)
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001859 do_swap_account = 1;
1860}
1861#else
1862static void __init enable_swap_cgroup(void)
1863{
1864}
1865#endif
1866
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001867static struct cgroup_subsys_state *
1868mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1869{
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001870 struct mem_cgroup *mem, *parent;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001871 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001872
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001873 mem = mem_cgroup_alloc();
1874 if (!mem)
1875 return ERR_PTR(-ENOMEM);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001876
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001877 for_each_node_state(node, N_POSSIBLE)
1878 if (alloc_mem_cgroup_per_zone_info(mem, node))
1879 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001880 /* root ? */
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001881 if (cont->parent == NULL) {
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001882 enable_swap_cgroup();
Balbir Singh28dbc4b2009-01-07 18:08:05 -08001883 parent = NULL;
1884 } else
1885 parent = mem_cgroup_from_cont(cont->parent);
1886
1887 res_counter_init(&mem->res, parent ? &parent->res : NULL);
1888 res_counter_init(&mem->memsw, parent ? &parent->memsw : NULL);
1889
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001890
Balbir Singh6d61ef42009-01-07 18:08:06 -08001891 mem->last_scanned_child = NULL;
1892
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001893 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001894free_out:
1895 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001896 free_mem_cgroup_per_zone_info(mem, node);
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001897 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001898 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001899}
1900
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001901static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1902 struct cgroup *cont)
1903{
1904 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001905 mem->obsolete = 1;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001906 mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001907}
1908
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001909static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1910 struct cgroup *cont)
1911{
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001912 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001913}
1914
1915static int mem_cgroup_populate(struct cgroup_subsys *ss,
1916 struct cgroup *cont)
1917{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001918 int ret;
1919
1920 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
1921 ARRAY_SIZE(mem_cgroup_files));
1922
1923 if (!ret)
1924 ret = register_memsw_files(cont, ss);
1925 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001926}
1927
Balbir Singh67e465a2008-02-07 00:13:54 -08001928static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1929 struct cgroup *cont,
1930 struct cgroup *old_cont,
1931 struct task_struct *p)
1932{
1933 struct mm_struct *mm;
1934 struct mem_cgroup *mem, *old_mem;
1935
1936 mm = get_task_mm(p);
1937 if (mm == NULL)
1938 return;
1939
1940 mem = mem_cgroup_from_cont(cont);
1941 old_mem = mem_cgroup_from_cont(old_cont);
1942
Balbir Singh67e465a2008-02-07 00:13:54 -08001943 /*
1944 * Only thread group leaders are allowed to migrate, the mm_struct is
1945 * in effect owned by the leader
1946 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001947 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001948 goto out;
1949
Balbir Singh67e465a2008-02-07 00:13:54 -08001950out:
1951 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001952}
1953
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001954struct cgroup_subsys mem_cgroup_subsys = {
1955 .name = "memory",
1956 .subsys_id = mem_cgroup_subsys_id,
1957 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001958 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001959 .destroy = mem_cgroup_destroy,
1960 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001961 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001962 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001963};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001964
1965#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1966
1967static int __init disable_swap_account(char *s)
1968{
1969 really_do_swap_account = 0;
1970 return 1;
1971}
1972__setup("noswapaccount", disable_swap_account);
1973#endif