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/pagemap.h>
25 #include <linux/smp.h>
26 #include <linux/page-flags.h>
27 #include <linux/backing-dev.h>
28 #include <linux/bit_spinlock.h>
29 #include <linux/rcupdate.h>
30 #include <linux/mutex.h>
31 #include <linux/slab.h>
32 #include <linux/swap.h>
33 #include <linux/spinlock.h>
35 #include <linux/seq_file.h>
36 #include <linux/vmalloc.h>
37 #include <linux/mm_inline.h>
38 #include <linux/page_cgroup.h>
41 #include <asm/uaccess.h>
43 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
44 #define MEM_CGROUP_RECLAIM_RETRIES 5
46 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
47 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
48 int do_swap_account __read_mostly;
49 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
51 #define do_swap_account (0)
56 * Statistics for memory cgroup.
58 enum mem_cgroup_stat_index {
60 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
62 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
63 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
64 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
65 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
67 MEM_CGROUP_STAT_NSTATS,
70 struct mem_cgroup_stat_cpu {
71 s64 count[MEM_CGROUP_STAT_NSTATS];
72 } ____cacheline_aligned_in_smp;
74 struct mem_cgroup_stat {
75 struct mem_cgroup_stat_cpu cpustat[0];
79 * For accounting under irq disable, no need for increment preempt count.
81 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
82 enum mem_cgroup_stat_index idx, int val)
84 stat->count[idx] += val;
87 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
88 enum mem_cgroup_stat_index idx)
92 for_each_possible_cpu(cpu)
93 ret += stat->cpustat[cpu].count[idx];
98 * per-zone information in memory controller.
100 struct mem_cgroup_per_zone {
102 * spin_lock to protect the per cgroup LRU
104 struct list_head lists[NR_LRU_LISTS];
105 unsigned long count[NR_LRU_LISTS];
107 /* Macro for accessing counter */
108 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
110 struct mem_cgroup_per_node {
111 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
114 struct mem_cgroup_lru_info {
115 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
119 * The memory controller data structure. The memory controller controls both
120 * page cache and RSS per cgroup. We would eventually like to provide
121 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
122 * to help the administrator determine what knobs to tune.
124 * TODO: Add a water mark for the memory controller. Reclaim will begin when
125 * we hit the water mark. May be even add a low water mark, such that
126 * no reclaim occurs from a cgroup at it's low water mark, this is
127 * a feature that will be implemented much later in the future.
130 struct cgroup_subsys_state css;
132 * the counter to account for memory usage
134 struct res_counter res;
136 * the counter to account for mem+swap usage.
138 struct res_counter memsw;
140 * Per cgroup active and inactive list, similar to the
141 * per zone LRU lists.
143 struct mem_cgroup_lru_info info;
145 int prev_priority; /* for recording reclaim priority */
148 * While reclaiming in a hiearchy, we cache the last child we
149 * reclaimed from. Protected by cgroup_lock()
151 struct mem_cgroup *last_scanned_child;
153 * Should the accounting and control be hierarchical, per subtree?
156 unsigned long last_oom_jiffies;
160 * statistics. This must be placed at the end of memcg.
162 struct mem_cgroup_stat stat;
166 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
167 MEM_CGROUP_CHARGE_TYPE_MAPPED,
168 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
169 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
170 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
174 /* only for here (for easy reading.) */
175 #define PCGF_CACHE (1UL << PCG_CACHE)
176 #define PCGF_USED (1UL << PCG_USED)
177 #define PCGF_LOCK (1UL << PCG_LOCK)
178 static const unsigned long
179 pcg_default_flags[NR_CHARGE_TYPE] = {
180 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
181 PCGF_USED | PCGF_LOCK, /* Anon */
182 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
187 /* for encoding cft->private value on file */
190 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
191 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
192 #define MEMFILE_ATTR(val) ((val) & 0xffff)
194 static void mem_cgroup_get(struct mem_cgroup *mem);
195 static void mem_cgroup_put(struct mem_cgroup *mem);
197 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
198 struct page_cgroup *pc,
201 int val = (charge)? 1 : -1;
202 struct mem_cgroup_stat *stat = &mem->stat;
203 struct mem_cgroup_stat_cpu *cpustat;
206 cpustat = &stat->cpustat[cpu];
207 if (PageCgroupCache(pc))
208 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
210 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
213 __mem_cgroup_stat_add_safe(cpustat,
214 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
216 __mem_cgroup_stat_add_safe(cpustat,
217 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
221 static struct mem_cgroup_per_zone *
222 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
224 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
227 static struct mem_cgroup_per_zone *
228 page_cgroup_zoneinfo(struct page_cgroup *pc)
230 struct mem_cgroup *mem = pc->mem_cgroup;
231 int nid = page_cgroup_nid(pc);
232 int zid = page_cgroup_zid(pc);
237 return mem_cgroup_zoneinfo(mem, nid, zid);
240 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
244 struct mem_cgroup_per_zone *mz;
247 for_each_online_node(nid)
248 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
249 mz = mem_cgroup_zoneinfo(mem, nid, zid);
250 total += MEM_CGROUP_ZSTAT(mz, idx);
255 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
257 return container_of(cgroup_subsys_state(cont,
258 mem_cgroup_subsys_id), struct mem_cgroup,
262 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
265 * mm_update_next_owner() may clear mm->owner to NULL
266 * if it races with swapoff, page migration, etc.
267 * So this can be called with p == NULL.
272 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
273 struct mem_cgroup, css);
277 * Following LRU functions are allowed to be used without PCG_LOCK.
278 * Operations are called by routine of global LRU independently from memcg.
279 * What we have to take care of here is validness of pc->mem_cgroup.
281 * Changes to pc->mem_cgroup happens when
284 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
285 * It is added to LRU before charge.
286 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
287 * When moving account, the page is not on LRU. It's isolated.
290 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
292 struct page_cgroup *pc;
293 struct mem_cgroup *mem;
294 struct mem_cgroup_per_zone *mz;
296 if (mem_cgroup_disabled())
298 pc = lookup_page_cgroup(page);
299 /* can happen while we handle swapcache. */
300 if (list_empty(&pc->lru))
302 mz = page_cgroup_zoneinfo(pc);
303 mem = pc->mem_cgroup;
304 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
305 list_del_init(&pc->lru);
309 void mem_cgroup_del_lru(struct page *page)
311 mem_cgroup_del_lru_list(page, page_lru(page));
314 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
316 struct mem_cgroup_per_zone *mz;
317 struct page_cgroup *pc;
319 if (mem_cgroup_disabled())
322 pc = lookup_page_cgroup(page);
324 /* unused page is not rotated. */
325 if (!PageCgroupUsed(pc))
327 mz = page_cgroup_zoneinfo(pc);
328 list_move(&pc->lru, &mz->lists[lru]);
331 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
333 struct page_cgroup *pc;
334 struct mem_cgroup_per_zone *mz;
336 if (mem_cgroup_disabled())
338 pc = lookup_page_cgroup(page);
339 /* barrier to sync with "charge" */
341 if (!PageCgroupUsed(pc))
344 mz = page_cgroup_zoneinfo(pc);
345 MEM_CGROUP_ZSTAT(mz, lru) += 1;
346 list_add(&pc->lru, &mz->lists[lru]);
349 * To add swapcache into LRU. Be careful to all this function.
350 * zone->lru_lock shouldn't be held and irq must not be disabled.
352 static void mem_cgroup_lru_fixup(struct page *page)
354 if (!isolate_lru_page(page))
355 putback_lru_page(page);
358 void mem_cgroup_move_lists(struct page *page,
359 enum lru_list from, enum lru_list to)
361 if (mem_cgroup_disabled())
363 mem_cgroup_del_lru_list(page, from);
364 mem_cgroup_add_lru_list(page, to);
367 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
372 ret = task->mm && mm_match_cgroup(task->mm, mem);
378 * Calculate mapped_ratio under memory controller. This will be used in
379 * vmscan.c for deteremining we have to reclaim mapped pages.
381 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
386 * usage is recorded in bytes. But, here, we assume the number of
387 * physical pages can be represented by "long" on any arch.
389 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
390 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
391 return (int)((rss * 100L) / total);
395 * prev_priority control...this will be used in memory reclaim path.
397 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
399 return mem->prev_priority;
402 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
404 if (priority < mem->prev_priority)
405 mem->prev_priority = priority;
408 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
410 mem->prev_priority = priority;
414 * Calculate # of pages to be scanned in this priority/zone.
417 * priority starts from "DEF_PRIORITY" and decremented in each loop.
418 * (see include/linux/mmzone.h)
421 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
422 int priority, enum lru_list lru)
425 int nid = zone->zone_pgdat->node_id;
426 int zid = zone_idx(zone);
427 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
429 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
431 return (nr_pages >> priority);
434 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
435 struct list_head *dst,
436 unsigned long *scanned, int order,
437 int mode, struct zone *z,
438 struct mem_cgroup *mem_cont,
439 int active, int file)
441 unsigned long nr_taken = 0;
445 struct list_head *src;
446 struct page_cgroup *pc, *tmp;
447 int nid = z->zone_pgdat->node_id;
448 int zid = zone_idx(z);
449 struct mem_cgroup_per_zone *mz;
450 int lru = LRU_FILE * !!file + !!active;
453 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
454 src = &mz->lists[lru];
457 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
458 if (scan >= nr_to_scan)
462 if (unlikely(!PageCgroupUsed(pc)))
464 if (unlikely(!PageLRU(page)))
468 if (__isolate_lru_page(page, mode, file) == 0) {
469 list_move(&page->lru, dst);
478 #define mem_cgroup_from_res_counter(counter, member) \
479 container_of(counter, struct mem_cgroup, member)
482 * This routine finds the DFS walk successor. This routine should be
483 * called with cgroup_mutex held
485 static struct mem_cgroup *
486 mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
488 struct cgroup *cgroup, *curr_cgroup, *root_cgroup;
490 curr_cgroup = curr->css.cgroup;
491 root_cgroup = root_mem->css.cgroup;
493 if (!list_empty(&curr_cgroup->children)) {
495 * Walk down to children
497 mem_cgroup_put(curr);
498 cgroup = list_entry(curr_cgroup->children.next,
499 struct cgroup, sibling);
500 curr = mem_cgroup_from_cont(cgroup);
501 mem_cgroup_get(curr);
506 if (curr_cgroup == root_cgroup) {
507 mem_cgroup_put(curr);
509 mem_cgroup_get(curr);
516 if (curr_cgroup->sibling.next != &curr_cgroup->parent->children) {
517 mem_cgroup_put(curr);
518 cgroup = list_entry(curr_cgroup->sibling.next, struct cgroup,
520 curr = mem_cgroup_from_cont(cgroup);
521 mem_cgroup_get(curr);
526 * Go up to next parent and next parent's sibling if need be
528 curr_cgroup = curr_cgroup->parent;
532 root_mem->last_scanned_child = curr;
537 * Visit the first child (need not be the first child as per the ordering
538 * of the cgroup list, since we track last_scanned_child) of @mem and use
539 * that to reclaim free pages from.
541 static struct mem_cgroup *
542 mem_cgroup_get_first_node(struct mem_cgroup *root_mem)
544 struct cgroup *cgroup;
545 struct mem_cgroup *ret;
546 bool obsolete = (root_mem->last_scanned_child &&
547 root_mem->last_scanned_child->obsolete);
550 * Scan all children under the mem_cgroup mem
553 if (list_empty(&root_mem->css.cgroup->children)) {
558 if (!root_mem->last_scanned_child || obsolete) {
561 mem_cgroup_put(root_mem->last_scanned_child);
563 cgroup = list_first_entry(&root_mem->css.cgroup->children,
564 struct cgroup, sibling);
565 ret = mem_cgroup_from_cont(cgroup);
568 ret = mem_cgroup_get_next_node(root_mem->last_scanned_child,
572 root_mem->last_scanned_child = ret;
577 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
579 if (do_swap_account) {
580 if (res_counter_check_under_limit(&mem->res) &&
581 res_counter_check_under_limit(&mem->memsw))
584 if (res_counter_check_under_limit(&mem->res))
590 * Dance down the hierarchy if needed to reclaim memory. We remember the
591 * last child we reclaimed from, so that we don't end up penalizing
592 * one child extensively based on its position in the children list.
594 * root_mem is the original ancestor that we've been reclaim from.
596 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
597 gfp_t gfp_mask, bool noswap)
599 struct mem_cgroup *next_mem;
603 * Reclaim unconditionally and don't check for return value.
604 * We need to reclaim in the current group and down the tree.
605 * One might think about checking for children before reclaiming,
606 * but there might be left over accounting, even after children
609 ret = try_to_free_mem_cgroup_pages(root_mem, gfp_mask, noswap);
610 if (mem_cgroup_check_under_limit(root_mem))
612 if (!root_mem->use_hierarchy)
615 next_mem = mem_cgroup_get_first_node(root_mem);
617 while (next_mem != root_mem) {
618 if (next_mem->obsolete) {
619 mem_cgroup_put(next_mem);
621 next_mem = mem_cgroup_get_first_node(root_mem);
625 ret = try_to_free_mem_cgroup_pages(next_mem, gfp_mask, noswap);
626 if (mem_cgroup_check_under_limit(root_mem))
629 next_mem = mem_cgroup_get_next_node(next_mem, root_mem);
635 bool mem_cgroup_oom_called(struct task_struct *task)
638 struct mem_cgroup *mem;
639 struct mm_struct *mm;
645 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
646 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
652 * Unlike exported interface, "oom" parameter is added. if oom==true,
653 * oom-killer can be invoked.
655 static int __mem_cgroup_try_charge(struct mm_struct *mm,
656 gfp_t gfp_mask, struct mem_cgroup **memcg,
659 struct mem_cgroup *mem, *mem_over_limit;
660 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
661 struct res_counter *fail_res;
663 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
664 /* Don't account this! */
670 * We always charge the cgroup the mm_struct belongs to.
671 * The mm_struct's mem_cgroup changes on task migration if the
672 * thread group leader migrates. It's possible that mm is not
673 * set, if so charge the init_mm (happens for pagecache usage).
675 if (likely(!*memcg)) {
677 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
678 if (unlikely(!mem)) {
683 * For every charge from the cgroup, increment reference count
697 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
699 if (!do_swap_account)
701 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
705 /* mem+swap counter fails */
706 res_counter_uncharge(&mem->res, PAGE_SIZE);
708 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
711 /* mem counter fails */
712 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
715 if (!(gfp_mask & __GFP_WAIT))
718 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
722 * try_to_free_mem_cgroup_pages() might not give us a full
723 * picture of reclaim. Some pages are reclaimed and might be
724 * moved to swap cache or just unmapped from the cgroup.
725 * Check the limit again to see if the reclaim reduced the
726 * current usage of the cgroup before giving up
729 if (mem_cgroup_check_under_limit(mem_over_limit))
734 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
735 mem_over_limit->last_oom_jiffies = jiffies;
747 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
748 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
749 * @gfp_mask: gfp_mask for reclaim.
750 * @memcg: a pointer to memory cgroup which is charged against.
752 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
753 * memory cgroup from @mm is got and stored in *memcg.
755 * Returns 0 if success. -ENOMEM at failure.
756 * This call can invoke OOM-Killer.
759 int mem_cgroup_try_charge(struct mm_struct *mm,
760 gfp_t mask, struct mem_cgroup **memcg)
762 return __mem_cgroup_try_charge(mm, mask, memcg, true);
766 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
767 * USED state. If already USED, uncharge and return.
770 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
771 struct page_cgroup *pc,
772 enum charge_type ctype)
774 /* try_charge() can return NULL to *memcg, taking care of it. */
778 lock_page_cgroup(pc);
779 if (unlikely(PageCgroupUsed(pc))) {
780 unlock_page_cgroup(pc);
781 res_counter_uncharge(&mem->res, PAGE_SIZE);
783 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
787 pc->mem_cgroup = mem;
789 pc->flags = pcg_default_flags[ctype];
791 mem_cgroup_charge_statistics(mem, pc, true);
793 unlock_page_cgroup(pc);
797 * mem_cgroup_move_account - move account of the page
798 * @pc: page_cgroup of the page.
799 * @from: mem_cgroup which the page is moved from.
800 * @to: mem_cgroup which the page is moved to. @from != @to.
802 * The caller must confirm following.
803 * - page is not on LRU (isolate_page() is useful.)
805 * returns 0 at success,
806 * returns -EBUSY when lock is busy or "pc" is unstable.
808 * This function does "uncharge" from old cgroup but doesn't do "charge" to
809 * new cgroup. It should be done by a caller.
812 static int mem_cgroup_move_account(struct page_cgroup *pc,
813 struct mem_cgroup *from, struct mem_cgroup *to)
815 struct mem_cgroup_per_zone *from_mz, *to_mz;
819 VM_BUG_ON(from == to);
820 VM_BUG_ON(PageLRU(pc->page));
822 nid = page_cgroup_nid(pc);
823 zid = page_cgroup_zid(pc);
824 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
825 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
827 if (!trylock_page_cgroup(pc))
830 if (!PageCgroupUsed(pc))
833 if (pc->mem_cgroup != from)
837 res_counter_uncharge(&from->res, PAGE_SIZE);
838 mem_cgroup_charge_statistics(from, pc, false);
840 res_counter_uncharge(&from->memsw, PAGE_SIZE);
842 mem_cgroup_charge_statistics(to, pc, true);
846 unlock_page_cgroup(pc);
851 * move charges to its parent.
854 static int mem_cgroup_move_parent(struct page_cgroup *pc,
855 struct mem_cgroup *child,
858 struct page *page = pc->page;
859 struct cgroup *cg = child->css.cgroup;
860 struct cgroup *pcg = cg->parent;
861 struct mem_cgroup *parent;
869 parent = mem_cgroup_from_cont(pcg);
872 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
876 if (!get_page_unless_zero(page))
879 ret = isolate_lru_page(page);
884 ret = mem_cgroup_move_account(pc, child, parent);
886 /* drop extra refcnt by try_charge() (move_account increment one) */
887 css_put(&parent->css);
888 putback_lru_page(page);
893 /* uncharge if move fails */
895 res_counter_uncharge(&parent->res, PAGE_SIZE);
897 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
903 * Charge the memory controller for page usage.
905 * 0 if the charge was successful
906 * < 0 if the cgroup is over its limit
908 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
909 gfp_t gfp_mask, enum charge_type ctype,
910 struct mem_cgroup *memcg)
912 struct mem_cgroup *mem;
913 struct page_cgroup *pc;
916 pc = lookup_page_cgroup(page);
917 /* can happen at boot */
923 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
927 __mem_cgroup_commit_charge(mem, pc, ctype);
931 int mem_cgroup_newpage_charge(struct page *page,
932 struct mm_struct *mm, gfp_t gfp_mask)
934 if (mem_cgroup_disabled())
936 if (PageCompound(page))
939 * If already mapped, we don't have to account.
940 * If page cache, page->mapping has address_space.
941 * But page->mapping may have out-of-use anon_vma pointer,
942 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
945 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
949 return mem_cgroup_charge_common(page, mm, gfp_mask,
950 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
953 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
956 if (mem_cgroup_disabled())
958 if (PageCompound(page))
961 * Corner case handling. This is called from add_to_page_cache()
962 * in usual. But some FS (shmem) precharges this page before calling it
963 * and call add_to_page_cache() with GFP_NOWAIT.
965 * For GFP_NOWAIT case, the page may be pre-charged before calling
966 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
967 * charge twice. (It works but has to pay a bit larger cost.)
969 if (!(gfp_mask & __GFP_WAIT)) {
970 struct page_cgroup *pc;
973 pc = lookup_page_cgroup(page);
976 lock_page_cgroup(pc);
977 if (PageCgroupUsed(pc)) {
978 unlock_page_cgroup(pc);
981 unlock_page_cgroup(pc);
987 if (page_is_file_cache(page))
988 return mem_cgroup_charge_common(page, mm, gfp_mask,
989 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
991 return mem_cgroup_charge_common(page, mm, gfp_mask,
992 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
995 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
997 gfp_t mask, struct mem_cgroup **ptr)
999 struct mem_cgroup *mem;
1002 if (mem_cgroup_disabled())
1005 if (!do_swap_account)
1009 * A racing thread's fault, or swapoff, may have already updated
1010 * the pte, and even removed page from swap cache: return success
1011 * to go on to do_swap_page()'s pte_same() test, which should fail.
1013 if (!PageSwapCache(page))
1016 ent.val = page_private(page);
1018 mem = lookup_swap_cgroup(ent);
1019 if (!mem || mem->obsolete)
1022 return __mem_cgroup_try_charge(NULL, mask, ptr, true);
1026 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1031 int mem_cgroup_cache_charge_swapin(struct page *page,
1032 struct mm_struct *mm, gfp_t mask, bool locked)
1036 if (mem_cgroup_disabled())
1043 * If not locked, the page can be dropped from SwapCache until
1046 if (PageSwapCache(page)) {
1047 struct mem_cgroup *mem = NULL;
1050 ent.val = page_private(page);
1051 if (do_swap_account) {
1052 mem = lookup_swap_cgroup(ent);
1053 if (mem && mem->obsolete)
1058 ret = mem_cgroup_charge_common(page, mm, mask,
1059 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1061 if (!ret && do_swap_account) {
1062 /* avoid double counting */
1063 mem = swap_cgroup_record(ent, NULL);
1065 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1066 mem_cgroup_put(mem);
1072 /* add this page(page_cgroup) to the LRU we want. */
1073 mem_cgroup_lru_fixup(page);
1079 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1081 struct page_cgroup *pc;
1083 if (mem_cgroup_disabled())
1087 pc = lookup_page_cgroup(page);
1088 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1090 * Now swap is on-memory. This means this page may be
1091 * counted both as mem and swap....double count.
1092 * Fix it by uncharging from memsw. This SwapCache is stable
1093 * because we're still under lock_page().
1095 if (do_swap_account) {
1096 swp_entry_t ent = {.val = page_private(page)};
1097 struct mem_cgroup *memcg;
1098 memcg = swap_cgroup_record(ent, NULL);
1100 /* If memcg is obsolete, memcg can be != ptr */
1101 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1102 mem_cgroup_put(memcg);
1106 /* add this page(page_cgroup) to the LRU we want. */
1107 mem_cgroup_lru_fixup(page);
1110 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1112 if (mem_cgroup_disabled())
1116 res_counter_uncharge(&mem->res, PAGE_SIZE);
1117 if (do_swap_account)
1118 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1124 * uncharge if !page_mapped(page)
1126 static struct mem_cgroup *
1127 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1129 struct page_cgroup *pc;
1130 struct mem_cgroup *mem = NULL;
1131 struct mem_cgroup_per_zone *mz;
1133 if (mem_cgroup_disabled())
1136 if (PageSwapCache(page))
1140 * Check if our page_cgroup is valid
1142 pc = lookup_page_cgroup(page);
1143 if (unlikely(!pc || !PageCgroupUsed(pc)))
1146 lock_page_cgroup(pc);
1148 mem = pc->mem_cgroup;
1150 if (!PageCgroupUsed(pc))
1154 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1155 if (page_mapped(page))
1158 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1159 if (!PageAnon(page)) { /* Shared memory */
1160 if (page->mapping && !page_is_file_cache(page))
1162 } else if (page_mapped(page)) /* Anon */
1169 res_counter_uncharge(&mem->res, PAGE_SIZE);
1170 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1171 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1173 mem_cgroup_charge_statistics(mem, pc, false);
1174 ClearPageCgroupUsed(pc);
1176 mz = page_cgroup_zoneinfo(pc);
1177 unlock_page_cgroup(pc);
1179 /* at swapout, this memcg will be accessed to record to swap */
1180 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1186 unlock_page_cgroup(pc);
1190 void mem_cgroup_uncharge_page(struct page *page)
1193 if (page_mapped(page))
1195 if (page->mapping && !PageAnon(page))
1197 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1200 void mem_cgroup_uncharge_cache_page(struct page *page)
1202 VM_BUG_ON(page_mapped(page));
1203 VM_BUG_ON(page->mapping);
1204 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1208 * called from __delete_from_swap_cache() and drop "page" account.
1209 * memcg information is recorded to swap_cgroup of "ent"
1211 void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
1213 struct mem_cgroup *memcg;
1215 memcg = __mem_cgroup_uncharge_common(page,
1216 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1217 /* record memcg information */
1218 if (do_swap_account && memcg) {
1219 swap_cgroup_record(ent, memcg);
1220 mem_cgroup_get(memcg);
1223 css_put(&memcg->css);
1226 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1228 * called from swap_entry_free(). remove record in swap_cgroup and
1229 * uncharge "memsw" account.
1231 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1233 struct mem_cgroup *memcg;
1235 if (!do_swap_account)
1238 memcg = swap_cgroup_record(ent, NULL);
1240 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1241 mem_cgroup_put(memcg);
1247 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1250 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1252 struct page_cgroup *pc;
1253 struct mem_cgroup *mem = NULL;
1256 if (mem_cgroup_disabled())
1259 pc = lookup_page_cgroup(page);
1260 lock_page_cgroup(pc);
1261 if (PageCgroupUsed(pc)) {
1262 mem = pc->mem_cgroup;
1265 unlock_page_cgroup(pc);
1268 ret = mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem);
1275 /* remove redundant charge if migration failed*/
1276 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1277 struct page *oldpage, struct page *newpage)
1279 struct page *target, *unused;
1280 struct page_cgroup *pc;
1281 enum charge_type ctype;
1286 /* at migration success, oldpage->mapping is NULL. */
1287 if (oldpage->mapping) {
1295 if (PageAnon(target))
1296 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1297 else if (page_is_file_cache(target))
1298 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1300 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1302 /* unused page is not on radix-tree now. */
1304 __mem_cgroup_uncharge_common(unused, ctype);
1306 pc = lookup_page_cgroup(target);
1308 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1309 * So, double-counting is effectively avoided.
1311 __mem_cgroup_commit_charge(mem, pc, ctype);
1314 * Both of oldpage and newpage are still under lock_page().
1315 * Then, we don't have to care about race in radix-tree.
1316 * But we have to be careful that this page is unmapped or not.
1318 * There is a case for !page_mapped(). At the start of
1319 * migration, oldpage was mapped. But now, it's zapped.
1320 * But we know *target* page is not freed/reused under us.
1321 * mem_cgroup_uncharge_page() does all necessary checks.
1323 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1324 mem_cgroup_uncharge_page(target);
1328 * A call to try to shrink memory usage under specified resource controller.
1329 * This is typically used for page reclaiming for shmem for reducing side
1330 * effect of page allocation from shmem, which is used by some mem_cgroup.
1332 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1334 struct mem_cgroup *mem;
1336 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1338 if (mem_cgroup_disabled())
1344 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1345 if (unlikely(!mem)) {
1353 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
1354 progress += mem_cgroup_check_under_limit(mem);
1355 } while (!progress && --retry);
1363 static DEFINE_MUTEX(set_limit_mutex);
1365 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1366 unsigned long long val)
1369 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1374 while (retry_count) {
1375 if (signal_pending(current)) {
1380 * Rather than hide all in some function, I do this in
1381 * open coded manner. You see what this really does.
1382 * We have to guarantee mem->res.limit < mem->memsw.limit.
1384 mutex_lock(&set_limit_mutex);
1385 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1386 if (memswlimit < val) {
1388 mutex_unlock(&set_limit_mutex);
1391 ret = res_counter_set_limit(&memcg->res, val);
1392 mutex_unlock(&set_limit_mutex);
1397 progress = try_to_free_mem_cgroup_pages(memcg,
1399 if (!progress) retry_count--;
1404 int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1405 unsigned long long val)
1407 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1408 u64 memlimit, oldusage, curusage;
1411 if (!do_swap_account)
1414 while (retry_count) {
1415 if (signal_pending(current)) {
1420 * Rather than hide all in some function, I do this in
1421 * open coded manner. You see what this really does.
1422 * We have to guarantee mem->res.limit < mem->memsw.limit.
1424 mutex_lock(&set_limit_mutex);
1425 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1426 if (memlimit > val) {
1428 mutex_unlock(&set_limit_mutex);
1431 ret = res_counter_set_limit(&memcg->memsw, val);
1432 mutex_unlock(&set_limit_mutex);
1437 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1438 try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, true);
1439 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1440 if (curusage >= oldusage)
1447 * This routine traverse page_cgroup in given list and drop them all.
1448 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1450 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1451 int node, int zid, enum lru_list lru)
1454 struct mem_cgroup_per_zone *mz;
1455 struct page_cgroup *pc, *busy;
1456 unsigned long flags, loop;
1457 struct list_head *list;
1460 zone = &NODE_DATA(node)->node_zones[zid];
1461 mz = mem_cgroup_zoneinfo(mem, node, zid);
1462 list = &mz->lists[lru];
1464 loop = MEM_CGROUP_ZSTAT(mz, lru);
1465 /* give some margin against EBUSY etc...*/
1470 spin_lock_irqsave(&zone->lru_lock, flags);
1471 if (list_empty(list)) {
1472 spin_unlock_irqrestore(&zone->lru_lock, flags);
1475 pc = list_entry(list->prev, struct page_cgroup, lru);
1477 list_move(&pc->lru, list);
1479 spin_unlock_irqrestore(&zone->lru_lock, flags);
1482 spin_unlock_irqrestore(&zone->lru_lock, flags);
1484 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
1488 if (ret == -EBUSY || ret == -EINVAL) {
1489 /* found lock contention or "pc" is obsolete. */
1496 if (!ret && !list_empty(list))
1502 * make mem_cgroup's charge to be 0 if there is no task.
1503 * This enables deleting this mem_cgroup.
1505 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1508 int node, zid, shrink;
1509 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1510 struct cgroup *cgrp = mem->css.cgroup;
1515 /* should free all ? */
1519 while (mem->res.usage > 0) {
1521 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1524 if (signal_pending(current))
1526 /* This is for making all *used* pages to be on LRU. */
1527 lru_add_drain_all();
1529 for_each_node_state(node, N_POSSIBLE) {
1530 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1533 ret = mem_cgroup_force_empty_list(mem,
1542 /* it seems parent cgroup doesn't have enough mem */
1553 /* returns EBUSY if there is a task or if we come here twice. */
1554 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
1558 /* we call try-to-free pages for make this cgroup empty */
1559 lru_add_drain_all();
1560 /* try to free all pages in this cgroup */
1562 while (nr_retries && mem->res.usage > 0) {
1565 if (signal_pending(current)) {
1569 progress = try_to_free_mem_cgroup_pages(mem,
1573 /* maybe some writeback is necessary */
1574 congestion_wait(WRITE, HZ/10);
1579 /* try move_account...there may be some *locked* pages. */
1586 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1588 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1592 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1594 return mem_cgroup_from_cont(cont)->use_hierarchy;
1597 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1601 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1602 struct cgroup *parent = cont->parent;
1603 struct mem_cgroup *parent_mem = NULL;
1606 parent_mem = mem_cgroup_from_cont(parent);
1610 * If parent's use_hiearchy is set, we can't make any modifications
1611 * in the child subtrees. If it is unset, then the change can
1612 * occur, provided the current cgroup has no children.
1614 * For the root cgroup, parent_mem is NULL, we allow value to be
1615 * set if there are no children.
1617 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1618 (val == 1 || val == 0)) {
1619 if (list_empty(&cont->children))
1620 mem->use_hierarchy = val;
1630 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
1632 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1636 type = MEMFILE_TYPE(cft->private);
1637 name = MEMFILE_ATTR(cft->private);
1640 val = res_counter_read_u64(&mem->res, name);
1643 if (do_swap_account)
1644 val = res_counter_read_u64(&mem->memsw, name);
1653 * The user of this function is...
1656 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1659 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
1661 unsigned long long val;
1664 type = MEMFILE_TYPE(cft->private);
1665 name = MEMFILE_ATTR(cft->private);
1668 /* This function does all necessary parse...reuse it */
1669 ret = res_counter_memparse_write_strategy(buffer, &val);
1673 ret = mem_cgroup_resize_limit(memcg, val);
1675 ret = mem_cgroup_resize_memsw_limit(memcg, val);
1678 ret = -EINVAL; /* should be BUG() ? */
1684 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
1686 struct mem_cgroup *mem;
1689 mem = mem_cgroup_from_cont(cont);
1690 type = MEMFILE_TYPE(event);
1691 name = MEMFILE_ATTR(event);
1695 res_counter_reset_max(&mem->res);
1697 res_counter_reset_max(&mem->memsw);
1701 res_counter_reset_failcnt(&mem->res);
1703 res_counter_reset_failcnt(&mem->memsw);
1709 static const struct mem_cgroup_stat_desc {
1712 } mem_cgroup_stat_desc[] = {
1713 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1714 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
1715 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1716 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
1719 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1720 struct cgroup_map_cb *cb)
1722 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1723 struct mem_cgroup_stat *stat = &mem_cont->stat;
1726 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1729 val = mem_cgroup_read_stat(stat, i);
1730 val *= mem_cgroup_stat_desc[i].unit;
1731 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
1733 /* showing # of active pages */
1735 unsigned long active_anon, inactive_anon;
1736 unsigned long active_file, inactive_file;
1737 unsigned long unevictable;
1739 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1741 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1743 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1745 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1747 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1750 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1751 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1752 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1753 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
1754 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1761 static struct cftype mem_cgroup_files[] = {
1763 .name = "usage_in_bytes",
1764 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
1765 .read_u64 = mem_cgroup_read,
1768 .name = "max_usage_in_bytes",
1769 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
1770 .trigger = mem_cgroup_reset,
1771 .read_u64 = mem_cgroup_read,
1774 .name = "limit_in_bytes",
1775 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
1776 .write_string = mem_cgroup_write,
1777 .read_u64 = mem_cgroup_read,
1781 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
1782 .trigger = mem_cgroup_reset,
1783 .read_u64 = mem_cgroup_read,
1787 .read_map = mem_control_stat_show,
1790 .name = "force_empty",
1791 .trigger = mem_cgroup_force_empty_write,
1794 .name = "use_hierarchy",
1795 .write_u64 = mem_cgroup_hierarchy_write,
1796 .read_u64 = mem_cgroup_hierarchy_read,
1800 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1801 static struct cftype memsw_cgroup_files[] = {
1803 .name = "memsw.usage_in_bytes",
1804 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1805 .read_u64 = mem_cgroup_read,
1808 .name = "memsw.max_usage_in_bytes",
1809 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1810 .trigger = mem_cgroup_reset,
1811 .read_u64 = mem_cgroup_read,
1814 .name = "memsw.limit_in_bytes",
1815 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1816 .write_string = mem_cgroup_write,
1817 .read_u64 = mem_cgroup_read,
1820 .name = "memsw.failcnt",
1821 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1822 .trigger = mem_cgroup_reset,
1823 .read_u64 = mem_cgroup_read,
1827 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1829 if (!do_swap_account)
1831 return cgroup_add_files(cont, ss, memsw_cgroup_files,
1832 ARRAY_SIZE(memsw_cgroup_files));
1835 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1841 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1843 struct mem_cgroup_per_node *pn;
1844 struct mem_cgroup_per_zone *mz;
1846 int zone, tmp = node;
1848 * This routine is called against possible nodes.
1849 * But it's BUG to call kmalloc() against offline node.
1851 * TODO: this routine can waste much memory for nodes which will
1852 * never be onlined. It's better to use memory hotplug callback
1855 if (!node_state(node, N_NORMAL_MEMORY))
1857 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1861 mem->info.nodeinfo[node] = pn;
1862 memset(pn, 0, sizeof(*pn));
1864 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1865 mz = &pn->zoneinfo[zone];
1867 INIT_LIST_HEAD(&mz->lists[l]);
1872 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1874 kfree(mem->info.nodeinfo[node]);
1877 static int mem_cgroup_size(void)
1879 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1880 return sizeof(struct mem_cgroup) + cpustat_size;
1883 static struct mem_cgroup *mem_cgroup_alloc(void)
1885 struct mem_cgroup *mem;
1886 int size = mem_cgroup_size();
1888 if (size < PAGE_SIZE)
1889 mem = kmalloc(size, GFP_KERNEL);
1891 mem = vmalloc(size);
1894 memset(mem, 0, size);
1899 * At destroying mem_cgroup, references from swap_cgroup can remain.
1900 * (scanning all at force_empty is too costly...)
1902 * Instead of clearing all references at force_empty, we remember
1903 * the number of reference from swap_cgroup and free mem_cgroup when
1904 * it goes down to 0.
1906 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1907 * entry which points to this memcg will be ignore at swapin.
1909 * Removal of cgroup itself succeeds regardless of refs from swap.
1912 static void mem_cgroup_free(struct mem_cgroup *mem)
1916 if (atomic_read(&mem->refcnt) > 0)
1920 for_each_node_state(node, N_POSSIBLE)
1921 free_mem_cgroup_per_zone_info(mem, node);
1923 if (mem_cgroup_size() < PAGE_SIZE)
1929 static void mem_cgroup_get(struct mem_cgroup *mem)
1931 atomic_inc(&mem->refcnt);
1934 static void mem_cgroup_put(struct mem_cgroup *mem)
1936 if (atomic_dec_and_test(&mem->refcnt)) {
1939 mem_cgroup_free(mem);
1944 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1945 static void __init enable_swap_cgroup(void)
1947 if (!mem_cgroup_disabled() && really_do_swap_account)
1948 do_swap_account = 1;
1951 static void __init enable_swap_cgroup(void)
1956 static struct cgroup_subsys_state *
1957 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1959 struct mem_cgroup *mem, *parent;
1962 mem = mem_cgroup_alloc();
1964 return ERR_PTR(-ENOMEM);
1966 for_each_node_state(node, N_POSSIBLE)
1967 if (alloc_mem_cgroup_per_zone_info(mem, node))
1970 if (cont->parent == NULL) {
1971 enable_swap_cgroup();
1974 parent = mem_cgroup_from_cont(cont->parent);
1975 mem->use_hierarchy = parent->use_hierarchy;
1978 if (parent && parent->use_hierarchy) {
1979 res_counter_init(&mem->res, &parent->res);
1980 res_counter_init(&mem->memsw, &parent->memsw);
1982 res_counter_init(&mem->res, NULL);
1983 res_counter_init(&mem->memsw, NULL);
1986 mem->last_scanned_child = NULL;
1990 for_each_node_state(node, N_POSSIBLE)
1991 free_mem_cgroup_per_zone_info(mem, node);
1992 mem_cgroup_free(mem);
1993 return ERR_PTR(-ENOMEM);
1996 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1997 struct cgroup *cont)
1999 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2001 mem_cgroup_force_empty(mem, false);
2004 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2005 struct cgroup *cont)
2007 mem_cgroup_free(mem_cgroup_from_cont(cont));
2010 static int mem_cgroup_populate(struct cgroup_subsys *ss,
2011 struct cgroup *cont)
2015 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2016 ARRAY_SIZE(mem_cgroup_files));
2019 ret = register_memsw_files(cont, ss);
2023 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2024 struct cgroup *cont,
2025 struct cgroup *old_cont,
2026 struct task_struct *p)
2029 * FIXME: It's better to move charges of this process from old
2030 * memcg to new memcg. But it's just on TODO-List now.
2034 struct cgroup_subsys mem_cgroup_subsys = {
2036 .subsys_id = mem_cgroup_subsys_id,
2037 .create = mem_cgroup_create,
2038 .pre_destroy = mem_cgroup_pre_destroy,
2039 .destroy = mem_cgroup_destroy,
2040 .populate = mem_cgroup_populate,
2041 .attach = mem_cgroup_move_task,
2045 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2047 static int __init disable_swap_account(char *s)
2049 really_do_swap_account = 0;
2052 __setup("noswapaccount", disable_swap_account);