1 /* memcontrol.c - Memory Controller
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
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.
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.
20 #include <linux/res_counter.h>
21 #include <linux/memcontrol.h>
22 #include <linux/cgroup.h>
24 #include <linux/page-flags.h>
25 #include <linux/backing-dev.h>
26 #include <linux/bit_spinlock.h>
27 #include <linux/rcupdate.h>
28 #include <linux/swap.h>
29 #include <linux/spinlock.h>
32 #include <asm/uaccess.h>
34 struct cgroup_subsys mem_cgroup_subsys;
35 static const int MEM_CGROUP_RECLAIM_RETRIES = 5;
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.
43 * TODO: Add a water mark for the memory controller. Reclaim will begin when
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.
49 struct cgroup_subsys_state css;
51 * the counter to account for memory usage
53 struct res_counter res;
55 * Per cgroup active and inactive list, similar to the
57 * TODO: Consider making these lists per zone
59 struct list_head active_list;
60 struct list_head inactive_list;
62 * spin_lock to protect the per cgroup LRU
65 unsigned long control_type; /* control RSS or RSS+Pagecache */
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)
73 #define PAGE_CGROUP_LOCK_BIT 0x0
74 #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
77 * A page_cgroup page is associated with every page descriptor. The
78 * page_cgroup helps us identify information about the cgroup
81 struct list_head lru; /* per cgroup LRU list */
83 struct mem_cgroup *mem_cgroup;
84 atomic_t ref_cnt; /* Helpful when pages move b/w */
85 /* mapped and cached states */
88 #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
91 MEM_CGROUP_TYPE_UNSPEC = 0,
92 MEM_CGROUP_TYPE_MAPPED,
93 MEM_CGROUP_TYPE_CACHED,
99 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
100 MEM_CGROUP_CHARGE_TYPE_MAPPED,
103 static struct mem_cgroup init_mem_cgroup;
106 struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
108 return container_of(cgroup_subsys_state(cont,
109 mem_cgroup_subsys_id), struct mem_cgroup,
114 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
116 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
117 struct mem_cgroup, css);
120 void mm_init_cgroup(struct mm_struct *mm, struct task_struct *p)
122 struct mem_cgroup *mem;
124 mem = mem_cgroup_from_task(p);
126 mm->mem_cgroup = mem;
129 void mm_free_cgroup(struct mm_struct *mm)
131 css_put(&mm->mem_cgroup->css);
134 static inline int page_cgroup_locked(struct page *page)
136 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT,
140 void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
145 * While resetting the page_cgroup we might not hold the
146 * page_cgroup lock. free_hot_cold_page() is an example
150 VM_BUG_ON(!page_cgroup_locked(page));
151 locked = (page->page_cgroup & PAGE_CGROUP_LOCK);
152 page->page_cgroup = ((unsigned long)pc | locked);
155 struct page_cgroup *page_get_page_cgroup(struct page *page)
157 return (struct page_cgroup *)
158 (page->page_cgroup & ~PAGE_CGROUP_LOCK);
161 static void __always_inline lock_page_cgroup(struct page *page)
163 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
164 VM_BUG_ON(!page_cgroup_locked(page));
167 static void __always_inline unlock_page_cgroup(struct page *page)
169 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
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.
178 page_cgroup_assign_new_page_cgroup(struct page *page, struct page_cgroup *pc)
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. */
187 unlock_page_cgroup(page);
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
200 static inline struct page_cgroup *
201 clear_page_cgroup(struct page *page, struct page_cgroup *pc)
203 struct page_cgroup *ret;
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);
214 static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
217 list_move(&pc->lru, &pc->mem_cgroup->active_list);
219 list_move(&pc->lru, &pc->mem_cgroup->inactive_list);
222 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
227 ret = task->mm && mm_cgroup(task->mm) == mem;
233 * This routine assumes that the appropriate zone's lru lock is already held
235 void mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
237 struct mem_cgroup *mem;
241 mem = pc->mem_cgroup;
243 spin_lock(&mem->lru_lock);
244 __mem_cgroup_move_lists(pc, active);
245 spin_unlock(&mem->lru_lock);
248 unsigned 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,
255 unsigned long nr_taken = 0;
259 struct list_head *src;
260 struct page_cgroup *pc, *tmp;
263 src = &mem_cont->active_list;
265 src = &mem_cont->inactive_list;
267 spin_lock(&mem_cont->lru_lock);
269 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
270 if (scan >= nr_to_scan)
275 if (unlikely(!PageLRU(page)))
278 if (PageActive(page) && !active) {
279 __mem_cgroup_move_lists(pc, true);
282 if (!PageActive(page) && active) {
283 __mem_cgroup_move_lists(pc, false);
289 * TODO: make the active/inactive lists per zone
291 if (page_zone(page) != z)
295 list_move(&pc->lru, &pc_list);
297 if (__isolate_lru_page(page, mode) == 0) {
298 list_move(&page->lru, dst);
303 list_splice(&pc_list, src);
304 spin_unlock(&mem_cont->lru_lock);
311 * Charge the memory controller for page usage.
313 * 0 if the charge was successful
314 * < 0 if the cgroup is over its limit
316 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
317 gfp_t gfp_mask, enum charge_type ctype)
319 struct mem_cgroup *mem;
320 struct page_cgroup *pc;
322 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
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
332 lock_page_cgroup(page);
333 pc = page_get_page_cgroup(page);
335 * The page_cgroup exists and the page has already been accounted
338 if (unlikely(!atomic_inc_not_zero(&pc->ref_cnt))) {
339 /* this page is under being uncharged ? */
340 unlock_page_cgroup(page);
344 unlock_page_cgroup(page);
349 unlock_page_cgroup(page);
351 pc = kzalloc(sizeof(struct page_cgroup), gfp_mask);
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).
365 mem = rcu_dereference(mm->mem_cgroup);
367 * For every charge from the cgroup, increment reference
374 * If we created the page_cgroup, we should free it on exceeding
377 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
378 bool is_atomic = gfp_mask & GFP_ATOMIC;
380 * We cannot reclaim under GFP_ATOMIC, fail the charge
385 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
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
395 if (res_counter_check_under_limit(&mem->res))
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
405 else if (nr_retries--) {
406 congestion_wait(WRITE, HZ/10);
412 mem_cgroup_out_of_memory(mem, GFP_KERNEL);
416 atomic_set(&pc->ref_cnt, 1);
417 pc->mem_cgroup = mem;
420 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
421 pc->flags |= PAGE_CGROUP_FLAG_CACHE;
422 if (page_cgroup_assign_new_page_cgroup(page, pc)) {
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.
428 res_counter_uncharge(&mem->res, PAGE_SIZE);
434 spin_lock_irqsave(&mem->lru_lock, flags);
435 list_add(&pc->lru, &mem->active_list);
436 spin_unlock_irqrestore(&mem->lru_lock, flags);
446 int mem_cgroup_charge(struct page *page, struct mm_struct *mm,
449 return mem_cgroup_charge_common(page, mm, gfp_mask,
450 MEM_CGROUP_CHARGE_TYPE_MAPPED);
454 * See if the cached pages should be charged at all?
456 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
459 struct mem_cgroup *mem;
463 mem = rcu_dereference(mm->mem_cgroup);
464 if (mem->control_type == MEM_CGROUP_TYPE_ALL)
465 return mem_cgroup_charge_common(page, mm, gfp_mask,
466 MEM_CGROUP_CHARGE_TYPE_CACHE);
472 * Uncharging is always a welcome operation, we never complain, simply
475 void mem_cgroup_uncharge(struct page_cgroup *pc)
477 struct mem_cgroup *mem;
482 * This can handle cases when a page is not charged at all and we
483 * are switching between handling the control_type.
488 if (atomic_dec_and_test(&pc->ref_cnt)) {
491 * get page->cgroup and clear it under lock.
492 * force_empty can drop page->cgroup without checking refcnt.
494 if (clear_page_cgroup(page, pc) == pc) {
495 mem = pc->mem_cgroup;
497 res_counter_uncharge(&mem->res, PAGE_SIZE);
498 spin_lock_irqsave(&mem->lru_lock, flags);
499 list_del_init(&pc->lru);
500 spin_unlock_irqrestore(&mem->lru_lock, flags);
506 * Returns non-zero if a page (under migration) has valid page_cgroup member.
507 * Refcnt of page_cgroup is incremented.
510 int mem_cgroup_prepare_migration(struct page *page)
512 struct page_cgroup *pc;
514 lock_page_cgroup(page);
515 pc = page_get_page_cgroup(page);
516 if (pc && atomic_inc_not_zero(&pc->ref_cnt))
518 unlock_page_cgroup(page);
522 void mem_cgroup_end_migration(struct page *page)
524 struct page_cgroup *pc = page_get_page_cgroup(page);
525 mem_cgroup_uncharge(pc);
528 * We know both *page* and *newpage* are now not-on-LRU and Pg_locked.
529 * And no race with uncharge() routines because page_cgroup for *page*
530 * has extra one reference by mem_cgroup_prepare_migration.
533 void mem_cgroup_page_migration(struct page *page, struct page *newpage)
535 struct page_cgroup *pc;
537 pc = page_get_page_cgroup(page);
540 if (clear_page_cgroup(page, pc) != pc)
543 lock_page_cgroup(newpage);
544 page_assign_page_cgroup(newpage, pc);
545 unlock_page_cgroup(newpage);
550 * This routine traverse page_cgroup in given list and drop them all.
551 * This routine ignores page_cgroup->ref_cnt.
552 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
554 #define FORCE_UNCHARGE_BATCH (128)
556 mem_cgroup_force_empty_list(struct mem_cgroup *mem, struct list_head *list)
558 struct page_cgroup *pc;
564 count = FORCE_UNCHARGE_BATCH;
565 spin_lock_irqsave(&mem->lru_lock, flags);
567 while (--count && !list_empty(list)) {
568 pc = list_entry(list->prev, struct page_cgroup, lru);
570 /* Avoid race with charge */
571 atomic_set(&pc->ref_cnt, 0);
572 if (clear_page_cgroup(page, pc) == pc) {
574 res_counter_uncharge(&mem->res, PAGE_SIZE);
575 list_del_init(&pc->lru);
577 } else /* being uncharged ? ...do relax */
580 spin_unlock_irqrestore(&mem->lru_lock, flags);
581 if (!list_empty(list)) {
589 * make mem_cgroup's charge to be 0 if there is no task.
590 * This enables deleting this mem_cgroup.
593 int mem_cgroup_force_empty(struct mem_cgroup *mem)
598 * page reclaim code (kswapd etc..) will move pages between
599 ` * active_list <-> inactive_list while we don't take a lock.
600 * So, we have to do loop here until all lists are empty.
602 while (!(list_empty(&mem->active_list) &&
603 list_empty(&mem->inactive_list))) {
604 if (atomic_read(&mem->css.cgroup->count) > 0)
606 /* drop all page_cgroup in active_list */
607 mem_cgroup_force_empty_list(mem, &mem->active_list);
608 /* drop all page_cgroup in inactive_list */
609 mem_cgroup_force_empty_list(mem, &mem->inactive_list);
619 int mem_cgroup_write_strategy(char *buf, unsigned long long *tmp)
621 *tmp = memparse(buf, &buf);
626 * Round up the value to the closest page size
628 *tmp = ((*tmp + PAGE_SIZE - 1) >> PAGE_SHIFT) << PAGE_SHIFT;
632 static ssize_t mem_cgroup_read(struct cgroup *cont,
633 struct cftype *cft, struct file *file,
634 char __user *userbuf, size_t nbytes, loff_t *ppos)
636 return res_counter_read(&mem_cgroup_from_cont(cont)->res,
637 cft->private, userbuf, nbytes, ppos,
641 static ssize_t mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
642 struct file *file, const char __user *userbuf,
643 size_t nbytes, loff_t *ppos)
645 return res_counter_write(&mem_cgroup_from_cont(cont)->res,
646 cft->private, userbuf, nbytes, ppos,
647 mem_cgroup_write_strategy);
650 static ssize_t mem_control_type_write(struct cgroup *cont,
651 struct cftype *cft, struct file *file,
652 const char __user *userbuf,
653 size_t nbytes, loff_t *pos)
658 struct mem_cgroup *mem;
660 mem = mem_cgroup_from_cont(cont);
661 buf = kmalloc(nbytes + 1, GFP_KERNEL);
668 if (copy_from_user(buf, userbuf, nbytes))
672 tmp = simple_strtoul(buf, &end, 10);
676 if (tmp <= MEM_CGROUP_TYPE_UNSPEC || tmp >= MEM_CGROUP_TYPE_MAX)
679 mem->control_type = tmp;
687 static ssize_t mem_control_type_read(struct cgroup *cont,
689 struct file *file, char __user *userbuf,
690 size_t nbytes, loff_t *ppos)
694 struct mem_cgroup *mem;
696 mem = mem_cgroup_from_cont(cont);
698 val = mem->control_type;
699 s += sprintf(s, "%lu\n", val);
700 return simple_read_from_buffer((void __user *)userbuf, nbytes,
705 static ssize_t mem_force_empty_write(struct cgroup *cont,
706 struct cftype *cft, struct file *file,
707 const char __user *userbuf,
708 size_t nbytes, loff_t *ppos)
710 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
712 ret = mem_cgroup_force_empty(mem);
719 * Note: This should be removed if cgroup supports write-only file.
722 static ssize_t mem_force_empty_read(struct cgroup *cont,
724 struct file *file, char __user *userbuf,
725 size_t nbytes, loff_t *ppos)
731 static struct cftype mem_cgroup_files[] = {
733 .name = "usage_in_bytes",
734 .private = RES_USAGE,
735 .read = mem_cgroup_read,
738 .name = "limit_in_bytes",
739 .private = RES_LIMIT,
740 .write = mem_cgroup_write,
741 .read = mem_cgroup_read,
745 .private = RES_FAILCNT,
746 .read = mem_cgroup_read,
749 .name = "control_type",
750 .write = mem_control_type_write,
751 .read = mem_control_type_read,
754 .name = "force_empty",
755 .write = mem_force_empty_write,
756 .read = mem_force_empty_read,
760 static struct mem_cgroup init_mem_cgroup;
762 static struct cgroup_subsys_state *
763 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
765 struct mem_cgroup *mem;
767 if (unlikely((cont->parent) == NULL)) {
768 mem = &init_mem_cgroup;
769 init_mm.mem_cgroup = mem;
771 mem = kzalloc(sizeof(struct mem_cgroup), GFP_KERNEL);
776 res_counter_init(&mem->res);
777 INIT_LIST_HEAD(&mem->active_list);
778 INIT_LIST_HEAD(&mem->inactive_list);
779 spin_lock_init(&mem->lru_lock);
780 mem->control_type = MEM_CGROUP_TYPE_ALL;
784 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
787 kfree(mem_cgroup_from_cont(cont));
790 static int mem_cgroup_populate(struct cgroup_subsys *ss,
793 return cgroup_add_files(cont, ss, mem_cgroup_files,
794 ARRAY_SIZE(mem_cgroup_files));
797 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
799 struct cgroup *old_cont,
800 struct task_struct *p)
802 struct mm_struct *mm;
803 struct mem_cgroup *mem, *old_mem;
809 mem = mem_cgroup_from_cont(cont);
810 old_mem = mem_cgroup_from_cont(old_cont);
816 * Only thread group leaders are allowed to migrate, the mm_struct is
817 * in effect owned by the leader
819 if (p->tgid != p->pid)
823 rcu_assign_pointer(mm->mem_cgroup, mem);
824 css_put(&old_mem->css);
831 struct cgroup_subsys mem_cgroup_subsys = {
833 .subsys_id = mem_cgroup_subsys_id,
834 .create = mem_cgroup_create,
835 .destroy = mem_cgroup_destroy,
836 .populate = mem_cgroup_populate,
837 .attach = mem_cgroup_move_task,