blob: 7288e9d85ca78f65cf084536b01da91dbbbfa594 [file] [log] [blame]
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
Pavel Emelianov78fb7462008-02-07 00:13:51 -08006 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
Balbir Singh8cdea7c2008-02-07 00:13:50 -08009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 */
19
20#include <linux/res_counter.h>
21#include <linux/memcontrol.h>
22#include <linux/cgroup.h>
Pavel Emelianov78fb7462008-02-07 00:13:51 -080023#include <linux/mm.h>
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -080024#include <linux/pagemap.h>
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080025#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080027#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080028#include <linux/bit_spinlock.h>
29#include <linux/rcupdate.h>
Balbir Singhb6ac57d2008-04-29 01:00:19 -070030#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080031#include <linux/swap.h>
32#include <linux/spinlock.h>
33#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080034#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070035#include <linux/vmalloc.h>
Christoph Lameterb69408e2008-10-18 20:26:14 -070036#include <linux/mm_inline.h>
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -070037#include <linux/page_cgroup.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080038
Balbir Singh8697d332008-02-07 00:13:59 -080039#include <asm/uaccess.h>
40
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070041struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070042#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080043
44/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080045 * Statistics for memory cgroup.
46 */
47enum mem_cgroup_stat_index {
48 /*
49 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
50 */
51 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
52 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070053 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
54 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080055
56 MEM_CGROUP_STAT_NSTATS,
57};
58
59struct mem_cgroup_stat_cpu {
60 s64 count[MEM_CGROUP_STAT_NSTATS];
61} ____cacheline_aligned_in_smp;
62
63struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080064 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080065};
66
67/*
68 * For accounting under irq disable, no need for increment preempt count.
69 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070070static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080071 enum mem_cgroup_stat_index idx, int val)
72{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070073 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080074}
75
76static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
77 enum mem_cgroup_stat_index idx)
78{
79 int cpu;
80 s64 ret = 0;
81 for_each_possible_cpu(cpu)
82 ret += stat->cpustat[cpu].count[idx];
83 return ret;
84}
85
86/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080087 * per-zone information in memory controller.
88 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080089struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -080090 /*
91 * spin_lock to protect the per cgroup LRU
92 */
93 spinlock_t lru_lock;
Christoph Lameterb69408e2008-10-18 20:26:14 -070094 struct list_head lists[NR_LRU_LISTS];
95 unsigned long count[NR_LRU_LISTS];
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080096};
97/* Macro for accessing counter */
98#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
99
100struct mem_cgroup_per_node {
101 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
102};
103
104struct mem_cgroup_lru_info {
105 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
106};
107
108/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800109 * The memory controller data structure. The memory controller controls both
110 * page cache and RSS per cgroup. We would eventually like to provide
111 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
112 * to help the administrator determine what knobs to tune.
113 *
114 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800115 * we hit the water mark. May be even add a low water mark, such that
116 * no reclaim occurs from a cgroup at it's low water mark, this is
117 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800118 */
119struct mem_cgroup {
120 struct cgroup_subsys_state css;
121 /*
122 * the counter to account for memory usage
123 */
124 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800125 /*
126 * Per cgroup active and inactive list, similar to the
127 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800128 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800129 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800130
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800131 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800132 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800133 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800134 */
135 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800136};
137
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800138enum charge_type {
139 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
140 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700141 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700142 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800143 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700144 NR_CHARGE_TYPE,
145};
146
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700147/* only for here (for easy reading.) */
148#define PCGF_CACHE (1UL << PCG_CACHE)
149#define PCGF_USED (1UL << PCG_USED)
150#define PCGF_ACTIVE (1UL << PCG_ACTIVE)
151#define PCGF_LOCK (1UL << PCG_LOCK)
152#define PCGF_FILE (1UL << PCG_FILE)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700153static const unsigned long
154pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700155 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
156 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
157 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
158 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800159};
160
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800161/*
162 * Always modified under lru lock. Then, not necessary to preempt_disable()
163 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700164static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
165 struct page_cgroup *pc,
166 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800167{
168 int val = (charge)? 1 : -1;
169 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700170 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800171
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800172 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700173
174 cpustat = &stat->cpustat[smp_processor_id()];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700175 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700176 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800177 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700178 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700179
180 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700181 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700182 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
183 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700184 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700185 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800186}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800187
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800188static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800189mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
190{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800191 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
192}
193
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800194static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800195page_cgroup_zoneinfo(struct page_cgroup *pc)
196{
197 struct mem_cgroup *mem = pc->mem_cgroup;
198 int nid = page_cgroup_nid(pc);
199 int zid = page_cgroup_zid(pc);
200
201 return mem_cgroup_zoneinfo(mem, nid, zid);
202}
203
204static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700205 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800206{
207 int nid, zid;
208 struct mem_cgroup_per_zone *mz;
209 u64 total = 0;
210
211 for_each_online_node(nid)
212 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
213 mz = mem_cgroup_zoneinfo(mem, nid, zid);
214 total += MEM_CGROUP_ZSTAT(mz, idx);
215 }
216 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800217}
218
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800219static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800220{
221 return container_of(cgroup_subsys_state(cont,
222 mem_cgroup_subsys_id), struct mem_cgroup,
223 css);
224}
225
Balbir Singhcf475ad2008-04-29 01:00:16 -0700226struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800227{
Balbir Singh31a78f22008-09-28 23:09:31 +0100228 /*
229 * mm_update_next_owner() may clear mm->owner to NULL
230 * if it races with swapoff, page migration, etc.
231 * So this can be called with p == NULL.
232 */
233 if (unlikely(!p))
234 return NULL;
235
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800236 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
237 struct mem_cgroup, css);
238}
239
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700240static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
241 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800242{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700243 int lru = LRU_BASE;
244
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700245 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700246 lru = LRU_UNEVICTABLE;
247 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700248 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700249 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700250 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700251 lru += LRU_FILE;
252 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800253
Christoph Lameterb69408e2008-10-18 20:26:14 -0700254 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800255
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700256 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700257 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800258}
259
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700260static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800261 struct page_cgroup *pc, bool hot)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800262{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700263 int lru = LRU_BASE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800264
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700265 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700266 lru = LRU_UNEVICTABLE;
267 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700268 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700269 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700270 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700271 lru += LRU_FILE;
272 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700273
274 MEM_CGROUP_ZSTAT(mz, lru) += 1;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800275 if (hot)
276 list_add(&pc->lru, &mz->lists[lru]);
277 else
278 list_add_tail(&pc->lru, &mz->lists[lru]);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700279
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700280 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800281}
282
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700283static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800284{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800285 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700286 int active = PageCgroupActive(pc);
287 int file = PageCgroupFile(pc);
288 int unevictable = PageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700289 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
290 (LRU_FILE * !!file + !!active);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800291
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700292 if (lru == from)
293 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700294
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700295 MEM_CGROUP_ZSTAT(mz, from) -= 1;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700296 /*
297 * However this is done under mz->lru_lock, another flags, which
298 * are not related to LRU, will be modified from out-of-lock.
299 * We have to use atomic set/clear flags.
300 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700301 if (is_unevictable_lru(lru)) {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700302 ClearPageCgroupActive(pc);
303 SetPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700304 } else {
305 if (is_active_lru(lru))
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700306 SetPageCgroupActive(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700307 else
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700308 ClearPageCgroupActive(pc);
309 ClearPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700310 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700311
Christoph Lameterb69408e2008-10-18 20:26:14 -0700312 MEM_CGROUP_ZSTAT(mz, lru) += 1;
313 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800314}
315
David Rientjes4c4a2212008-02-07 00:14:06 -0800316int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
317{
318 int ret;
319
320 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800321 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800322 task_unlock(task);
323 return ret;
324}
325
Balbir Singh66e17072008-02-07 00:13:56 -0800326/*
327 * This routine assumes that the appropriate zone's lru lock is already held
328 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700329void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800330{
Hugh Dickins427d5412008-03-04 14:29:03 -0800331 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800332 struct mem_cgroup_per_zone *mz;
333 unsigned long flags;
334
Li Zefancede86a2008-07-25 01:47:18 -0700335 if (mem_cgroup_subsys.disabled)
336 return;
337
Hugh Dickins2680eed2008-03-04 14:29:13 -0800338 /*
339 * We cannot lock_page_cgroup while holding zone's lru_lock,
340 * because other holders of lock_page_cgroup can be interrupted
341 * with an attempt to rotate_reclaimable_page. But we cannot
342 * safely get to page_cgroup without it, so just try_lock it:
343 * mem_cgroup_isolate_pages allows for page left on wrong list.
344 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700345 pc = lookup_page_cgroup(page);
346 if (!trylock_page_cgroup(pc))
Balbir Singh66e17072008-02-07 00:13:56 -0800347 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700348 if (pc && PageCgroupUsed(pc)) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800349 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800350 spin_lock_irqsave(&mz->lru_lock, flags);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700351 __mem_cgroup_move_lists(pc, lru);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800352 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800353 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700354 unlock_page_cgroup(pc);
Balbir Singh66e17072008-02-07 00:13:56 -0800355}
356
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800357/*
358 * Calculate mapped_ratio under memory controller. This will be used in
359 * vmscan.c for deteremining we have to reclaim mapped pages.
360 */
361int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
362{
363 long total, rss;
364
365 /*
366 * usage is recorded in bytes. But, here, we assume the number of
367 * physical pages can be represented by "long" on any arch.
368 */
369 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
370 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
371 return (int)((rss * 100L) / total);
372}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800373
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800374/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800375 * prev_priority control...this will be used in memory reclaim path.
376 */
377int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
378{
379 return mem->prev_priority;
380}
381
382void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
383{
384 if (priority < mem->prev_priority)
385 mem->prev_priority = priority;
386}
387
388void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
389{
390 mem->prev_priority = priority;
391}
392
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800393/*
394 * Calculate # of pages to be scanned in this priority/zone.
395 * See also vmscan.c
396 *
397 * priority starts from "DEF_PRIORITY" and decremented in each loop.
398 * (see include/linux/mmzone.h)
399 */
400
Christoph Lameterb69408e2008-10-18 20:26:14 -0700401long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
402 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800403{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700404 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800405 int nid = zone->zone_pgdat->node_id;
406 int zid = zone_idx(zone);
407 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
408
Christoph Lameterb69408e2008-10-18 20:26:14 -0700409 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800410
Christoph Lameterb69408e2008-10-18 20:26:14 -0700411 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800412}
413
Balbir Singh66e17072008-02-07 00:13:56 -0800414unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
415 struct list_head *dst,
416 unsigned long *scanned, int order,
417 int mode, struct zone *z,
418 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700419 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800420{
421 unsigned long nr_taken = 0;
422 struct page *page;
423 unsigned long scan;
424 LIST_HEAD(pc_list);
425 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800426 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800427 int nid = z->zone_pgdat->node_id;
428 int zid = zone_idx(z);
429 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700430 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800431
Balbir Singhcf475ad2008-04-29 01:00:16 -0700432 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800433 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700434 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800435
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800436 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800437 scan = 0;
438 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800439 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800440 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700441 if (unlikely(!PageCgroupUsed(pc)))
442 continue;
Balbir Singh66e17072008-02-07 00:13:56 -0800443 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800444
Hugh Dickins436c65412008-02-07 00:14:12 -0800445 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800446 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800447
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700448 /*
449 * TODO: play better with lumpy reclaim, grabbing anything.
450 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700451 if (PageUnevictable(page) ||
452 (PageActive(page) && !active) ||
453 (!PageActive(page) && active)) {
454 __mem_cgroup_move_lists(pc, page_lru(page));
Balbir Singh66e17072008-02-07 00:13:56 -0800455 continue;
456 }
457
Hugh Dickins436c65412008-02-07 00:14:12 -0800458 scan++;
459 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800460
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
467 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800468 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800469
470 *scanned = scan;
471 return nr_taken;
472}
473
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800474/*
475 * Unlike exported interface, "oom" parameter is added. if oom==true,
476 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800477 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800478static int __mem_cgroup_try_charge(struct mm_struct *mm,
479 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800480{
481 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800482 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800483 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800484 * We always charge the cgroup the mm_struct belongs to.
485 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800486 * thread group leader migrates. It's possible that mm is not
487 * set, if so charge the init_mm (happens for pagecache usage).
488 */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800489 if (likely(!*memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700490 rcu_read_lock();
491 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100492 if (unlikely(!mem)) {
493 rcu_read_unlock();
Balbir Singh31a78f22008-09-28 23:09:31 +0100494 return 0;
495 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700496 /*
497 * For every charge from the cgroup, increment reference count
498 */
499 css_get(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800500 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700501 rcu_read_unlock();
502 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800503 mem = *memcg;
504 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700505 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800506
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800507
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700508 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
Hugh Dickins3be912772008-02-07 00:14:19 -0800509 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800510 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800511
512 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800513 continue;
514
515 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800516 * try_to_free_mem_cgroup_pages() might not give us a full
517 * picture of reclaim. Some pages are reclaimed and might be
518 * moved to swap cache or just unmapped from the cgroup.
519 * Check the limit again to see if the reclaim reduced the
520 * current usage of the cgroup before giving up
521 */
Balbir Singh66e17072008-02-07 00:13:56 -0800522 if (res_counter_check_under_limit(&mem->res))
523 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -0800524
525 if (!nr_retries--) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800526 if (oom)
527 mem_cgroup_out_of_memory(mem, gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800528 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800529 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800530 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800531 return 0;
532nomem:
533 css_put(&mem->css);
534 return -ENOMEM;
535}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800536
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800537/**
538 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
539 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
540 * @gfp_mask: gfp_mask for reclaim.
541 * @memcg: a pointer to memory cgroup which is charged against.
542 *
543 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
544 * memory cgroup from @mm is got and stored in *memcg.
545 *
546 * Returns 0 if success. -ENOMEM at failure.
547 * This call can invoke OOM-Killer.
548 */
549
550int mem_cgroup_try_charge(struct mm_struct *mm,
551 gfp_t mask, struct mem_cgroup **memcg)
552{
553 return __mem_cgroup_try_charge(mm, mask, memcg, true);
554}
555
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800556/*
557 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
558 * USED state. If already USED, uncharge and return.
559 */
560
561static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
562 struct page_cgroup *pc,
563 enum charge_type ctype)
564{
565 struct mem_cgroup_per_zone *mz;
566 unsigned long flags;
567
568 /* try_charge() can return NULL to *memcg, taking care of it. */
569 if (!mem)
570 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700571
572 lock_page_cgroup(pc);
573 if (unlikely(PageCgroupUsed(pc))) {
574 unlock_page_cgroup(pc);
575 res_counter_uncharge(&mem->res, PAGE_SIZE);
576 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800577 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700578 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800579 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700580 /*
581 * If a page is accounted as a page cache, insert to inactive list.
582 * If anon, insert to active list.
583 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700584 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be912772008-02-07 00:14:19 -0800585
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800586 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700587
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800588 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800589 __mem_cgroup_add_list(mz, pc, true);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800590 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700591 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800592}
593
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800594/**
595 * mem_cgroup_move_account - move account of the page
596 * @pc: page_cgroup of the page.
597 * @from: mem_cgroup which the page is moved from.
598 * @to: mem_cgroup which the page is moved to. @from != @to.
599 *
600 * The caller must confirm following.
601 * 1. disable irq.
602 * 2. lru_lock of old mem_cgroup(@from) should be held.
603 *
604 * returns 0 at success,
605 * returns -EBUSY when lock is busy or "pc" is unstable.
606 *
607 * This function does "uncharge" from old cgroup but doesn't do "charge" to
608 * new cgroup. It should be done by a caller.
609 */
610
611static int mem_cgroup_move_account(struct page_cgroup *pc,
612 struct mem_cgroup *from, struct mem_cgroup *to)
613{
614 struct mem_cgroup_per_zone *from_mz, *to_mz;
615 int nid, zid;
616 int ret = -EBUSY;
617
618 VM_BUG_ON(!irqs_disabled());
619 VM_BUG_ON(from == to);
620
621 nid = page_cgroup_nid(pc);
622 zid = page_cgroup_zid(pc);
623 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
624 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
625
626
627 if (!trylock_page_cgroup(pc))
628 return ret;
629
630 if (!PageCgroupUsed(pc))
631 goto out;
632
633 if (pc->mem_cgroup != from)
634 goto out;
635
636 if (spin_trylock(&to_mz->lru_lock)) {
637 __mem_cgroup_remove_list(from_mz, pc);
638 css_put(&from->css);
639 res_counter_uncharge(&from->res, PAGE_SIZE);
640 pc->mem_cgroup = to;
641 css_get(&to->css);
642 __mem_cgroup_add_list(to_mz, pc, false);
643 ret = 0;
644 spin_unlock(&to_mz->lru_lock);
645 }
646out:
647 unlock_page_cgroup(pc);
648 return ret;
649}
650
651/*
652 * move charges to its parent.
653 */
654
655static int mem_cgroup_move_parent(struct page_cgroup *pc,
656 struct mem_cgroup *child,
657 gfp_t gfp_mask)
658{
659 struct cgroup *cg = child->css.cgroup;
660 struct cgroup *pcg = cg->parent;
661 struct mem_cgroup *parent;
662 struct mem_cgroup_per_zone *mz;
663 unsigned long flags;
664 int ret;
665
666 /* Is ROOT ? */
667 if (!pcg)
668 return -EINVAL;
669
670 parent = mem_cgroup_from_cont(pcg);
671
672 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
673 if (ret)
674 return ret;
675
676 mz = mem_cgroup_zoneinfo(child,
677 page_cgroup_nid(pc), page_cgroup_zid(pc));
678
679 spin_lock_irqsave(&mz->lru_lock, flags);
680 ret = mem_cgroup_move_account(pc, child, parent);
681 spin_unlock_irqrestore(&mz->lru_lock, flags);
682
683 /* drop extra refcnt */
684 css_put(&parent->css);
685 /* uncharge if move fails */
686 if (ret)
687 res_counter_uncharge(&parent->res, PAGE_SIZE);
688
689 return ret;
690}
691
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800692/*
693 * Charge the memory controller for page usage.
694 * Return
695 * 0 if the charge was successful
696 * < 0 if the cgroup is over its limit
697 */
698static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
699 gfp_t gfp_mask, enum charge_type ctype,
700 struct mem_cgroup *memcg)
701{
702 struct mem_cgroup *mem;
703 struct page_cgroup *pc;
704 int ret;
705
706 pc = lookup_page_cgroup(page);
707 /* can happen at boot */
708 if (unlikely(!pc))
709 return 0;
710 prefetchw(pc);
711
712 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800713 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800714 if (ret)
715 return ret;
716
717 __mem_cgroup_commit_charge(mem, pc, ctype);
718 return 0;
719}
720
721int mem_cgroup_newpage_charge(struct page *page,
722 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800723{
Li Zefancede86a2008-07-25 01:47:18 -0700724 if (mem_cgroup_subsys.disabled)
725 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700726 if (PageCompound(page))
727 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700728 /*
729 * If already mapped, we don't have to account.
730 * If page cache, page->mapping has address_space.
731 * But page->mapping may have out-of-use anon_vma pointer,
732 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
733 * is NULL.
734 */
735 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
736 return 0;
737 if (unlikely(!mm))
738 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800739 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700740 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800741}
742
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800743int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
744 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800745{
Li Zefancede86a2008-07-25 01:47:18 -0700746 if (mem_cgroup_subsys.disabled)
747 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700748 if (PageCompound(page))
749 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700750 /*
751 * Corner case handling. This is called from add_to_page_cache()
752 * in usual. But some FS (shmem) precharges this page before calling it
753 * and call add_to_page_cache() with GFP_NOWAIT.
754 *
755 * For GFP_NOWAIT case, the page may be pre-charged before calling
756 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
757 * charge twice. (It works but has to pay a bit larger cost.)
758 */
759 if (!(gfp_mask & __GFP_WAIT)) {
760 struct page_cgroup *pc;
761
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700762
763 pc = lookup_page_cgroup(page);
764 if (!pc)
765 return 0;
766 lock_page_cgroup(pc);
767 if (PageCgroupUsed(pc)) {
768 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700769 return 0;
770 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700771 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700772 }
773
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700774 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800775 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700776
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700777 if (page_is_file_cache(page))
778 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700779 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700780 else
781 return mem_cgroup_charge_common(page, mm, gfp_mask,
782 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700783}
784
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800785#ifdef CONFIG_SWAP
786int mem_cgroup_cache_charge_swapin(struct page *page,
787 struct mm_struct *mm, gfp_t mask, bool locked)
788{
789 int ret = 0;
790
791 if (mem_cgroup_subsys.disabled)
792 return 0;
793 if (unlikely(!mm))
794 mm = &init_mm;
795 if (!locked)
796 lock_page(page);
797 /*
798 * If not locked, the page can be dropped from SwapCache until
799 * we reach here.
800 */
801 if (PageSwapCache(page)) {
802 ret = mem_cgroup_charge_common(page, mm, mask,
803 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
804 }
805 if (!locked)
806 unlock_page(page);
807
808 return ret;
809}
810#endif
811
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800812void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
813{
814 struct page_cgroup *pc;
815
816 if (mem_cgroup_subsys.disabled)
817 return;
818 if (!ptr)
819 return;
820 pc = lookup_page_cgroup(page);
821 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
822}
823
824void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
825{
826 if (mem_cgroup_subsys.disabled)
827 return;
828 if (!mem)
829 return;
830 res_counter_uncharge(&mem->res, PAGE_SIZE);
831 css_put(&mem->css);
832}
833
834
Balbir Singh8697d332008-02-07 00:13:59 -0800835/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700836 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800837 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700838static void
839__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800840{
Hugh Dickins82895462008-03-04 14:29:08 -0800841 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800842 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800843 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800844 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800845
Balbir Singh40779602008-04-04 14:29:59 -0700846 if (mem_cgroup_subsys.disabled)
847 return;
848
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800849 if (PageSwapCache(page))
850 return;
851
Balbir Singh8697d332008-02-07 00:13:59 -0800852 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800853 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800854 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700855 pc = lookup_page_cgroup(page);
856 if (unlikely(!pc || !PageCgroupUsed(pc)))
857 return;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800858
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700859 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800860
861 if (!PageCgroupUsed(pc))
862 goto unlock_out;
863
864 switch (ctype) {
865 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
866 if (page_mapped(page))
867 goto unlock_out;
868 break;
869 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
870 if (!PageAnon(page)) { /* Shared memory */
871 if (page->mapping && !page_is_file_cache(page))
872 goto unlock_out;
873 } else if (page_mapped(page)) /* Anon */
874 goto unlock_out;
875 break;
876 default:
877 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700878 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800879
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700880 ClearPageCgroupUsed(pc);
881 mem = pc->mem_cgroup;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800882
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700883 mz = page_cgroup_zoneinfo(pc);
884 spin_lock_irqsave(&mz->lru_lock, flags);
885 __mem_cgroup_remove_list(mz, pc);
886 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700887 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800888
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700889 res_counter_uncharge(&mem->res, PAGE_SIZE);
890 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800891
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700892 return;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800893
894unlock_out:
895 unlock_page_cgroup(pc);
896 return;
Balbir Singh3c541e12008-02-07 00:14:41 -0800897}
898
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700899void mem_cgroup_uncharge_page(struct page *page)
900{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700901 /* early check. */
902 if (page_mapped(page))
903 return;
904 if (page->mapping && !PageAnon(page))
905 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700906 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
907}
908
909void mem_cgroup_uncharge_cache_page(struct page *page)
910{
911 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -0700912 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700913 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
914}
915
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800916void mem_cgroup_uncharge_swapcache(struct page *page)
917{
918 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
919}
920
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800921/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800922 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
923 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800924 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800925int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800926{
927 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700928 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700929 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800930
Balbir Singh40779602008-04-04 14:29:59 -0700931 if (mem_cgroup_subsys.disabled)
932 return 0;
933
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700934 pc = lookup_page_cgroup(page);
935 lock_page_cgroup(pc);
936 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700937 mem = pc->mem_cgroup;
938 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800939 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700940 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800941
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700942 if (mem) {
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800943 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700944 css_put(&mem->css);
945 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800946 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700947 return ret;
948}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800949
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700950/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800951void mem_cgroup_end_migration(struct mem_cgroup *mem,
952 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700953{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800954 struct page *target, *unused;
955 struct page_cgroup *pc;
956 enum charge_type ctype;
957
958 if (!mem)
959 return;
960
961 /* at migration success, oldpage->mapping is NULL. */
962 if (oldpage->mapping) {
963 target = oldpage;
964 unused = NULL;
965 } else {
966 target = newpage;
967 unused = oldpage;
968 }
969
970 if (PageAnon(target))
971 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
972 else if (page_is_file_cache(target))
973 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
974 else
975 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
976
977 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800978 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800979 __mem_cgroup_uncharge_common(unused, ctype);
980
981 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700982 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800983 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
984 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700985 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -0800986 __mem_cgroup_commit_charge(mem, pc, ctype);
987
988 /*
989 * Both of oldpage and newpage are still under lock_page().
990 * Then, we don't have to care about race in radix-tree.
991 * But we have to be careful that this page is unmapped or not.
992 *
993 * There is a case for !page_mapped(). At the start of
994 * migration, oldpage was mapped. But now, it's zapped.
995 * But we know *target* page is not freed/reused under us.
996 * mem_cgroup_uncharge_page() does all necessary checks.
997 */
998 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
999 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001000}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001001
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001002/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001003 * A call to try to shrink memory usage under specified resource controller.
1004 * This is typically used for page reclaiming for shmem for reducing side
1005 * effect of page allocation from shmem, which is used by some mem_cgroup.
1006 */
1007int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1008{
1009 struct mem_cgroup *mem;
1010 int progress = 0;
1011 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1012
Li Zefancede86a2008-07-25 01:47:18 -07001013 if (mem_cgroup_subsys.disabled)
1014 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -07001015 if (!mm)
1016 return 0;
Li Zefancede86a2008-07-25 01:47:18 -07001017
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001018 rcu_read_lock();
1019 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +01001020 if (unlikely(!mem)) {
1021 rcu_read_unlock();
1022 return 0;
1023 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001024 css_get(&mem->css);
1025 rcu_read_unlock();
1026
1027 do {
1028 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -07001029 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001030 } while (!progress && --retry);
1031
1032 css_put(&mem->css);
1033 if (!retry)
1034 return -ENOMEM;
1035 return 0;
1036}
1037
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001038static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1039 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001040{
1041
1042 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1043 int progress;
1044 int ret = 0;
1045
1046 while (res_counter_set_limit(&memcg->res, val)) {
1047 if (signal_pending(current)) {
1048 ret = -EINTR;
1049 break;
1050 }
1051 if (!retry_count) {
1052 ret = -EBUSY;
1053 break;
1054 }
KAMEZAWA Hiroyukibced0522009-01-07 18:07:49 -08001055 progress = try_to_free_mem_cgroup_pages(memcg,
1056 GFP_HIGHUSER_MOVABLE);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001057 if (!progress)
1058 retry_count--;
1059 }
1060 return ret;
1061}
1062
1063
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001064/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001065 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001066 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1067 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001068static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001069 struct mem_cgroup_per_zone *mz,
Christoph Lameterb69408e2008-10-18 20:26:14 -07001070 enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001071{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001072 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001073 unsigned long flags;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001074 unsigned long loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001075 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001076 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001077
Christoph Lameterb69408e2008-10-18 20:26:14 -07001078 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001079
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001080 loop = MEM_CGROUP_ZSTAT(mz, lru);
1081 /* give some margin against EBUSY etc...*/
1082 loop += 256;
1083 busy = NULL;
1084 while (loop--) {
1085 ret = 0;
1086 spin_lock_irqsave(&mz->lru_lock, flags);
1087 if (list_empty(list)) {
1088 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001089 break;
1090 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001091 pc = list_entry(list->prev, struct page_cgroup, lru);
1092 if (busy == pc) {
1093 list_move(&pc->lru, list);
1094 busy = 0;
1095 spin_unlock_irqrestore(&mz->lru_lock, flags);
1096 continue;
1097 }
1098 spin_unlock_irqrestore(&mz->lru_lock, flags);
1099
1100 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1101 if (ret == -ENOMEM)
1102 break;
1103
1104 if (ret == -EBUSY || ret == -EINVAL) {
1105 /* found lock contention or "pc" is obsolete. */
1106 busy = pc;
1107 cond_resched();
1108 } else
1109 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001110 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001111 if (!ret && !list_empty(list))
1112 return -EBUSY;
1113 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001114}
1115
1116/*
1117 * make mem_cgroup's charge to be 0 if there is no task.
1118 * This enables deleting this mem_cgroup.
1119 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001120static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001121{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001122 int ret;
1123 int node, zid, shrink;
1124 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001125 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001126
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001127 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001128
1129 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001130 /* should free all ? */
1131 if (free_all)
1132 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001133move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001134 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001135 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001136 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001137 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001138 ret = -EINTR;
1139 if (signal_pending(current))
1140 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001141 /* This is for making all *used* pages to be on LRU. */
1142 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001143 ret = 0;
1144 for_each_node_state(node, N_POSSIBLE) {
1145 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001146 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001147 enum lru_list l;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001148 mz = mem_cgroup_zoneinfo(mem, node, zid);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001149 for_each_lru(l) {
1150 ret = mem_cgroup_force_empty_list(mem,
1151 mz, l);
1152 if (ret)
1153 break;
1154 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001155 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001156 if (ret)
1157 break;
1158 }
1159 /* it seems parent cgroup doesn't have enough mem */
1160 if (ret == -ENOMEM)
1161 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001162 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001163 }
1164 ret = 0;
1165out:
1166 css_put(&mem->css);
1167 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001168
1169try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001170 /* returns EBUSY if there is a task or if we come here twice. */
1171 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001172 ret = -EBUSY;
1173 goto out;
1174 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001175 /* we call try-to-free pages for make this cgroup empty */
1176 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001177 /* try to free all pages in this cgroup */
1178 shrink = 1;
1179 while (nr_retries && mem->res.usage > 0) {
1180 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001181
1182 if (signal_pending(current)) {
1183 ret = -EINTR;
1184 goto out;
1185 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001186 progress = try_to_free_mem_cgroup_pages(mem,
1187 GFP_HIGHUSER_MOVABLE);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001188 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001189 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001190 /* maybe some writeback is necessary */
1191 congestion_wait(WRITE, HZ/10);
1192 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001193
1194 }
1195 /* try move_account...there may be some *locked* pages. */
1196 if (mem->res.usage)
1197 goto move_account;
1198 ret = 0;
1199 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001200}
1201
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001202int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1203{
1204 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1205}
1206
1207
Paul Menage2c3daa72008-04-29 00:59:58 -07001208static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001209{
Paul Menage2c3daa72008-04-29 00:59:58 -07001210 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
1211 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001212}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001213/*
1214 * The user of this function is...
1215 * RES_LIMIT.
1216 */
Paul Menage856c13a2008-07-25 01:47:04 -07001217static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1218 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001219{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001220 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
1221 unsigned long long val;
1222 int ret;
1223
1224 switch (cft->private) {
1225 case RES_LIMIT:
1226 /* This function does all necessary parse...reuse it */
1227 ret = res_counter_memparse_write_strategy(buffer, &val);
1228 if (!ret)
1229 ret = mem_cgroup_resize_limit(memcg, val);
1230 break;
1231 default:
1232 ret = -EINVAL; /* should be BUG() ? */
1233 break;
1234 }
1235 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001236}
1237
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001238static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001239{
1240 struct mem_cgroup *mem;
1241
1242 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001243 switch (event) {
1244 case RES_MAX_USAGE:
1245 res_counter_reset_max(&mem->res);
1246 break;
1247 case RES_FAILCNT:
1248 res_counter_reset_failcnt(&mem->res);
1249 break;
1250 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001251 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001252}
1253
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001254static const struct mem_cgroup_stat_desc {
1255 const char *msg;
1256 u64 unit;
1257} mem_cgroup_stat_desc[] = {
1258 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1259 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -07001260 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1261 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001262};
1263
Paul Menagec64745c2008-04-29 01:00:02 -07001264static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1265 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001266{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001267 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1268 struct mem_cgroup_stat *stat = &mem_cont->stat;
1269 int i;
1270
1271 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1272 s64 val;
1273
1274 val = mem_cgroup_read_stat(stat, i);
1275 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001276 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001277 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001278 /* showing # of active pages */
1279 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001280 unsigned long active_anon, inactive_anon;
1281 unsigned long active_file, inactive_file;
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001282 unsigned long unevictable;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001283
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001284 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1285 LRU_INACTIVE_ANON);
1286 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1287 LRU_ACTIVE_ANON);
1288 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1289 LRU_INACTIVE_FILE);
1290 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1291 LRU_ACTIVE_FILE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001292 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1293 LRU_UNEVICTABLE);
1294
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001295 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1296 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1297 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1298 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001299 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1300
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001301 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001302 return 0;
1303}
1304
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001305
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001306static struct cftype mem_cgroup_files[] = {
1307 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001308 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001309 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -07001310 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001311 },
1312 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001313 .name = "max_usage_in_bytes",
1314 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001315 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001316 .read_u64 = mem_cgroup_read,
1317 },
1318 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001319 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001320 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -07001321 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001322 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001323 },
1324 {
1325 .name = "failcnt",
1326 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001327 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001328 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001329 },
Balbir Singh8697d332008-02-07 00:13:59 -08001330 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001331 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001332 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001333 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001334 {
1335 .name = "force_empty",
1336 .trigger = mem_cgroup_force_empty_write,
1337 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001338};
1339
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001340static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1341{
1342 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001343 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001344 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001345 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001346 /*
1347 * This routine is called against possible nodes.
1348 * But it's BUG to call kmalloc() against offline node.
1349 *
1350 * TODO: this routine can waste much memory for nodes which will
1351 * never be onlined. It's better to use memory hotplug callback
1352 * function.
1353 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001354 if (!node_state(node, N_NORMAL_MEMORY))
1355 tmp = -1;
1356 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001357 if (!pn)
1358 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001359
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001360 mem->info.nodeinfo[node] = pn;
1361 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001362
1363 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1364 mz = &pn->zoneinfo[zone];
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001365 spin_lock_init(&mz->lru_lock);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001366 for_each_lru(l)
1367 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001368 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001369 return 0;
1370}
1371
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001372static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1373{
1374 kfree(mem->info.nodeinfo[node]);
1375}
1376
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001377static int mem_cgroup_size(void)
1378{
1379 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1380 return sizeof(struct mem_cgroup) + cpustat_size;
1381}
1382
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001383static struct mem_cgroup *mem_cgroup_alloc(void)
1384{
1385 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001386 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001387
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001388 if (size < PAGE_SIZE)
1389 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001390 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001391 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001392
1393 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001394 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001395 return mem;
1396}
1397
1398static void mem_cgroup_free(struct mem_cgroup *mem)
1399{
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001400 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001401 kfree(mem);
1402 else
1403 vfree(mem);
1404}
1405
1406
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001407static struct cgroup_subsys_state *
1408mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1409{
1410 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001411 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001412
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001413 mem = mem_cgroup_alloc();
1414 if (!mem)
1415 return ERR_PTR(-ENOMEM);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001416
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001417 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001418
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001419 for_each_node_state(node, N_POSSIBLE)
1420 if (alloc_mem_cgroup_per_zone_info(mem, node))
1421 goto free_out;
1422
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001423 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001424free_out:
1425 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001426 free_mem_cgroup_per_zone_info(mem, node);
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001427 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001428 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001429}
1430
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001431static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1432 struct cgroup *cont)
1433{
1434 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001435 mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001436}
1437
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001438static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1439 struct cgroup *cont)
1440{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001441 int node;
1442 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1443
1444 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001445 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001446
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001447 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001448}
1449
1450static int mem_cgroup_populate(struct cgroup_subsys *ss,
1451 struct cgroup *cont)
1452{
1453 return cgroup_add_files(cont, ss, mem_cgroup_files,
1454 ARRAY_SIZE(mem_cgroup_files));
1455}
1456
Balbir Singh67e465a2008-02-07 00:13:54 -08001457static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1458 struct cgroup *cont,
1459 struct cgroup *old_cont,
1460 struct task_struct *p)
1461{
1462 struct mm_struct *mm;
1463 struct mem_cgroup *mem, *old_mem;
1464
1465 mm = get_task_mm(p);
1466 if (mm == NULL)
1467 return;
1468
1469 mem = mem_cgroup_from_cont(cont);
1470 old_mem = mem_cgroup_from_cont(old_cont);
1471
Balbir Singh67e465a2008-02-07 00:13:54 -08001472 /*
1473 * Only thread group leaders are allowed to migrate, the mm_struct is
1474 * in effect owned by the leader
1475 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001476 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001477 goto out;
1478
Balbir Singh67e465a2008-02-07 00:13:54 -08001479out:
1480 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001481}
1482
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001483struct cgroup_subsys mem_cgroup_subsys = {
1484 .name = "memory",
1485 .subsys_id = mem_cgroup_subsys_id,
1486 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001487 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001488 .destroy = mem_cgroup_destroy,
1489 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001490 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001491 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001492};