]> bbs.cooldavid.org Git - net-next-2.6.git/blame - mm/memcontrol.c
Memory controller use rcu_read_lock() in mem_cgroup_cache_charge()
[net-next-2.6.git] / mm / memcontrol.c
CommitLineData
8cdea7c0
BS
1/* memcontrol.c - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
78fb7466
PE
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
8cdea7c0
BS
9 * 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>
78fb7466 23#include <linux/mm.h>
8a9f3ccd 24#include <linux/page-flags.h>
66e1707b 25#include <linux/backing-dev.h>
8a9f3ccd
BS
26#include <linux/bit_spinlock.h>
27#include <linux/rcupdate.h>
66e1707b
BS
28#include <linux/swap.h>
29#include <linux/spinlock.h>
30#include <linux/fs.h>
8cdea7c0 31
8697d331
BS
32#include <asm/uaccess.h>
33
8cdea7c0 34struct cgroup_subsys mem_cgroup_subsys;
66e1707b 35static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
8cdea7c0
BS
36
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
8a9f3ccd
BS
44 * 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.
8cdea7c0
BS
47 */
48struct mem_cgroup {
49 struct cgroup_subsys_state css;
50 /*
51 * the counter to account for memory usage
52 */
53 struct res_counter res;
78fb7466
PE
54 /*
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;
66e1707b
BS
61 /*
62 * spin_lock to protect the per cgroup LRU
63 */
64 spinlock_t lru_lock;
8697d331 65 unsigned long control_type; /* control RSS or RSS+Pagecache */
8cdea7c0
BS
66};
67
8a9f3ccd
BS
68/*
69 * 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
8cdea7c0
BS
76/*
77 * 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;
8a9f3ccd
BS
84 atomic_t ref_cnt; /* Helpful when pages move b/w */
85 /* mapped and cached states */
217bc319 86 int flags;
8cdea7c0 87};
217bc319 88#define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
8cdea7c0 89
8697d331
BS
90enum {
91 MEM_CGROUP_TYPE_UNSPEC = 0,
92 MEM_CGROUP_TYPE_MAPPED,
93 MEM_CGROUP_TYPE_CACHED,
94 MEM_CGROUP_TYPE_ALL,
95 MEM_CGROUP_TYPE_MAX,
96};
97
217bc319
KH
98enum charge_type {
99 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
100 MEM_CGROUP_CHARGE_TYPE_MAPPED,
101};
102
8697d331 103static struct mem_cgroup init_mem_cgroup;
8cdea7c0
BS
104
105static inline
106struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
107{
108 return container_of(cgroup_subsys_state(cont,
109 mem_cgroup_subsys_id), struct mem_cgroup,
110 css);
111}
112
78fb7466
PE
113static inline
114struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
115{
116 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
117 struct mem_cgroup, css);
118}
119
120void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
121{
122 struct mem_cgroup *mem;
123
124 mem = mem_cgroup_from_task(p);
125 css_get(&mem->css);
126 mm->mem_cgroup = mem;
127}
128
129void mm_free_cgroup(struct mm_struct *mm)
130{
131 css_put(&mm->mem_cgroup->css);
132}
133
8a9f3ccd
BS
134static inline int page_cgroup_locked(struct page *page)
135{
136 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
137 &page->page_cgroup);
138}
139
78fb7466
PE
140void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
141{
8a9f3ccd
BS
142 int locked;
143
144 /*
145 * While resetting the page_cgroup we might not hold the
146 * page_cgroup lock. free_hot_cold_page() is an example
147 * of such a scenario
148 */
149 if (pc)
150 VM_BUG_ON(!page_cgroup_locked(page));
151 locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
152 page->page_cgroup = ((unsigned long)pc | locked);
78fb7466
PE
153}
154
155struct page_cgroup *page_get_page_cgroup(struct page *page)
156{
8a9f3ccd
BS
157 return (struct page_cgroup *)
158 (page->page_cgroup & ~PAGE_CGROUP_LOCK);
159}
160
8697d331 161static void __always_inline lock_page_cgroup(struct page *page)
8a9f3ccd
BS
162{
163 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
164 VM_BUG_ON(!page_cgroup_locked(page));
165}
166
8697d331 167static void __always_inline unlock_page_cgroup(struct page *page)
8a9f3ccd
BS
168{
169 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
170}
171
9175e031
KH
172/*
173 * Tie new page_cgroup to struct page under lock_page_cgroup()
174 * This can fail if the page has been tied to a page_cgroup.
175 * If success, returns 0.
176 */
177static inline int
178page_cgroup_assign_new_page_cgroup(struct page *page, struct page_cgroup *pc)
179{
180 int ret = 0;
181
182 lock_page_cgroup(page);
183 if (!page_get_page_cgroup(page))
184 page_assign_page_cgroup(page, pc);
185 else /* A page is tied to other pc. */
186 ret = 1;
187 unlock_page_cgroup(page);
188 return ret;
189}
190
191/*
192 * Clear page->page_cgroup member under lock_page_cgroup().
193 * If given "pc" value is different from one page->page_cgroup,
194 * page->cgroup is not cleared.
195 * Returns a value of page->page_cgroup at lock taken.
196 * A can can detect failure of clearing by following
197 * clear_page_cgroup(page, pc) == pc
198 */
199
200static inline struct page_cgroup *
201clear_page_cgroup(struct page *page, struct page_cgroup *pc)
202{
203 struct page_cgroup *ret;
204 /* lock and clear */
205 lock_page_cgroup(page);
206 ret = page_get_page_cgroup(page);
207 if (likely(ret == pc))
208 page_assign_page_cgroup(page, NULL);
209 unlock_page_cgroup(page);
210 return ret;
211}
212
213
8697d331 214static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
66e1707b
BS
215{
216 if (active)
217 list_move(&pc->lru, &pc->mem_cgroup->active_list);
218 else
219 list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
220}
221
4c4a2214
DR
222int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
223{
224 int ret;
225
226 task_lock(task);
227 ret = task->mm && mm_cgroup(task->mm) == mem;
228 task_unlock(task);
229 return ret;
230}
231
66e1707b
BS
232/*
233 * This routine assumes that the appropriate zone's lru lock is already held
234 */
235void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
236{
237 struct mem_cgroup *mem;
238 if (!pc)
239 return;
240
241 mem = pc->mem_cgroup;
242
243 spin_lock(&mem->lru_lock);
244 __mem_cgroup_move_lists(pc, active);
245 spin_unlock(&mem->lru_lock);
246}
247
248unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
249 struct list_head *dst,
250 unsigned long *scanned, int order,
251 int mode, struct zone *z,
252 struct mem_cgroup *mem_cont,
253 int active)
254{
255 unsigned long nr_taken = 0;
256 struct page *page;
257 unsigned long scan;
258 LIST_HEAD(pc_list);
259 struct list_head *src;
ff7283fa 260 struct page_cgroup *pc, *tmp;
66e1707b
BS
261
262 if (active)
263 src = &mem_cont->active_list;
264 else
265 src = &mem_cont->inactive_list;
266
267 spin_lock(&mem_cont->lru_lock);
ff7283fa
KH
268 scan = 0;
269 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
436c6541 270 if (scan >= nr_to_scan)
ff7283fa 271 break;
66e1707b
BS
272 page = pc->page;
273 VM_BUG_ON(!pc);
274
436c6541 275 if (unlikely(!PageLRU(page)))
ff7283fa 276 continue;
ff7283fa 277
66e1707b
BS
278 if (PageActive(page) && !active) {
279 __mem_cgroup_move_lists(pc, true);
66e1707b
BS
280 continue;
281 }
282 if (!PageActive(page) && active) {
283 __mem_cgroup_move_lists(pc, false);
66e1707b
BS
284 continue;
285 }
286
287 /*
288 * Reclaim, per zone
289 * TODO: make the active/inactive lists per zone
290 */
291 if (page_zone(page) != z)
292 continue;
293
436c6541
HD
294 scan++;
295 list_move(&pc->lru, &pc_list);
66e1707b
BS
296
297 if (__isolate_lru_page(page, mode) == 0) {
298 list_move(&page->lru, dst);
299 nr_taken++;
300 }
301 }
302
303 list_splice(&pc_list, src);
304 spin_unlock(&mem_cont->lru_lock);
305
306 *scanned = scan;
307 return nr_taken;
308}
309
8a9f3ccd
BS
310/*
311 * Charge the memory controller for page usage.
312 * Return
313 * 0 if the charge was successful
314 * < 0 if the cgroup is over its limit
315 */
217bc319
KH
316static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
317 gfp_t gfp_mask, enum charge_type ctype)
8a9f3ccd
BS
318{
319 struct mem_cgroup *mem;
9175e031 320 struct page_cgroup *pc;
66e1707b
BS
321 unsigned long flags;
322 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
8a9f3ccd
BS
323
324 /*
325 * Should page_cgroup's go to their own slab?
326 * One could optimize the performance of the charging routine
327 * by saving a bit in the page_flags and using it as a lock
328 * to see if the cgroup page already has a page_cgroup associated
329 * with it
330 */
66e1707b 331retry:
8a9f3ccd
BS
332 lock_page_cgroup(page);
333 pc = page_get_page_cgroup(page);
334 /*
335 * The page_cgroup exists and the page has already been accounted
336 */
337 if (pc) {
66e1707b
BS
338 if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
339 /* this page is under being uncharged ? */
340 unlock_page_cgroup(page);
341 cpu_relax();
342 goto retry;
9175e031
KH
343 } else {
344 unlock_page_cgroup(page);
66e1707b 345 goto done;
9175e031 346 }
8a9f3ccd
BS
347 }
348
349 unlock_page_cgroup(page);
350
e1a1cd59 351 pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
8a9f3ccd
BS
352 if (pc == NULL)
353 goto err;
354
355 rcu_read_lock();
356 /*
357 * We always charge the cgroup the mm_struct belongs to
358 * the mm_struct's mem_cgroup changes on task migration if the
359 * thread group leader migrates. It's possible that mm is not
360 * set, if so charge the init_mm (happens for pagecache usage).
361 */
362 if (!mm)
363 mm = &init_mm;
364
365 mem = rcu_dereference(mm->mem_cgroup);
366 /*
367 * For every charge from the cgroup, increment reference
368 * count
369 */
370 css_get(&mem->css);
371 rcu_read_unlock();
372
373 /*
374 * If we created the page_cgroup, we should free it on exceeding
375 * the cgroup limit.
376 */
0eea1030 377 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
e1a1cd59
BS
378 bool is_atomic = gfp_mask & GFP_ATOMIC;
379 /*
380 * We cannot reclaim under GFP_ATOMIC, fail the charge
381 */
382 if (is_atomic)
383 goto noreclaim;
384
385 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
66e1707b
BS
386 continue;
387
388 /*
389 * try_to_free_mem_cgroup_pages() might not give us a full
390 * picture of reclaim. Some pages are reclaimed and might be
391 * moved to swap cache or just unmapped from the cgroup.
392 * Check the limit again to see if the reclaim reduced the
393 * current usage of the cgroup before giving up
394 */
395 if (res_counter_check_under_limit(&mem->res))
396 continue;
397 /*
398 * Since we control both RSS and cache, we end up with a
399 * very interesting scenario where we end up reclaiming
400 * memory (essentially RSS), since the memory is pushed
401 * to swap cache, we eventually end up adding those
402 * pages back to our list. Hence we give ourselves a
403 * few chances before we fail
404 */
405 else if (nr_retries--) {
406 congestion_wait(WRITE, HZ/10);
407 continue;
408 }
e1a1cd59 409noreclaim:
8a9f3ccd 410 css_put(&mem->css);
e1a1cd59
BS
411 if (!is_atomic)
412 mem_cgroup_out_of_memory(mem, GFP_KERNEL);
8a9f3ccd
BS
413 goto free_pc;
414 }
415
8a9f3ccd
BS
416 atomic_set(&pc->ref_cnt, 1);
417 pc->mem_cgroup = mem;
418 pc->page = page;
217bc319
KH
419 pc->flags = 0;
420 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
421 pc->flags |= PAGE_CGROUP_FLAG_CACHE;
9175e031
KH
422 if (page_cgroup_assign_new_page_cgroup(page, pc)) {
423 /*
424 * an another charge is added to this page already.
425 * we do take lock_page_cgroup(page) again and read
426 * page->cgroup, increment refcnt.... just retry is OK.
427 */
428 res_counter_uncharge(&mem->res, PAGE_SIZE);
429 css_put(&mem->css);
430 kfree(pc);
431 goto retry;
432 }
8a9f3ccd 433
66e1707b
BS
434 spin_lock_irqsave(&mem->lru_lock, flags);
435 list_add(&pc->lru, &mem->active_list);
436 spin_unlock_irqrestore(&mem->lru_lock, flags);
437
8a9f3ccd 438done:
8a9f3ccd
BS
439 return 0;
440free_pc:
441 kfree(pc);
8a9f3ccd 442err:
8a9f3ccd
BS
443 return -ENOMEM;
444}
445
217bc319
KH
446int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
447 gfp_t gfp_mask)
448{
449 return mem_cgroup_charge_common(page, mm, gfp_mask,
450 MEM_CGROUP_CHARGE_TYPE_MAPPED);
451}
452
8697d331
BS
453/*
454 * See if the cached pages should be charged at all?
455 */
e1a1cd59
BS
456int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
457 gfp_t gfp_mask)
8697d331 458{
ac44d354 459 int ret = 0;
8697d331
BS
460 struct mem_cgroup *mem;
461 if (!mm)
462 mm = &init_mm;
463
ac44d354 464 rcu_read_lock();
8697d331 465 mem = rcu_dereference(mm->mem_cgroup);
ac44d354
BS
466 css_get(&mem->css);
467 rcu_read_unlock();
8697d331 468 if (mem->control_type == MEM_CGROUP_TYPE_ALL)
ac44d354 469 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
217bc319 470 MEM_CGROUP_CHARGE_TYPE_CACHE);
ac44d354
BS
471 css_put(&mem->css);
472 return ret;
8697d331
BS
473}
474
8a9f3ccd
BS
475/*
476 * Uncharging is always a welcome operation, we never complain, simply
477 * uncharge.
478 */
479void mem_cgroup_uncharge(struct page_cgroup *pc)
480{
481 struct mem_cgroup *mem;
482 struct page *page;
66e1707b 483 unsigned long flags;
8a9f3ccd 484
8697d331
BS
485 /*
486 * This can handle cases when a page is not charged at all and we
487 * are switching between handling the control_type.
488 */
8a9f3ccd
BS
489 if (!pc)
490 return;
491
492 if (atomic_dec_and_test(&pc->ref_cnt)) {
493 page = pc->page;
9175e031
KH
494 /*
495 * get page->cgroup and clear it under lock.
cc847582 496 * force_empty can drop page->cgroup without checking refcnt.
9175e031
KH
497 */
498 if (clear_page_cgroup(page, pc) == pc) {
499 mem = pc->mem_cgroup;
500 css_put(&mem->css);
501 res_counter_uncharge(&mem->res, PAGE_SIZE);
502 spin_lock_irqsave(&mem->lru_lock, flags);
503 list_del_init(&pc->lru);
504 spin_unlock_irqrestore(&mem->lru_lock, flags);
505 kfree(pc);
9175e031 506 }
8a9f3ccd 507 }
78fb7466 508}
ae41be37
KH
509/*
510 * Returns non-zero if a page (under migration) has valid page_cgroup member.
511 * Refcnt of page_cgroup is incremented.
512 */
513
514int mem_cgroup_prepare_migration(struct page *page)
515{
516 struct page_cgroup *pc;
517 int ret = 0;
518 lock_page_cgroup(page);
519 pc = page_get_page_cgroup(page);
520 if (pc && atomic_inc_not_zero(&pc->ref_cnt))
521 ret = 1;
522 unlock_page_cgroup(page);
523 return ret;
524}
525
526void mem_cgroup_end_migration(struct page *page)
527{
528 struct page_cgroup *pc = page_get_page_cgroup(page);
529 mem_cgroup_uncharge(pc);
530}
531/*
532 * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
533 * And no race with uncharge() routines because page_cgroup for *page*
534 * has extra one reference by mem_cgroup_prepare_migration.
535 */
536
537void mem_cgroup_page_migration(struct page *page, struct page *newpage)
538{
539 struct page_cgroup *pc;
540retry:
541 pc = page_get_page_cgroup(page);
542 if (!pc)
543 return;
544 if (clear_page_cgroup(page, pc) != pc)
545 goto retry;
546 pc->page = newpage;
547 lock_page_cgroup(newpage);
548 page_assign_page_cgroup(newpage, pc);
549 unlock_page_cgroup(newpage);
550 return;
551}
78fb7466 552
cc847582
KH
553/*
554 * This routine traverse page_cgroup in given list and drop them all.
555 * This routine ignores page_cgroup->ref_cnt.
556 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
557 */
558#define FORCE_UNCHARGE_BATCH (128)
559static void
560mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list)
561{
562 struct page_cgroup *pc;
563 struct page *page;
564 int count;
565 unsigned long flags;
566
567retry:
568 count = FORCE_UNCHARGE_BATCH;
569 spin_lock_irqsave(&mem->lru_lock, flags);
570
571 while (--count && !list_empty(list)) {
572 pc = list_entry(list->prev, struct page_cgroup, lru);
573 page = pc->page;
574 /* Avoid race with charge */
575 atomic_set(&pc->ref_cnt, 0);
576 if (clear_page_cgroup(page, pc) == pc) {
577 css_put(&mem->css);
578 res_counter_uncharge(&mem->res, PAGE_SIZE);
579 list_del_init(&pc->lru);
580 kfree(pc);
581 } else /* being uncharged ? ...do relax */
582 break;
583 }
584 spin_unlock_irqrestore(&mem->lru_lock, flags);
585 if (!list_empty(list)) {
586 cond_resched();
587 goto retry;
588 }
589 return;
590}
591
592/*
593 * make mem_cgroup's charge to be 0 if there is no task.
594 * This enables deleting this mem_cgroup.
595 */
596
597int mem_cgroup_force_empty(struct mem_cgroup *mem)
598{
599 int ret = -EBUSY;
600 css_get(&mem->css);
601 /*
602 * page reclaim code (kswapd etc..) will move pages between
603` * active_list <-> inactive_list while we don't take a lock.
604 * So, we have to do loop here until all lists are empty.
605 */
606 while (!(list_empty(&mem->active_list) &&
607 list_empty(&mem->inactive_list))) {
608 if (atomic_read(&mem->css.cgroup->count) > 0)
609 goto out;
610 /* drop all page_cgroup in active_list */
611 mem_cgroup_force_empty_list(mem, &mem->active_list);
612 /* drop all page_cgroup in inactive_list */
613 mem_cgroup_force_empty_list(mem, &mem->inactive_list);
614 }
615 ret = 0;
616out:
617 css_put(&mem->css);
618 return ret;
619}
620
621
622
0eea1030
BS
623int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
624{
625 *tmp = memparse(buf, &buf);
626 if (*buf != '\0')
627 return -EINVAL;
628
629 /*
630 * Round up the value to the closest page size
631 */
632 *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
633 return 0;
634}
635
636static ssize_t mem_cgroup_read(struct cgroup *cont,
637 struct cftype *cft, struct file *file,
638 char __user *userbuf, size_t nbytes, loff_t *ppos)
8cdea7c0
BS
639{
640 return res_counter_read(&mem_cgroup_from_cont(cont)->res,
0eea1030
BS
641 cft->private, userbuf, nbytes, ppos,
642 NULL);
8cdea7c0
BS
643}
644
645static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
646 struct file *file, const char __user *userbuf,
647 size_t nbytes, loff_t *ppos)
648{
649 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
0eea1030
BS
650 cft->private, userbuf, nbytes, ppos,
651 mem_cgroup_write_strategy);
8cdea7c0
BS
652}
653
8697d331
BS
654static ssize_t mem_control_type_write(struct cgroup *cont,
655 struct cftype *cft, struct file *file,
656 const char __user *userbuf,
657 size_t nbytes, loff_t *pos)
658{
659 int ret;
660 char *buf, *end;
661 unsigned long tmp;
662 struct mem_cgroup *mem;
663
664 mem = mem_cgroup_from_cont(cont);
665 buf = kmalloc(nbytes + 1, GFP_KERNEL);
666 ret = -ENOMEM;
667 if (buf == NULL)
668 goto out;
669
670 buf[nbytes] = 0;
671 ret = -EFAULT;
672 if (copy_from_user(buf, userbuf, nbytes))
673 goto out_free;
674
675 ret = -EINVAL;
676 tmp = simple_strtoul(buf, &end, 10);
677 if (*end != '\0')
678 goto out_free;
679
680 if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
681 goto out_free;
682
683 mem->control_type = tmp;
684 ret = nbytes;
685out_free:
686 kfree(buf);
687out:
688 return ret;
689}
690
691static ssize_t mem_control_type_read(struct cgroup *cont,
692 struct cftype *cft,
693 struct file *file, char __user *userbuf,
694 size_t nbytes, loff_t *ppos)
695{
696 unsigned long val;
697 char buf[64], *s;
698 struct mem_cgroup *mem;
699
700 mem = mem_cgroup_from_cont(cont);
701 s = buf;
702 val = mem->control_type;
703 s += sprintf(s, "%lu\n", val);
704 return simple_read_from_buffer((void __user *)userbuf, nbytes,
705 ppos, buf, s - buf);
706}
707
cc847582
KH
708
709static ssize_t mem_force_empty_write(struct cgroup *cont,
710 struct cftype *cft, struct file *file,
711 const char __user *userbuf,
712 size_t nbytes, loff_t *ppos)
713{
714 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
715 int ret;
716 ret = mem_cgroup_force_empty(mem);
717 if (!ret)
718 ret = nbytes;
719 return ret;
720}
721
722/*
723 * Note: This should be removed if cgroup supports write-only file.
724 */
725
726static ssize_t mem_force_empty_read(struct cgroup *cont,
727 struct cftype *cft,
728 struct file *file, char __user *userbuf,
729 size_t nbytes, loff_t *ppos)
730{
731 return -EINVAL;
732}
733
734
8cdea7c0
BS
735static struct cftype mem_cgroup_files[] = {
736 {
0eea1030 737 .name = "usage_in_bytes",
8cdea7c0
BS
738 .private = RES_USAGE,
739 .read = mem_cgroup_read,
740 },
741 {
0eea1030 742 .name = "limit_in_bytes",
8cdea7c0
BS
743 .private = RES_LIMIT,
744 .write = mem_cgroup_write,
745 .read = mem_cgroup_read,
746 },
747 {
748 .name = "failcnt",
749 .private = RES_FAILCNT,
750 .read = mem_cgroup_read,
751 },
8697d331
BS
752 {
753 .name = "control_type",
754 .write = mem_control_type_write,
755 .read = mem_control_type_read,
756 },
cc847582
KH
757 {
758 .name = "force_empty",
759 .write = mem_force_empty_write,
760 .read = mem_force_empty_read,
761 },
8cdea7c0
BS
762};
763
78fb7466
PE
764static struct mem_cgroup init_mem_cgroup;
765
8cdea7c0
BS
766static struct cgroup_subsys_state *
767mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
768{
769 struct mem_cgroup *mem;
770
78fb7466
PE
771 if (unlikely((cont->parent) == NULL)) {
772 mem = &init_mem_cgroup;
773 init_mm.mem_cgroup = mem;
774 } else
775 mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
776
777 if (mem == NULL)
778 return NULL;
8cdea7c0
BS
779
780 res_counter_init(&mem->res);
8a9f3ccd
BS
781 INIT_LIST_HEAD(&mem->active_list);
782 INIT_LIST_HEAD(&mem->inactive_list);
66e1707b 783 spin_lock_init(&mem->lru_lock);
8697d331 784 mem->control_type = MEM_CGROUP_TYPE_ALL;
8cdea7c0
BS
785 return &mem->css;
786}
787
788static void mem_cgroup_destroy(struct cgroup_subsys *ss,
789 struct cgroup *cont)
790{
791 kfree(mem_cgroup_from_cont(cont));
792}
793
794static int mem_cgroup_populate(struct cgroup_subsys *ss,
795 struct cgroup *cont)
796{
797 return cgroup_add_files(cont, ss, mem_cgroup_files,
798 ARRAY_SIZE(mem_cgroup_files));
799}
800
67e465a7
BS
801static void mem_cgroup_move_task(struct cgroup_subsys *ss,
802 struct cgroup *cont,
803 struct cgroup *old_cont,
804 struct task_struct *p)
805{
806 struct mm_struct *mm;
807 struct mem_cgroup *mem, *old_mem;
808
809 mm = get_task_mm(p);
810 if (mm == NULL)
811 return;
812
813 mem = mem_cgroup_from_cont(cont);
814 old_mem = mem_cgroup_from_cont(old_cont);
815
816 if (mem == old_mem)
817 goto out;
818
819 /*
820 * Only thread group leaders are allowed to migrate, the mm_struct is
821 * in effect owned by the leader
822 */
823 if (p->tgid != p->pid)
824 goto out;
825
826 css_get(&mem->css);
827 rcu_assign_pointer(mm->mem_cgroup, mem);
828 css_put(&old_mem->css);
829
830out:
831 mmput(mm);
832 return;
833}
834
8cdea7c0
BS
835struct cgroup_subsys mem_cgroup_subsys = {
836 .name = "memory",
837 .subsys_id = mem_cgroup_subsys_id,
838 .create = mem_cgroup_create,
839 .destroy = mem_cgroup_destroy,
840 .populate = mem_cgroup_populate,
67e465a7 841 .attach = mem_cgroup_move_task,
78fb7466 842 .early_init = 1,
8cdea7c0 843};