blob: 10833d969e3fa0c5c276b65e7d86bdfd6bc6f042 [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>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080024#include <linux/page-flags.h>
Balbir Singh66e17072008-02-07 00:13:56 -080025#include <linux/backing-dev.h>
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080026#include <linux/bit_spinlock.h>
27#include <linux/rcupdate.h>
Balbir Singh66e17072008-02-07 00:13:56 -080028#include <linux/swap.h>
29#include <linux/spinlock.h>
30#include <linux/fs.h>
Balbir Singh8cdea7c2008-02-07 00:13:50 -080031
Balbir Singh8697d332008-02-07 00:13:59 -080032#include <asm/uaccess.h>
33
Balbir Singh8cdea7c2008-02-07 00:13:50 -080034struct cgroup_subsys mem_cgroup_subsys;
Balbir Singh66e17072008-02-07 00:13:56 -080035static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080036
37/*
38 * The memory controller data structure. The memory controller controls both
39 * page cache and RSS per cgroup. We would eventually like to provide
40 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
41 * to help the administrator determine what knobs to tune.
42 *
43 * TODO: Add a water mark for the memory controller. Reclaim will begin when
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080044 * we hit the water mark. May be even add a low water mark, such that
45 * no reclaim occurs from a cgroup at it's low water mark, this is
46 * a feature that will be implemented much later in the future.
Balbir Singh8cdea7c2008-02-07 00:13:50 -080047 */
48struct mem_cgroup {
49 struct cgroup_subsys_state css;
50 /*
51 * the counter to account for memory usage
52 */
53 struct res_counter res;
Pavel Emelianov78fb7462008-02-07 00:13:51 -080054 /*
55 * Per cgroup active and inactive list, similar to the
56 * per zone LRU lists.
57 * TODO: Consider making these lists per zone
58 */
59 struct list_head active_list;
60 struct list_head inactive_list;
Balbir Singh66e17072008-02-07 00:13:56 -080061 /*
62 * spin_lock to protect the per cgroup LRU
63 */
64 spinlock_t lru_lock;
Balbir Singh8697d332008-02-07 00:13:59 -080065 unsigned long control_type; /* control RSS or RSS+Pagecache */
Balbir Singh8cdea7c2008-02-07 00:13:50 -080066};
67
68/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080069 * We use the lower bit of the page->page_cgroup pointer as a bit spin
70 * lock. We need to ensure that page->page_cgroup is atleast two
71 * byte aligned (based on comments from Nick Piggin)
72 */
73#define PAGE_CGROUP_LOCK_BIT 0x0
74#define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
75
76/*
Balbir Singh8cdea7c2008-02-07 00:13:50 -080077 * A page_cgroup page is associated with every page descriptor. The
78 * page_cgroup helps us identify information about the cgroup
79 */
80struct page_cgroup {
81 struct list_head lru; /* per cgroup LRU list */
82 struct page *page;
83 struct mem_cgroup *mem_cgroup;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -080084 atomic_t ref_cnt; /* Helpful when pages move b/w */
85 /* mapped and cached states */
Balbir Singh8cdea7c2008-02-07 00:13:50 -080086};
87
Balbir Singh8697d332008-02-07 00:13:59 -080088enum {
89 MEM_CGROUP_TYPE_UNSPEC = 0,
90 MEM_CGROUP_TYPE_MAPPED,
91 MEM_CGROUP_TYPE_CACHED,
92 MEM_CGROUP_TYPE_ALL,
93 MEM_CGROUP_TYPE_MAX,
94};
95
96static struct mem_cgroup init_mem_cgroup;
Balbir Singh8cdea7c2008-02-07 00:13:50 -080097
98static inline
99struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
100{
101 return container_of(cgroup_subsys_state(cont,
102 mem_cgroup_subsys_id), struct mem_cgroup,
103 css);
104}
105
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800106static inline
107struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
108{
109 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
110 struct mem_cgroup, css);
111}
112
113void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
114{
115 struct mem_cgroup *mem;
116
117 mem = mem_cgroup_from_task(p);
118 css_get(&mem->css);
119 mm->mem_cgroup = mem;
120}
121
122void mm_free_cgroup(struct mm_struct *mm)
123{
124 css_put(&mm->mem_cgroup->css);
125}
126
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800127static inline int page_cgroup_locked(struct page *page)
128{
129 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
130 &page->page_cgroup);
131}
132
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800133void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
134{
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800135 int locked;
136
137 /*
138 * While resetting the page_cgroup we might not hold the
139 * page_cgroup lock. free_hot_cold_page() is an example
140 * of such a scenario
141 */
142 if (pc)
143 VM_BUG_ON(!page_cgroup_locked(page));
144 locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
145 page->page_cgroup = ((unsigned long)pc | locked);
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800146}
147
148struct page_cgroup *page_get_page_cgroup(struct page *page)
149{
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800150 return (struct page_cgroup *)
151 (page->page_cgroup & ~PAGE_CGROUP_LOCK);
152}
153
Balbir Singh8697d332008-02-07 00:13:59 -0800154static void __always_inline lock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800155{
156 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
157 VM_BUG_ON(!page_cgroup_locked(page));
158}
159
Balbir Singh8697d332008-02-07 00:13:59 -0800160static void __always_inline unlock_page_cgroup(struct page *page)
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800161{
162 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
163}
164
Balbir Singh8697d332008-02-07 00:13:59 -0800165static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
Balbir Singh66e17072008-02-07 00:13:56 -0800166{
167 if (active)
168 list_move(&pc->lru, &pc->mem_cgroup->active_list);
169 else
170 list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
171}
172
173/*
174 * This routine assumes that the appropriate zone's lru lock is already held
175 */
176void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
177{
178 struct mem_cgroup *mem;
179 if (!pc)
180 return;
181
182 mem = pc->mem_cgroup;
183
184 spin_lock(&mem->lru_lock);
185 __mem_cgroup_move_lists(pc, active);
186 spin_unlock(&mem->lru_lock);
187}
188
189unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
190 struct list_head *dst,
191 unsigned long *scanned, int order,
192 int mode, struct zone *z,
193 struct mem_cgroup *mem_cont,
194 int active)
195{
196 unsigned long nr_taken = 0;
197 struct page *page;
198 unsigned long scan;
199 LIST_HEAD(pc_list);
200 struct list_head *src;
201 struct page_cgroup *pc;
202
203 if (active)
204 src = &mem_cont->active_list;
205 else
206 src = &mem_cont->inactive_list;
207
208 spin_lock(&mem_cont->lru_lock);
209 for (scan = 0; scan < nr_to_scan && !list_empty(src); scan++) {
210 pc = list_entry(src->prev, struct page_cgroup, lru);
211 page = pc->page;
212 VM_BUG_ON(!pc);
213
214 if (PageActive(page) && !active) {
215 __mem_cgroup_move_lists(pc, true);
216 scan--;
217 continue;
218 }
219 if (!PageActive(page) && active) {
220 __mem_cgroup_move_lists(pc, false);
221 scan--;
222 continue;
223 }
224
225 /*
226 * Reclaim, per zone
227 * TODO: make the active/inactive lists per zone
228 */
229 if (page_zone(page) != z)
230 continue;
231
232 /*
233 * Check if the meta page went away from under us
234 */
235 if (!list_empty(&pc->lru))
236 list_move(&pc->lru, &pc_list);
237 else
238 continue;
239
240 if (__isolate_lru_page(page, mode) == 0) {
241 list_move(&page->lru, dst);
242 nr_taken++;
243 }
244 }
245
246 list_splice(&pc_list, src);
247 spin_unlock(&mem_cont->lru_lock);
248
249 *scanned = scan;
250 return nr_taken;
251}
252
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800253/*
254 * Charge the memory controller for page usage.
255 * Return
256 * 0 if the charge was successful
257 * < 0 if the cgroup is over its limit
258 */
259int mem_cgroup_charge(struct page *page, struct mm_struct *mm)
260{
261 struct mem_cgroup *mem;
262 struct page_cgroup *pc, *race_pc;
Balbir Singh66e17072008-02-07 00:13:56 -0800263 unsigned long flags;
264 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800265
266 /*
267 * Should page_cgroup's go to their own slab?
268 * One could optimize the performance of the charging routine
269 * by saving a bit in the page_flags and using it as a lock
270 * to see if the cgroup page already has a page_cgroup associated
271 * with it
272 */
Balbir Singh66e17072008-02-07 00:13:56 -0800273retry:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800274 lock_page_cgroup(page);
275 pc = page_get_page_cgroup(page);
276 /*
277 * The page_cgroup exists and the page has already been accounted
278 */
279 if (pc) {
Balbir Singh66e17072008-02-07 00:13:56 -0800280 if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
281 /* this page is under being uncharged ? */
282 unlock_page_cgroup(page);
283 cpu_relax();
284 goto retry;
285 } else
286 goto done;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800287 }
288
289 unlock_page_cgroup(page);
290
291 pc = kzalloc(sizeof(struct page_cgroup), GFP_KERNEL);
292 if (pc == NULL)
293 goto err;
294
295 rcu_read_lock();
296 /*
297 * We always charge the cgroup the mm_struct belongs to
298 * the mm_struct's mem_cgroup changes on task migration if the
299 * thread group leader migrates. It's possible that mm is not
300 * set, if so charge the init_mm (happens for pagecache usage).
301 */
302 if (!mm)
303 mm = &init_mm;
304
305 mem = rcu_dereference(mm->mem_cgroup);
306 /*
307 * For every charge from the cgroup, increment reference
308 * count
309 */
310 css_get(&mem->css);
311 rcu_read_unlock();
312
313 /*
314 * If we created the page_cgroup, we should free it on exceeding
315 * the cgroup limit.
316 */
Balbir Singh0eea1032008-02-07 00:13:57 -0800317 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
Balbir Singh66e17072008-02-07 00:13:56 -0800318 if (try_to_free_mem_cgroup_pages(mem))
319 continue;
320
321 /*
322 * try_to_free_mem_cgroup_pages() might not give us a full
323 * picture of reclaim. Some pages are reclaimed and might be
324 * moved to swap cache or just unmapped from the cgroup.
325 * Check the limit again to see if the reclaim reduced the
326 * current usage of the cgroup before giving up
327 */
328 if (res_counter_check_under_limit(&mem->res))
329 continue;
330 /*
331 * Since we control both RSS and cache, we end up with a
332 * very interesting scenario where we end up reclaiming
333 * memory (essentially RSS), since the memory is pushed
334 * to swap cache, we eventually end up adding those
335 * pages back to our list. Hence we give ourselves a
336 * few chances before we fail
337 */
338 else if (nr_retries--) {
339 congestion_wait(WRITE, HZ/10);
340 continue;
341 }
342
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800343 css_put(&mem->css);
Pavel Emelianovc7ba5c92008-02-07 00:13:58 -0800344 mem_cgroup_out_of_memory(mem, GFP_KERNEL);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800345 goto free_pc;
346 }
347
348 lock_page_cgroup(page);
349 /*
350 * Check if somebody else beat us to allocating the page_cgroup
351 */
352 race_pc = page_get_page_cgroup(page);
353 if (race_pc) {
354 kfree(pc);
355 pc = race_pc;
356 atomic_inc(&pc->ref_cnt);
Balbir Singh0eea1032008-02-07 00:13:57 -0800357 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800358 css_put(&mem->css);
359 goto done;
360 }
361
362 atomic_set(&pc->ref_cnt, 1);
363 pc->mem_cgroup = mem;
364 pc->page = page;
365 page_assign_page_cgroup(page, pc);
366
Balbir Singh66e17072008-02-07 00:13:56 -0800367 spin_lock_irqsave(&mem->lru_lock, flags);
368 list_add(&pc->lru, &mem->active_list);
369 spin_unlock_irqrestore(&mem->lru_lock, flags);
370
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800371done:
372 unlock_page_cgroup(page);
373 return 0;
374free_pc:
375 kfree(pc);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800376err:
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800377 return -ENOMEM;
378}
379
380/*
Balbir Singh8697d332008-02-07 00:13:59 -0800381 * See if the cached pages should be charged at all?
382 */
383int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm)
384{
385 struct mem_cgroup *mem;
386 if (!mm)
387 mm = &init_mm;
388
389 mem = rcu_dereference(mm->mem_cgroup);
390 if (mem->control_type == MEM_CGROUP_TYPE_ALL)
391 return mem_cgroup_charge(page, mm);
392 else
393 return 0;
394}
395
396/*
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800397 * Uncharging is always a welcome operation, we never complain, simply
398 * uncharge.
399 */
400void mem_cgroup_uncharge(struct page_cgroup *pc)
401{
402 struct mem_cgroup *mem;
403 struct page *page;
Balbir Singh66e17072008-02-07 00:13:56 -0800404 unsigned long flags;
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800405
Balbir Singh8697d332008-02-07 00:13:59 -0800406 /*
407 * This can handle cases when a page is not charged at all and we
408 * are switching between handling the control_type.
409 */
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800410 if (!pc)
411 return;
412
413 if (atomic_dec_and_test(&pc->ref_cnt)) {
414 page = pc->page;
415 lock_page_cgroup(page);
416 mem = pc->mem_cgroup;
417 css_put(&mem->css);
418 page_assign_page_cgroup(page, NULL);
419 unlock_page_cgroup(page);
Balbir Singh0eea1032008-02-07 00:13:57 -0800420 res_counter_uncharge(&mem->res, PAGE_SIZE);
Balbir Singh66e17072008-02-07 00:13:56 -0800421
422 spin_lock_irqsave(&mem->lru_lock, flags);
423 list_del_init(&pc->lru);
424 spin_unlock_irqrestore(&mem->lru_lock, flags);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800425 kfree(pc);
426 }
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800427}
428
Balbir Singh0eea1032008-02-07 00:13:57 -0800429int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
430{
431 *tmp = memparse(buf, &buf);
432 if (*buf != '\0')
433 return -EINVAL;
434
435 /*
436 * Round up the value to the closest page size
437 */
438 *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
439 return 0;
440}
441
442static ssize_t mem_cgroup_read(struct cgroup *cont,
443 struct cftype *cft, struct file *file,
444 char __user *userbuf, size_t nbytes, loff_t *ppos)
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800445{
446 return res_counter_read(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800447 cft->private, userbuf, nbytes, ppos,
448 NULL);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800449}
450
451static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
452 struct file *file, const char __user *userbuf,
453 size_t nbytes, loff_t *ppos)
454{
455 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
Balbir Singh0eea1032008-02-07 00:13:57 -0800456 cft->private, userbuf, nbytes, ppos,
457 mem_cgroup_write_strategy);
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800458}
459
Balbir Singh8697d332008-02-07 00:13:59 -0800460static ssize_t mem_control_type_write(struct cgroup *cont,
461 struct cftype *cft, struct file *file,
462 const char __user *userbuf,
463 size_t nbytes, loff_t *pos)
464{
465 int ret;
466 char *buf, *end;
467 unsigned long tmp;
468 struct mem_cgroup *mem;
469
470 mem = mem_cgroup_from_cont(cont);
471 buf = kmalloc(nbytes + 1, GFP_KERNEL);
472 ret = -ENOMEM;
473 if (buf == NULL)
474 goto out;
475
476 buf[nbytes] = 0;
477 ret = -EFAULT;
478 if (copy_from_user(buf, userbuf, nbytes))
479 goto out_free;
480
481 ret = -EINVAL;
482 tmp = simple_strtoul(buf, &end, 10);
483 if (*end != '\0')
484 goto out_free;
485
486 if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
487 goto out_free;
488
489 mem->control_type = tmp;
490 ret = nbytes;
491out_free:
492 kfree(buf);
493out:
494 return ret;
495}
496
497static ssize_t mem_control_type_read(struct cgroup *cont,
498 struct cftype *cft,
499 struct file *file, char __user *userbuf,
500 size_t nbytes, loff_t *ppos)
501{
502 unsigned long val;
503 char buf[64], *s;
504 struct mem_cgroup *mem;
505
506 mem = mem_cgroup_from_cont(cont);
507 s = buf;
508 val = mem->control_type;
509 s += sprintf(s, "%lu\n", val);
510 return simple_read_from_buffer((void __user *)userbuf, nbytes,
511 ppos, buf, s - buf);
512}
513
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800514static struct cftype mem_cgroup_files[] = {
515 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800516 .name = "usage_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800517 .private = RES_USAGE,
518 .read = mem_cgroup_read,
519 },
520 {
Balbir Singh0eea1032008-02-07 00:13:57 -0800521 .name = "limit_in_bytes",
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800522 .private = RES_LIMIT,
523 .write = mem_cgroup_write,
524 .read = mem_cgroup_read,
525 },
526 {
527 .name = "failcnt",
528 .private = RES_FAILCNT,
529 .read = mem_cgroup_read,
530 },
Balbir Singh8697d332008-02-07 00:13:59 -0800531 {
532 .name = "control_type",
533 .write = mem_control_type_write,
534 .read = mem_control_type_read,
535 },
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800536};
537
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800538static struct mem_cgroup init_mem_cgroup;
539
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800540static struct cgroup_subsys_state *
541mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
542{
543 struct mem_cgroup *mem;
544
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800545 if (unlikely((cont->parent) == NULL)) {
546 mem = &init_mem_cgroup;
547 init_mm.mem_cgroup = mem;
548 } else
549 mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
550
551 if (mem == NULL)
552 return NULL;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800553
554 res_counter_init(&mem->res);
Balbir Singh8a9f3cc2008-02-07 00:13:53 -0800555 INIT_LIST_HEAD(&mem->active_list);
556 INIT_LIST_HEAD(&mem->inactive_list);
Balbir Singh66e17072008-02-07 00:13:56 -0800557 spin_lock_init(&mem->lru_lock);
Balbir Singh8697d332008-02-07 00:13:59 -0800558 mem->control_type = MEM_CGROUP_TYPE_ALL;
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800559 return &mem->css;
560}
561
562static void mem_cgroup_destroy(struct cgroup_subsys *ss,
563 struct cgroup *cont)
564{
565 kfree(mem_cgroup_from_cont(cont));
566}
567
568static int mem_cgroup_populate(struct cgroup_subsys *ss,
569 struct cgroup *cont)
570{
571 return cgroup_add_files(cont, ss, mem_cgroup_files,
572 ARRAY_SIZE(mem_cgroup_files));
573}
574
Balbir Singh67e465a2008-02-07 00:13:54 -0800575static void mem_cgroup_move_task(struct cgroup_subsys *ss,
576 struct cgroup *cont,
577 struct cgroup *old_cont,
578 struct task_struct *p)
579{
580 struct mm_struct *mm;
581 struct mem_cgroup *mem, *old_mem;
582
583 mm = get_task_mm(p);
584 if (mm == NULL)
585 return;
586
587 mem = mem_cgroup_from_cont(cont);
588 old_mem = mem_cgroup_from_cont(old_cont);
589
590 if (mem == old_mem)
591 goto out;
592
593 /*
594 * Only thread group leaders are allowed to migrate, the mm_struct is
595 * in effect owned by the leader
596 */
597 if (p->tgid != p->pid)
598 goto out;
599
600 css_get(&mem->css);
601 rcu_assign_pointer(mm->mem_cgroup, mem);
602 css_put(&old_mem->css);
603
604out:
605 mmput(mm);
606 return;
607}
608
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800609struct cgroup_subsys mem_cgroup_subsys = {
610 .name = "memory",
611 .subsys_id = mem_cgroup_subsys_id,
612 .create = mem_cgroup_create,
613 .destroy = mem_cgroup_destroy,
614 .populate = mem_cgroup_populate,
Balbir Singh67e465a2008-02-07 00:13:54 -0800615 .attach = mem_cgroup_move_task,
Pavel Emelianov78fb7462008-02-07 00:13:51 -0800616 .early_init = 1,
Balbir Singh8cdea7c2008-02-07 00:13:50 -0800617};