blob: 2efcf38f3b73b18ef6040b607ad956c404c0d5b0 [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>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080039
Balbir Singh8697d332008-02-07 00:13:59 -080040#include <asm/uaccess.h>
41
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070042struct cgroup_subsys mem_cgroup_subsys __read_mostly;
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070043#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080044
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -080045#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
46/* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
47int do_swap_account __read_mostly;
48static int really_do_swap_account __initdata = 1; /* for remember boot option*/
49#else
50#define do_swap_account (0)
51#endif
52
53
Balbir Singh8cdea7c2008-02-07 00:13:50 -080054/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080055 * Statistics for memory cgroup.
56 */
57enum mem_cgroup_stat_index {
58 /*
59 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
60 */
61 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
62 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070063 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
64 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080065
66 MEM_CGROUP_STAT_NSTATS,
67};
68
69struct mem_cgroup_stat_cpu {
70 s64 count[MEM_CGROUP_STAT_NSTATS];
71} ____cacheline_aligned_in_smp;
72
73struct mem_cgroup_stat {
Jan Blunckc8dad2b2009-01-07 18:07:53 -080074 struct mem_cgroup_stat_cpu cpustat[0];
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080075};
76
77/*
78 * For accounting under irq disable, no need for increment preempt count.
79 */
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070080static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080081 enum mem_cgroup_stat_index idx, int val)
82{
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -070083 stat->count[idx] += val;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080084}
85
86static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
87 enum mem_cgroup_stat_index idx)
88{
89 int cpu;
90 s64 ret = 0;
91 for_each_possible_cpu(cpu)
92 ret += stat->cpustat[cpu].count[idx];
93 return ret;
94}
95
96/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080097 * per-zone information in memory controller.
98 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080099struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800100 /*
101 * spin_lock to protect the per cgroup LRU
102 */
103 spinlock_t lru_lock;
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 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800146 int obsolete;
147 atomic_t refcnt;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800148 /*
Jan Blunckc8dad2b2009-01-07 18:07:53 -0800149 * statistics. This must be placed at the end of memcg.
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800150 */
151 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800152};
153
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800154enum charge_type {
155 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
156 MEM_CGROUP_CHARGE_TYPE_MAPPED,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700157 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700158 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800159 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700160 NR_CHARGE_TYPE,
161};
162
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700163/* only for here (for easy reading.) */
164#define PCGF_CACHE (1UL << PCG_CACHE)
165#define PCGF_USED (1UL << PCG_USED)
166#define PCGF_ACTIVE (1UL << PCG_ACTIVE)
167#define PCGF_LOCK (1UL << PCG_LOCK)
168#define PCGF_FILE (1UL << PCG_FILE)
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700169static const unsigned long
170pcg_default_flags[NR_CHARGE_TYPE] = {
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700171 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
172 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
173 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
174 0, /* FORCE */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800175};
176
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800177
178/* for encoding cft->private value on file */
179#define _MEM (0)
180#define _MEMSWAP (1)
181#define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
182#define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
183#define MEMFILE_ATTR(val) ((val) & 0xffff)
184
185static void mem_cgroup_get(struct mem_cgroup *mem);
186static void mem_cgroup_put(struct mem_cgroup *mem);
187
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800188/*
189 * Always modified under lru lock. Then, not necessary to preempt_disable()
190 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700191static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
192 struct page_cgroup *pc,
193 bool charge)
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800194{
195 int val = (charge)? 1 : -1;
196 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700197 struct mem_cgroup_stat_cpu *cpustat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800198
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800199 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700200
201 cpustat = &stat->cpustat[smp_processor_id()];
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700202 if (PageCgroupCache(pc))
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700203 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800204 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700205 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700206
207 if (charge)
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700208 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700209 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
210 else
KAMEZAWA Hiroyukiaddb9ef2008-10-18 20:28:10 -0700211 __mem_cgroup_stat_add_safe(cpustat,
Balaji Rao55e462b2008-05-01 04:35:12 -0700212 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800213}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800214
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800215static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800216mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
217{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800218 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
219}
220
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800221static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800222page_cgroup_zoneinfo(struct page_cgroup *pc)
223{
224 struct mem_cgroup *mem = pc->mem_cgroup;
225 int nid = page_cgroup_nid(pc);
226 int zid = page_cgroup_zid(pc);
227
228 return mem_cgroup_zoneinfo(mem, nid, zid);
229}
230
231static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
Christoph Lameterb69408e2008-10-18 20:26:14 -0700232 enum lru_list idx)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800233{
234 int nid, zid;
235 struct mem_cgroup_per_zone *mz;
236 u64 total = 0;
237
238 for_each_online_node(nid)
239 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
240 mz = mem_cgroup_zoneinfo(mem, nid, zid);
241 total += MEM_CGROUP_ZSTAT(mz, idx);
242 }
243 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800244}
245
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800246static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800247{
248 return container_of(cgroup_subsys_state(cont,
249 mem_cgroup_subsys_id), struct mem_cgroup,
250 css);
251}
252
Balbir Singhcf475ad2008-04-29 01:00:16 -0700253struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800254{
Balbir Singh31a78f22008-09-28 23:09:31 +0100255 /*
256 * mm_update_next_owner() may clear mm->owner to NULL
257 * if it races with swapoff, page migration, etc.
258 * So this can be called with p == NULL.
259 */
260 if (unlikely(!p))
261 return NULL;
262
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800263 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
264 struct mem_cgroup, css);
265}
266
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700267static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
268 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800269{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700270 int lru = LRU_BASE;
271
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700272 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700273 lru = LRU_UNEVICTABLE;
274 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700275 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700276 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700277 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700278 lru += LRU_FILE;
279 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800280
Christoph Lameterb69408e2008-10-18 20:26:14 -0700281 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800282
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700283 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700284 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800285}
286
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700287static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800288 struct page_cgroup *pc, bool hot)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800289{
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700290 int lru = LRU_BASE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800291
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700292 if (PageCgroupUnevictable(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700293 lru = LRU_UNEVICTABLE;
294 else {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700295 if (PageCgroupActive(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700296 lru += LRU_ACTIVE;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700297 if (PageCgroupFile(pc))
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700298 lru += LRU_FILE;
299 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700300
301 MEM_CGROUP_ZSTAT(mz, lru) += 1;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800302 if (hot)
303 list_add(&pc->lru, &mz->lists[lru]);
304 else
305 list_add_tail(&pc->lru, &mz->lists[lru]);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700306
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700307 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800308}
309
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700310static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800311{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800312 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700313 int active = PageCgroupActive(pc);
314 int file = PageCgroupFile(pc);
315 int unevictable = PageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700316 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
317 (LRU_FILE * !!file + !!active);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800318
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700319 if (lru == from)
320 return;
Christoph Lameterb69408e2008-10-18 20:26:14 -0700321
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700322 MEM_CGROUP_ZSTAT(mz, from) -= 1;
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700323 /*
324 * However this is done under mz->lru_lock, another flags, which
325 * are not related to LRU, will be modified from out-of-lock.
326 * We have to use atomic set/clear flags.
327 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700328 if (is_unevictable_lru(lru)) {
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700329 ClearPageCgroupActive(pc);
330 SetPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700331 } else {
332 if (is_active_lru(lru))
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700333 SetPageCgroupActive(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700334 else
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700335 ClearPageCgroupActive(pc);
336 ClearPageCgroupUnevictable(pc);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700337 }
Christoph Lameterb69408e2008-10-18 20:26:14 -0700338
Christoph Lameterb69408e2008-10-18 20:26:14 -0700339 MEM_CGROUP_ZSTAT(mz, lru) += 1;
340 list_move(&pc->lru, &mz->lists[lru]);
Balbir Singh66e17072008-02-07 00:13:56 -0800341}
342
David Rientjes4c4a2212008-02-07 00:14:06 -0800343int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
344{
345 int ret;
346
347 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800348 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800349 task_unlock(task);
350 return ret;
351}
352
Balbir Singh66e17072008-02-07 00:13:56 -0800353/*
354 * This routine assumes that the appropriate zone's lru lock is already held
355 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700356void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
Balbir Singh66e17072008-02-07 00:13:56 -0800357{
Hugh Dickins427d5412008-03-04 14:29:03 -0800358 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800359 struct mem_cgroup_per_zone *mz;
360 unsigned long flags;
361
Li Zefancede86a2008-07-25 01:47:18 -0700362 if (mem_cgroup_subsys.disabled)
363 return;
364
Hugh Dickins2680eed2008-03-04 14:29:13 -0800365 /*
366 * We cannot lock_page_cgroup while holding zone's lru_lock,
367 * because other holders of lock_page_cgroup can be interrupted
368 * with an attempt to rotate_reclaimable_page. But we cannot
369 * safely get to page_cgroup without it, so just try_lock it:
370 * mem_cgroup_isolate_pages allows for page left on wrong list.
371 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700372 pc = lookup_page_cgroup(page);
373 if (!trylock_page_cgroup(pc))
Balbir Singh66e17072008-02-07 00:13:56 -0800374 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700375 if (pc && PageCgroupUsed(pc)) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800376 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800377 spin_lock_irqsave(&mz->lru_lock, flags);
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700378 __mem_cgroup_move_lists(pc, lru);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800379 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800380 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700381 unlock_page_cgroup(pc);
Balbir Singh66e17072008-02-07 00:13:56 -0800382}
383
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800384/*
385 * Calculate mapped_ratio under memory controller. This will be used in
386 * vmscan.c for deteremining we have to reclaim mapped pages.
387 */
388int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
389{
390 long total, rss;
391
392 /*
393 * usage is recorded in bytes. But, here, we assume the number of
394 * physical pages can be represented by "long" on any arch.
395 */
396 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
397 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
398 return (int)((rss * 100L) / total);
399}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800400
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800401/*
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800402 * prev_priority control...this will be used in memory reclaim path.
403 */
404int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
405{
406 return mem->prev_priority;
407}
408
409void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
410{
411 if (priority < mem->prev_priority)
412 mem->prev_priority = priority;
413}
414
415void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
416{
417 mem->prev_priority = priority;
418}
419
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800420/*
421 * Calculate # of pages to be scanned in this priority/zone.
422 * See also vmscan.c
423 *
424 * priority starts from "DEF_PRIORITY" and decremented in each loop.
425 * (see include/linux/mmzone.h)
426 */
427
Christoph Lameterb69408e2008-10-18 20:26:14 -0700428long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
429 int priority, enum lru_list lru)
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800430{
Christoph Lameterb69408e2008-10-18 20:26:14 -0700431 long nr_pages;
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800432 int nid = zone->zone_pgdat->node_id;
433 int zid = zone_idx(zone);
434 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
435
Christoph Lameterb69408e2008-10-18 20:26:14 -0700436 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800437
Christoph Lameterb69408e2008-10-18 20:26:14 -0700438 return (nr_pages >> priority);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800439}
440
Balbir Singh66e17072008-02-07 00:13:56 -0800441unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
442 struct list_head *dst,
443 unsigned long *scanned, int order,
444 int mode, struct zone *z,
445 struct mem_cgroup *mem_cont,
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700446 int active, int file)
Balbir Singh66e17072008-02-07 00:13:56 -0800447{
448 unsigned long nr_taken = 0;
449 struct page *page;
450 unsigned long scan;
451 LIST_HEAD(pc_list);
452 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800453 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800454 int nid = z->zone_pgdat->node_id;
455 int zid = zone_idx(z);
456 struct mem_cgroup_per_zone *mz;
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700457 int lru = LRU_FILE * !!file + !!active;
Balbir Singh66e17072008-02-07 00:13:56 -0800458
Balbir Singhcf475ad2008-04-29 01:00:16 -0700459 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800460 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Christoph Lameterb69408e2008-10-18 20:26:14 -0700461 src = &mz->lists[lru];
Balbir Singh66e17072008-02-07 00:13:56 -0800462
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800463 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800464 scan = 0;
465 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800466 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800467 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700468 if (unlikely(!PageCgroupUsed(pc)))
469 continue;
Balbir Singh66e17072008-02-07 00:13:56 -0800470 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800471
Hugh Dickins436c65412008-02-07 00:14:12 -0800472 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800473 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800474
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700475 /*
476 * TODO: play better with lumpy reclaim, grabbing anything.
477 */
Lee Schermerhorn894bc312008-10-18 20:26:39 -0700478 if (PageUnevictable(page) ||
479 (PageActive(page) && !active) ||
480 (!PageActive(page) && active)) {
481 __mem_cgroup_move_lists(pc, page_lru(page));
Balbir Singh66e17072008-02-07 00:13:56 -0800482 continue;
483 }
484
Hugh Dickins436c65412008-02-07 00:14:12 -0800485 scan++;
486 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800487
Rik van Riel4f98a2f2008-10-18 20:26:32 -0700488 if (__isolate_lru_page(page, mode, file) == 0) {
Balbir Singh66e17072008-02-07 00:13:56 -0800489 list_move(&page->lru, dst);
490 nr_taken++;
491 }
492 }
493
494 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800495 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800496
497 *scanned = scan;
498 return nr_taken;
499}
500
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800501/*
502 * Unlike exported interface, "oom" parameter is added. if oom==true,
503 * oom-killer can be invoked.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800504 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800505static int __mem_cgroup_try_charge(struct mm_struct *mm,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800506 gfp_t gfp_mask, struct mem_cgroup **memcg,
507 bool oom)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800508{
509 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800510 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800511 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800512 * We always charge the cgroup the mm_struct belongs to.
513 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800514 * thread group leader migrates. It's possible that mm is not
515 * set, if so charge the init_mm (happens for pagecache usage).
516 */
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800517 if (likely(!*memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700518 rcu_read_lock();
519 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +0100520 if (unlikely(!mem)) {
521 rcu_read_unlock();
Balbir Singh31a78f22008-09-28 23:09:31 +0100522 return 0;
523 }
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700524 /*
525 * For every charge from the cgroup, increment reference count
526 */
527 css_get(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800528 *memcg = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700529 rcu_read_unlock();
530 } else {
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800531 mem = *memcg;
532 css_get(&mem->css);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700533 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800534
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800535 while (1) {
536 int ret;
537 bool noswap = false;
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800538
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800539 ret = res_counter_charge(&mem->res, PAGE_SIZE);
540 if (likely(!ret)) {
541 if (!do_swap_account)
542 break;
543 ret = res_counter_charge(&mem->memsw, PAGE_SIZE);
544 if (likely(!ret))
545 break;
546 /* mem+swap counter fails */
547 res_counter_uncharge(&mem->res, PAGE_SIZE);
548 noswap = true;
549 }
Hugh Dickins3be912772008-02-07 00:14:19 -0800550 if (!(gfp_mask & __GFP_WAIT))
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800551 goto nomem;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800552
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800553 if (try_to_free_mem_cgroup_pages(mem, gfp_mask, noswap))
Balbir Singh66e17072008-02-07 00:13:56 -0800554 continue;
555
556 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800557 * try_to_free_mem_cgroup_pages() might not give us a full
558 * picture of reclaim. Some pages are reclaimed and might be
559 * moved to swap cache or just unmapped from the cgroup.
560 * Check the limit again to see if the reclaim reduced the
561 * current usage of the cgroup before giving up
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800562 *
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800563 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800564 if (!do_swap_account &&
565 res_counter_check_under_limit(&mem->res))
566 continue;
567 if (do_swap_account &&
568 res_counter_check_under_limit(&mem->memsw))
Balbir Singh66e17072008-02-07 00:13:56 -0800569 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -0800570
571 if (!nr_retries--) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800572 if (oom)
573 mem_cgroup_out_of_memory(mem, gfp_mask);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800574 goto nomem;
Balbir Singh66e17072008-02-07 00:13:56 -0800575 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800576 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800577 return 0;
578nomem:
579 css_put(&mem->css);
580 return -ENOMEM;
581}
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800582
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800583/**
584 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
585 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
586 * @gfp_mask: gfp_mask for reclaim.
587 * @memcg: a pointer to memory cgroup which is charged against.
588 *
589 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
590 * memory cgroup from @mm is got and stored in *memcg.
591 *
592 * Returns 0 if success. -ENOMEM at failure.
593 * This call can invoke OOM-Killer.
594 */
595
596int mem_cgroup_try_charge(struct mm_struct *mm,
597 gfp_t mask, struct mem_cgroup **memcg)
598{
599 return __mem_cgroup_try_charge(mm, mask, memcg, true);
600}
601
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800602/*
603 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
604 * USED state. If already USED, uncharge and return.
605 */
606
607static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
608 struct page_cgroup *pc,
609 enum charge_type ctype)
610{
611 struct mem_cgroup_per_zone *mz;
612 unsigned long flags;
613
614 /* try_charge() can return NULL to *memcg, taking care of it. */
615 if (!mem)
616 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700617
618 lock_page_cgroup(pc);
619 if (unlikely(PageCgroupUsed(pc))) {
620 unlock_page_cgroup(pc);
621 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800622 if (do_swap_account)
623 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700624 css_put(&mem->css);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800625 return;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700626 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800627 pc->mem_cgroup = mem;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700628 /*
629 * If a page is accounted as a page cache, insert to inactive list.
630 * If anon, insert to active list.
631 */
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700632 pc->flags = pcg_default_flags[ctype];
Hugh Dickins3be912772008-02-07 00:14:19 -0800633
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800634 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700635
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800636 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800637 __mem_cgroup_add_list(mz, pc, true);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800638 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700639 unlock_page_cgroup(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800640}
641
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800642/**
643 * mem_cgroup_move_account - move account of the page
644 * @pc: page_cgroup of the page.
645 * @from: mem_cgroup which the page is moved from.
646 * @to: mem_cgroup which the page is moved to. @from != @to.
647 *
648 * The caller must confirm following.
649 * 1. disable irq.
650 * 2. lru_lock of old mem_cgroup(@from) should be held.
651 *
652 * returns 0 at success,
653 * returns -EBUSY when lock is busy or "pc" is unstable.
654 *
655 * This function does "uncharge" from old cgroup but doesn't do "charge" to
656 * new cgroup. It should be done by a caller.
657 */
658
659static int mem_cgroup_move_account(struct page_cgroup *pc,
660 struct mem_cgroup *from, struct mem_cgroup *to)
661{
662 struct mem_cgroup_per_zone *from_mz, *to_mz;
663 int nid, zid;
664 int ret = -EBUSY;
665
666 VM_BUG_ON(!irqs_disabled());
667 VM_BUG_ON(from == to);
668
669 nid = page_cgroup_nid(pc);
670 zid = page_cgroup_zid(pc);
671 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
672 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
673
674
675 if (!trylock_page_cgroup(pc))
676 return ret;
677
678 if (!PageCgroupUsed(pc))
679 goto out;
680
681 if (pc->mem_cgroup != from)
682 goto out;
683
684 if (spin_trylock(&to_mz->lru_lock)) {
685 __mem_cgroup_remove_list(from_mz, pc);
686 css_put(&from->css);
687 res_counter_uncharge(&from->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800688 if (do_swap_account)
689 res_counter_uncharge(&from->memsw, PAGE_SIZE);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800690 pc->mem_cgroup = to;
691 css_get(&to->css);
692 __mem_cgroup_add_list(to_mz, pc, false);
693 ret = 0;
694 spin_unlock(&to_mz->lru_lock);
695 }
696out:
697 unlock_page_cgroup(pc);
698 return ret;
699}
700
701/*
702 * move charges to its parent.
703 */
704
705static int mem_cgroup_move_parent(struct page_cgroup *pc,
706 struct mem_cgroup *child,
707 gfp_t gfp_mask)
708{
709 struct cgroup *cg = child->css.cgroup;
710 struct cgroup *pcg = cg->parent;
711 struct mem_cgroup *parent;
712 struct mem_cgroup_per_zone *mz;
713 unsigned long flags;
714 int ret;
715
716 /* Is ROOT ? */
717 if (!pcg)
718 return -EINVAL;
719
720 parent = mem_cgroup_from_cont(pcg);
721
722 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
723 if (ret)
724 return ret;
725
726 mz = mem_cgroup_zoneinfo(child,
727 page_cgroup_nid(pc), page_cgroup_zid(pc));
728
729 spin_lock_irqsave(&mz->lru_lock, flags);
730 ret = mem_cgroup_move_account(pc, child, parent);
731 spin_unlock_irqrestore(&mz->lru_lock, flags);
732
733 /* drop extra refcnt */
734 css_put(&parent->css);
735 /* uncharge if move fails */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800736 if (ret) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800737 res_counter_uncharge(&parent->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800738 if (do_swap_account)
739 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
740 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800741
742 return ret;
743}
744
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800745/*
746 * Charge the memory controller for page usage.
747 * Return
748 * 0 if the charge was successful
749 * < 0 if the cgroup is over its limit
750 */
751static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
752 gfp_t gfp_mask, enum charge_type ctype,
753 struct mem_cgroup *memcg)
754{
755 struct mem_cgroup *mem;
756 struct page_cgroup *pc;
757 int ret;
758
759 pc = lookup_page_cgroup(page);
760 /* can happen at boot */
761 if (unlikely(!pc))
762 return 0;
763 prefetchw(pc);
764
765 mem = memcg;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -0800766 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800767 if (ret)
768 return ret;
769
770 __mem_cgroup_commit_charge(mem, pc, ctype);
771 return 0;
772}
773
774int mem_cgroup_newpage_charge(struct page *page,
775 struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800776{
Li Zefancede86a2008-07-25 01:47:18 -0700777 if (mem_cgroup_subsys.disabled)
778 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700779 if (PageCompound(page))
780 return 0;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700781 /*
782 * If already mapped, we don't have to account.
783 * If page cache, page->mapping has address_space.
784 * But page->mapping may have out-of-use anon_vma pointer,
785 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
786 * is NULL.
787 */
788 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
789 return 0;
790 if (unlikely(!mm))
791 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800792 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700793 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800794}
795
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800796int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
797 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800798{
Li Zefancede86a2008-07-25 01:47:18 -0700799 if (mem_cgroup_subsys.disabled)
800 return 0;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700801 if (PageCompound(page))
802 return 0;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700803 /*
804 * Corner case handling. This is called from add_to_page_cache()
805 * in usual. But some FS (shmem) precharges this page before calling it
806 * and call add_to_page_cache() with GFP_NOWAIT.
807 *
808 * For GFP_NOWAIT case, the page may be pre-charged before calling
809 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
810 * charge twice. (It works but has to pay a bit larger cost.)
811 */
812 if (!(gfp_mask & __GFP_WAIT)) {
813 struct page_cgroup *pc;
814
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700815
816 pc = lookup_page_cgroup(page);
817 if (!pc)
818 return 0;
819 lock_page_cgroup(pc);
820 if (PageCgroupUsed(pc)) {
821 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700822 return 0;
823 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700824 unlock_page_cgroup(pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700825 }
826
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700827 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800828 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700829
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700830 if (page_is_file_cache(page))
831 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700832 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
KAMEZAWA Hiroyukic05555b2008-10-18 20:28:11 -0700833 else
834 return mem_cgroup_charge_common(page, mm, gfp_mask,
835 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700836}
837
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800838int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
839 struct page *page,
840 gfp_t mask, struct mem_cgroup **ptr)
841{
842 struct mem_cgroup *mem;
843 swp_entry_t ent;
844
845 if (mem_cgroup_subsys.disabled)
846 return 0;
847
848 if (!do_swap_account)
849 goto charge_cur_mm;
850
851 /*
852 * A racing thread's fault, or swapoff, may have already updated
853 * the pte, and even removed page from swap cache: return success
854 * to go on to do_swap_page()'s pte_same() test, which should fail.
855 */
856 if (!PageSwapCache(page))
857 return 0;
858
859 ent.val = page_private(page);
860
861 mem = lookup_swap_cgroup(ent);
862 if (!mem || mem->obsolete)
863 goto charge_cur_mm;
864 *ptr = mem;
865 return __mem_cgroup_try_charge(NULL, mask, ptr, true);
866charge_cur_mm:
867 if (unlikely(!mm))
868 mm = &init_mm;
869 return __mem_cgroup_try_charge(mm, mask, ptr, true);
870}
871
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800872#ifdef CONFIG_SWAP
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800873
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800874int mem_cgroup_cache_charge_swapin(struct page *page,
875 struct mm_struct *mm, gfp_t mask, bool locked)
876{
877 int ret = 0;
878
879 if (mem_cgroup_subsys.disabled)
880 return 0;
881 if (unlikely(!mm))
882 mm = &init_mm;
883 if (!locked)
884 lock_page(page);
885 /*
886 * If not locked, the page can be dropped from SwapCache until
887 * we reach here.
888 */
889 if (PageSwapCache(page)) {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800890 struct mem_cgroup *mem = NULL;
891 swp_entry_t ent;
892
893 ent.val = page_private(page);
894 if (do_swap_account) {
895 mem = lookup_swap_cgroup(ent);
896 if (mem && mem->obsolete)
897 mem = NULL;
898 if (mem)
899 mm = NULL;
900 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800901 ret = mem_cgroup_charge_common(page, mm, mask,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800902 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
903
904 if (!ret && do_swap_account) {
905 /* avoid double counting */
906 mem = swap_cgroup_record(ent, NULL);
907 if (mem) {
908 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
909 mem_cgroup_put(mem);
910 }
911 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800912 }
913 if (!locked)
914 unlock_page(page);
915
916 return ret;
917}
918#endif
919
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800920void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
921{
922 struct page_cgroup *pc;
923
924 if (mem_cgroup_subsys.disabled)
925 return;
926 if (!ptr)
927 return;
928 pc = lookup_page_cgroup(page);
929 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800930 /*
931 * Now swap is on-memory. This means this page may be
932 * counted both as mem and swap....double count.
933 * Fix it by uncharging from memsw. This SwapCache is stable
934 * because we're still under lock_page().
935 */
936 if (do_swap_account) {
937 swp_entry_t ent = {.val = page_private(page)};
938 struct mem_cgroup *memcg;
939 memcg = swap_cgroup_record(ent, NULL);
940 if (memcg) {
941 /* If memcg is obsolete, memcg can be != ptr */
942 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
943 mem_cgroup_put(memcg);
944 }
945
946 }
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800947}
948
949void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
950{
951 if (mem_cgroup_subsys.disabled)
952 return;
953 if (!mem)
954 return;
955 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800956 if (do_swap_account)
957 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
KAMEZAWA Hiroyuki7a81b882009-01-07 18:07:48 -0800958 css_put(&mem->css);
959}
960
961
Balbir Singh8697d332008-02-07 00:13:59 -0800962/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700963 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800964 */
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800965static struct mem_cgroup *
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700966__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800967{
Hugh Dickins82895462008-03-04 14:29:08 -0800968 struct page_cgroup *pc;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800969 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800970 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800971 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800972
Balbir Singh40779602008-04-04 14:29:59 -0700973 if (mem_cgroup_subsys.disabled)
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800974 return NULL;
Balbir Singh40779602008-04-04 14:29:59 -0700975
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800976 if (PageSwapCache(page))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800977 return NULL;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800978
Balbir Singh8697d332008-02-07 00:13:59 -0800979 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800980 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800981 */
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700982 pc = lookup_page_cgroup(page);
983 if (unlikely(!pc || !PageCgroupUsed(pc)))
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800984 return NULL;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800985
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -0700986 lock_page_cgroup(pc);
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800987
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -0800988 mem = pc->mem_cgroup;
989
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -0800990 if (!PageCgroupUsed(pc))
991 goto unlock_out;
992
993 switch (ctype) {
994 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
995 if (page_mapped(page))
996 goto unlock_out;
997 break;
998 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
999 if (!PageAnon(page)) { /* Shared memory */
1000 if (page->mapping && !page_is_file_cache(page))
1001 goto unlock_out;
1002 } else if (page_mapped(page)) /* Anon */
1003 goto unlock_out;
1004 break;
1005 default:
1006 break;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001007 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001008
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001009 res_counter_uncharge(&mem->res, PAGE_SIZE);
1010 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1011 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1012
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001013 ClearPageCgroupUsed(pc);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001014
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001015 mz = page_cgroup_zoneinfo(pc);
1016 spin_lock_irqsave(&mz->lru_lock, flags);
1017 __mem_cgroup_remove_list(mz, pc);
1018 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001019 unlock_page_cgroup(pc);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001020
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001021 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001022
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001023 return mem;
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001024
1025unlock_out:
1026 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001027 return NULL;
Balbir Singh3c541e12008-02-07 00:14:41 -08001028}
1029
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001030void mem_cgroup_uncharge_page(struct page *page)
1031{
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001032 /* early check. */
1033 if (page_mapped(page))
1034 return;
1035 if (page->mapping && !PageAnon(page))
1036 return;
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001037 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1038}
1039
1040void mem_cgroup_uncharge_cache_page(struct page *page)
1041{
1042 VM_BUG_ON(page_mapped(page));
KAMEZAWA Hiroyukib7abea92008-10-18 20:28:09 -07001043 VM_BUG_ON(page->mapping);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001044 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1045}
1046
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001047/*
1048 * called from __delete_from_swap_cache() and drop "page" account.
1049 * memcg information is recorded to swap_cgroup of "ent"
1050 */
1051void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001052{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001053 struct mem_cgroup *memcg;
1054
1055 memcg = __mem_cgroup_uncharge_common(page,
1056 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1057 /* record memcg information */
1058 if (do_swap_account && memcg) {
1059 swap_cgroup_record(ent, memcg);
1060 mem_cgroup_get(memcg);
1061 }
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001062}
1063
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001064#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1065/*
1066 * called from swap_entry_free(). remove record in swap_cgroup and
1067 * uncharge "memsw" account.
1068 */
1069void mem_cgroup_uncharge_swap(swp_entry_t ent)
1070{
1071 struct mem_cgroup *memcg;
1072
1073 if (!do_swap_account)
1074 return;
1075
1076 memcg = swap_cgroup_record(ent, NULL);
1077 if (memcg) {
1078 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1079 mem_cgroup_put(memcg);
1080 }
1081}
1082#endif
1083
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001084/*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001085 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1086 * page belongs to.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001087 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001088int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001089{
1090 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001091 struct mem_cgroup *mem = NULL;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001092 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001093
Balbir Singh40779602008-04-04 14:29:59 -07001094 if (mem_cgroup_subsys.disabled)
1095 return 0;
1096
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001097 pc = lookup_page_cgroup(page);
1098 lock_page_cgroup(pc);
1099 if (PageCgroupUsed(pc)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001100 mem = pc->mem_cgroup;
1101 css_get(&mem->css);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -08001102 }
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001103 unlock_page_cgroup(pc);
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001104
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001105 if (mem) {
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001106 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001107 css_put(&mem->css);
1108 }
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001109 *ptr = mem;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001110 return ret;
1111}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -08001112
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001113/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001114void mem_cgroup_end_migration(struct mem_cgroup *mem,
1115 struct page *oldpage, struct page *newpage)
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -07001116{
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001117 struct page *target, *unused;
1118 struct page_cgroup *pc;
1119 enum charge_type ctype;
1120
1121 if (!mem)
1122 return;
1123
1124 /* at migration success, oldpage->mapping is NULL. */
1125 if (oldpage->mapping) {
1126 target = oldpage;
1127 unused = NULL;
1128 } else {
1129 target = newpage;
1130 unused = oldpage;
1131 }
1132
1133 if (PageAnon(target))
1134 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1135 else if (page_is_file_cache(target))
1136 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1137 else
1138 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1139
1140 /* unused page is not on radix-tree now. */
KAMEZAWA Hiroyukid13d1442009-01-07 18:07:56 -08001141 if (unused)
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001142 __mem_cgroup_uncharge_common(unused, ctype);
1143
1144 pc = lookup_page_cgroup(target);
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001145 /*
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001146 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1147 * So, double-counting is effectively avoided.
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -07001148 */
KAMEZAWA Hiroyuki01b1ae62009-01-07 18:07:50 -08001149 __mem_cgroup_commit_charge(mem, pc, ctype);
1150
1151 /*
1152 * Both of oldpage and newpage are still under lock_page().
1153 * Then, we don't have to care about race in radix-tree.
1154 * But we have to be careful that this page is unmapped or not.
1155 *
1156 * There is a case for !page_mapped(). At the start of
1157 * migration, oldpage was mapped. But now, it's zapped.
1158 * But we know *target* page is not freed/reused under us.
1159 * mem_cgroup_uncharge_page() does all necessary checks.
1160 */
1161 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1162 mem_cgroup_uncharge_page(target);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -08001163}
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001164
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001165/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001166 * A call to try to shrink memory usage under specified resource controller.
1167 * This is typically used for page reclaiming for shmem for reducing side
1168 * effect of page allocation from shmem, which is used by some mem_cgroup.
1169 */
1170int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1171{
1172 struct mem_cgroup *mem;
1173 int progress = 0;
1174 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1175
Li Zefancede86a2008-07-25 01:47:18 -07001176 if (mem_cgroup_subsys.disabled)
1177 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -07001178 if (!mm)
1179 return 0;
Li Zefancede86a2008-07-25 01:47:18 -07001180
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001181 rcu_read_lock();
1182 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
Balbir Singh31a78f22008-09-28 23:09:31 +01001183 if (unlikely(!mem)) {
1184 rcu_read_unlock();
1185 return 0;
1186 }
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001187 css_get(&mem->css);
1188 rcu_read_unlock();
1189
1190 do {
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001191 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -07001192 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001193 } while (!progress && --retry);
1194
1195 css_put(&mem->css);
1196 if (!retry)
1197 return -ENOMEM;
1198 return 0;
1199}
1200
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001201static DEFINE_MUTEX(set_limit_mutex);
1202
KOSAKI Motohirod38d2a72009-01-06 14:39:44 -08001203static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001204 unsigned long long val)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001205{
1206
1207 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1208 int progress;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001209 u64 memswlimit;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001210 int ret = 0;
1211
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001212 while (retry_count) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001213 if (signal_pending(current)) {
1214 ret = -EINTR;
1215 break;
1216 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001217 /*
1218 * Rather than hide all in some function, I do this in
1219 * open coded manner. You see what this really does.
1220 * We have to guarantee mem->res.limit < mem->memsw.limit.
1221 */
1222 mutex_lock(&set_limit_mutex);
1223 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1224 if (memswlimit < val) {
1225 ret = -EINVAL;
1226 mutex_unlock(&set_limit_mutex);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001227 break;
1228 }
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001229 ret = res_counter_set_limit(&memcg->res, val);
1230 mutex_unlock(&set_limit_mutex);
1231
1232 if (!ret)
1233 break;
1234
KAMEZAWA Hiroyukibced0522009-01-07 18:07:49 -08001235 progress = try_to_free_mem_cgroup_pages(memcg,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001236 GFP_HIGHUSER_MOVABLE, false);
1237 if (!progress) retry_count--;
1238 }
1239 return ret;
1240}
1241
1242int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1243 unsigned long long val)
1244{
1245 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1246 u64 memlimit, oldusage, curusage;
1247 int ret;
1248
1249 if (!do_swap_account)
1250 return -EINVAL;
1251
1252 while (retry_count) {
1253 if (signal_pending(current)) {
1254 ret = -EINTR;
1255 break;
1256 }
1257 /*
1258 * Rather than hide all in some function, I do this in
1259 * open coded manner. You see what this really does.
1260 * We have to guarantee mem->res.limit < mem->memsw.limit.
1261 */
1262 mutex_lock(&set_limit_mutex);
1263 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1264 if (memlimit > val) {
1265 ret = -EINVAL;
1266 mutex_unlock(&set_limit_mutex);
1267 break;
1268 }
1269 ret = res_counter_set_limit(&memcg->memsw, val);
1270 mutex_unlock(&set_limit_mutex);
1271
1272 if (!ret)
1273 break;
1274
1275 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1276 try_to_free_mem_cgroup_pages(memcg, GFP_HIGHUSER_MOVABLE, true);
1277 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1278 if (curusage >= oldusage)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001279 retry_count--;
1280 }
1281 return ret;
1282}
1283
1284
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -07001285/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001286 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001287 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1288 */
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001289static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001290 struct mem_cgroup_per_zone *mz,
Christoph Lameterb69408e2008-10-18 20:26:14 -07001291 enum lru_list lru)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001292{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001293 struct page_cgroup *pc, *busy;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001294 unsigned long flags;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001295 unsigned long loop;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001296 struct list_head *list;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001297 int ret = 0;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001298
Christoph Lameterb69408e2008-10-18 20:26:14 -07001299 list = &mz->lists[lru];
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001300
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001301 loop = MEM_CGROUP_ZSTAT(mz, lru);
1302 /* give some margin against EBUSY etc...*/
1303 loop += 256;
1304 busy = NULL;
1305 while (loop--) {
1306 ret = 0;
1307 spin_lock_irqsave(&mz->lru_lock, flags);
1308 if (list_empty(list)) {
1309 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001310 break;
1311 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001312 pc = list_entry(list->prev, struct page_cgroup, lru);
1313 if (busy == pc) {
1314 list_move(&pc->lru, list);
1315 busy = 0;
1316 spin_unlock_irqrestore(&mz->lru_lock, flags);
1317 continue;
1318 }
1319 spin_unlock_irqrestore(&mz->lru_lock, flags);
1320
1321 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1322 if (ret == -ENOMEM)
1323 break;
1324
1325 if (ret == -EBUSY || ret == -EINVAL) {
1326 /* found lock contention or "pc" is obsolete. */
1327 busy = pc;
1328 cond_resched();
1329 } else
1330 busy = NULL;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001331 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001332 if (!ret && !list_empty(list))
1333 return -EBUSY;
1334 return ret;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001335}
1336
1337/*
1338 * make mem_cgroup's charge to be 0 if there is no task.
1339 * This enables deleting this mem_cgroup.
1340 */
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001341static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001342{
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001343 int ret;
1344 int node, zid, shrink;
1345 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001346 struct cgroup *cgrp = mem->css.cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -08001347
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001348 css_get(&mem->css);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001349
1350 shrink = 0;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001351 /* should free all ? */
1352 if (free_all)
1353 goto try_to_free;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001354move_account:
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001355 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001356 ret = -EBUSY;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001357 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001358 goto out;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001359 ret = -EINTR;
1360 if (signal_pending(current))
1361 goto out;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001362 /* This is for making all *used* pages to be on LRU. */
1363 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001364 ret = 0;
1365 for_each_node_state(node, N_POSSIBLE) {
1366 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001367 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001368 enum lru_list l;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001369 mz = mem_cgroup_zoneinfo(mem, node, zid);
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001370 for_each_lru(l) {
1371 ret = mem_cgroup_force_empty_list(mem,
1372 mz, l);
1373 if (ret)
1374 break;
1375 }
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001376 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001377 if (ret)
1378 break;
1379 }
1380 /* it seems parent cgroup doesn't have enough mem */
1381 if (ret == -ENOMEM)
1382 goto try_to_free;
KAMEZAWA Hiroyuki52d4b9a2008-10-18 20:28:16 -07001383 cond_resched();
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001384 }
1385 ret = 0;
1386out:
1387 css_put(&mem->css);
1388 return ret;
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001389
1390try_to_free:
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001391 /* returns EBUSY if there is a task or if we come here twice. */
1392 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001393 ret = -EBUSY;
1394 goto out;
1395 }
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001396 /* we call try-to-free pages for make this cgroup empty */
1397 lru_add_drain_all();
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001398 /* try to free all pages in this cgroup */
1399 shrink = 1;
1400 while (nr_retries && mem->res.usage > 0) {
1401 int progress;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001402
1403 if (signal_pending(current)) {
1404 ret = -EINTR;
1405 goto out;
1406 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001407 progress = try_to_free_mem_cgroup_pages(mem,
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001408 GFP_HIGHUSER_MOVABLE, false);
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001409 if (!progress) {
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001410 nr_retries--;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001411 /* maybe some writeback is necessary */
1412 congestion_wait(WRITE, HZ/10);
1413 }
KAMEZAWA Hiroyukif817ed42009-01-07 18:07:53 -08001414
1415 }
1416 /* try move_account...there may be some *locked* pages. */
1417 if (mem->res.usage)
1418 goto move_account;
1419 ret = 0;
1420 goto out;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001421}
1422
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001423int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1424{
1425 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1426}
1427
1428
Paul Menage2c3daa72008-04-29 00:59:58 -07001429static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001430{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001431 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1432 u64 val = 0;
1433 int type, name;
1434
1435 type = MEMFILE_TYPE(cft->private);
1436 name = MEMFILE_ATTR(cft->private);
1437 switch (type) {
1438 case _MEM:
1439 val = res_counter_read_u64(&mem->res, name);
1440 break;
1441 case _MEMSWAP:
1442 if (do_swap_account)
1443 val = res_counter_read_u64(&mem->memsw, name);
1444 break;
1445 default:
1446 BUG();
1447 break;
1448 }
1449 return val;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001450}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001451/*
1452 * The user of this function is...
1453 * RES_LIMIT.
1454 */
Paul Menage856c13a2008-07-25 01:47:04 -07001455static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1456 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001457{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001458 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001459 int type, name;
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001460 unsigned long long val;
1461 int ret;
1462
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001463 type = MEMFILE_TYPE(cft->private);
1464 name = MEMFILE_ATTR(cft->private);
1465 switch (name) {
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001466 case RES_LIMIT:
1467 /* This function does all necessary parse...reuse it */
1468 ret = res_counter_memparse_write_strategy(buffer, &val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001469 if (ret)
1470 break;
1471 if (type == _MEM)
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001472 ret = mem_cgroup_resize_limit(memcg, val);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001473 else
1474 ret = mem_cgroup_resize_memsw_limit(memcg, val);
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -07001475 break;
1476 default:
1477 ret = -EINVAL; /* should be BUG() ? */
1478 break;
1479 }
1480 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001481}
1482
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001483static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001484{
1485 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001486 int type, name;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001487
1488 mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001489 type = MEMFILE_TYPE(event);
1490 name = MEMFILE_ATTR(event);
1491 switch (name) {
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001492 case RES_MAX_USAGE:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001493 if (type == _MEM)
1494 res_counter_reset_max(&mem->res);
1495 else
1496 res_counter_reset_max(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001497 break;
1498 case RES_FAILCNT:
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001499 if (type == _MEM)
1500 res_counter_reset_failcnt(&mem->res);
1501 else
1502 res_counter_reset_failcnt(&mem->memsw);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001503 break;
1504 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001505 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001506}
1507
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001508static const struct mem_cgroup_stat_desc {
1509 const char *msg;
1510 u64 unit;
1511} mem_cgroup_stat_desc[] = {
1512 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1513 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -07001514 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1515 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001516};
1517
Paul Menagec64745c2008-04-29 01:00:02 -07001518static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1519 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001520{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001521 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1522 struct mem_cgroup_stat *stat = &mem_cont->stat;
1523 int i;
1524
1525 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1526 s64 val;
1527
1528 val = mem_cgroup_read_stat(stat, i);
1529 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -07001530 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001531 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001532 /* showing # of active pages */
1533 {
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001534 unsigned long active_anon, inactive_anon;
1535 unsigned long active_file, inactive_file;
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001536 unsigned long unevictable;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001537
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001538 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1539 LRU_INACTIVE_ANON);
1540 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1541 LRU_ACTIVE_ANON);
1542 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1543 LRU_INACTIVE_FILE);
1544 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1545 LRU_ACTIVE_FILE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001546 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1547 LRU_UNEVICTABLE);
1548
Rik van Riel4f98a2f2008-10-18 20:26:32 -07001549 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1550 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1551 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1552 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
Lee Schermerhorn7b854122008-10-18 20:26:40 -07001553 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1554
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001555 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001556 return 0;
1557}
1558
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001559
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001560static struct cftype mem_cgroup_files[] = {
1561 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001562 .name = "usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001563 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
Paul Menage2c3daa72008-04-29 00:59:58 -07001564 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001565 },
1566 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001567 .name = "max_usage_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001568 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001569 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001570 .read_u64 = mem_cgroup_read,
1571 },
1572 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001573 .name = "limit_in_bytes",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001574 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
Paul Menage856c13a2008-07-25 01:47:04 -07001575 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001576 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001577 },
1578 {
1579 .name = "failcnt",
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001580 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001581 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001582 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001583 },
Balbir Singh8697d332008-02-07 00:13:59 -08001584 {
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001585 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001586 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001587 },
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001588 {
1589 .name = "force_empty",
1590 .trigger = mem_cgroup_force_empty_write,
1591 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001592};
1593
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001594#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1595static struct cftype memsw_cgroup_files[] = {
1596 {
1597 .name = "memsw.usage_in_bytes",
1598 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1599 .read_u64 = mem_cgroup_read,
1600 },
1601 {
1602 .name = "memsw.max_usage_in_bytes",
1603 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1604 .trigger = mem_cgroup_reset,
1605 .read_u64 = mem_cgroup_read,
1606 },
1607 {
1608 .name = "memsw.limit_in_bytes",
1609 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1610 .write_string = mem_cgroup_write,
1611 .read_u64 = mem_cgroup_read,
1612 },
1613 {
1614 .name = "memsw.failcnt",
1615 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1616 .trigger = mem_cgroup_reset,
1617 .read_u64 = mem_cgroup_read,
1618 },
1619};
1620
1621static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1622{
1623 if (!do_swap_account)
1624 return 0;
1625 return cgroup_add_files(cont, ss, memsw_cgroup_files,
1626 ARRAY_SIZE(memsw_cgroup_files));
1627};
1628#else
1629static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1630{
1631 return 0;
1632}
1633#endif
1634
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001635static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1636{
1637 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001638 struct mem_cgroup_per_zone *mz;
Christoph Lameterb69408e2008-10-18 20:26:14 -07001639 enum lru_list l;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001640 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001641 /*
1642 * This routine is called against possible nodes.
1643 * But it's BUG to call kmalloc() against offline node.
1644 *
1645 * TODO: this routine can waste much memory for nodes which will
1646 * never be onlined. It's better to use memory hotplug callback
1647 * function.
1648 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001649 if (!node_state(node, N_NORMAL_MEMORY))
1650 tmp = -1;
1651 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001652 if (!pn)
1653 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001654
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001655 mem->info.nodeinfo[node] = pn;
1656 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001657
1658 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1659 mz = &pn->zoneinfo[zone];
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001660 spin_lock_init(&mz->lru_lock);
Christoph Lameterb69408e2008-10-18 20:26:14 -07001661 for_each_lru(l)
1662 INIT_LIST_HEAD(&mz->lists[l]);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001663 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001664 return 0;
1665}
1666
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001667static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1668{
1669 kfree(mem->info.nodeinfo[node]);
1670}
1671
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001672static int mem_cgroup_size(void)
1673{
1674 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1675 return sizeof(struct mem_cgroup) + cpustat_size;
1676}
1677
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001678static struct mem_cgroup *mem_cgroup_alloc(void)
1679{
1680 struct mem_cgroup *mem;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001681 int size = mem_cgroup_size();
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001682
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001683 if (size < PAGE_SIZE)
1684 mem = kmalloc(size, GFP_KERNEL);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001685 else
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001686 mem = vmalloc(size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001687
1688 if (mem)
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001689 memset(mem, 0, size);
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001690 return mem;
1691}
1692
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001693/*
1694 * At destroying mem_cgroup, references from swap_cgroup can remain.
1695 * (scanning all at force_empty is too costly...)
1696 *
1697 * Instead of clearing all references at force_empty, we remember
1698 * the number of reference from swap_cgroup and free mem_cgroup when
1699 * it goes down to 0.
1700 *
1701 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1702 * entry which points to this memcg will be ignore at swapin.
1703 *
1704 * Removal of cgroup itself succeeds regardless of refs from swap.
1705 */
1706
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001707static void mem_cgroup_free(struct mem_cgroup *mem)
1708{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001709 if (atomic_read(&mem->refcnt) > 0)
1710 return;
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001711 if (mem_cgroup_size() < PAGE_SIZE)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001712 kfree(mem);
1713 else
1714 vfree(mem);
1715}
1716
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001717static void mem_cgroup_get(struct mem_cgroup *mem)
1718{
1719 atomic_inc(&mem->refcnt);
1720}
1721
1722static void mem_cgroup_put(struct mem_cgroup *mem)
1723{
1724 if (atomic_dec_and_test(&mem->refcnt)) {
1725 if (!mem->obsolete)
1726 return;
1727 mem_cgroup_free(mem);
1728 }
1729}
1730
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001731
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001732#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1733static void __init enable_swap_cgroup(void)
1734{
1735 if (!mem_cgroup_subsys.disabled && really_do_swap_account)
1736 do_swap_account = 1;
1737}
1738#else
1739static void __init enable_swap_cgroup(void)
1740{
1741}
1742#endif
1743
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001744static struct cgroup_subsys_state *
1745mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1746{
1747 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001748 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001749
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001750 mem = mem_cgroup_alloc();
1751 if (!mem)
1752 return ERR_PTR(-ENOMEM);
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001753
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001754 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001755 res_counter_init(&mem->memsw);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001756
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001757 for_each_node_state(node, N_POSSIBLE)
1758 if (alloc_mem_cgroup_per_zone_info(mem, node))
1759 goto free_out;
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001760 /* root ? */
1761 if (cont->parent == NULL)
1762 enable_swap_cgroup();
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001763
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001764 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001765free_out:
1766 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001767 free_mem_cgroup_per_zone_info(mem, node);
Jan Blunckc8dad2b2009-01-07 18:07:53 -08001768 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001769 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001770}
1771
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001772static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1773 struct cgroup *cont)
1774{
1775 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001776 mem->obsolete = 1;
KAMEZAWA Hiroyukic1e862c2009-01-07 18:07:55 -08001777 mem_cgroup_force_empty(mem, false);
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001778}
1779
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001780static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1781 struct cgroup *cont)
1782{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001783 int node;
1784 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1785
1786 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001787 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001788
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001789 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001790}
1791
1792static int mem_cgroup_populate(struct cgroup_subsys *ss,
1793 struct cgroup *cont)
1794{
KAMEZAWA Hiroyuki8c7c6e342009-01-07 18:08:00 -08001795 int ret;
1796
1797 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
1798 ARRAY_SIZE(mem_cgroup_files));
1799
1800 if (!ret)
1801 ret = register_memsw_files(cont, ss);
1802 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001803}
1804
Balbir Singh67e465a2008-02-07 00:13:54 -08001805static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1806 struct cgroup *cont,
1807 struct cgroup *old_cont,
1808 struct task_struct *p)
1809{
1810 struct mm_struct *mm;
1811 struct mem_cgroup *mem, *old_mem;
1812
1813 mm = get_task_mm(p);
1814 if (mm == NULL)
1815 return;
1816
1817 mem = mem_cgroup_from_cont(cont);
1818 old_mem = mem_cgroup_from_cont(old_cont);
1819
Balbir Singh67e465a2008-02-07 00:13:54 -08001820 /*
1821 * Only thread group leaders are allowed to migrate, the mm_struct is
1822 * in effect owned by the leader
1823 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001824 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001825 goto out;
1826
Balbir Singh67e465a2008-02-07 00:13:54 -08001827out:
1828 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001829}
1830
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001831struct cgroup_subsys mem_cgroup_subsys = {
1832 .name = "memory",
1833 .subsys_id = mem_cgroup_subsys_id,
1834 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001835 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001836 .destroy = mem_cgroup_destroy,
1837 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001838 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001839 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001840};
KAMEZAWA Hiroyukic0777192009-01-07 18:07:57 -08001841
1842#ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1843
1844static int __init disable_swap_account(char *s)
1845{
1846 really_do_swap_account = 0;
1847 return 1;
1848}
1849__setup("noswapaccount", disable_swap_account);
1850#endif