blob: a59f946c9338b6ecaef4668545be73d969529ea3 [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 Hiroyukid52aa412008-02-07 00:14:24 -080024#include <linux/smp.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080025#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080026#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080027#include <linux/bit_spinlock.h>
28#include <linux/rcupdate.h>
Balbir Singh66e17072008-02-07 00:13:56 -080029#include <linux/swap.h>
30#include <linux/spinlock.h>
31#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080032#include <linux/seq_file.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080033
Balbir Singh8697d332008-02-07 00:13:59 -080034#include <asm/uaccess.h>
35
Balbir Singh8cdea7c2008-02-07 00:13:50 -080036struct cgroup_subsys mem_cgroup_subsys;
Balbir Singh66e17072008-02-07 00:13:56 -080037static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080038
39/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080040 * Statistics for memory cgroup.
41 */
42enum mem_cgroup_stat_index {
43 /*
44 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
45 */
46 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
47 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
48
49 MEM_CGROUP_STAT_NSTATS,
50};
51
52struct mem_cgroup_stat_cpu {
53 s64 count[MEM_CGROUP_STAT_NSTATS];
54} ____cacheline_aligned_in_smp;
55
56struct mem_cgroup_stat {
57 struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
58};
59
60/*
61 * For accounting under irq disable, no need for increment preempt count.
62 */
63static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
64 enum mem_cgroup_stat_index idx, int val)
65{
66 int cpu = smp_processor_id();
67 stat->cpustat[cpu].count[idx] += val;
68}
69
70static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
71 enum mem_cgroup_stat_index idx)
72{
73 int cpu;
74 s64 ret = 0;
75 for_each_possible_cpu(cpu)
76 ret += stat->cpustat[cpu].count[idx];
77 return ret;
78}
79
80/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080081 * per-zone information in memory controller.
82 */
83
84enum mem_cgroup_zstat_index {
85 MEM_CGROUP_ZSTAT_ACTIVE,
86 MEM_CGROUP_ZSTAT_INACTIVE,
87
88 NR_MEM_CGROUP_ZSTAT,
89};
90
91struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -080092 /*
93 * spin_lock to protect the per cgroup LRU
94 */
95 spinlock_t lru_lock;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -080096 struct list_head active_list;
97 struct list_head inactive_list;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080098 unsigned long count[NR_MEM_CGROUP_ZSTAT];
99};
100/* Macro for accessing counter */
101#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
102
103struct mem_cgroup_per_node {
104 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
105};
106
107struct mem_cgroup_lru_info {
108 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
109};
110
111/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800112 * The memory controller data structure. The memory controller controls both
113 * page cache and RSS per cgroup. We would eventually like to provide
114 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
115 * to help the administrator determine what knobs to tune.
116 *
117 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800118 * we hit the water mark. May be even add a low water mark, such that
119 * no reclaim occurs from a cgroup at it's low water mark, this is
120 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800121 */
122struct mem_cgroup {
123 struct cgroup_subsys_state css;
124 /*
125 * the counter to account for memory usage
126 */
127 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800128 /*
129 * Per cgroup active and inactive list, similar to the
130 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800131 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800132 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800133
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800134 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800135 /*
136 * statistics.
137 */
138 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800139};
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800140static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800141
142/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800143 * We use the lower bit of the page->page_cgroup pointer as a bit spin
Hugh Dickins9442ec92008-03-04 14:29:07 -0800144 * lock. We need to ensure that page->page_cgroup is at least two
145 * byte aligned (based on comments from Nick Piggin). But since
146 * bit_spin_lock doesn't actually set that lock bit in a non-debug
147 * uniprocessor kernel, we should avoid setting it here too.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800148 */
149#define PAGE_CGROUP_LOCK_BIT 0x0
Hugh Dickins9442ec92008-03-04 14:29:07 -0800150#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
151#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
152#else
153#define PAGE_CGROUP_LOCK 0x0
154#endif
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800155
156/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800157 * A page_cgroup page is associated with every page descriptor. The
158 * page_cgroup helps us identify information about the cgroup
159 */
160struct page_cgroup {
161 struct list_head lru; /* per cgroup LRU list */
162 struct page *page;
163 struct mem_cgroup *mem_cgroup;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800164 atomic_t ref_cnt; /* Helpful when pages move b/w */
165 /* mapped and cached states */
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800166 int flags;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800167};
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800168#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800169#define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800170
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800171static int page_cgroup_nid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800172{
173 return page_to_nid(pc->page);
174}
175
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800176static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800177{
178 return page_zonenum(pc->page);
179}
180
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800181enum charge_type {
182 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
183 MEM_CGROUP_CHARGE_TYPE_MAPPED,
184};
185
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800186/*
187 * Always modified under lru lock. Then, not necessary to preempt_disable()
188 */
189static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
190 bool charge)
191{
192 int val = (charge)? 1 : -1;
193 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800194
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800195 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800196 if (flags & PAGE_CGROUP_FLAG_CACHE)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800197 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800198 else
199 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800200}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800201
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800202static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800203mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
204{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800205 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
206}
207
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800208static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800209page_cgroup_zoneinfo(struct page_cgroup *pc)
210{
211 struct mem_cgroup *mem = pc->mem_cgroup;
212 int nid = page_cgroup_nid(pc);
213 int zid = page_cgroup_zid(pc);
214
215 return mem_cgroup_zoneinfo(mem, nid, zid);
216}
217
218static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
219 enum mem_cgroup_zstat_index idx)
220{
221 int nid, zid;
222 struct mem_cgroup_per_zone *mz;
223 u64 total = 0;
224
225 for_each_online_node(nid)
226 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
227 mz = mem_cgroup_zoneinfo(mem, nid, zid);
228 total += MEM_CGROUP_ZSTAT(mz, idx);
229 }
230 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800231}
232
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800233static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800234{
235 return container_of(cgroup_subsys_state(cont,
236 mem_cgroup_subsys_id), struct mem_cgroup,
237 css);
238}
239
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800240static struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800241{
242 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
243 struct mem_cgroup, css);
244}
245
246void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
247{
248 struct mem_cgroup *mem;
249
250 mem = mem_cgroup_from_task(p);
251 css_get(&mem->css);
252 mm->mem_cgroup = mem;
253}
254
255void mm_free_cgroup(struct mm_struct *mm)
256{
257 css_put(&mm->mem_cgroup->css);
258}
259
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800260static inline int page_cgroup_locked(struct page *page)
261{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800262 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800263}
264
Hugh Dickins9442ec92008-03-04 14:29:07 -0800265static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800266{
Hugh Dickins9442ec92008-03-04 14:29:07 -0800267 VM_BUG_ON(!page_cgroup_locked(page));
268 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800269}
270
271struct page_cgroup *page_get_page_cgroup(struct page *page)
272{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800273 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800274}
275
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800276static void lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800277{
278 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800279}
280
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800281static void unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800282{
283 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
284}
285
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800286/*
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800287 * Clear page->page_cgroup member under lock_page_cgroup().
288 * If given "pc" value is different from one page->page_cgroup,
289 * page->cgroup is not cleared.
290 * Returns a value of page->page_cgroup at lock taken.
291 * A can can detect failure of clearing by following
292 * clear_page_cgroup(page, pc) == pc
293 */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800294static struct page_cgroup *clear_page_cgroup(struct page *page,
295 struct page_cgroup *pc)
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800296{
297 struct page_cgroup *ret;
298 /* lock and clear */
299 lock_page_cgroup(page);
300 ret = page_get_page_cgroup(page);
301 if (likely(ret == pc))
302 page_assign_page_cgroup(page, NULL);
303 unlock_page_cgroup(page);
304 return ret;
305}
306
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800307static void __mem_cgroup_remove_list(struct page_cgroup *pc)
308{
309 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
310 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
311
312 if (from)
313 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
314 else
315 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
316
317 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
318 list_del_init(&pc->lru);
319}
320
321static void __mem_cgroup_add_list(struct page_cgroup *pc)
322{
323 int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
324 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
325
326 if (!to) {
327 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800328 list_add(&pc->lru, &mz->inactive_list);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800329 } else {
330 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800331 list_add(&pc->lru, &mz->active_list);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800332 }
333 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
334}
335
Balbir Singh8697d332008-02-07 00:13:59 -0800336static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800337{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800338 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
339 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
340
341 if (from)
342 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
343 else
344 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
345
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800346 if (active) {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800347 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800348 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800349 list_move(&pc->lru, &mz->active_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800350 } else {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800351 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800352 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800353 list_move(&pc->lru, &mz->inactive_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800354 }
Balbir Singh66e17072008-02-07 00:13:56 -0800355}
356
David Rientjes4c4a2212008-02-07 00:14:06 -0800357int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
358{
359 int ret;
360
361 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800362 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800363 task_unlock(task);
364 return ret;
365}
366
Balbir Singh66e17072008-02-07 00:13:56 -0800367/*
368 * This routine assumes that the appropriate zone's lru lock is already held
369 */
Hugh Dickins427d5412008-03-04 14:29:03 -0800370void mem_cgroup_move_lists(struct page *page, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800371{
Hugh Dickins427d5412008-03-04 14:29:03 -0800372 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800373 struct mem_cgroup_per_zone *mz;
374 unsigned long flags;
375
Hugh Dickins427d5412008-03-04 14:29:03 -0800376 pc = page_get_page_cgroup(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800377 if (!pc)
378 return;
379
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800380 mz = page_cgroup_zoneinfo(pc);
381 spin_lock_irqsave(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800382 __mem_cgroup_move_lists(pc, active);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800383 spin_unlock_irqrestore(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800384}
385
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800386/*
387 * Calculate mapped_ratio under memory controller. This will be used in
388 * vmscan.c for deteremining we have to reclaim mapped pages.
389 */
390int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
391{
392 long total, rss;
393
394 /*
395 * usage is recorded in bytes. But, here, we assume the number of
396 * physical pages can be represented by "long" on any arch.
397 */
398 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
399 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
400 return (int)((rss * 100L) / total);
401}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800402
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800403/*
404 * This function is called from vmscan.c. In page reclaiming loop. balance
405 * between active and inactive list is calculated. For memory controller
406 * page reclaiming, we should use using mem_cgroup's imbalance rather than
407 * zone's global lru imbalance.
408 */
409long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
410{
411 unsigned long active, inactive;
412 /* active and inactive are the number of pages. 'long' is ok.*/
413 active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
414 inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
415 return (long) (active / (inactive + 1));
416}
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800417
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800418/*
419 * prev_priority control...this will be used in memory reclaim path.
420 */
421int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
422{
423 return mem->prev_priority;
424}
425
426void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
427{
428 if (priority < mem->prev_priority)
429 mem->prev_priority = priority;
430}
431
432void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
433{
434 mem->prev_priority = priority;
435}
436
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800437/*
438 * Calculate # of pages to be scanned in this priority/zone.
439 * See also vmscan.c
440 *
441 * priority starts from "DEF_PRIORITY" and decremented in each loop.
442 * (see include/linux/mmzone.h)
443 */
444
445long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
446 struct zone *zone, int priority)
447{
448 long nr_active;
449 int nid = zone->zone_pgdat->node_id;
450 int zid = zone_idx(zone);
451 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
452
453 nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
454 return (nr_active >> priority);
455}
456
457long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
458 struct zone *zone, int priority)
459{
460 long nr_inactive;
461 int nid = zone->zone_pgdat->node_id;
462 int zid = zone_idx(zone);
463 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
464
465 nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800466 return (nr_inactive >> priority);
467}
468
Balbir Singh66e17072008-02-07 00:13:56 -0800469unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
470 struct list_head *dst,
471 unsigned long *scanned, int order,
472 int mode, struct zone *z,
473 struct mem_cgroup *mem_cont,
474 int active)
475{
476 unsigned long nr_taken = 0;
477 struct page *page;
478 unsigned long scan;
479 LIST_HEAD(pc_list);
480 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800481 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800482 int nid = z->zone_pgdat->node_id;
483 int zid = zone_idx(z);
484 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800485
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800486 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Balbir Singh66e17072008-02-07 00:13:56 -0800487 if (active)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800488 src = &mz->active_list;
Balbir Singh66e17072008-02-07 00:13:56 -0800489 else
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800490 src = &mz->inactive_list;
491
Balbir Singh66e17072008-02-07 00:13:56 -0800492
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800493 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800494 scan = 0;
495 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800496 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800497 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800498 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800499
Hugh Dickins436c65412008-02-07 00:14:12 -0800500 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800501 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800502
Balbir Singh66e17072008-02-07 00:13:56 -0800503 if (PageActive(page) && !active) {
504 __mem_cgroup_move_lists(pc, true);
Balbir Singh66e17072008-02-07 00:13:56 -0800505 continue;
506 }
507 if (!PageActive(page) && active) {
508 __mem_cgroup_move_lists(pc, false);
Balbir Singh66e17072008-02-07 00:13:56 -0800509 continue;
510 }
511
Hugh Dickins436c65412008-02-07 00:14:12 -0800512 scan++;
513 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800514
515 if (__isolate_lru_page(page, mode) == 0) {
516 list_move(&page->lru, dst);
517 nr_taken++;
518 }
519 }
520
521 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800522 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800523
524 *scanned = scan;
525 return nr_taken;
526}
527
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800528/*
529 * Charge the memory controller for page usage.
530 * Return
531 * 0 if the charge was successful
532 * < 0 if the cgroup is over its limit
533 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800534static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
535 gfp_t gfp_mask, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800536{
537 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800538 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800539 unsigned long flags;
540 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800541 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800542
543 /*
544 * Should page_cgroup's go to their own slab?
545 * One could optimize the performance of the charging routine
546 * by saving a bit in the page_flags and using it as a lock
547 * to see if the cgroup page already has a page_cgroup associated
548 * with it
549 */
Balbir Singh66e17072008-02-07 00:13:56 -0800550retry:
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800551 lock_page_cgroup(page);
552 pc = page_get_page_cgroup(page);
553 /*
554 * The page_cgroup exists and
555 * the page has already been accounted.
556 */
557 if (pc) {
558 if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
559 /* this page is under being uncharged ? */
560 unlock_page_cgroup(page);
561 cpu_relax();
562 goto retry;
563 } else {
564 unlock_page_cgroup(page);
565 goto done;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800566 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800567 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800568 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800569
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800570 pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800571 if (pc == NULL)
572 goto err;
573
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800574 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800575 * We always charge the cgroup the mm_struct belongs to.
576 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800577 * thread group leader migrates. It's possible that mm is not
578 * set, if so charge the init_mm (happens for pagecache usage).
579 */
580 if (!mm)
581 mm = &init_mm;
582
Hugh Dickins3be912772008-02-07 00:14:19 -0800583 rcu_read_lock();
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800584 mem = rcu_dereference(mm->mem_cgroup);
585 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800586 * For every charge from the cgroup, increment reference count
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800587 */
588 css_get(&mem->css);
589 rcu_read_unlock();
590
Balbir Singh0eea1032008-02-07 00:13:57 -0800591 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Hugh Dickins3be912772008-02-07 00:14:19 -0800592 if (!(gfp_mask & __GFP_WAIT))
593 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800594
595 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800596 continue;
597
598 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800599 * try_to_free_mem_cgroup_pages() might not give us a full
600 * picture of reclaim. Some pages are reclaimed and might be
601 * moved to swap cache or just unmapped from the cgroup.
602 * Check the limit again to see if the reclaim reduced the
603 * current usage of the cgroup before giving up
604 */
Balbir Singh66e17072008-02-07 00:13:56 -0800605 if (res_counter_check_under_limit(&mem->res))
606 continue;
Hugh Dickins3be912772008-02-07 00:14:19 -0800607
608 if (!nr_retries--) {
609 mem_cgroup_out_of_memory(mem, gfp_mask);
610 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800611 }
Hugh Dickins3be912772008-02-07 00:14:19 -0800612 congestion_wait(WRITE, HZ/10);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800613 }
614
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800615 atomic_set(&pc->ref_cnt, 1);
616 pc->mem_cgroup = mem;
617 pc->page = page;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800618 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800619 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
620 pc->flags |= PAGE_CGROUP_FLAG_CACHE;
Hugh Dickins3be912772008-02-07 00:14:19 -0800621
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800622 lock_page_cgroup(page);
623 if (page_get_page_cgroup(page)) {
624 unlock_page_cgroup(page);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800625 /*
Hugh Dickins3be912772008-02-07 00:14:19 -0800626 * Another charge has been added to this page already.
627 * We take lock_page_cgroup(page) again and read
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800628 * page->cgroup, increment refcnt.... just retry is OK.
629 */
630 res_counter_uncharge(&mem->res, PAGE_SIZE);
631 css_put(&mem->css);
632 kfree(pc);
633 goto retry;
634 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800635 page_assign_page_cgroup(page, pc);
636 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800637
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800638 mz = page_cgroup_zoneinfo(pc);
639 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800640 __mem_cgroup_add_list(pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800641 spin_unlock_irqrestore(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800642
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800643done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800644 return 0;
Hugh Dickins3be912772008-02-07 00:14:19 -0800645out:
646 css_put(&mem->css);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800647 kfree(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800648err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800649 return -ENOMEM;
650}
651
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800652int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800653{
654 return mem_cgroup_charge_common(page, mm, gfp_mask,
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800655 MEM_CGROUP_CHARGE_TYPE_MAPPED);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800656}
657
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800658int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
659 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800660{
Balbir Singh8697d332008-02-07 00:13:59 -0800661 if (!mm)
662 mm = &init_mm;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800663 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800664 MEM_CGROUP_CHARGE_TYPE_CACHE);
Balbir Singh8697d332008-02-07 00:13:59 -0800665}
666
667/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800668 * Uncharging is always a welcome operation, we never complain, simply
Hugh Dickins82895462008-03-04 14:29:08 -0800669 * uncharge.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800670 */
Hugh Dickins82895462008-03-04 14:29:08 -0800671void mem_cgroup_uncharge_page(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800672{
Hugh Dickins82895462008-03-04 14:29:08 -0800673 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800674 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800675 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800676 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800677
Balbir Singh8697d332008-02-07 00:13:59 -0800678 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800679 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800680 */
Hugh Dickins82895462008-03-04 14:29:08 -0800681 lock_page_cgroup(page);
682 pc = page_get_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800683 if (!pc)
Hugh Dickins82895462008-03-04 14:29:08 -0800684 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800685
686 if (atomic_dec_and_test(&pc->ref_cnt)) {
687 page = pc->page;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800688 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800689 /*
690 * get page->cgroup and clear it under lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800691 * force_empty can drop page->cgroup without checking refcnt.
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800692 */
Balbir Singh3c541e12008-02-07 00:14:41 -0800693 unlock_page_cgroup(page);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800694 if (clear_page_cgroup(page, pc) == pc) {
695 mem = pc->mem_cgroup;
696 css_put(&mem->css);
697 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800698 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800699 __mem_cgroup_remove_list(pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800700 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800701 kfree(pc);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800702 }
Balbir Singh3c541e12008-02-07 00:14:41 -0800703 lock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800704 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800705
Hugh Dickins82895462008-03-04 14:29:08 -0800706unlock:
Balbir Singh3c541e12008-02-07 00:14:41 -0800707 unlock_page_cgroup(page);
708}
709
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800710/*
711 * Returns non-zero if a page (under migration) has valid page_cgroup member.
712 * Refcnt of page_cgroup is incremented.
713 */
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800714int mem_cgroup_prepare_migration(struct page *page)
715{
716 struct page_cgroup *pc;
717 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800718
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800719 lock_page_cgroup(page);
720 pc = page_get_page_cgroup(page);
721 if (pc && atomic_inc_not_zero(&pc->ref_cnt))
722 ret = 1;
723 unlock_page_cgroup(page);
724 return ret;
725}
726
727void mem_cgroup_end_migration(struct page *page)
728{
Hugh Dickins82895462008-03-04 14:29:08 -0800729 mem_cgroup_uncharge_page(page);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800730}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800731
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800732/*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800733 * We know both *page* and *newpage* are now not-on-LRU and PG_locked.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800734 * And no race with uncharge() routines because page_cgroup for *page*
735 * has extra one reference by mem_cgroup_prepare_migration.
736 */
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800737void mem_cgroup_page_migration(struct page *page, struct page *newpage)
738{
739 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800740 struct mem_cgroup_per_zone *mz;
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800741 unsigned long flags;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800742
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800743retry:
744 pc = page_get_page_cgroup(page);
745 if (!pc)
746 return;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800747
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800748 mz = page_cgroup_zoneinfo(pc);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800749 if (clear_page_cgroup(page, pc) != pc)
750 goto retry;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800751
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800752 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800753 __mem_cgroup_remove_list(pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800754 spin_unlock_irqrestore(&mz->lru_lock, flags);
755
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800756 pc->page = newpage;
757 lock_page_cgroup(newpage);
758 page_assign_page_cgroup(newpage, pc);
759 unlock_page_cgroup(newpage);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800760
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800761 mz = page_cgroup_zoneinfo(pc);
762 spin_lock_irqsave(&mz->lru_lock, flags);
763 __mem_cgroup_add_list(pc);
764 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800765}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800766
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800767/*
768 * This routine traverse page_cgroup in given list and drop them all.
769 * This routine ignores page_cgroup->ref_cnt.
770 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
771 */
772#define FORCE_UNCHARGE_BATCH (128)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800773static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800774 struct mem_cgroup_per_zone *mz,
775 int active)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800776{
777 struct page_cgroup *pc;
778 struct page *page;
779 int count;
780 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800781 struct list_head *list;
782
783 if (active)
784 list = &mz->active_list;
785 else
786 list = &mz->inactive_list;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800787
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800788 if (list_empty(list))
789 return;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800790retry:
791 count = FORCE_UNCHARGE_BATCH;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800792 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800793
794 while (--count && !list_empty(list)) {
795 pc = list_entry(list->prev, struct page_cgroup, lru);
796 page = pc->page;
797 /* Avoid race with charge */
798 atomic_set(&pc->ref_cnt, 0);
799 if (clear_page_cgroup(page, pc) == pc) {
800 css_put(&mem->css);
801 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800802 __mem_cgroup_remove_list(pc);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800803 kfree(pc);
804 } else /* being uncharged ? ...do relax */
805 break;
806 }
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800807
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800808 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800809 if (!list_empty(list)) {
810 cond_resched();
811 goto retry;
812 }
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800813}
814
815/*
816 * make mem_cgroup's charge to be 0 if there is no task.
817 * This enables deleting this mem_cgroup.
818 */
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800819static int mem_cgroup_force_empty(struct mem_cgroup *mem)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800820{
821 int ret = -EBUSY;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800822 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800823
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800824 css_get(&mem->css);
825 /*
826 * page reclaim code (kswapd etc..) will move pages between
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800827 * active_list <-> inactive_list while we don't take a lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800828 * So, we have to do loop here until all lists are empty.
829 */
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800830 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800831 if (atomic_read(&mem->css.cgroup->count) > 0)
832 goto out;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800833 for_each_node_state(node, N_POSSIBLE)
834 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
835 struct mem_cgroup_per_zone *mz;
836 mz = mem_cgroup_zoneinfo(mem, node, zid);
837 /* drop all page_cgroup in active_list */
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800838 mem_cgroup_force_empty_list(mem, mz, 1);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800839 /* drop all page_cgroup in inactive_list */
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800840 mem_cgroup_force_empty_list(mem, mz, 0);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800841 }
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800842 }
843 ret = 0;
844out:
845 css_put(&mem->css);
846 return ret;
847}
848
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800849static int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
Balbir Singh0eea1032008-02-07 00:13:57 -0800850{
851 *tmp = memparse(buf, &buf);
852 if (*buf != '\0')
853 return -EINVAL;
854
855 /*
856 * Round up the value to the closest page size
857 */
858 *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
859 return 0;
860}
861
862static ssize_t mem_cgroup_read(struct cgroup *cont,
863 struct cftype *cft, struct file *file,
864 char __user *userbuf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800865{
866 return res_counter_read(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800867 cft->private, userbuf, nbytes, ppos,
868 NULL);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800869}
870
871static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
872 struct file *file, const char __user *userbuf,
873 size_t nbytes, loff_t *ppos)
874{
875 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800876 cft->private, userbuf, nbytes, ppos,
877 mem_cgroup_write_strategy);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800878}
879
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800880static ssize_t mem_force_empty_write(struct cgroup *cont,
881 struct cftype *cft, struct file *file,
882 const char __user *userbuf,
883 size_t nbytes, loff_t *ppos)
884{
885 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800886 int ret = mem_cgroup_force_empty(mem);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800887 if (!ret)
888 ret = nbytes;
889 return ret;
890}
891
892/*
893 * Note: This should be removed if cgroup supports write-only file.
894 */
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800895static ssize_t mem_force_empty_read(struct cgroup *cont,
896 struct cftype *cft,
897 struct file *file, char __user *userbuf,
898 size_t nbytes, loff_t *ppos)
899{
900 return -EINVAL;
901}
902
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800903static const struct mem_cgroup_stat_desc {
904 const char *msg;
905 u64 unit;
906} mem_cgroup_stat_desc[] = {
907 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
908 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
909};
910
911static int mem_control_stat_show(struct seq_file *m, void *arg)
912{
913 struct cgroup *cont = m->private;
914 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
915 struct mem_cgroup_stat *stat = &mem_cont->stat;
916 int i;
917
918 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
919 s64 val;
920
921 val = mem_cgroup_read_stat(stat, i);
922 val *= mem_cgroup_stat_desc[i].unit;
923 seq_printf(m, "%s %lld\n", mem_cgroup_stat_desc[i].msg,
924 (long long)val);
925 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800926 /* showing # of active pages */
927 {
928 unsigned long active, inactive;
929
930 inactive = mem_cgroup_get_all_zonestat(mem_cont,
931 MEM_CGROUP_ZSTAT_INACTIVE);
932 active = mem_cgroup_get_all_zonestat(mem_cont,
933 MEM_CGROUP_ZSTAT_ACTIVE);
934 seq_printf(m, "active %ld\n", (active) * PAGE_SIZE);
935 seq_printf(m, "inactive %ld\n", (inactive) * PAGE_SIZE);
936 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800937 return 0;
938}
939
940static const struct file_operations mem_control_stat_file_operations = {
941 .read = seq_read,
942 .llseek = seq_lseek,
943 .release = single_release,
944};
945
946static int mem_control_stat_open(struct inode *unused, struct file *file)
947{
948 /* XXX __d_cont */
949 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
950
951 file->f_op = &mem_control_stat_file_operations;
952 return single_open(file, mem_control_stat_show, cont);
953}
954
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800955static struct cftype mem_cgroup_files[] = {
956 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800957 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800958 .private = RES_USAGE,
959 .read = mem_cgroup_read,
960 },
961 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800962 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800963 .private = RES_LIMIT,
964 .write = mem_cgroup_write,
965 .read = mem_cgroup_read,
966 },
967 {
968 .name = "failcnt",
969 .private = RES_FAILCNT,
970 .read = mem_cgroup_read,
971 },
Balbir Singh8697d332008-02-07 00:13:59 -0800972 {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800973 .name = "force_empty",
974 .write = mem_force_empty_write,
975 .read = mem_force_empty_read,
976 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800977 {
978 .name = "stat",
979 .open = mem_control_stat_open,
980 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800981};
982
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800983static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
984{
985 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800986 struct mem_cgroup_per_zone *mz;
987 int zone;
988 /*
989 * This routine is called against possible nodes.
990 * But it's BUG to call kmalloc() against offline node.
991 *
992 * TODO: this routine can waste much memory for nodes which will
993 * never be onlined. It's better to use memory hotplug callback
994 * function.
995 */
996 if (node_state(node, N_HIGH_MEMORY))
997 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, node);
998 else
999 pn = kmalloc(sizeof(*pn), GFP_KERNEL);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001000 if (!pn)
1001 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001002
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001003 mem->info.nodeinfo[node] = pn;
1004 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001005
1006 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1007 mz = &pn->zoneinfo[zone];
1008 INIT_LIST_HEAD(&mz->active_list);
1009 INIT_LIST_HEAD(&mz->inactive_list);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001010 spin_lock_init(&mz->lru_lock);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001011 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001012 return 0;
1013}
1014
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001015static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1016{
1017 kfree(mem->info.nodeinfo[node]);
1018}
1019
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001020static struct cgroup_subsys_state *
1021mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1022{
1023 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001024 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001025
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001026 if (unlikely((cont->parent) == NULL)) {
1027 mem = &init_mem_cgroup;
1028 init_mm.mem_cgroup = mem;
1029 } else
1030 mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
1031
1032 if (mem == NULL)
Li Zefan2dda81c2008-02-23 15:24:14 -08001033 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001034
1035 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001036
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001037 memset(&mem->info, 0, sizeof(mem->info));
1038
1039 for_each_node_state(node, N_POSSIBLE)
1040 if (alloc_mem_cgroup_per_zone_info(mem, node))
1041 goto free_out;
1042
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001043 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001044free_out:
1045 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001046 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001047 if (cont->parent != NULL)
1048 kfree(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001049 return ERR_PTR(-ENOMEM);
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001050}
1051
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001052static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1053 struct cgroup *cont)
1054{
1055 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1056 mem_cgroup_force_empty(mem);
1057}
1058
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001059static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1060 struct cgroup *cont)
1061{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001062 int node;
1063 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1064
1065 for_each_node_state(node, N_POSSIBLE)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001066 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001067
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001068 kfree(mem_cgroup_from_cont(cont));
1069}
1070
1071static int mem_cgroup_populate(struct cgroup_subsys *ss,
1072 struct cgroup *cont)
1073{
1074 return cgroup_add_files(cont, ss, mem_cgroup_files,
1075 ARRAY_SIZE(mem_cgroup_files));
1076}
1077
Balbir Singh67e465a2008-02-07 00:13:54 -08001078static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1079 struct cgroup *cont,
1080 struct cgroup *old_cont,
1081 struct task_struct *p)
1082{
1083 struct mm_struct *mm;
1084 struct mem_cgroup *mem, *old_mem;
1085
1086 mm = get_task_mm(p);
1087 if (mm == NULL)
1088 return;
1089
1090 mem = mem_cgroup_from_cont(cont);
1091 old_mem = mem_cgroup_from_cont(old_cont);
1092
1093 if (mem == old_mem)
1094 goto out;
1095
1096 /*
1097 * Only thread group leaders are allowed to migrate, the mm_struct is
1098 * in effect owned by the leader
1099 */
1100 if (p->tgid != p->pid)
1101 goto out;
1102
1103 css_get(&mem->css);
1104 rcu_assign_pointer(mm->mem_cgroup, mem);
1105 css_put(&old_mem->css);
1106
1107out:
1108 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001109}
1110
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001111struct cgroup_subsys mem_cgroup_subsys = {
1112 .name = "memory",
1113 .subsys_id = mem_cgroup_subsys_id,
1114 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001115 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001116 .destroy = mem_cgroup_destroy,
1117 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001118 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001119 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001120};