blob: 40cdba68de34c0d5960105795a05c9b2792ab2e1 [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 {
92 unsigned long count[NR_MEM_CGROUP_ZSTAT];
93};
94/* Macro for accessing counter */
95#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
96
97struct mem_cgroup_per_node {
98 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
99};
100
101struct mem_cgroup_lru_info {
102 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
103};
104
105/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800106 * The memory controller data structure. The memory controller controls both
107 * page cache and RSS per cgroup. We would eventually like to provide
108 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
109 * to help the administrator determine what knobs to tune.
110 *
111 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800112 * we hit the water mark. May be even add a low water mark, such that
113 * no reclaim occurs from a cgroup at it's low water mark, this is
114 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800115 */
116struct mem_cgroup {
117 struct cgroup_subsys_state css;
118 /*
119 * the counter to account for memory usage
120 */
121 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800122 /*
123 * Per cgroup active and inactive list, similar to the
124 * per zone LRU lists.
125 * TODO: Consider making these lists per zone
126 */
127 struct list_head active_list;
128 struct list_head inactive_list;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800129 struct mem_cgroup_lru_info info;
Balbir Singh66e17072008-02-07 00:13:56 -0800130 /*
131 * spin_lock to protect the per cgroup LRU
132 */
133 spinlock_t lru_lock;
Balbir Singh8697d332008-02-07 00:13:59 -0800134 unsigned long control_type; /* control RSS or RSS+Pagecache */
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800135 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800136 /*
137 * statistics.
138 */
139 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800140};
141
142/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800143 * We use the lower bit of the page->page_cgroup pointer as a bit spin
144 * lock. We need to ensure that page->page_cgroup is atleast two
145 * byte aligned (based on comments from Nick Piggin)
146 */
147#define PAGE_CGROUP_LOCK_BIT 0x0
148#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
149
150/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800151 * A page_cgroup page is associated with every page descriptor. The
152 * page_cgroup helps us identify information about the cgroup
153 */
154struct page_cgroup {
155 struct list_head lru; /* per cgroup LRU list */
156 struct page *page;
157 struct mem_cgroup *mem_cgroup;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800158 atomic_t ref_cnt; /* Helpful when pages move b/w */
159 /* mapped and cached states */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800160 int flags;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800161};
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800162#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800163#define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800164
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800165static inline int page_cgroup_nid(struct page_cgroup *pc)
166{
167 return page_to_nid(pc->page);
168}
169
170static inline enum zone_type page_cgroup_zid(struct page_cgroup *pc)
171{
172 return page_zonenum(pc->page);
173}
174
Balbir Singh8697d332008-02-07 00:13:59 -0800175enum {
176 MEM_CGROUP_TYPE_UNSPEC = 0,
177 MEM_CGROUP_TYPE_MAPPED,
178 MEM_CGROUP_TYPE_CACHED,
179 MEM_CGROUP_TYPE_ALL,
180 MEM_CGROUP_TYPE_MAX,
181};
182
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800183enum charge_type {
184 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
185 MEM_CGROUP_CHARGE_TYPE_MAPPED,
186};
187
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800188
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800189/*
190 * Always modified under lru lock. Then, not necessary to preempt_disable()
191 */
192static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
193 bool charge)
194{
195 int val = (charge)? 1 : -1;
196 struct mem_cgroup_stat *stat = &mem->stat;
197 VM_BUG_ON(!irqs_disabled());
198
199 if (flags & PAGE_CGROUP_FLAG_CACHE)
200 __mem_cgroup_stat_add_safe(stat,
201 MEM_CGROUP_STAT_CACHE, val);
202 else
203 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800204}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800205
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800206static inline struct mem_cgroup_per_zone *
207mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
208{
209 BUG_ON(!mem->info.nodeinfo[nid]);
210 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
211}
212
213static inline struct mem_cgroup_per_zone *
214page_cgroup_zoneinfo(struct page_cgroup *pc)
215{
216 struct mem_cgroup *mem = pc->mem_cgroup;
217 int nid = page_cgroup_nid(pc);
218 int zid = page_cgroup_zid(pc);
219
220 return mem_cgroup_zoneinfo(mem, nid, zid);
221}
222
223static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
224 enum mem_cgroup_zstat_index idx)
225{
226 int nid, zid;
227 struct mem_cgroup_per_zone *mz;
228 u64 total = 0;
229
230 for_each_online_node(nid)
231 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
232 mz = mem_cgroup_zoneinfo(mem, nid, zid);
233 total += MEM_CGROUP_ZSTAT(mz, idx);
234 }
235 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800236}
237
Balbir Singh8697d332008-02-07 00:13:59 -0800238static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800239
240static inline
241struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
242{
243 return container_of(cgroup_subsys_state(cont,
244 mem_cgroup_subsys_id), struct mem_cgroup,
245 css);
246}
247
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800248static inline
249struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
250{
251 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
252 struct mem_cgroup, css);
253}
254
255void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
256{
257 struct mem_cgroup *mem;
258
259 mem = mem_cgroup_from_task(p);
260 css_get(&mem->css);
261 mm->mem_cgroup = mem;
262}
263
264void mm_free_cgroup(struct mm_struct *mm)
265{
266 css_put(&mm->mem_cgroup->css);
267}
268
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800269static inline int page_cgroup_locked(struct page *page)
270{
271 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
272 &page->page_cgroup);
273}
274
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800275void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
276{
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800277 int locked;
278
279 /*
280 * While resetting the page_cgroup we might not hold the
281 * page_cgroup lock. free_hot_cold_page() is an example
282 * of such a scenario
283 */
284 if (pc)
285 VM_BUG_ON(!page_cgroup_locked(page));
286 locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
287 page->page_cgroup = ((unsigned long)pc | locked);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800288}
289
290struct page_cgroup *page_get_page_cgroup(struct page *page)
291{
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800292 return (struct page_cgroup *)
293 (page->page_cgroup & ~PAGE_CGROUP_LOCK);
294}
295
Balbir Singh8697d332008-02-07 00:13:59 -0800296static void __always_inline lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800297{
298 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
299 VM_BUG_ON(!page_cgroup_locked(page));
300}
301
Balbir Singh8697d332008-02-07 00:13:59 -0800302static void __always_inline unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800303{
304 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
305}
306
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800307/*
308 * Tie new page_cgroup to struct page under lock_page_cgroup()
309 * This can fail if the page has been tied to a page_cgroup.
310 * If success, returns 0.
311 */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800312static int page_cgroup_assign_new_page_cgroup(struct page *page,
313 struct page_cgroup *pc)
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800314{
315 int ret = 0;
316
317 lock_page_cgroup(page);
318 if (!page_get_page_cgroup(page))
319 page_assign_page_cgroup(page, pc);
320 else /* A page is tied to other pc. */
321 ret = 1;
322 unlock_page_cgroup(page);
323 return ret;
324}
325
326/*
327 * Clear page->page_cgroup member under lock_page_cgroup().
328 * If given "pc" value is different from one page->page_cgroup,
329 * page->cgroup is not cleared.
330 * Returns a value of page->page_cgroup at lock taken.
331 * A can can detect failure of clearing by following
332 * clear_page_cgroup(page, pc) == pc
333 */
334
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800335static struct page_cgroup *clear_page_cgroup(struct page *page,
336 struct page_cgroup *pc)
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800337{
338 struct page_cgroup *ret;
339 /* lock and clear */
340 lock_page_cgroup(page);
341 ret = page_get_page_cgroup(page);
342 if (likely(ret == pc))
343 page_assign_page_cgroup(page, NULL);
344 unlock_page_cgroup(page);
345 return ret;
346}
347
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800348static void __mem_cgroup_remove_list(struct page_cgroup *pc)
349{
350 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
351 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
352
353 if (from)
354 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
355 else
356 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
357
358 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
359 list_del_init(&pc->lru);
360}
361
362static void __mem_cgroup_add_list(struct page_cgroup *pc)
363{
364 int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
365 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
366
367 if (!to) {
368 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
369 list_add(&pc->lru, &pc->mem_cgroup->inactive_list);
370 } else {
371 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
372 list_add(&pc->lru, &pc->mem_cgroup->active_list);
373 }
374 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
375}
376
Balbir Singh8697d332008-02-07 00:13:59 -0800377static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800378{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800379 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
380 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
381
382 if (from)
383 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
384 else
385 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
386
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800387 if (active) {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800388 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800389 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
Balbir Singh66e17072008-02-07 00:13:56 -0800390 list_move(&pc->lru, &pc->mem_cgroup->active_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800391 } else {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800392 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800393 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
Balbir Singh66e17072008-02-07 00:13:56 -0800394 list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800395 }
Balbir Singh66e17072008-02-07 00:13:56 -0800396}
397
David Rientjes4c4a2212008-02-07 00:14:06 -0800398int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
399{
400 int ret;
401
402 task_lock(task);
403 ret = task->mm && mm_cgroup(task->mm) == mem;
404 task_unlock(task);
405 return ret;
406}
407
Balbir Singh66e17072008-02-07 00:13:56 -0800408/*
409 * This routine assumes that the appropriate zone's lru lock is already held
410 */
411void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
412{
413 struct mem_cgroup *mem;
414 if (!pc)
415 return;
416
417 mem = pc->mem_cgroup;
418
419 spin_lock(&mem->lru_lock);
420 __mem_cgroup_move_lists(pc, active);
421 spin_unlock(&mem->lru_lock);
422}
423
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800424/*
425 * Calculate mapped_ratio under memory controller. This will be used in
426 * vmscan.c for deteremining we have to reclaim mapped pages.
427 */
428int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
429{
430 long total, rss;
431
432 /*
433 * usage is recorded in bytes. But, here, we assume the number of
434 * physical pages can be represented by "long" on any arch.
435 */
436 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
437 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
438 return (int)((rss * 100L) / total);
439}
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800440/*
441 * This function is called from vmscan.c. In page reclaiming loop. balance
442 * between active and inactive list is calculated. For memory controller
443 * page reclaiming, we should use using mem_cgroup's imbalance rather than
444 * zone's global lru imbalance.
445 */
446long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
447{
448 unsigned long active, inactive;
449 /* active and inactive are the number of pages. 'long' is ok.*/
450 active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
451 inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
452 return (long) (active / (inactive + 1));
453}
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800454
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800455/*
456 * prev_priority control...this will be used in memory reclaim path.
457 */
458int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
459{
460 return mem->prev_priority;
461}
462
463void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
464{
465 if (priority < mem->prev_priority)
466 mem->prev_priority = priority;
467}
468
469void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
470{
471 mem->prev_priority = priority;
472}
473
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800474/*
475 * Calculate # of pages to be scanned in this priority/zone.
476 * See also vmscan.c
477 *
478 * priority starts from "DEF_PRIORITY" and decremented in each loop.
479 * (see include/linux/mmzone.h)
480 */
481
482long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
483 struct zone *zone, int priority)
484{
485 long nr_active;
486 int nid = zone->zone_pgdat->node_id;
487 int zid = zone_idx(zone);
488 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
489
490 nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
491 return (nr_active >> priority);
492}
493
494long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
495 struct zone *zone, int priority)
496{
497 long nr_inactive;
498 int nid = zone->zone_pgdat->node_id;
499 int zid = zone_idx(zone);
500 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
501
502 nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
503
504 return (nr_inactive >> priority);
505}
506
Balbir Singh66e17072008-02-07 00:13:56 -0800507unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
508 struct list_head *dst,
509 unsigned long *scanned, int order,
510 int mode, struct zone *z,
511 struct mem_cgroup *mem_cont,
512 int active)
513{
514 unsigned long nr_taken = 0;
515 struct page *page;
516 unsigned long scan;
517 LIST_HEAD(pc_list);
518 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800519 struct page_cgroup *pc, *tmp;
Balbir Singh66e17072008-02-07 00:13:56 -0800520
521 if (active)
522 src = &mem_cont->active_list;
523 else
524 src = &mem_cont->inactive_list;
525
526 spin_lock(&mem_cont->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800527 scan = 0;
528 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800529 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800530 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800531 page = pc->page;
532 VM_BUG_ON(!pc);
533
Hugh Dickins436c65412008-02-07 00:14:12 -0800534 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800535 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800536
Balbir Singh66e17072008-02-07 00:13:56 -0800537 if (PageActive(page) && !active) {
538 __mem_cgroup_move_lists(pc, true);
Balbir Singh66e17072008-02-07 00:13:56 -0800539 continue;
540 }
541 if (!PageActive(page) && active) {
542 __mem_cgroup_move_lists(pc, false);
Balbir Singh66e17072008-02-07 00:13:56 -0800543 continue;
544 }
545
546 /*
547 * Reclaim, per zone
548 * TODO: make the active/inactive lists per zone
549 */
550 if (page_zone(page) != z)
551 continue;
552
Hugh Dickins436c65412008-02-07 00:14:12 -0800553 scan++;
554 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800555
556 if (__isolate_lru_page(page, mode) == 0) {
557 list_move(&page->lru, dst);
558 nr_taken++;
559 }
560 }
561
562 list_splice(&pc_list, src);
563 spin_unlock(&mem_cont->lru_lock);
564
565 *scanned = scan;
566 return nr_taken;
567}
568
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800569/*
570 * Charge the memory controller for page usage.
571 * Return
572 * 0 if the charge was successful
573 * < 0 if the cgroup is over its limit
574 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800575static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
576 gfp_t gfp_mask, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800577{
578 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800579 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800580 unsigned long flags;
581 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800582
583 /*
584 * Should page_cgroup's go to their own slab?
585 * One could optimize the performance of the charging routine
586 * by saving a bit in the page_flags and using it as a lock
587 * to see if the cgroup page already has a page_cgroup associated
588 * with it
589 */
Balbir Singh66e17072008-02-07 00:13:56 -0800590retry:
Hugh Dickins82369552008-02-07 00:14:22 -0800591 if (page) {
592 lock_page_cgroup(page);
593 pc = page_get_page_cgroup(page);
594 /*
595 * The page_cgroup exists and
596 * the page has already been accounted.
597 */
598 if (pc) {
599 if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
600 /* this page is under being uncharged ? */
601 unlock_page_cgroup(page);
602 cpu_relax();
603 goto retry;
604 } else {
605 unlock_page_cgroup(page);
606 goto done;
607 }
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800608 }
Hugh Dickins82369552008-02-07 00:14:22 -0800609 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800610 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800611
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800612 pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800613 if (pc == NULL)
614 goto err;
615
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800616 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800617 * We always charge the cgroup the mm_struct belongs to.
618 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800619 * thread group leader migrates. It's possible that mm is not
620 * set, if so charge the init_mm (happens for pagecache usage).
621 */
622 if (!mm)
623 mm = &init_mm;
624
Hugh Dickins3be91272008-02-07 00:14:19 -0800625 rcu_read_lock();
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800626 mem = rcu_dereference(mm->mem_cgroup);
627 /*
628 * For every charge from the cgroup, increment reference
629 * count
630 */
631 css_get(&mem->css);
632 rcu_read_unlock();
633
634 /*
635 * If we created the page_cgroup, we should free it on exceeding
636 * the cgroup limit.
637 */
Balbir Singh0eea1032008-02-07 00:13:57 -0800638 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Hugh Dickins3be91272008-02-07 00:14:19 -0800639 if (!(gfp_mask & __GFP_WAIT))
640 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800641
642 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800643 continue;
644
645 /*
646 * try_to_free_mem_cgroup_pages() might not give us a full
647 * picture of reclaim. Some pages are reclaimed and might be
648 * moved to swap cache or just unmapped from the cgroup.
649 * Check the limit again to see if the reclaim reduced the
650 * current usage of the cgroup before giving up
651 */
652 if (res_counter_check_under_limit(&mem->res))
653 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800654
655 if (!nr_retries--) {
656 mem_cgroup_out_of_memory(mem, gfp_mask);
657 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800658 }
Hugh Dickins3be91272008-02-07 00:14:19 -0800659 congestion_wait(WRITE, HZ/10);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800660 }
661
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800662 atomic_set(&pc->ref_cnt, 1);
663 pc->mem_cgroup = mem;
664 pc->page = page;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800665 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800666 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
667 pc->flags |= PAGE_CGROUP_FLAG_CACHE;
Hugh Dickins3be91272008-02-07 00:14:19 -0800668
Hugh Dickins82369552008-02-07 00:14:22 -0800669 if (!page || page_cgroup_assign_new_page_cgroup(page, pc)) {
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800670 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800671 * Another charge has been added to this page already.
672 * We take lock_page_cgroup(page) again and read
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800673 * page->cgroup, increment refcnt.... just retry is OK.
674 */
675 res_counter_uncharge(&mem->res, PAGE_SIZE);
676 css_put(&mem->css);
677 kfree(pc);
Hugh Dickins82369552008-02-07 00:14:22 -0800678 if (!page)
679 goto done;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800680 goto retry;
681 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800682
Balbir Singh66e17072008-02-07 00:13:56 -0800683 spin_lock_irqsave(&mem->lru_lock, flags);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800684 /* Update statistics vector */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800685 __mem_cgroup_add_list(pc);
Balbir Singh66e17072008-02-07 00:13:56 -0800686 spin_unlock_irqrestore(&mem->lru_lock, flags);
687
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800688done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800689 return 0;
Hugh Dickins3be91272008-02-07 00:14:19 -0800690out:
691 css_put(&mem->css);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800692 kfree(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800693err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800694 return -ENOMEM;
695}
696
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800697int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
698 gfp_t gfp_mask)
699{
700 return mem_cgroup_charge_common(page, mm, gfp_mask,
701 MEM_CGROUP_CHARGE_TYPE_MAPPED);
702}
703
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800704/*
Balbir Singh8697d332008-02-07 00:13:59 -0800705 * See if the cached pages should be charged at all?
706 */
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800707int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
708 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800709{
Balbir Singhac44d352008-02-07 00:14:18 -0800710 int ret = 0;
Balbir Singh8697d332008-02-07 00:13:59 -0800711 struct mem_cgroup *mem;
712 if (!mm)
713 mm = &init_mm;
714
Balbir Singhac44d352008-02-07 00:14:18 -0800715 rcu_read_lock();
Balbir Singh8697d332008-02-07 00:13:59 -0800716 mem = rcu_dereference(mm->mem_cgroup);
Balbir Singhac44d352008-02-07 00:14:18 -0800717 css_get(&mem->css);
718 rcu_read_unlock();
Balbir Singh8697d332008-02-07 00:13:59 -0800719 if (mem->control_type == MEM_CGROUP_TYPE_ALL)
Balbir Singhac44d352008-02-07 00:14:18 -0800720 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800721 MEM_CGROUP_CHARGE_TYPE_CACHE);
Balbir Singhac44d352008-02-07 00:14:18 -0800722 css_put(&mem->css);
723 return ret;
Balbir Singh8697d332008-02-07 00:13:59 -0800724}
725
726/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800727 * Uncharging is always a welcome operation, we never complain, simply
728 * uncharge.
729 */
730void mem_cgroup_uncharge(struct page_cgroup *pc)
731{
732 struct mem_cgroup *mem;
733 struct page *page;
Balbir Singh66e17072008-02-07 00:13:56 -0800734 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800735
Balbir Singh8697d332008-02-07 00:13:59 -0800736 /*
737 * This can handle cases when a page is not charged at all and we
738 * are switching between handling the control_type.
739 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800740 if (!pc)
741 return;
742
743 if (atomic_dec_and_test(&pc->ref_cnt)) {
744 page = pc->page;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800745 /*
746 * get page->cgroup and clear it under lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800747 * force_empty can drop page->cgroup without checking refcnt.
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800748 */
749 if (clear_page_cgroup(page, pc) == pc) {
750 mem = pc->mem_cgroup;
751 css_put(&mem->css);
752 res_counter_uncharge(&mem->res, PAGE_SIZE);
753 spin_lock_irqsave(&mem->lru_lock, flags);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800754 __mem_cgroup_remove_list(pc);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800755 spin_unlock_irqrestore(&mem->lru_lock, flags);
756 kfree(pc);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800757 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800758 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800759}
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800760
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800761/*
762 * Returns non-zero if a page (under migration) has valid page_cgroup member.
763 * Refcnt of page_cgroup is incremented.
764 */
765
766int mem_cgroup_prepare_migration(struct page *page)
767{
768 struct page_cgroup *pc;
769 int ret = 0;
770 lock_page_cgroup(page);
771 pc = page_get_page_cgroup(page);
772 if (pc && atomic_inc_not_zero(&pc->ref_cnt))
773 ret = 1;
774 unlock_page_cgroup(page);
775 return ret;
776}
777
778void mem_cgroup_end_migration(struct page *page)
779{
780 struct page_cgroup *pc = page_get_page_cgroup(page);
781 mem_cgroup_uncharge(pc);
782}
783/*
784 * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
785 * And no race with uncharge() routines because page_cgroup for *page*
786 * has extra one reference by mem_cgroup_prepare_migration.
787 */
788
789void mem_cgroup_page_migration(struct page *page, struct page *newpage)
790{
791 struct page_cgroup *pc;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800792 struct mem_cgroup *mem;
793 unsigned long flags;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800794retry:
795 pc = page_get_page_cgroup(page);
796 if (!pc)
797 return;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800798 mem = pc->mem_cgroup;
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800799 if (clear_page_cgroup(page, pc) != pc)
800 goto retry;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800801
802 spin_lock_irqsave(&mem->lru_lock, flags);
803
804 __mem_cgroup_remove_list(pc);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800805 pc->page = newpage;
806 lock_page_cgroup(newpage);
807 page_assign_page_cgroup(newpage, pc);
808 unlock_page_cgroup(newpage);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800809 __mem_cgroup_add_list(pc);
810
811 spin_unlock_irqrestore(&mem->lru_lock, flags);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800812 return;
813}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800814
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800815/*
816 * This routine traverse page_cgroup in given list and drop them all.
817 * This routine ignores page_cgroup->ref_cnt.
818 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
819 */
820#define FORCE_UNCHARGE_BATCH (128)
821static void
822mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list)
823{
824 struct page_cgroup *pc;
825 struct page *page;
826 int count;
827 unsigned long flags;
828
829retry:
830 count = FORCE_UNCHARGE_BATCH;
831 spin_lock_irqsave(&mem->lru_lock, flags);
832
833 while (--count && !list_empty(list)) {
834 pc = list_entry(list->prev, struct page_cgroup, lru);
835 page = pc->page;
836 /* Avoid race with charge */
837 atomic_set(&pc->ref_cnt, 0);
838 if (clear_page_cgroup(page, pc) == pc) {
839 css_put(&mem->css);
840 res_counter_uncharge(&mem->res, PAGE_SIZE);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800841 __mem_cgroup_remove_list(pc);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800842 kfree(pc);
843 } else /* being uncharged ? ...do relax */
844 break;
845 }
846 spin_unlock_irqrestore(&mem->lru_lock, flags);
847 if (!list_empty(list)) {
848 cond_resched();
849 goto retry;
850 }
851 return;
852}
853
854/*
855 * make mem_cgroup's charge to be 0 if there is no task.
856 * This enables deleting this mem_cgroup.
857 */
858
859int mem_cgroup_force_empty(struct mem_cgroup *mem)
860{
861 int ret = -EBUSY;
862 css_get(&mem->css);
863 /*
864 * page reclaim code (kswapd etc..) will move pages between
865` * active_list <-> inactive_list while we don't take a lock.
866 * So, we have to do loop here until all lists are empty.
867 */
868 while (!(list_empty(&mem->active_list) &&
869 list_empty(&mem->inactive_list))) {
870 if (atomic_read(&mem->css.cgroup->count) > 0)
871 goto out;
872 /* drop all page_cgroup in active_list */
873 mem_cgroup_force_empty_list(mem, &mem->active_list);
874 /* drop all page_cgroup in inactive_list */
875 mem_cgroup_force_empty_list(mem, &mem->inactive_list);
876 }
877 ret = 0;
878out:
879 css_put(&mem->css);
880 return ret;
881}
882
883
884
Balbir Singh0eea1032008-02-07 00:13:57 -0800885int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
886{
887 *tmp = memparse(buf, &buf);
888 if (*buf != '\0')
889 return -EINVAL;
890
891 /*
892 * Round up the value to the closest page size
893 */
894 *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
895 return 0;
896}
897
898static ssize_t mem_cgroup_read(struct cgroup *cont,
899 struct cftype *cft, struct file *file,
900 char __user *userbuf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800901{
902 return res_counter_read(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800903 cft->private, userbuf, nbytes, ppos,
904 NULL);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800905}
906
907static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
908 struct file *file, const char __user *userbuf,
909 size_t nbytes, loff_t *ppos)
910{
911 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800912 cft->private, userbuf, nbytes, ppos,
913 mem_cgroup_write_strategy);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800914}
915
Balbir Singh8697d332008-02-07 00:13:59 -0800916static ssize_t mem_control_type_write(struct cgroup *cont,
917 struct cftype *cft, struct file *file,
918 const char __user *userbuf,
919 size_t nbytes, loff_t *pos)
920{
921 int ret;
922 char *buf, *end;
923 unsigned long tmp;
924 struct mem_cgroup *mem;
925
926 mem = mem_cgroup_from_cont(cont);
927 buf = kmalloc(nbytes + 1, GFP_KERNEL);
928 ret = -ENOMEM;
929 if (buf == NULL)
930 goto out;
931
932 buf[nbytes] = 0;
933 ret = -EFAULT;
934 if (copy_from_user(buf, userbuf, nbytes))
935 goto out_free;
936
937 ret = -EINVAL;
938 tmp = simple_strtoul(buf, &end, 10);
939 if (*end != '\0')
940 goto out_free;
941
942 if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
943 goto out_free;
944
945 mem->control_type = tmp;
946 ret = nbytes;
947out_free:
948 kfree(buf);
949out:
950 return ret;
951}
952
953static ssize_t mem_control_type_read(struct cgroup *cont,
954 struct cftype *cft,
955 struct file *file, char __user *userbuf,
956 size_t nbytes, loff_t *ppos)
957{
958 unsigned long val;
959 char buf[64], *s;
960 struct mem_cgroup *mem;
961
962 mem = mem_cgroup_from_cont(cont);
963 s = buf;
964 val = mem->control_type;
965 s += sprintf(s, "%lu\n", val);
966 return simple_read_from_buffer((void __user *)userbuf, nbytes,
967 ppos, buf, s - buf);
968}
969
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800970
971static ssize_t mem_force_empty_write(struct cgroup *cont,
972 struct cftype *cft, struct file *file,
973 const char __user *userbuf,
974 size_t nbytes, loff_t *ppos)
975{
976 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
977 int ret;
978 ret = mem_cgroup_force_empty(mem);
979 if (!ret)
980 ret = nbytes;
981 return ret;
982}
983
984/*
985 * Note: This should be removed if cgroup supports write-only file.
986 */
987
988static ssize_t mem_force_empty_read(struct cgroup *cont,
989 struct cftype *cft,
990 struct file *file, char __user *userbuf,
991 size_t nbytes, loff_t *ppos)
992{
993 return -EINVAL;
994}
995
996
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800997static const struct mem_cgroup_stat_desc {
998 const char *msg;
999 u64 unit;
1000} mem_cgroup_stat_desc[] = {
1001 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1002 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
1003};
1004
1005static int mem_control_stat_show(struct seq_file *m, void *arg)
1006{
1007 struct cgroup *cont = m->private;
1008 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1009 struct mem_cgroup_stat *stat = &mem_cont->stat;
1010 int i;
1011
1012 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1013 s64 val;
1014
1015 val = mem_cgroup_read_stat(stat, i);
1016 val *= mem_cgroup_stat_desc[i].unit;
1017 seq_printf(m, "%s %lld\n", mem_cgroup_stat_desc[i].msg,
1018 (long long)val);
1019 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001020 /* showing # of active pages */
1021 {
1022 unsigned long active, inactive;
1023
1024 inactive = mem_cgroup_get_all_zonestat(mem_cont,
1025 MEM_CGROUP_ZSTAT_INACTIVE);
1026 active = mem_cgroup_get_all_zonestat(mem_cont,
1027 MEM_CGROUP_ZSTAT_ACTIVE);
1028 seq_printf(m, "active %ld\n", (active) * PAGE_SIZE);
1029 seq_printf(m, "inactive %ld\n", (inactive) * PAGE_SIZE);
1030 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001031 return 0;
1032}
1033
1034static const struct file_operations mem_control_stat_file_operations = {
1035 .read = seq_read,
1036 .llseek = seq_lseek,
1037 .release = single_release,
1038};
1039
1040static int mem_control_stat_open(struct inode *unused, struct file *file)
1041{
1042 /* XXX __d_cont */
1043 struct cgroup *cont = file->f_dentry->d_parent->d_fsdata;
1044
1045 file->f_op = &mem_control_stat_file_operations;
1046 return single_open(file, mem_control_stat_show, cont);
1047}
1048
1049
1050
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001051static struct cftype mem_cgroup_files[] = {
1052 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001053 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001054 .private = RES_USAGE,
1055 .read = mem_cgroup_read,
1056 },
1057 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001058 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001059 .private = RES_LIMIT,
1060 .write = mem_cgroup_write,
1061 .read = mem_cgroup_read,
1062 },
1063 {
1064 .name = "failcnt",
1065 .private = RES_FAILCNT,
1066 .read = mem_cgroup_read,
1067 },
Balbir Singh8697d332008-02-07 00:13:59 -08001068 {
1069 .name = "control_type",
1070 .write = mem_control_type_write,
1071 .read = mem_control_type_read,
1072 },
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001073 {
1074 .name = "force_empty",
1075 .write = mem_force_empty_write,
1076 .read = mem_force_empty_read,
1077 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001078 {
1079 .name = "stat",
1080 .open = mem_control_stat_open,
1081 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001082};
1083
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001084static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1085{
1086 struct mem_cgroup_per_node *pn;
1087
1088 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, node);
1089 if (!pn)
1090 return 1;
1091 mem->info.nodeinfo[node] = pn;
1092 memset(pn, 0, sizeof(*pn));
1093 return 0;
1094}
1095
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001096static struct mem_cgroup init_mem_cgroup;
1097
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001098static struct cgroup_subsys_state *
1099mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1100{
1101 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001102 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001103
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001104 if (unlikely((cont->parent) == NULL)) {
1105 mem = &init_mem_cgroup;
1106 init_mm.mem_cgroup = mem;
1107 } else
1108 mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
1109
1110 if (mem == NULL)
1111 return NULL;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001112
1113 res_counter_init(&mem->res);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -08001114 INIT_LIST_HEAD(&mem->active_list);
1115 INIT_LIST_HEAD(&mem->inactive_list);
Balbir Singh66e17072008-02-07 00:13:56 -08001116 spin_lock_init(&mem->lru_lock);
Balbir Singh8697d332008-02-07 00:13:59 -08001117 mem->control_type = MEM_CGROUP_TYPE_ALL;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001118 memset(&mem->info, 0, sizeof(mem->info));
1119
1120 for_each_node_state(node, N_POSSIBLE)
1121 if (alloc_mem_cgroup_per_zone_info(mem, node))
1122 goto free_out;
1123
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001124 return &mem->css;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001125free_out:
1126 for_each_node_state(node, N_POSSIBLE)
1127 kfree(mem->info.nodeinfo[node]);
1128 if (cont->parent != NULL)
1129 kfree(mem);
1130 return NULL;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001131}
1132
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001133static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1134 struct cgroup *cont)
1135{
1136 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1137 mem_cgroup_force_empty(mem);
1138}
1139
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001140static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1141 struct cgroup *cont)
1142{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001143 int node;
1144 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1145
1146 for_each_node_state(node, N_POSSIBLE)
1147 kfree(mem->info.nodeinfo[node]);
1148
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001149 kfree(mem_cgroup_from_cont(cont));
1150}
1151
1152static int mem_cgroup_populate(struct cgroup_subsys *ss,
1153 struct cgroup *cont)
1154{
1155 return cgroup_add_files(cont, ss, mem_cgroup_files,
1156 ARRAY_SIZE(mem_cgroup_files));
1157}
1158
Balbir Singh67e465a2008-02-07 00:13:54 -08001159static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1160 struct cgroup *cont,
1161 struct cgroup *old_cont,
1162 struct task_struct *p)
1163{
1164 struct mm_struct *mm;
1165 struct mem_cgroup *mem, *old_mem;
1166
1167 mm = get_task_mm(p);
1168 if (mm == NULL)
1169 return;
1170
1171 mem = mem_cgroup_from_cont(cont);
1172 old_mem = mem_cgroup_from_cont(old_cont);
1173
1174 if (mem == old_mem)
1175 goto out;
1176
1177 /*
1178 * Only thread group leaders are allowed to migrate, the mm_struct is
1179 * in effect owned by the leader
1180 */
1181 if (p->tgid != p->pid)
1182 goto out;
1183
1184 css_get(&mem->css);
1185 rcu_assign_pointer(mm->mem_cgroup, mem);
1186 css_put(&old_mem->css);
1187
1188out:
1189 mmput(mm);
1190 return;
1191}
1192
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001193struct cgroup_subsys mem_cgroup_subsys = {
1194 .name = "memory",
1195 .subsys_id = mem_cgroup_subsys_id,
1196 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001197 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001198 .destroy = mem_cgroup_destroy,
1199 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001200 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001201 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001202};