blob: c0500e4d3a2f9d3c5d392888326780379d064a4f [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 Singhb6ac57d2008-04-29 01:00:19 -070029#include <linux/slab.h>
Balbir Singh66e17072008-02-07 00:13:56 -080030#include <linux/swap.h>
31#include <linux/spinlock.h>
32#include <linux/fs.h>
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -080033#include <linux/seq_file.h>
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -070034#include <linux/vmalloc.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080035
Balbir Singh8697d332008-02-07 00:13:59 -080036#include <asm/uaccess.h>
37
KAMEZAWA Hiroyukia181b0e2008-07-25 01:47:08 -070038struct cgroup_subsys mem_cgroup_subsys __read_mostly;
39static struct kmem_cache *page_cgroup_cache __read_mostly;
40#define MEM_CGROUP_RECLAIM_RETRIES 5
Balbir Singh8cdea7c2008-02-07 00:13:50 -080041
42/*
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080043 * Statistics for memory cgroup.
44 */
45enum mem_cgroup_stat_index {
46 /*
47 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
48 */
49 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
50 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
Balaji Rao55e462b2008-05-01 04:35:12 -070051 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
52 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -080053
54 MEM_CGROUP_STAT_NSTATS,
55};
56
57struct mem_cgroup_stat_cpu {
58 s64 count[MEM_CGROUP_STAT_NSTATS];
59} ____cacheline_aligned_in_smp;
60
61struct mem_cgroup_stat {
62 struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
63};
64
65/*
66 * For accounting under irq disable, no need for increment preempt count.
67 */
68static void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
69 enum mem_cgroup_stat_index idx, int val)
70{
71 int cpu = smp_processor_id();
72 stat->cpustat[cpu].count[idx] += val;
73}
74
75static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
76 enum mem_cgroup_stat_index idx)
77{
78 int cpu;
79 s64 ret = 0;
80 for_each_possible_cpu(cpu)
81 ret += stat->cpustat[cpu].count[idx];
82 return ret;
83}
84
85/*
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -080086 * per-zone information in memory controller.
87 */
88
89enum mem_cgroup_zstat_index {
90 MEM_CGROUP_ZSTAT_ACTIVE,
91 MEM_CGROUP_ZSTAT_INACTIVE,
92
93 NR_MEM_CGROUP_ZSTAT,
94};
95
96struct mem_cgroup_per_zone {
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -080097 /*
98 * spin_lock to protect the per cgroup LRU
99 */
100 spinlock_t lru_lock;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800101 struct list_head active_list;
102 struct list_head inactive_list;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800103 unsigned long count[NR_MEM_CGROUP_ZSTAT];
104};
105/* Macro for accessing counter */
106#define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
107
108struct mem_cgroup_per_node {
109 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
110};
111
112struct mem_cgroup_lru_info {
113 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
114};
115
116/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800117 * The memory controller data structure. The memory controller controls both
118 * page cache and RSS per cgroup. We would eventually like to provide
119 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
120 * to help the administrator determine what knobs to tune.
121 *
122 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800123 * we hit the water mark. May be even add a low water mark, such that
124 * no reclaim occurs from a cgroup at it's low water mark, this is
125 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800126 */
127struct mem_cgroup {
128 struct cgroup_subsys_state css;
129 /*
130 * the counter to account for memory usage
131 */
132 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800133 /*
134 * Per cgroup active and inactive list, similar to the
135 * per zone LRU lists.
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800136 */
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800137 struct mem_cgroup_lru_info info;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800138
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800139 int prev_priority; /* for recording reclaim priority */
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800140 /*
141 * statistics.
142 */
143 struct mem_cgroup_stat stat;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800144};
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800145static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800146
147/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800148 * We use the lower bit of the page->page_cgroup pointer as a bit spin
Hugh Dickins9442ec92008-03-04 14:29:07 -0800149 * lock. We need to ensure that page->page_cgroup is at least two
150 * byte aligned (based on comments from Nick Piggin). But since
151 * bit_spin_lock doesn't actually set that lock bit in a non-debug
152 * uniprocessor kernel, we should avoid setting it here too.
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800153 */
154#define PAGE_CGROUP_LOCK_BIT 0x0
Hugh Dickins9442ec92008-03-04 14:29:07 -0800155#if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
156#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
157#else
158#define PAGE_CGROUP_LOCK 0x0
159#endif
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800160
161/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800162 * A page_cgroup page is associated with every page descriptor. The
163 * page_cgroup helps us identify information about the cgroup
164 */
165struct page_cgroup {
166 struct list_head lru; /* per cgroup LRU list */
167 struct page *page;
168 struct mem_cgroup *mem_cgroup;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800169 int flags;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800170};
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800171#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800172#define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800173
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800174static int page_cgroup_nid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800175{
176 return page_to_nid(pc->page);
177}
178
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800179static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
KAMEZAWA Hiroyukic01495302008-02-07 00:14:30 -0800180{
181 return page_zonenum(pc->page);
182}
183
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800184enum charge_type {
185 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
186 MEM_CGROUP_CHARGE_TYPE_MAPPED,
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700187 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800188};
189
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800190/*
191 * Always modified under lru lock. Then, not necessary to preempt_disable()
192 */
193static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
194 bool charge)
195{
196 int val = (charge)? 1 : -1;
197 struct mem_cgroup_stat *stat = &mem->stat;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800198
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800199 VM_BUG_ON(!irqs_disabled());
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800200 if (flags & PAGE_CGROUP_FLAG_CACHE)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800201 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800202 else
203 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
Balaji Rao55e462b2008-05-01 04:35:12 -0700204
205 if (charge)
206 __mem_cgroup_stat_add_safe(stat,
207 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
208 else
209 __mem_cgroup_stat_add_safe(stat,
210 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800211}
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800212
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800213static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800214mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
215{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800216 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
217}
218
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800219static struct mem_cgroup_per_zone *
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800220page_cgroup_zoneinfo(struct page_cgroup *pc)
221{
222 struct mem_cgroup *mem = pc->mem_cgroup;
223 int nid = page_cgroup_nid(pc);
224 int zid = page_cgroup_zid(pc);
225
226 return mem_cgroup_zoneinfo(mem, nid, zid);
227}
228
229static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
230 enum mem_cgroup_zstat_index idx)
231{
232 int nid, zid;
233 struct mem_cgroup_per_zone *mz;
234 u64 total = 0;
235
236 for_each_online_node(nid)
237 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
238 mz = mem_cgroup_zoneinfo(mem, nid, zid);
239 total += MEM_CGROUP_ZSTAT(mz, idx);
240 }
241 return total;
KAMEZAWA Hiroyukid52aa412008-02-07 00:14:24 -0800242}
243
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800244static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800245{
246 return container_of(cgroup_subsys_state(cont,
247 mem_cgroup_subsys_id), struct mem_cgroup,
248 css);
249}
250
Balbir Singhcf475ad2008-04-29 01:00:16 -0700251struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800252{
253 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
254 struct mem_cgroup, css);
255}
256
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800257static inline int page_cgroup_locked(struct page *page)
258{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800259 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800260}
261
Hugh Dickins9442ec92008-03-04 14:29:07 -0800262static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800263{
Hugh Dickins9442ec92008-03-04 14:29:07 -0800264 VM_BUG_ON(!page_cgroup_locked(page));
265 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800266}
267
268struct page_cgroup *page_get_page_cgroup(struct page *page)
269{
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800270 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800271}
272
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800273static void lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800274{
275 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800276}
277
Hugh Dickins2680eed2008-03-04 14:29:13 -0800278static int try_lock_page_cgroup(struct page *page)
279{
280 return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
281}
282
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800283static void unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800284{
285 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
286}
287
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700288static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
289 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800290{
291 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800292
293 if (from)
294 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
295 else
296 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
297
298 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700299 list_del(&pc->lru);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800300}
301
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700302static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
303 struct page_cgroup *pc)
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800304{
305 int to = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800306
307 if (!to) {
308 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800309 list_add(&pc->lru, &mz->inactive_list);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800310 } else {
311 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800312 list_add(&pc->lru, &mz->active_list);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800313 }
314 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
315}
316
Balbir Singh8697d332008-02-07 00:13:59 -0800317static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800318{
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800319 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
320 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
321
322 if (from)
323 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) -= 1;
324 else
325 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) -= 1;
326
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800327 if (active) {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800328 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800329 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800330 list_move(&pc->lru, &mz->active_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800331 } else {
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800332 MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE) += 1;
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800333 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800334 list_move(&pc->lru, &mz->inactive_list);
KAMEZAWA Hiroyuki3564c7c2008-02-07 00:14:23 -0800335 }
Balbir Singh66e17072008-02-07 00:13:56 -0800336}
337
David Rientjes4c4a2212008-02-07 00:14:06 -0800338int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
339{
340 int ret;
341
342 task_lock(task);
Hugh Dickinsbd845e32008-03-04 14:29:01 -0800343 ret = task->mm && mm_match_cgroup(task->mm, mem);
David Rientjes4c4a2212008-02-07 00:14:06 -0800344 task_unlock(task);
345 return ret;
346}
347
Balbir Singh66e17072008-02-07 00:13:56 -0800348/*
349 * This routine assumes that the appropriate zone's lru lock is already held
350 */
Hugh Dickins427d5412008-03-04 14:29:03 -0800351void mem_cgroup_move_lists(struct page *page, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800352{
Hugh Dickins427d5412008-03-04 14:29:03 -0800353 struct page_cgroup *pc;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800354 struct mem_cgroup_per_zone *mz;
355 unsigned long flags;
356
Li Zefancede86a2008-07-25 01:47:18 -0700357 if (mem_cgroup_subsys.disabled)
358 return;
359
Hugh Dickins2680eed2008-03-04 14:29:13 -0800360 /*
361 * We cannot lock_page_cgroup while holding zone's lru_lock,
362 * because other holders of lock_page_cgroup can be interrupted
363 * with an attempt to rotate_reclaimable_page. But we cannot
364 * safely get to page_cgroup without it, so just try_lock it:
365 * mem_cgroup_isolate_pages allows for page left on wrong list.
366 */
367 if (!try_lock_page_cgroup(page))
Balbir Singh66e17072008-02-07 00:13:56 -0800368 return;
369
Hugh Dickins2680eed2008-03-04 14:29:13 -0800370 pc = page_get_page_cgroup(page);
371 if (pc) {
Hugh Dickins2680eed2008-03-04 14:29:13 -0800372 mz = page_cgroup_zoneinfo(pc);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800373 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800374 __mem_cgroup_move_lists(pc, active);
Hugh Dickins2680eed2008-03-04 14:29:13 -0800375 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800376 }
377 unlock_page_cgroup(page);
Balbir Singh66e17072008-02-07 00:13:56 -0800378}
379
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800380/*
381 * Calculate mapped_ratio under memory controller. This will be used in
382 * vmscan.c for deteremining we have to reclaim mapped pages.
383 */
384int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
385{
386 long total, rss;
387
388 /*
389 * usage is recorded in bytes. But, here, we assume the number of
390 * physical pages can be represented by "long" on any arch.
391 */
392 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
393 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
394 return (int)((rss * 100L) / total);
395}
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800396
KAMEZAWA Hiroyuki5932f362008-02-07 00:14:33 -0800397/*
398 * This function is called from vmscan.c. In page reclaiming loop. balance
399 * between active and inactive list is calculated. For memory controller
400 * page reclaiming, we should use using mem_cgroup's imbalance rather than
401 * zone's global lru imbalance.
402 */
403long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
404{
405 unsigned long active, inactive;
406 /* active and inactive are the number of pages. 'long' is ok.*/
407 active = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_ACTIVE);
408 inactive = mem_cgroup_get_all_zonestat(mem, MEM_CGROUP_ZSTAT_INACTIVE);
409 return (long) (active / (inactive + 1));
410}
KAMEZAWA Hiroyuki58ae83d2008-02-07 00:14:32 -0800411
KAMEZAWA Hiroyuki6c48a1d2008-02-07 00:14:34 -0800412/*
413 * prev_priority control...this will be used in memory reclaim path.
414 */
415int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
416{
417 return mem->prev_priority;
418}
419
420void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
421{
422 if (priority < mem->prev_priority)
423 mem->prev_priority = priority;
424}
425
426void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
427{
428 mem->prev_priority = priority;
429}
430
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800431/*
432 * Calculate # of pages to be scanned in this priority/zone.
433 * See also vmscan.c
434 *
435 * priority starts from "DEF_PRIORITY" and decremented in each loop.
436 * (see include/linux/mmzone.h)
437 */
438
439long mem_cgroup_calc_reclaim_active(struct mem_cgroup *mem,
440 struct zone *zone, int priority)
441{
442 long nr_active;
443 int nid = zone->zone_pgdat->node_id;
444 int zid = zone_idx(zone);
445 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
446
447 nr_active = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_ACTIVE);
448 return (nr_active >> priority);
449}
450
451long mem_cgroup_calc_reclaim_inactive(struct mem_cgroup *mem,
452 struct zone *zone, int priority)
453{
454 long nr_inactive;
455 int nid = zone->zone_pgdat->node_id;
456 int zid = zone_idx(zone);
457 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
458
459 nr_inactive = MEM_CGROUP_ZSTAT(mz, MEM_CGROUP_ZSTAT_INACTIVE);
KAMEZAWA Hiroyukicc381082008-02-07 00:14:35 -0800460 return (nr_inactive >> priority);
461}
462
Balbir Singh66e17072008-02-07 00:13:56 -0800463unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
464 struct list_head *dst,
465 unsigned long *scanned, int order,
466 int mode, struct zone *z,
467 struct mem_cgroup *mem_cont,
468 int active)
469{
470 unsigned long nr_taken = 0;
471 struct page *page;
472 unsigned long scan;
473 LIST_HEAD(pc_list);
474 struct list_head *src;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800475 struct page_cgroup *pc, *tmp;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800476 int nid = z->zone_pgdat->node_id;
477 int zid = zone_idx(z);
478 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800479
Balbir Singhcf475ad2008-04-29 01:00:16 -0700480 BUG_ON(!mem_cont);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800481 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
Balbir Singh66e17072008-02-07 00:13:56 -0800482 if (active)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800483 src = &mz->active_list;
Balbir Singh66e17072008-02-07 00:13:56 -0800484 else
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800485 src = &mz->inactive_list;
486
Balbir Singh66e17072008-02-07 00:13:56 -0800487
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800488 spin_lock(&mz->lru_lock);
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800489 scan = 0;
490 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
Hugh Dickins436c65412008-02-07 00:14:12 -0800491 if (scan >= nr_to_scan)
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800492 break;
Balbir Singh66e17072008-02-07 00:13:56 -0800493 page = pc->page;
Balbir Singh66e17072008-02-07 00:13:56 -0800494
Hugh Dickins436c65412008-02-07 00:14:12 -0800495 if (unlikely(!PageLRU(page)))
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800496 continue;
KAMEZAWA Hiroyukiff7283f2008-02-07 00:14:11 -0800497
Balbir Singh66e17072008-02-07 00:13:56 -0800498 if (PageActive(page) && !active) {
499 __mem_cgroup_move_lists(pc, true);
Balbir Singh66e17072008-02-07 00:13:56 -0800500 continue;
501 }
502 if (!PageActive(page) && active) {
503 __mem_cgroup_move_lists(pc, false);
Balbir Singh66e17072008-02-07 00:13:56 -0800504 continue;
505 }
506
Hugh Dickins436c65412008-02-07 00:14:12 -0800507 scan++;
508 list_move(&pc->lru, &pc_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800509
510 if (__isolate_lru_page(page, mode) == 0) {
511 list_move(&page->lru, dst);
512 nr_taken++;
513 }
514 }
515
516 list_splice(&pc_list, src);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800517 spin_unlock(&mz->lru_lock);
Balbir Singh66e17072008-02-07 00:13:56 -0800518
519 *scanned = scan;
520 return nr_taken;
521}
522
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800523/*
524 * Charge the memory controller for page usage.
525 * Return
526 * 0 if the charge was successful
527 * < 0 if the cgroup is over its limit
528 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800529static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700530 gfp_t gfp_mask, enum charge_type ctype,
531 struct mem_cgroup *memcg)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800532{
533 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800534 struct page_cgroup *pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800535 unsigned long flags;
536 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800537 struct mem_cgroup_per_zone *mz;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800538
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700539 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700540 if (unlikely(pc == NULL))
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800541 goto err;
542
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800543 /*
Hugh Dickins3be91272008-02-07 00:14:19 -0800544 * We always charge the cgroup the mm_struct belongs to.
545 * The mm_struct's mem_cgroup changes on task migration if the
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800546 * thread group leader migrates. It's possible that mm is not
547 * set, if so charge the init_mm (happens for pagecache usage).
548 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700549 if (likely(!memcg)) {
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700550 rcu_read_lock();
551 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
552 /*
553 * For every charge from the cgroup, increment reference count
554 */
555 css_get(&mem->css);
556 rcu_read_unlock();
557 } else {
558 mem = memcg;
559 css_get(&memcg->css);
560 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800561
Balbir Singh0eea1032008-02-07 00:13:57 -0800562 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Hugh Dickins3be91272008-02-07 00:14:19 -0800563 if (!(gfp_mask & __GFP_WAIT))
564 goto out;
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800565
566 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
Balbir Singh66e17072008-02-07 00:13:56 -0800567 continue;
568
569 /*
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800570 * try_to_free_mem_cgroup_pages() might not give us a full
571 * picture of reclaim. Some pages are reclaimed and might be
572 * moved to swap cache or just unmapped from the cgroup.
573 * Check the limit again to see if the reclaim reduced the
574 * current usage of the cgroup before giving up
575 */
Balbir Singh66e17072008-02-07 00:13:56 -0800576 if (res_counter_check_under_limit(&mem->res))
577 continue;
Hugh Dickins3be91272008-02-07 00:14:19 -0800578
579 if (!nr_retries--) {
580 mem_cgroup_out_of_memory(mem, gfp_mask);
581 goto out;
Balbir Singh66e17072008-02-07 00:13:56 -0800582 }
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800583 }
584
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800585 pc->mem_cgroup = mem;
586 pc->page = page;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700587 /*
588 * If a page is accounted as a page cache, insert to inactive list.
589 * If anon, insert to active list.
590 */
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800591 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
Balbir Singh4a56d022008-04-29 01:00:23 -0700592 pc->flags = PAGE_CGROUP_FLAG_CACHE;
KAMEZAWA Hiroyuki508b7be2008-07-25 01:47:09 -0700593 else
594 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
Hugh Dickins3be91272008-02-07 00:14:19 -0800595
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800596 lock_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700597 if (unlikely(page_get_page_cgroup(page))) {
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800598 unlock_page_cgroup(page);
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800599 res_counter_uncharge(&mem->res, PAGE_SIZE);
600 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700601 kmem_cache_free(page_cgroup_cache, pc);
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700602 goto done;
KAMEZAWA Hiroyuki9175e032008-02-07 00:14:08 -0800603 }
Hugh Dickins7e924aa2008-03-04 14:29:08 -0800604 page_assign_page_cgroup(page, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800605
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800606 mz = page_cgroup_zoneinfo(pc);
607 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyuki3eae90c2008-04-29 01:00:22 -0700608 __mem_cgroup_add_list(mz, pc);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800609 spin_unlock_irqrestore(&mz->lru_lock, flags);
Balbir Singh66e17072008-02-07 00:13:56 -0800610
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800611 unlock_page_cgroup(page);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800612done:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800613 return 0;
Hugh Dickins3be91272008-02-07 00:14:19 -0800614out:
615 css_put(&mem->css);
Balbir Singhb6ac57d2008-04-29 01:00:19 -0700616 kmem_cache_free(page_cgroup_cache, pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800617err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800618 return -ENOMEM;
619}
620
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800621int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800622{
Li Zefancede86a2008-07-25 01:47:18 -0700623 if (mem_cgroup_subsys.disabled)
624 return 0;
625
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700626 /*
627 * If already mapped, we don't have to account.
628 * If page cache, page->mapping has address_space.
629 * But page->mapping may have out-of-use anon_vma pointer,
630 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
631 * is NULL.
632 */
633 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
634 return 0;
635 if (unlikely(!mm))
636 mm = &init_mm;
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800637 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700638 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
KAMEZAWA Hiroyuki217bc312008-02-07 00:14:17 -0800639}
640
Balbir Singhe1a1cd52008-02-07 00:14:02 -0800641int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
642 gfp_t gfp_mask)
Balbir Singh8697d332008-02-07 00:13:59 -0800643{
Li Zefancede86a2008-07-25 01:47:18 -0700644 if (mem_cgroup_subsys.disabled)
645 return 0;
646
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700647 /*
648 * Corner case handling. This is called from add_to_page_cache()
649 * in usual. But some FS (shmem) precharges this page before calling it
650 * and call add_to_page_cache() with GFP_NOWAIT.
651 *
652 * For GFP_NOWAIT case, the page may be pre-charged before calling
653 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
654 * charge twice. (It works but has to pay a bit larger cost.)
655 */
656 if (!(gfp_mask & __GFP_WAIT)) {
657 struct page_cgroup *pc;
658
659 lock_page_cgroup(page);
660 pc = page_get_page_cgroup(page);
661 if (pc) {
662 VM_BUG_ON(pc->page != page);
663 VM_BUG_ON(!pc->mem_cgroup);
664 unlock_page_cgroup(page);
665 return 0;
666 }
667 unlock_page_cgroup(page);
668 }
669
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700670 if (unlikely(!mm))
Balbir Singh8697d332008-02-07 00:13:59 -0800671 mm = &init_mm;
KAMEZAWA Hiroyukiaccf1632008-07-25 01:47:17 -0700672
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800673 return mem_cgroup_charge_common(page, mm, gfp_mask,
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700674 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
675}
676
Balbir Singh8697d332008-02-07 00:13:59 -0800677/*
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700678 * uncharge if !page_mapped(page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800679 */
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700680static void
681__mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800682{
Hugh Dickins82895462008-03-04 14:29:08 -0800683 struct page_cgroup *pc;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800684 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800685 struct mem_cgroup_per_zone *mz;
Balbir Singh66e17072008-02-07 00:13:56 -0800686 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800687
Balbir Singh40779602008-04-04 14:29:59 -0700688 if (mem_cgroup_subsys.disabled)
689 return;
690
Balbir Singh8697d332008-02-07 00:13:59 -0800691 /*
Balbir Singh3c541e12008-02-07 00:14:41 -0800692 * Check if our page_cgroup is valid
Balbir Singh8697d332008-02-07 00:13:59 -0800693 */
Hugh Dickins82895462008-03-04 14:29:08 -0800694 lock_page_cgroup(page);
695 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukib76734e2008-07-25 01:47:16 -0700696 if (unlikely(!pc))
Hugh Dickins82895462008-03-04 14:29:08 -0800697 goto unlock;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800698
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800699 VM_BUG_ON(pc->page != page);
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800700
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700701 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
702 && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
703 || page_mapped(page)))
704 goto unlock;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800705
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700706 mz = page_cgroup_zoneinfo(pc);
707 spin_lock_irqsave(&mz->lru_lock, flags);
708 __mem_cgroup_remove_list(mz, pc);
709 spin_unlock_irqrestore(&mz->lru_lock, flags);
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800710
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700711 page_assign_page_cgroup(page, NULL);
712 unlock_page_cgroup(page);
Hugh Dickins6d48ff82008-03-04 14:29:12 -0800713
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700714 mem = pc->mem_cgroup;
715 res_counter_uncharge(&mem->res, PAGE_SIZE);
716 css_put(&mem->css);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800717
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700718 kmem_cache_free(page_cgroup_cache, pc);
719 return;
Hugh Dickins82895462008-03-04 14:29:08 -0800720unlock:
Balbir Singh3c541e12008-02-07 00:14:41 -0800721 unlock_page_cgroup(page);
722}
723
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700724void mem_cgroup_uncharge_page(struct page *page)
725{
726 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
727}
728
729void mem_cgroup_uncharge_cache_page(struct page *page)
730{
731 VM_BUG_ON(page_mapped(page));
732 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
733}
734
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800735/*
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700736 * Before starting migration, account against new page.
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800737 */
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700738int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800739{
740 struct page_cgroup *pc;
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700741 struct mem_cgroup *mem = NULL;
742 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
743 int ret = 0;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800744
Balbir Singh40779602008-04-04 14:29:59 -0700745 if (mem_cgroup_subsys.disabled)
746 return 0;
747
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800748 lock_page_cgroup(page);
749 pc = page_get_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700750 if (pc) {
751 mem = pc->mem_cgroup;
752 css_get(&mem->css);
753 if (pc->flags & PAGE_CGROUP_FLAG_CACHE)
754 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
Hugh Dickinsb9c565d2008-03-04 14:29:11 -0800755 }
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800756 unlock_page_cgroup(page);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700757 if (mem) {
758 ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
759 ctype, mem);
760 css_put(&mem->css);
761 }
762 return ret;
763}
Hugh Dickinsfb59e9f2008-03-04 14:29:16 -0800764
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700765/* remove redundant charge if migration failed*/
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700766void mem_cgroup_end_migration(struct page *newpage)
767{
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700768 /*
769 * At success, page->mapping is not NULL.
770 * special rollback care is necessary when
771 * 1. at migration failure. (newpage->mapping is cleared in this case)
772 * 2. the newpage was moved but not remapped again because the task
773 * exits and the newpage is obsolete. In this case, the new page
774 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
775 * always for avoiding mess. The page_cgroup will be removed if
776 * unnecessary. File cache pages is still on radix-tree. Don't
777 * care it.
778 */
779 if (!newpage->mapping)
780 __mem_cgroup_uncharge_common(newpage,
781 MEM_CGROUP_CHARGE_TYPE_FORCE);
782 else if (PageAnon(newpage))
783 mem_cgroup_uncharge_page(newpage);
KAMEZAWA Hiroyukiae41be32008-02-07 00:14:10 -0800784}
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800785
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800786/*
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700787 * A call to try to shrink memory usage under specified resource controller.
788 * This is typically used for page reclaiming for shmem for reducing side
789 * effect of page allocation from shmem, which is used by some mem_cgroup.
790 */
791int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
792{
793 struct mem_cgroup *mem;
794 int progress = 0;
795 int retry = MEM_CGROUP_RECLAIM_RETRIES;
796
Li Zefancede86a2008-07-25 01:47:18 -0700797 if (mem_cgroup_subsys.disabled)
798 return 0;
Hugh Dickins9623e072008-08-12 15:08:41 -0700799 if (!mm)
800 return 0;
Li Zefancede86a2008-07-25 01:47:18 -0700801
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700802 rcu_read_lock();
803 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
804 css_get(&mem->css);
805 rcu_read_unlock();
806
807 do {
808 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
Daisuke Nishimuraa10cebf2008-09-22 13:57:52 -0700809 progress += res_counter_check_under_limit(&mem->res);
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700810 } while (!progress && --retry);
811
812 css_put(&mem->css);
813 if (!retry)
814 return -ENOMEM;
815 return 0;
816}
817
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700818int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
819{
820
821 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
822 int progress;
823 int ret = 0;
824
825 while (res_counter_set_limit(&memcg->res, val)) {
826 if (signal_pending(current)) {
827 ret = -EINTR;
828 break;
829 }
830 if (!retry_count) {
831 ret = -EBUSY;
832 break;
833 }
834 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
835 if (!progress)
836 retry_count--;
837 }
838 return ret;
839}
840
841
KAMEZAWA Hiroyukic9b0ed52008-07-25 01:47:15 -0700842/*
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800843 * This routine traverse page_cgroup in given list and drop them all.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800844 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
845 */
846#define FORCE_UNCHARGE_BATCH (128)
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800847static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800848 struct mem_cgroup_per_zone *mz,
849 int active)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800850{
851 struct page_cgroup *pc;
852 struct page *page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800853 int count = FORCE_UNCHARGE_BATCH;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800854 unsigned long flags;
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800855 struct list_head *list;
856
857 if (active)
858 list = &mz->active_list;
859 else
860 list = &mz->inactive_list;
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800861
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800862 spin_lock_irqsave(&mz->lru_lock, flags);
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800863 while (!list_empty(list)) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800864 pc = list_entry(list->prev, struct page_cgroup, lru);
865 page = pc->page;
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800866 get_page(page);
867 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700868 /*
869 * Check if this page is on LRU. !LRU page can be found
870 * if it's under page migration.
871 */
872 if (PageLRU(page)) {
KAMEZAWA Hiroyuki69029cd2008-07-25 01:47:14 -0700873 __mem_cgroup_uncharge_common(page,
874 MEM_CGROUP_CHARGE_TYPE_FORCE);
KAMEZAWA Hiroyukie8589cc2008-07-25 01:47:10 -0700875 put_page(page);
876 if (--count <= 0) {
877 count = FORCE_UNCHARGE_BATCH;
878 cond_resched();
879 }
880 } else
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800881 cond_resched();
Hirokazu Takahashi9b3c0a02008-03-04 14:29:15 -0800882 spin_lock_irqsave(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800883 }
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800884 spin_unlock_irqrestore(&mz->lru_lock, flags);
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800885}
886
887/*
888 * make mem_cgroup's charge to be 0 if there is no task.
889 * This enables deleting this mem_cgroup.
890 */
Hugh Dickinsd5b69e32008-03-04 14:29:10 -0800891static int mem_cgroup_force_empty(struct mem_cgroup *mem)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800892{
893 int ret = -EBUSY;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800894 int node, zid;
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800895
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800896 css_get(&mem->css);
897 /*
898 * page reclaim code (kswapd etc..) will move pages between
Hugh Dickins8869b8f2008-03-04 14:29:09 -0800899 * active_list <-> inactive_list while we don't take a lock.
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800900 * So, we have to do loop here until all lists are empty.
901 */
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800902 while (mem->res.usage > 0) {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800903 if (atomic_read(&mem->css.cgroup->count) > 0)
904 goto out;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800905 for_each_node_state(node, N_POSSIBLE)
906 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
907 struct mem_cgroup_per_zone *mz;
908 mz = mem_cgroup_zoneinfo(mem, node, zid);
909 /* drop all page_cgroup in active_list */
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800910 mem_cgroup_force_empty_list(mem, mz, 1);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800911 /* drop all page_cgroup in inactive_list */
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -0800912 mem_cgroup_force_empty_list(mem, mz, 0);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -0800913 }
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800914 }
915 ret = 0;
916out:
917 css_put(&mem->css);
918 return ret;
919}
920
Paul Menage2c3daa72008-04-29 00:59:58 -0700921static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800922{
Paul Menage2c3daa72008-04-29 00:59:58 -0700923 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
924 cft->private);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800925}
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700926/*
927 * The user of this function is...
928 * RES_LIMIT.
929 */
Paul Menage856c13a2008-07-25 01:47:04 -0700930static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
931 const char *buffer)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800932{
KAMEZAWA Hiroyuki628f4232008-07-25 01:47:20 -0700933 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
934 unsigned long long val;
935 int ret;
936
937 switch (cft->private) {
938 case RES_LIMIT:
939 /* This function does all necessary parse...reuse it */
940 ret = res_counter_memparse_write_strategy(buffer, &val);
941 if (!ret)
942 ret = mem_cgroup_resize_limit(memcg, val);
943 break;
944 default:
945 ret = -EINVAL; /* should be BUG() ? */
946 break;
947 }
948 return ret;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800949}
950
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700951static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700952{
953 struct mem_cgroup *mem;
954
955 mem = mem_cgroup_from_cont(cont);
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -0700956 switch (event) {
957 case RES_MAX_USAGE:
958 res_counter_reset_max(&mem->res);
959 break;
960 case RES_FAILCNT:
961 res_counter_reset_failcnt(&mem->res);
962 break;
963 }
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700964 return 0;
Pavel Emelyanovc84872e2008-04-29 01:00:17 -0700965}
966
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700967static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800968{
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -0700969 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -0800970}
971
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800972static const struct mem_cgroup_stat_desc {
973 const char *msg;
974 u64 unit;
975} mem_cgroup_stat_desc[] = {
976 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
977 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
Balaji Rao55e462b2008-05-01 04:35:12 -0700978 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
979 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800980};
981
Paul Menagec64745c2008-04-29 01:00:02 -0700982static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
983 struct cgroup_map_cb *cb)
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800984{
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800985 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
986 struct mem_cgroup_stat *stat = &mem_cont->stat;
987 int i;
988
989 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
990 s64 val;
991
992 val = mem_cgroup_read_stat(stat, i);
993 val *= mem_cgroup_stat_desc[i].unit;
Paul Menagec64745c2008-04-29 01:00:02 -0700994 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -0800995 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -0800996 /* showing # of active pages */
997 {
998 unsigned long active, inactive;
999
1000 inactive = mem_cgroup_get_all_zonestat(mem_cont,
1001 MEM_CGROUP_ZSTAT_INACTIVE);
1002 active = mem_cgroup_get_all_zonestat(mem_cont,
1003 MEM_CGROUP_ZSTAT_ACTIVE);
Paul Menagec64745c2008-04-29 01:00:02 -07001004 cb->fill(cb, "active", (active) * PAGE_SIZE);
1005 cb->fill(cb, "inactive", (inactive) * PAGE_SIZE);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001006 }
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001007 return 0;
1008}
1009
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001010static struct cftype mem_cgroup_files[] = {
1011 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001012 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001013 .private = RES_USAGE,
Paul Menage2c3daa72008-04-29 00:59:58 -07001014 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001015 },
1016 {
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001017 .name = "max_usage_in_bytes",
1018 .private = RES_MAX_USAGE,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001019 .trigger = mem_cgroup_reset,
Pavel Emelyanovc84872e2008-04-29 01:00:17 -07001020 .read_u64 = mem_cgroup_read,
1021 },
1022 {
Balbir Singh0eea1032008-02-07 00:13:57 -08001023 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001024 .private = RES_LIMIT,
Paul Menage856c13a2008-07-25 01:47:04 -07001025 .write_string = mem_cgroup_write,
Paul Menage2c3daa72008-04-29 00:59:58 -07001026 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001027 },
1028 {
1029 .name = "failcnt",
1030 .private = RES_FAILCNT,
Pavel Emelyanov29f2a4d2008-04-29 01:00:21 -07001031 .trigger = mem_cgroup_reset,
Paul Menage2c3daa72008-04-29 00:59:58 -07001032 .read_u64 = mem_cgroup_read,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001033 },
Balbir Singh8697d332008-02-07 00:13:59 -08001034 {
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001035 .name = "force_empty",
Pavel Emelyanov85cc59d2008-04-29 01:00:20 -07001036 .trigger = mem_force_empty_write,
KAMEZAWA Hiroyukicc847582008-02-07 00:14:16 -08001037 },
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001038 {
1039 .name = "stat",
Paul Menagec64745c2008-04-29 01:00:02 -07001040 .read_map = mem_control_stat_show,
KAMEZAWA Hiroyukid2ceb9b2008-02-07 00:14:25 -08001041 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001042};
1043
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001044static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1045{
1046 struct mem_cgroup_per_node *pn;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001047 struct mem_cgroup_per_zone *mz;
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001048 int zone, tmp = node;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001049 /*
1050 * This routine is called against possible nodes.
1051 * But it's BUG to call kmalloc() against offline node.
1052 *
1053 * TODO: this routine can waste much memory for nodes which will
1054 * never be onlined. It's better to use memory hotplug callback
1055 * function.
1056 */
KAMEZAWA Hiroyuki41e33552008-04-08 17:41:54 -07001057 if (!node_state(node, N_NORMAL_MEMORY))
1058 tmp = -1;
1059 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001060 if (!pn)
1061 return 1;
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001062
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001063 mem->info.nodeinfo[node] = pn;
1064 memset(pn, 0, sizeof(*pn));
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001065
1066 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1067 mz = &pn->zoneinfo[zone];
1068 INIT_LIST_HEAD(&mz->active_list);
1069 INIT_LIST_HEAD(&mz->inactive_list);
KAMEZAWA Hiroyuki072c56c12008-02-07 00:14:39 -08001070 spin_lock_init(&mz->lru_lock);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001071 }
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001072 return 0;
1073}
1074
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001075static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1076{
1077 kfree(mem->info.nodeinfo[node]);
1078}
1079
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001080static struct mem_cgroup *mem_cgroup_alloc(void)
1081{
1082 struct mem_cgroup *mem;
1083
1084 if (sizeof(*mem) < PAGE_SIZE)
1085 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1086 else
1087 mem = vmalloc(sizeof(*mem));
1088
1089 if (mem)
1090 memset(mem, 0, sizeof(*mem));
1091 return mem;
1092}
1093
1094static void mem_cgroup_free(struct mem_cgroup *mem)
1095{
1096 if (sizeof(*mem) < PAGE_SIZE)
1097 kfree(mem);
1098 else
1099 vfree(mem);
1100}
1101
1102
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001103static struct cgroup_subsys_state *
1104mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1105{
1106 struct mem_cgroup *mem;
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001107 int node;
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001108
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001109 if (unlikely((cont->parent) == NULL)) {
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001110 mem = &init_mem_cgroup;
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001111 page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
1112 } else {
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001113 mem = mem_cgroup_alloc();
1114 if (!mem)
1115 return ERR_PTR(-ENOMEM);
Balbir Singhb6ac57d2008-04-29 01:00:19 -07001116 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -08001117
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001118 res_counter_init(&mem->res);
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001119
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001120 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)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001127 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001128 if (cont->parent != NULL)
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001129 mem_cgroup_free(mem);
Li Zefan2dda81c2008-02-23 15:24:14 -08001130 return ERR_PTR(-ENOMEM);
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)
KAMEZAWA Hiroyuki1ecaab22008-02-07 00:14:38 -08001147 free_mem_cgroup_per_zone_info(mem, node);
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001148
KAMEZAWA Hiroyuki33327942008-04-29 01:00:24 -07001149 mem_cgroup_free(mem_cgroup_from_cont(cont));
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001150}
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
Balbir Singh67e465a2008-02-07 00:13:54 -08001174 /*
1175 * Only thread group leaders are allowed to migrate, the mm_struct is
1176 * in effect owned by the leader
1177 */
Pavel Emelyanov52ea27e2008-03-19 17:00:45 -07001178 if (!thread_group_leader(p))
Balbir Singh67e465a2008-02-07 00:13:54 -08001179 goto out;
1180
Balbir Singh67e465a2008-02-07 00:13:54 -08001181out:
1182 mmput(mm);
Balbir Singh67e465a2008-02-07 00:13:54 -08001183}
1184
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001185struct cgroup_subsys mem_cgroup_subsys = {
1186 .name = "memory",
1187 .subsys_id = mem_cgroup_subsys_id,
1188 .create = mem_cgroup_create,
KAMEZAWA Hiroyukidf878fb2008-02-07 00:14:28 -08001189 .pre_destroy = mem_cgroup_pre_destroy,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001190 .destroy = mem_cgroup_destroy,
1191 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -08001192 .attach = mem_cgroup_move_task,
KAMEZAWA Hiroyuki6d12e2d2008-02-07 00:14:31 -08001193 .early_init = 0,
Balbir Singh8cdea7c2008-02-07 00:13:50 -08001194};