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/limits.h>
31 #include <linux/mutex.h>
32 #include <linux/rbtree.h>
33 #include <linux/slab.h>
34 #include <linux/swap.h>
35 #include <linux/spinlock.h>
37 #include <linux/seq_file.h>
38 #include <linux/vmalloc.h>
39 #include <linux/mm_inline.h>
40 #include <linux/page_cgroup.h>
41 #include <linux/cpu.h>
44 #include <asm/uaccess.h>
46 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
47 #define MEM_CGROUP_RECLAIM_RETRIES 5
48 struct mem_cgroup *root_mem_cgroup __read_mostly;
50 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
51 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
52 int do_swap_account __read_mostly;
53 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
55 #define do_swap_account (0)
58 #define SOFTLIMIT_EVENTS_THRESH (1000)
61 * Statistics for memory cgroup.
63 enum mem_cgroup_stat_index {
65 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
67 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
68 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
69 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
70 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
72 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
73 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
75 MEM_CGROUP_STAT_NSTATS,
78 struct mem_cgroup_stat_cpu {
79 s64 count[MEM_CGROUP_STAT_NSTATS];
80 } ____cacheline_aligned_in_smp;
82 struct mem_cgroup_stat {
83 struct mem_cgroup_stat_cpu cpustat[0];
87 __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
88 enum mem_cgroup_stat_index idx)
94 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
95 enum mem_cgroup_stat_index idx)
97 return stat->count[idx];
101 * For accounting under irq disable, no need for increment preempt count.
103 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
104 enum mem_cgroup_stat_index idx, int val)
106 stat->count[idx] += val;
109 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
110 enum mem_cgroup_stat_index idx)
114 for_each_possible_cpu(cpu)
115 ret += stat->cpustat[cpu].count[idx];
119 static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
123 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
124 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
129 * per-zone information in memory controller.
131 struct mem_cgroup_per_zone {
133 * spin_lock to protect the per cgroup LRU
135 struct list_head lists[NR_LRU_LISTS];
136 unsigned long count[NR_LRU_LISTS];
138 struct zone_reclaim_stat reclaim_stat;
139 struct rb_node tree_node; /* RB tree node */
140 unsigned long long usage_in_excess;/* Set to the value by which */
141 /* the soft limit is exceeded*/
143 struct mem_cgroup *mem; /* Back pointer, we cannot */
144 /* use container_of */
146 /* Macro for accessing counter */
147 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
149 struct mem_cgroup_per_node {
150 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
153 struct mem_cgroup_lru_info {
154 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
158 * Cgroups above their limits are maintained in a RB-Tree, independent of
159 * their hierarchy representation
162 struct mem_cgroup_tree_per_zone {
163 struct rb_root rb_root;
167 struct mem_cgroup_tree_per_node {
168 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
171 struct mem_cgroup_tree {
172 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
175 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
178 * The memory controller data structure. The memory controller controls both
179 * page cache and RSS per cgroup. We would eventually like to provide
180 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
181 * to help the administrator determine what knobs to tune.
183 * TODO: Add a water mark for the memory controller. Reclaim will begin when
184 * we hit the water mark. May be even add a low water mark, such that
185 * no reclaim occurs from a cgroup at it's low water mark, this is
186 * a feature that will be implemented much later in the future.
189 struct cgroup_subsys_state css;
191 * the counter to account for memory usage
193 struct res_counter res;
195 * the counter to account for mem+swap usage.
197 struct res_counter memsw;
199 * Per cgroup active and inactive list, similar to the
200 * per zone LRU lists.
202 struct mem_cgroup_lru_info info;
205 protect against reclaim related member.
207 spinlock_t reclaim_param_lock;
209 int prev_priority; /* for recording reclaim priority */
212 * While reclaiming in a hierarchy, we cache the last child we
215 int last_scanned_child;
217 * Should the accounting and control be hierarchical, per subtree?
220 unsigned long last_oom_jiffies;
223 unsigned int swappiness;
225 /* set when res.limit == memsw.limit */
226 bool memsw_is_minimum;
229 * statistics. This must be placed at the end of memcg.
231 struct mem_cgroup_stat stat;
235 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
236 * limit reclaim to prevent infinite loops, if they ever occur.
238 #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
239 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
242 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
243 MEM_CGROUP_CHARGE_TYPE_MAPPED,
244 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
245 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
246 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
247 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
251 /* only for here (for easy reading.) */
252 #define PCGF_CACHE (1UL << PCG_CACHE)
253 #define PCGF_USED (1UL << PCG_USED)
254 #define PCGF_LOCK (1UL << PCG_LOCK)
255 /* Not used, but added here for completeness */
256 #define PCGF_ACCT (1UL << PCG_ACCT)
258 /* for encoding cft->private value on file */
261 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
262 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
263 #define MEMFILE_ATTR(val) ((val) & 0xffff)
266 * Reclaim flags for mem_cgroup_hierarchical_reclaim
268 #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
269 #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
270 #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
271 #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
272 #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
273 #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
275 static void mem_cgroup_get(struct mem_cgroup *mem);
276 static void mem_cgroup_put(struct mem_cgroup *mem);
277 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
278 static void drain_all_stock_async(void);
280 static struct mem_cgroup_per_zone *
281 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
283 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
286 struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
291 static struct mem_cgroup_per_zone *
292 page_cgroup_zoneinfo(struct page_cgroup *pc)
294 struct mem_cgroup *mem = pc->mem_cgroup;
295 int nid = page_cgroup_nid(pc);
296 int zid = page_cgroup_zid(pc);
301 return mem_cgroup_zoneinfo(mem, nid, zid);
304 static struct mem_cgroup_tree_per_zone *
305 soft_limit_tree_node_zone(int nid, int zid)
307 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
310 static struct mem_cgroup_tree_per_zone *
311 soft_limit_tree_from_page(struct page *page)
313 int nid = page_to_nid(page);
314 int zid = page_zonenum(page);
316 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
320 __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
321 struct mem_cgroup_per_zone *mz,
322 struct mem_cgroup_tree_per_zone *mctz,
323 unsigned long long new_usage_in_excess)
325 struct rb_node **p = &mctz->rb_root.rb_node;
326 struct rb_node *parent = NULL;
327 struct mem_cgroup_per_zone *mz_node;
332 mz->usage_in_excess = new_usage_in_excess;
333 if (!mz->usage_in_excess)
337 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
339 if (mz->usage_in_excess < mz_node->usage_in_excess)
342 * We can't avoid mem cgroups that are over their soft
343 * limit by the same amount
345 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
348 rb_link_node(&mz->tree_node, parent, p);
349 rb_insert_color(&mz->tree_node, &mctz->rb_root);
354 __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
355 struct mem_cgroup_per_zone *mz,
356 struct mem_cgroup_tree_per_zone *mctz)
360 rb_erase(&mz->tree_node, &mctz->rb_root);
365 mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
366 struct mem_cgroup_per_zone *mz,
367 struct mem_cgroup_tree_per_zone *mctz)
369 spin_lock(&mctz->lock);
370 __mem_cgroup_remove_exceeded(mem, mz, mctz);
371 spin_unlock(&mctz->lock);
374 static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
379 struct mem_cgroup_stat_cpu *cpustat;
382 cpustat = &mem->stat.cpustat[cpu];
383 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
384 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
385 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
392 static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
394 unsigned long long excess;
395 struct mem_cgroup_per_zone *mz;
396 struct mem_cgroup_tree_per_zone *mctz;
397 int nid = page_to_nid(page);
398 int zid = page_zonenum(page);
399 mctz = soft_limit_tree_from_page(page);
402 * Necessary to update all ancestors when hierarchy is used.
403 * because their event counter is not touched.
405 for (; mem; mem = parent_mem_cgroup(mem)) {
406 mz = mem_cgroup_zoneinfo(mem, nid, zid);
407 excess = res_counter_soft_limit_excess(&mem->res);
409 * We have to update the tree if mz is on RB-tree or
410 * mem is over its softlimit.
412 if (excess || mz->on_tree) {
413 spin_lock(&mctz->lock);
414 /* if on-tree, remove it */
416 __mem_cgroup_remove_exceeded(mem, mz, mctz);
418 * Insert again. mz->usage_in_excess will be updated.
419 * If excess is 0, no tree ops.
421 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
422 spin_unlock(&mctz->lock);
427 static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
430 struct mem_cgroup_per_zone *mz;
431 struct mem_cgroup_tree_per_zone *mctz;
433 for_each_node_state(node, N_POSSIBLE) {
434 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
435 mz = mem_cgroup_zoneinfo(mem, node, zone);
436 mctz = soft_limit_tree_node_zone(node, zone);
437 mem_cgroup_remove_exceeded(mem, mz, mctz);
442 static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
444 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
447 static struct mem_cgroup_per_zone *
448 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
450 struct rb_node *rightmost = NULL;
451 struct mem_cgroup_per_zone *mz;
455 rightmost = rb_last(&mctz->rb_root);
457 goto done; /* Nothing to reclaim from */
459 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
461 * Remove the node now but someone else can add it back,
462 * we will to add it back at the end of reclaim to its correct
463 * position in the tree.
465 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
466 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
467 !css_tryget(&mz->mem->css))
473 static struct mem_cgroup_per_zone *
474 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
476 struct mem_cgroup_per_zone *mz;
478 spin_lock(&mctz->lock);
479 mz = __mem_cgroup_largest_soft_limit_node(mctz);
480 spin_unlock(&mctz->lock);
484 static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
487 int val = (charge) ? 1 : -1;
488 struct mem_cgroup_stat *stat = &mem->stat;
489 struct mem_cgroup_stat_cpu *cpustat;
492 cpustat = &stat->cpustat[cpu];
493 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
497 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
498 struct page_cgroup *pc,
501 int val = (charge) ? 1 : -1;
502 struct mem_cgroup_stat *stat = &mem->stat;
503 struct mem_cgroup_stat_cpu *cpustat;
506 cpustat = &stat->cpustat[cpu];
507 if (PageCgroupCache(pc))
508 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
510 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
513 __mem_cgroup_stat_add_safe(cpustat,
514 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
516 __mem_cgroup_stat_add_safe(cpustat,
517 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
518 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
522 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
526 struct mem_cgroup_per_zone *mz;
529 for_each_online_node(nid)
530 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
531 mz = mem_cgroup_zoneinfo(mem, nid, zid);
532 total += MEM_CGROUP_ZSTAT(mz, idx);
537 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
539 return container_of(cgroup_subsys_state(cont,
540 mem_cgroup_subsys_id), struct mem_cgroup,
544 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
547 * mm_update_next_owner() may clear mm->owner to NULL
548 * if it races with swapoff, page migration, etc.
549 * So this can be called with p == NULL.
554 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
555 struct mem_cgroup, css);
558 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
560 struct mem_cgroup *mem = NULL;
565 * Because we have no locks, mm->owner's may be being moved to other
566 * cgroup. We use css_tryget() here even if this looks
567 * pessimistic (rather than adding locks here).
571 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
574 } while (!css_tryget(&mem->css));
580 * Call callback function against all cgroup under hierarchy tree.
582 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
583 int (*func)(struct mem_cgroup *, void *))
585 int found, ret, nextid;
586 struct cgroup_subsys_state *css;
587 struct mem_cgroup *mem;
589 if (!root->use_hierarchy)
590 return (*func)(root, data);
598 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
600 if (css && css_tryget(css))
601 mem = container_of(css, struct mem_cgroup, css);
605 ret = (*func)(mem, data);
609 } while (!ret && css);
614 static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
616 return (mem == root_mem_cgroup);
620 * Following LRU functions are allowed to be used without PCG_LOCK.
621 * Operations are called by routine of global LRU independently from memcg.
622 * What we have to take care of here is validness of pc->mem_cgroup.
624 * Changes to pc->mem_cgroup happens when
627 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
628 * It is added to LRU before charge.
629 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
630 * When moving account, the page is not on LRU. It's isolated.
633 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
635 struct page_cgroup *pc;
636 struct mem_cgroup_per_zone *mz;
638 if (mem_cgroup_disabled())
640 pc = lookup_page_cgroup(page);
641 /* can happen while we handle swapcache. */
642 if (!TestClearPageCgroupAcctLRU(pc))
644 VM_BUG_ON(!pc->mem_cgroup);
646 * We don't check PCG_USED bit. It's cleared when the "page" is finally
647 * removed from global LRU.
649 mz = page_cgroup_zoneinfo(pc);
650 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
651 if (mem_cgroup_is_root(pc->mem_cgroup))
653 VM_BUG_ON(list_empty(&pc->lru));
654 list_del_init(&pc->lru);
658 void mem_cgroup_del_lru(struct page *page)
660 mem_cgroup_del_lru_list(page, page_lru(page));
663 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
665 struct mem_cgroup_per_zone *mz;
666 struct page_cgroup *pc;
668 if (mem_cgroup_disabled())
671 pc = lookup_page_cgroup(page);
673 * Used bit is set without atomic ops but after smp_wmb().
674 * For making pc->mem_cgroup visible, insert smp_rmb() here.
677 /* unused or root page is not rotated. */
678 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
680 mz = page_cgroup_zoneinfo(pc);
681 list_move(&pc->lru, &mz->lists[lru]);
684 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
686 struct page_cgroup *pc;
687 struct mem_cgroup_per_zone *mz;
689 if (mem_cgroup_disabled())
691 pc = lookup_page_cgroup(page);
692 VM_BUG_ON(PageCgroupAcctLRU(pc));
694 * Used bit is set without atomic ops but after smp_wmb().
695 * For making pc->mem_cgroup visible, insert smp_rmb() here.
698 if (!PageCgroupUsed(pc))
701 mz = page_cgroup_zoneinfo(pc);
702 MEM_CGROUP_ZSTAT(mz, lru) += 1;
703 SetPageCgroupAcctLRU(pc);
704 if (mem_cgroup_is_root(pc->mem_cgroup))
706 list_add(&pc->lru, &mz->lists[lru]);
710 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
711 * lru because the page may.be reused after it's fully uncharged (because of
712 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
713 * it again. This function is only used to charge SwapCache. It's done under
714 * lock_page and expected that zone->lru_lock is never held.
716 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
719 struct zone *zone = page_zone(page);
720 struct page_cgroup *pc = lookup_page_cgroup(page);
722 spin_lock_irqsave(&zone->lru_lock, flags);
724 * Forget old LRU when this page_cgroup is *not* used. This Used bit
725 * is guarded by lock_page() because the page is SwapCache.
727 if (!PageCgroupUsed(pc))
728 mem_cgroup_del_lru_list(page, page_lru(page));
729 spin_unlock_irqrestore(&zone->lru_lock, flags);
732 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
735 struct zone *zone = page_zone(page);
736 struct page_cgroup *pc = lookup_page_cgroup(page);
738 spin_lock_irqsave(&zone->lru_lock, flags);
739 /* link when the page is linked to LRU but page_cgroup isn't */
740 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
741 mem_cgroup_add_lru_list(page, page_lru(page));
742 spin_unlock_irqrestore(&zone->lru_lock, flags);
746 void mem_cgroup_move_lists(struct page *page,
747 enum lru_list from, enum lru_list to)
749 if (mem_cgroup_disabled())
751 mem_cgroup_del_lru_list(page, from);
752 mem_cgroup_add_lru_list(page, to);
755 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
758 struct mem_cgroup *curr = NULL;
762 curr = try_get_mem_cgroup_from_mm(task->mm);
768 * We should check use_hierarchy of "mem" not "curr". Because checking
769 * use_hierarchy of "curr" here make this function true if hierarchy is
770 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
771 * hierarchy(even if use_hierarchy is disabled in "mem").
773 if (mem->use_hierarchy)
774 ret = css_is_ancestor(&curr->css, &mem->css);
782 * prev_priority control...this will be used in memory reclaim path.
784 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
788 spin_lock(&mem->reclaim_param_lock);
789 prev_priority = mem->prev_priority;
790 spin_unlock(&mem->reclaim_param_lock);
792 return prev_priority;
795 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
797 spin_lock(&mem->reclaim_param_lock);
798 if (priority < mem->prev_priority)
799 mem->prev_priority = priority;
800 spin_unlock(&mem->reclaim_param_lock);
803 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
805 spin_lock(&mem->reclaim_param_lock);
806 mem->prev_priority = priority;
807 spin_unlock(&mem->reclaim_param_lock);
810 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
812 unsigned long active;
813 unsigned long inactive;
815 unsigned long inactive_ratio;
817 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
818 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
820 gb = (inactive + active) >> (30 - PAGE_SHIFT);
822 inactive_ratio = int_sqrt(10 * gb);
827 present_pages[0] = inactive;
828 present_pages[1] = active;
831 return inactive_ratio;
834 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
836 unsigned long active;
837 unsigned long inactive;
838 unsigned long present_pages[2];
839 unsigned long inactive_ratio;
841 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
843 inactive = present_pages[0];
844 active = present_pages[1];
846 if (inactive * inactive_ratio < active)
852 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
854 unsigned long active;
855 unsigned long inactive;
857 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
858 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
860 return (active > inactive);
863 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
867 int nid = zone->zone_pgdat->node_id;
868 int zid = zone_idx(zone);
869 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
871 return MEM_CGROUP_ZSTAT(mz, lru);
874 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
877 int nid = zone->zone_pgdat->node_id;
878 int zid = zone_idx(zone);
879 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
881 return &mz->reclaim_stat;
884 struct zone_reclaim_stat *
885 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
887 struct page_cgroup *pc;
888 struct mem_cgroup_per_zone *mz;
890 if (mem_cgroup_disabled())
893 pc = lookup_page_cgroup(page);
895 * Used bit is set without atomic ops but after smp_wmb().
896 * For making pc->mem_cgroup visible, insert smp_rmb() here.
899 if (!PageCgroupUsed(pc))
902 mz = page_cgroup_zoneinfo(pc);
906 return &mz->reclaim_stat;
909 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
910 struct list_head *dst,
911 unsigned long *scanned, int order,
912 int mode, struct zone *z,
913 struct mem_cgroup *mem_cont,
914 int active, int file)
916 unsigned long nr_taken = 0;
920 struct list_head *src;
921 struct page_cgroup *pc, *tmp;
922 int nid = z->zone_pgdat->node_id;
923 int zid = zone_idx(z);
924 struct mem_cgroup_per_zone *mz;
925 int lru = LRU_FILE * file + active;
929 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
930 src = &mz->lists[lru];
933 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
934 if (scan >= nr_to_scan)
938 if (unlikely(!PageCgroupUsed(pc)))
940 if (unlikely(!PageLRU(page)))
944 ret = __isolate_lru_page(page, mode, file);
947 list_move(&page->lru, dst);
948 mem_cgroup_del_lru(page);
952 /* we don't affect global LRU but rotate in our LRU */
953 mem_cgroup_rotate_lru_list(page, page_lru(page));
964 #define mem_cgroup_from_res_counter(counter, member) \
965 container_of(counter, struct mem_cgroup, member)
967 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
969 if (do_swap_account) {
970 if (res_counter_check_under_limit(&mem->res) &&
971 res_counter_check_under_limit(&mem->memsw))
974 if (res_counter_check_under_limit(&mem->res))
979 static unsigned int get_swappiness(struct mem_cgroup *memcg)
981 struct cgroup *cgrp = memcg->css.cgroup;
982 unsigned int swappiness;
985 if (cgrp->parent == NULL)
986 return vm_swappiness;
988 spin_lock(&memcg->reclaim_param_lock);
989 swappiness = memcg->swappiness;
990 spin_unlock(&memcg->reclaim_param_lock);
995 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1003 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
1004 * @memcg: The memory cgroup that went over limit
1005 * @p: Task that is going to be killed
1007 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1010 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1012 struct cgroup *task_cgrp;
1013 struct cgroup *mem_cgrp;
1015 * Need a buffer in BSS, can't rely on allocations. The code relies
1016 * on the assumption that OOM is serialized for memory controller.
1017 * If this assumption is broken, revisit this code.
1019 static char memcg_name[PATH_MAX];
1028 mem_cgrp = memcg->css.cgroup;
1029 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1031 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1034 * Unfortunately, we are unable to convert to a useful name
1035 * But we'll still print out the usage information
1042 printk(KERN_INFO "Task in %s killed", memcg_name);
1045 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1053 * Continues from above, so we don't need an KERN_ level
1055 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1058 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1059 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1060 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1061 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1062 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1064 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1065 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1066 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1070 * This function returns the number of memcg under hierarchy tree. Returns
1071 * 1(self count) if no children.
1073 static int mem_cgroup_count_children(struct mem_cgroup *mem)
1076 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1081 * Visit the first child (need not be the first child as per the ordering
1082 * of the cgroup list, since we track last_scanned_child) of @mem and use
1083 * that to reclaim free pages from.
1085 static struct mem_cgroup *
1086 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1088 struct mem_cgroup *ret = NULL;
1089 struct cgroup_subsys_state *css;
1092 if (!root_mem->use_hierarchy) {
1093 css_get(&root_mem->css);
1099 nextid = root_mem->last_scanned_child + 1;
1100 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1102 if (css && css_tryget(css))
1103 ret = container_of(css, struct mem_cgroup, css);
1106 /* Updates scanning parameter */
1107 spin_lock(&root_mem->reclaim_param_lock);
1109 /* this means start scan from ID:1 */
1110 root_mem->last_scanned_child = 0;
1112 root_mem->last_scanned_child = found;
1113 spin_unlock(&root_mem->reclaim_param_lock);
1120 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1121 * we reclaimed from, so that we don't end up penalizing one child extensively
1122 * based on its position in the children list.
1124 * root_mem is the original ancestor that we've been reclaim from.
1126 * We give up and return to the caller when we visit root_mem twice.
1127 * (other groups can be removed while we're walking....)
1129 * If shrink==true, for avoiding to free too much, this returns immedieately.
1131 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1134 unsigned long reclaim_options)
1136 struct mem_cgroup *victim;
1139 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1140 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
1141 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1142 unsigned long excess = mem_cgroup_get_excess(root_mem);
1144 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1145 if (root_mem->memsw_is_minimum)
1149 victim = mem_cgroup_select_victim(root_mem);
1150 if (victim == root_mem) {
1153 drain_all_stock_async();
1156 * If we have not been able to reclaim
1157 * anything, it might because there are
1158 * no reclaimable pages under this hierarchy
1160 if (!check_soft || !total) {
1161 css_put(&victim->css);
1165 * We want to do more targetted reclaim.
1166 * excess >> 2 is not to excessive so as to
1167 * reclaim too much, nor too less that we keep
1168 * coming back to reclaim from this cgroup
1170 if (total >= (excess >> 2) ||
1171 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1172 css_put(&victim->css);
1177 if (!mem_cgroup_local_usage(&victim->stat)) {
1178 /* this cgroup's local usage == 0 */
1179 css_put(&victim->css);
1182 /* we use swappiness of local cgroup */
1184 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1185 noswap, get_swappiness(victim), zone,
1186 zone->zone_pgdat->node_id);
1188 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1189 noswap, get_swappiness(victim));
1190 css_put(&victim->css);
1192 * At shrinking usage, we can't check we should stop here or
1193 * reclaim more. It's depends on callers. last_scanned_child
1194 * will work enough for keeping fairness under tree.
1200 if (res_counter_check_under_soft_limit(&root_mem->res))
1202 } else if (mem_cgroup_check_under_limit(root_mem))
1208 bool mem_cgroup_oom_called(struct task_struct *task)
1211 struct mem_cgroup *mem;
1212 struct mm_struct *mm;
1218 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1219 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1225 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1227 mem->last_oom_jiffies = jiffies;
1231 static void record_last_oom(struct mem_cgroup *mem)
1233 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1237 * Currently used to update mapped file statistics, but the routine can be
1238 * generalized to update other statistics as well.
1240 void mem_cgroup_update_file_mapped(struct page *page, int val)
1242 struct mem_cgroup *mem;
1243 struct mem_cgroup_stat *stat;
1244 struct mem_cgroup_stat_cpu *cpustat;
1246 struct page_cgroup *pc;
1248 pc = lookup_page_cgroup(page);
1252 lock_page_cgroup(pc);
1253 mem = pc->mem_cgroup;
1257 if (!PageCgroupUsed(pc))
1261 * Preemption is already disabled, we don't need get_cpu()
1263 cpu = smp_processor_id();
1265 cpustat = &stat->cpustat[cpu];
1267 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
1269 unlock_page_cgroup(pc);
1273 * size of first charge trial. "32" comes from vmscan.c's magic value.
1274 * TODO: maybe necessary to use big numbers in big irons.
1276 #define CHARGE_SIZE (32 * PAGE_SIZE)
1277 struct memcg_stock_pcp {
1278 struct mem_cgroup *cached; /* this never be root cgroup */
1280 struct work_struct work;
1282 static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1283 static atomic_t memcg_drain_count;
1286 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1287 * from local stock and true is returned. If the stock is 0 or charges from a
1288 * cgroup which is not current target, returns false. This stock will be
1291 static bool consume_stock(struct mem_cgroup *mem)
1293 struct memcg_stock_pcp *stock;
1296 stock = &get_cpu_var(memcg_stock);
1297 if (mem == stock->cached && stock->charge)
1298 stock->charge -= PAGE_SIZE;
1299 else /* need to call res_counter_charge */
1301 put_cpu_var(memcg_stock);
1306 * Returns stocks cached in percpu to res_counter and reset cached information.
1308 static void drain_stock(struct memcg_stock_pcp *stock)
1310 struct mem_cgroup *old = stock->cached;
1312 if (stock->charge) {
1313 res_counter_uncharge(&old->res, stock->charge);
1314 if (do_swap_account)
1315 res_counter_uncharge(&old->memsw, stock->charge);
1317 stock->cached = NULL;
1322 * This must be called under preempt disabled or must be called by
1323 * a thread which is pinned to local cpu.
1325 static void drain_local_stock(struct work_struct *dummy)
1327 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1332 * Cache charges(val) which is from res_counter, to local per_cpu area.
1333 * This will be consumed by consumt_stock() function, later.
1335 static void refill_stock(struct mem_cgroup *mem, int val)
1337 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1339 if (stock->cached != mem) { /* reset if necessary */
1341 stock->cached = mem;
1343 stock->charge += val;
1344 put_cpu_var(memcg_stock);
1348 * Tries to drain stocked charges in other cpus. This function is asynchronous
1349 * and just put a work per cpu for draining localy on each cpu. Caller can
1350 * expects some charges will be back to res_counter later but cannot wait for
1353 static void drain_all_stock_async(void)
1356 /* This function is for scheduling "drain" in asynchronous way.
1357 * The result of "drain" is not directly handled by callers. Then,
1358 * if someone is calling drain, we don't have to call drain more.
1359 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1360 * there is a race. We just do loose check here.
1362 if (atomic_read(&memcg_drain_count))
1364 /* Notify other cpus that system-wide "drain" is running */
1365 atomic_inc(&memcg_drain_count);
1367 for_each_online_cpu(cpu) {
1368 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1369 schedule_work_on(cpu, &stock->work);
1372 atomic_dec(&memcg_drain_count);
1373 /* We don't wait for flush_work */
1376 /* This is a synchronous drain interface. */
1377 static void drain_all_stock_sync(void)
1379 /* called when force_empty is called */
1380 atomic_inc(&memcg_drain_count);
1381 schedule_on_each_cpu(drain_local_stock);
1382 atomic_dec(&memcg_drain_count);
1385 static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1386 unsigned long action,
1389 int cpu = (unsigned long)hcpu;
1390 struct memcg_stock_pcp *stock;
1392 if (action != CPU_DEAD)
1394 stock = &per_cpu(memcg_stock, cpu);
1400 * Unlike exported interface, "oom" parameter is added. if oom==true,
1401 * oom-killer can be invoked.
1403 static int __mem_cgroup_try_charge(struct mm_struct *mm,
1404 gfp_t gfp_mask, struct mem_cgroup **memcg,
1405 bool oom, struct page *page)
1407 struct mem_cgroup *mem, *mem_over_limit;
1408 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1409 struct res_counter *fail_res;
1410 int csize = CHARGE_SIZE;
1412 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1413 /* Don't account this! */
1419 * We always charge the cgroup the mm_struct belongs to.
1420 * The mm_struct's mem_cgroup changes on task migration if the
1421 * thread group leader migrates. It's possible that mm is not
1422 * set, if so charge the init_mm (happens for pagecache usage).
1426 mem = try_get_mem_cgroup_from_mm(mm);
1434 VM_BUG_ON(css_is_removed(&mem->css));
1435 if (mem_cgroup_is_root(mem))
1440 unsigned long flags = 0;
1442 if (consume_stock(mem))
1445 ret = res_counter_charge(&mem->res, csize, &fail_res);
1447 if (!do_swap_account)
1449 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
1452 /* mem+swap counter fails */
1453 res_counter_uncharge(&mem->res, csize);
1454 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1455 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1458 /* mem counter fails */
1459 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1462 /* reduce request size and retry */
1463 if (csize > PAGE_SIZE) {
1467 if (!(gfp_mask & __GFP_WAIT))
1470 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1476 * try_to_free_mem_cgroup_pages() might not give us a full
1477 * picture of reclaim. Some pages are reclaimed and might be
1478 * moved to swap cache or just unmapped from the cgroup.
1479 * Check the limit again to see if the reclaim reduced the
1480 * current usage of the cgroup before giving up
1483 if (mem_cgroup_check_under_limit(mem_over_limit))
1486 if (!nr_retries--) {
1488 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1489 record_last_oom(mem_over_limit);
1494 if (csize > PAGE_SIZE)
1495 refill_stock(mem, csize - PAGE_SIZE);
1498 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1499 * if they exceeds softlimit.
1501 if (mem_cgroup_soft_limit_check(mem))
1502 mem_cgroup_update_tree(mem, page);
1511 * Somemtimes we have to undo a charge we got by try_charge().
1512 * This function is for that and do uncharge, put css's refcnt.
1513 * gotten by try_charge().
1515 static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1517 if (!mem_cgroup_is_root(mem)) {
1518 res_counter_uncharge(&mem->res, PAGE_SIZE);
1519 if (do_swap_account)
1520 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1526 * A helper function to get mem_cgroup from ID. must be called under
1527 * rcu_read_lock(). The caller must check css_is_removed() or some if
1528 * it's concern. (dropping refcnt from swap can be called against removed
1531 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1533 struct cgroup_subsys_state *css;
1535 /* ID 0 is unused ID */
1538 css = css_lookup(&mem_cgroup_subsys, id);
1541 return container_of(css, struct mem_cgroup, css);
1544 struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
1546 struct mem_cgroup *mem = NULL;
1547 struct page_cgroup *pc;
1551 VM_BUG_ON(!PageLocked(page));
1553 pc = lookup_page_cgroup(page);
1554 lock_page_cgroup(pc);
1555 if (PageCgroupUsed(pc)) {
1556 mem = pc->mem_cgroup;
1557 if (mem && !css_tryget(&mem->css))
1559 } else if (PageSwapCache(page)) {
1560 ent.val = page_private(page);
1561 id = lookup_swap_cgroup(ent);
1563 mem = mem_cgroup_lookup(id);
1564 if (mem && !css_tryget(&mem->css))
1568 unlock_page_cgroup(pc);
1573 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1574 * USED state. If already USED, uncharge and return.
1577 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1578 struct page_cgroup *pc,
1579 enum charge_type ctype)
1581 /* try_charge() can return NULL to *memcg, taking care of it. */
1585 lock_page_cgroup(pc);
1586 if (unlikely(PageCgroupUsed(pc))) {
1587 unlock_page_cgroup(pc);
1588 mem_cgroup_cancel_charge(mem);
1592 pc->mem_cgroup = mem;
1594 * We access a page_cgroup asynchronously without lock_page_cgroup().
1595 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1596 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1597 * before USED bit, we need memory barrier here.
1598 * See mem_cgroup_add_lru_list(), etc.
1602 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1603 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1604 SetPageCgroupCache(pc);
1605 SetPageCgroupUsed(pc);
1607 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1608 ClearPageCgroupCache(pc);
1609 SetPageCgroupUsed(pc);
1615 mem_cgroup_charge_statistics(mem, pc, true);
1617 unlock_page_cgroup(pc);
1621 * __mem_cgroup_move_account - move account of the page
1622 * @pc: page_cgroup of the page.
1623 * @from: mem_cgroup which the page is moved from.
1624 * @to: mem_cgroup which the page is moved to. @from != @to.
1626 * The caller must confirm following.
1627 * - page is not on LRU (isolate_page() is useful.)
1628 * - the pc is locked, used, and ->mem_cgroup points to @from.
1630 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1631 * new cgroup. It should be done by a caller.
1634 static void __mem_cgroup_move_account(struct page_cgroup *pc,
1635 struct mem_cgroup *from, struct mem_cgroup *to)
1639 struct mem_cgroup_stat *stat;
1640 struct mem_cgroup_stat_cpu *cpustat;
1642 VM_BUG_ON(from == to);
1643 VM_BUG_ON(PageLRU(pc->page));
1644 VM_BUG_ON(!PageCgroupLocked(pc));
1645 VM_BUG_ON(!PageCgroupUsed(pc));
1646 VM_BUG_ON(pc->mem_cgroup != from);
1648 if (!mem_cgroup_is_root(from))
1649 res_counter_uncharge(&from->res, PAGE_SIZE);
1650 mem_cgroup_charge_statistics(from, pc, false);
1653 if (page_mapped(page) && !PageAnon(page)) {
1654 cpu = smp_processor_id();
1655 /* Update mapped_file data for mem_cgroup "from" */
1657 cpustat = &stat->cpustat[cpu];
1658 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
1661 /* Update mapped_file data for mem_cgroup "to" */
1663 cpustat = &stat->cpustat[cpu];
1664 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
1668 if (do_swap_account && !mem_cgroup_is_root(from))
1669 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1670 css_put(&from->css);
1673 pc->mem_cgroup = to;
1674 mem_cgroup_charge_statistics(to, pc, true);
1676 * We charges against "to" which may not have any tasks. Then, "to"
1677 * can be under rmdir(). But in current implementation, caller of
1678 * this function is just force_empty() and it's garanteed that
1679 * "to" is never removed. So, we don't check rmdir status here.
1684 * check whether the @pc is valid for moving account and call
1685 * __mem_cgroup_move_account()
1687 static int mem_cgroup_move_account(struct page_cgroup *pc,
1688 struct mem_cgroup *from, struct mem_cgroup *to)
1691 lock_page_cgroup(pc);
1692 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
1693 __mem_cgroup_move_account(pc, from, to);
1696 unlock_page_cgroup(pc);
1701 * move charges to its parent.
1704 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1705 struct mem_cgroup *child,
1708 struct page *page = pc->page;
1709 struct cgroup *cg = child->css.cgroup;
1710 struct cgroup *pcg = cg->parent;
1711 struct mem_cgroup *parent;
1719 if (!get_page_unless_zero(page))
1721 if (isolate_lru_page(page))
1724 parent = mem_cgroup_from_cont(pcg);
1725 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
1729 ret = mem_cgroup_move_account(pc, child, parent);
1731 css_put(&parent->css); /* drop extra refcnt by try_charge() */
1733 mem_cgroup_cancel_charge(parent); /* does css_put */
1735 putback_lru_page(page);
1743 * Charge the memory controller for page usage.
1745 * 0 if the charge was successful
1746 * < 0 if the cgroup is over its limit
1748 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1749 gfp_t gfp_mask, enum charge_type ctype,
1750 struct mem_cgroup *memcg)
1752 struct mem_cgroup *mem;
1753 struct page_cgroup *pc;
1756 pc = lookup_page_cgroup(page);
1757 /* can happen at boot */
1763 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
1767 __mem_cgroup_commit_charge(mem, pc, ctype);
1771 int mem_cgroup_newpage_charge(struct page *page,
1772 struct mm_struct *mm, gfp_t gfp_mask)
1774 if (mem_cgroup_disabled())
1776 if (PageCompound(page))
1779 * If already mapped, we don't have to account.
1780 * If page cache, page->mapping has address_space.
1781 * But page->mapping may have out-of-use anon_vma pointer,
1782 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1785 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1789 return mem_cgroup_charge_common(page, mm, gfp_mask,
1790 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1794 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1795 enum charge_type ctype);
1797 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1800 struct mem_cgroup *mem = NULL;
1803 if (mem_cgroup_disabled())
1805 if (PageCompound(page))
1808 * Corner case handling. This is called from add_to_page_cache()
1809 * in usual. But some FS (shmem) precharges this page before calling it
1810 * and call add_to_page_cache() with GFP_NOWAIT.
1812 * For GFP_NOWAIT case, the page may be pre-charged before calling
1813 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1814 * charge twice. (It works but has to pay a bit larger cost.)
1815 * And when the page is SwapCache, it should take swap information
1816 * into account. This is under lock_page() now.
1818 if (!(gfp_mask & __GFP_WAIT)) {
1819 struct page_cgroup *pc;
1822 pc = lookup_page_cgroup(page);
1825 lock_page_cgroup(pc);
1826 if (PageCgroupUsed(pc)) {
1827 unlock_page_cgroup(pc);
1830 unlock_page_cgroup(pc);
1833 if (unlikely(!mm && !mem))
1836 if (page_is_file_cache(page))
1837 return mem_cgroup_charge_common(page, mm, gfp_mask,
1838 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1841 if (PageSwapCache(page)) {
1842 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1844 __mem_cgroup_commit_charge_swapin(page, mem,
1845 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1847 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1848 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1854 * While swap-in, try_charge -> commit or cancel, the page is locked.
1855 * And when try_charge() successfully returns, one refcnt to memcg without
1856 * struct page_cgroup is acquired. This refcnt will be consumed by
1857 * "commit()" or removed by "cancel()"
1859 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1861 gfp_t mask, struct mem_cgroup **ptr)
1863 struct mem_cgroup *mem;
1866 if (mem_cgroup_disabled())
1869 if (!do_swap_account)
1872 * A racing thread's fault, or swapoff, may have already updated
1873 * the pte, and even removed page from swap cache: in those cases
1874 * do_swap_page()'s pte_same() test will fail; but there's also a
1875 * KSM case which does need to charge the page.
1877 if (!PageSwapCache(page))
1879 mem = try_get_mem_cgroup_from_page(page);
1883 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
1884 /* drop extra refcnt from tryget */
1890 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
1894 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1895 enum charge_type ctype)
1897 struct page_cgroup *pc;
1899 if (mem_cgroup_disabled())
1903 cgroup_exclude_rmdir(&ptr->css);
1904 pc = lookup_page_cgroup(page);
1905 mem_cgroup_lru_del_before_commit_swapcache(page);
1906 __mem_cgroup_commit_charge(ptr, pc, ctype);
1907 mem_cgroup_lru_add_after_commit_swapcache(page);
1909 * Now swap is on-memory. This means this page may be
1910 * counted both as mem and swap....double count.
1911 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1912 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1913 * may call delete_from_swap_cache() before reach here.
1915 if (do_swap_account && PageSwapCache(page)) {
1916 swp_entry_t ent = {.val = page_private(page)};
1918 struct mem_cgroup *memcg;
1920 id = swap_cgroup_record(ent, 0);
1922 memcg = mem_cgroup_lookup(id);
1925 * This recorded memcg can be obsolete one. So, avoid
1926 * calling css_tryget
1928 if (!mem_cgroup_is_root(memcg))
1929 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1930 mem_cgroup_swap_statistics(memcg, false);
1931 mem_cgroup_put(memcg);
1936 * At swapin, we may charge account against cgroup which has no tasks.
1937 * So, rmdir()->pre_destroy() can be called while we do this charge.
1938 * In that case, we need to call pre_destroy() again. check it here.
1940 cgroup_release_and_wakeup_rmdir(&ptr->css);
1943 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1945 __mem_cgroup_commit_charge_swapin(page, ptr,
1946 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1949 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1951 if (mem_cgroup_disabled())
1955 mem_cgroup_cancel_charge(mem);
1959 __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
1961 struct memcg_batch_info *batch = NULL;
1962 bool uncharge_memsw = true;
1963 /* If swapout, usage of swap doesn't decrease */
1964 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1965 uncharge_memsw = false;
1967 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
1968 * In those cases, all pages freed continously can be expected to be in
1969 * the same cgroup and we have chance to coalesce uncharges.
1970 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
1971 * because we want to do uncharge as soon as possible.
1973 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
1974 goto direct_uncharge;
1976 batch = ¤t->memcg_batch;
1978 * In usual, we do css_get() when we remember memcg pointer.
1979 * But in this case, we keep res->usage until end of a series of
1980 * uncharges. Then, it's ok to ignore memcg's refcnt.
1985 * In typical case, batch->memcg == mem. This means we can
1986 * merge a series of uncharges to an uncharge of res_counter.
1987 * If not, we uncharge res_counter ony by one.
1989 if (batch->memcg != mem)
1990 goto direct_uncharge;
1991 /* remember freed charge and uncharge it later */
1992 batch->bytes += PAGE_SIZE;
1994 batch->memsw_bytes += PAGE_SIZE;
1997 res_counter_uncharge(&mem->res, PAGE_SIZE);
1999 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2004 * uncharge if !page_mapped(page)
2006 static struct mem_cgroup *
2007 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
2009 struct page_cgroup *pc;
2010 struct mem_cgroup *mem = NULL;
2011 struct mem_cgroup_per_zone *mz;
2013 if (mem_cgroup_disabled())
2016 if (PageSwapCache(page))
2020 * Check if our page_cgroup is valid
2022 pc = lookup_page_cgroup(page);
2023 if (unlikely(!pc || !PageCgroupUsed(pc)))
2026 lock_page_cgroup(pc);
2028 mem = pc->mem_cgroup;
2030 if (!PageCgroupUsed(pc))
2034 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2035 case MEM_CGROUP_CHARGE_TYPE_DROP:
2036 if (page_mapped(page))
2039 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2040 if (!PageAnon(page)) { /* Shared memory */
2041 if (page->mapping && !page_is_file_cache(page))
2043 } else if (page_mapped(page)) /* Anon */
2050 if (!mem_cgroup_is_root(mem))
2051 __do_uncharge(mem, ctype);
2052 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2053 mem_cgroup_swap_statistics(mem, true);
2054 mem_cgroup_charge_statistics(mem, pc, false);
2056 ClearPageCgroupUsed(pc);
2058 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2059 * freed from LRU. This is safe because uncharged page is expected not
2060 * to be reused (freed soon). Exception is SwapCache, it's handled by
2061 * special functions.
2064 mz = page_cgroup_zoneinfo(pc);
2065 unlock_page_cgroup(pc);
2067 if (mem_cgroup_soft_limit_check(mem))
2068 mem_cgroup_update_tree(mem, page);
2069 /* at swapout, this memcg will be accessed to record to swap */
2070 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2076 unlock_page_cgroup(pc);
2080 void mem_cgroup_uncharge_page(struct page *page)
2083 if (page_mapped(page))
2085 if (page->mapping && !PageAnon(page))
2087 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2090 void mem_cgroup_uncharge_cache_page(struct page *page)
2092 VM_BUG_ON(page_mapped(page));
2093 VM_BUG_ON(page->mapping);
2094 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2098 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2099 * In that cases, pages are freed continuously and we can expect pages
2100 * are in the same memcg. All these calls itself limits the number of
2101 * pages freed at once, then uncharge_start/end() is called properly.
2102 * This may be called prural(2) times in a context,
2105 void mem_cgroup_uncharge_start(void)
2107 current->memcg_batch.do_batch++;
2108 /* We can do nest. */
2109 if (current->memcg_batch.do_batch == 1) {
2110 current->memcg_batch.memcg = NULL;
2111 current->memcg_batch.bytes = 0;
2112 current->memcg_batch.memsw_bytes = 0;
2116 void mem_cgroup_uncharge_end(void)
2118 struct memcg_batch_info *batch = ¤t->memcg_batch;
2120 if (!batch->do_batch)
2124 if (batch->do_batch) /* If stacked, do nothing. */
2130 * This "batch->memcg" is valid without any css_get/put etc...
2131 * bacause we hide charges behind us.
2134 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2135 if (batch->memsw_bytes)
2136 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2137 /* forget this pointer (for sanity check) */
2138 batch->memcg = NULL;
2143 * called after __delete_from_swap_cache() and drop "page" account.
2144 * memcg information is recorded to swap_cgroup of "ent"
2147 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
2149 struct mem_cgroup *memcg;
2150 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
2152 if (!swapout) /* this was a swap cache but the swap is unused ! */
2153 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2155 memcg = __mem_cgroup_uncharge_common(page, ctype);
2157 /* record memcg information */
2158 if (do_swap_account && swapout && memcg) {
2159 swap_cgroup_record(ent, css_id(&memcg->css));
2160 mem_cgroup_get(memcg);
2162 if (swapout && memcg)
2163 css_put(&memcg->css);
2167 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2169 * called from swap_entry_free(). remove record in swap_cgroup and
2170 * uncharge "memsw" account.
2172 void mem_cgroup_uncharge_swap(swp_entry_t ent)
2174 struct mem_cgroup *memcg;
2177 if (!do_swap_account)
2180 id = swap_cgroup_record(ent, 0);
2182 memcg = mem_cgroup_lookup(id);
2185 * We uncharge this because swap is freed.
2186 * This memcg can be obsolete one. We avoid calling css_tryget
2188 if (!mem_cgroup_is_root(memcg))
2189 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
2190 mem_cgroup_swap_statistics(memcg, false);
2191 mem_cgroup_put(memcg);
2198 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2201 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
2203 struct page_cgroup *pc;
2204 struct mem_cgroup *mem = NULL;
2207 if (mem_cgroup_disabled())
2210 pc = lookup_page_cgroup(page);
2211 lock_page_cgroup(pc);
2212 if (PageCgroupUsed(pc)) {
2213 mem = pc->mem_cgroup;
2216 unlock_page_cgroup(pc);
2219 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2227 /* remove redundant charge if migration failed*/
2228 void mem_cgroup_end_migration(struct mem_cgroup *mem,
2229 struct page *oldpage, struct page *newpage)
2231 struct page *target, *unused;
2232 struct page_cgroup *pc;
2233 enum charge_type ctype;
2237 cgroup_exclude_rmdir(&mem->css);
2238 /* at migration success, oldpage->mapping is NULL. */
2239 if (oldpage->mapping) {
2247 if (PageAnon(target))
2248 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2249 else if (page_is_file_cache(target))
2250 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2252 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2254 /* unused page is not on radix-tree now. */
2256 __mem_cgroup_uncharge_common(unused, ctype);
2258 pc = lookup_page_cgroup(target);
2260 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2261 * So, double-counting is effectively avoided.
2263 __mem_cgroup_commit_charge(mem, pc, ctype);
2266 * Both of oldpage and newpage are still under lock_page().
2267 * Then, we don't have to care about race in radix-tree.
2268 * But we have to be careful that this page is unmapped or not.
2270 * There is a case for !page_mapped(). At the start of
2271 * migration, oldpage was mapped. But now, it's zapped.
2272 * But we know *target* page is not freed/reused under us.
2273 * mem_cgroup_uncharge_page() does all necessary checks.
2275 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2276 mem_cgroup_uncharge_page(target);
2278 * At migration, we may charge account against cgroup which has no tasks
2279 * So, rmdir()->pre_destroy() can be called while we do this charge.
2280 * In that case, we need to call pre_destroy() again. check it here.
2282 cgroup_release_and_wakeup_rmdir(&mem->css);
2286 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2287 * Calling hierarchical_reclaim is not enough because we should update
2288 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2289 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2290 * not from the memcg which this page would be charged to.
2291 * try_charge_swapin does all of these works properly.
2293 int mem_cgroup_shmem_charge_fallback(struct page *page,
2294 struct mm_struct *mm,
2297 struct mem_cgroup *mem = NULL;
2300 if (mem_cgroup_disabled())
2303 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2305 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
2310 static DEFINE_MUTEX(set_limit_mutex);
2312 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
2313 unsigned long long val)
2318 int children = mem_cgroup_count_children(memcg);
2319 u64 curusage, oldusage;
2322 * For keeping hierarchical_reclaim simple, how long we should retry
2323 * is depends on callers. We set our retry-count to be function
2324 * of # of children which we should visit in this loop.
2326 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2328 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2330 while (retry_count) {
2331 if (signal_pending(current)) {
2336 * Rather than hide all in some function, I do this in
2337 * open coded manner. You see what this really does.
2338 * We have to guarantee mem->res.limit < mem->memsw.limit.
2340 mutex_lock(&set_limit_mutex);
2341 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2342 if (memswlimit < val) {
2344 mutex_unlock(&set_limit_mutex);
2347 ret = res_counter_set_limit(&memcg->res, val);
2349 if (memswlimit == val)
2350 memcg->memsw_is_minimum = true;
2352 memcg->memsw_is_minimum = false;
2354 mutex_unlock(&set_limit_mutex);
2359 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2360 MEM_CGROUP_RECLAIM_SHRINK);
2361 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2362 /* Usage is reduced ? */
2363 if (curusage >= oldusage)
2366 oldusage = curusage;
2372 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2373 unsigned long long val)
2376 u64 memlimit, oldusage, curusage;
2377 int children = mem_cgroup_count_children(memcg);
2380 /* see mem_cgroup_resize_res_limit */
2381 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2382 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2383 while (retry_count) {
2384 if (signal_pending(current)) {
2389 * Rather than hide all in some function, I do this in
2390 * open coded manner. You see what this really does.
2391 * We have to guarantee mem->res.limit < mem->memsw.limit.
2393 mutex_lock(&set_limit_mutex);
2394 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2395 if (memlimit > val) {
2397 mutex_unlock(&set_limit_mutex);
2400 ret = res_counter_set_limit(&memcg->memsw, val);
2402 if (memlimit == val)
2403 memcg->memsw_is_minimum = true;
2405 memcg->memsw_is_minimum = false;
2407 mutex_unlock(&set_limit_mutex);
2412 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2413 MEM_CGROUP_RECLAIM_NOSWAP |
2414 MEM_CGROUP_RECLAIM_SHRINK);
2415 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2416 /* Usage is reduced ? */
2417 if (curusage >= oldusage)
2420 oldusage = curusage;
2425 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2426 gfp_t gfp_mask, int nid,
2429 unsigned long nr_reclaimed = 0;
2430 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2431 unsigned long reclaimed;
2433 struct mem_cgroup_tree_per_zone *mctz;
2434 unsigned long long excess;
2439 mctz = soft_limit_tree_node_zone(nid, zid);
2441 * This loop can run a while, specially if mem_cgroup's continuously
2442 * keep exceeding their soft limit and putting the system under
2449 mz = mem_cgroup_largest_soft_limit_node(mctz);
2453 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2455 MEM_CGROUP_RECLAIM_SOFT);
2456 nr_reclaimed += reclaimed;
2457 spin_lock(&mctz->lock);
2460 * If we failed to reclaim anything from this memory cgroup
2461 * it is time to move on to the next cgroup
2467 * Loop until we find yet another one.
2469 * By the time we get the soft_limit lock
2470 * again, someone might have aded the
2471 * group back on the RB tree. Iterate to
2472 * make sure we get a different mem.
2473 * mem_cgroup_largest_soft_limit_node returns
2474 * NULL if no other cgroup is present on
2478 __mem_cgroup_largest_soft_limit_node(mctz);
2479 if (next_mz == mz) {
2480 css_put(&next_mz->mem->css);
2482 } else /* next_mz == NULL or other memcg */
2486 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
2487 excess = res_counter_soft_limit_excess(&mz->mem->res);
2489 * One school of thought says that we should not add
2490 * back the node to the tree if reclaim returns 0.
2491 * But our reclaim could return 0, simply because due
2492 * to priority we are exposing a smaller subset of
2493 * memory to reclaim from. Consider this as a longer
2496 /* If excess == 0, no tree ops */
2497 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
2498 spin_unlock(&mctz->lock);
2499 css_put(&mz->mem->css);
2502 * Could not reclaim anything and there are no more
2503 * mem cgroups to try or we seem to be looping without
2504 * reclaiming anything.
2506 if (!nr_reclaimed &&
2508 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2510 } while (!nr_reclaimed);
2512 css_put(&next_mz->mem->css);
2513 return nr_reclaimed;
2517 * This routine traverse page_cgroup in given list and drop them all.
2518 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2520 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
2521 int node, int zid, enum lru_list lru)
2524 struct mem_cgroup_per_zone *mz;
2525 struct page_cgroup *pc, *busy;
2526 unsigned long flags, loop;
2527 struct list_head *list;
2530 zone = &NODE_DATA(node)->node_zones[zid];
2531 mz = mem_cgroup_zoneinfo(mem, node, zid);
2532 list = &mz->lists[lru];
2534 loop = MEM_CGROUP_ZSTAT(mz, lru);
2535 /* give some margin against EBUSY etc...*/
2540 spin_lock_irqsave(&zone->lru_lock, flags);
2541 if (list_empty(list)) {
2542 spin_unlock_irqrestore(&zone->lru_lock, flags);
2545 pc = list_entry(list->prev, struct page_cgroup, lru);
2547 list_move(&pc->lru, list);
2549 spin_unlock_irqrestore(&zone->lru_lock, flags);
2552 spin_unlock_irqrestore(&zone->lru_lock, flags);
2554 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
2558 if (ret == -EBUSY || ret == -EINVAL) {
2559 /* found lock contention or "pc" is obsolete. */
2566 if (!ret && !list_empty(list))
2572 * make mem_cgroup's charge to be 0 if there is no task.
2573 * This enables deleting this mem_cgroup.
2575 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
2578 int node, zid, shrink;
2579 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
2580 struct cgroup *cgrp = mem->css.cgroup;
2585 /* should free all ? */
2591 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
2594 if (signal_pending(current))
2596 /* This is for making all *used* pages to be on LRU. */
2597 lru_add_drain_all();
2598 drain_all_stock_sync();
2600 for_each_node_state(node, N_HIGH_MEMORY) {
2601 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
2604 ret = mem_cgroup_force_empty_list(mem,
2613 /* it seems parent cgroup doesn't have enough mem */
2617 /* "ret" should also be checked to ensure all lists are empty. */
2618 } while (mem->res.usage > 0 || ret);
2624 /* returns EBUSY if there is a task or if we come here twice. */
2625 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
2629 /* we call try-to-free pages for make this cgroup empty */
2630 lru_add_drain_all();
2631 /* try to free all pages in this cgroup */
2633 while (nr_retries && mem->res.usage > 0) {
2636 if (signal_pending(current)) {
2640 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2641 false, get_swappiness(mem));
2644 /* maybe some writeback is necessary */
2645 congestion_wait(BLK_RW_ASYNC, HZ/10);
2650 /* try move_account...there may be some *locked* pages. */
2654 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2656 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2660 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2662 return mem_cgroup_from_cont(cont)->use_hierarchy;
2665 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2669 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2670 struct cgroup *parent = cont->parent;
2671 struct mem_cgroup *parent_mem = NULL;
2674 parent_mem = mem_cgroup_from_cont(parent);
2678 * If parent's use_hierarchy is set, we can't make any modifications
2679 * in the child subtrees. If it is unset, then the change can
2680 * occur, provided the current cgroup has no children.
2682 * For the root cgroup, parent_mem is NULL, we allow value to be
2683 * set if there are no children.
2685 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2686 (val == 1 || val == 0)) {
2687 if (list_empty(&cont->children))
2688 mem->use_hierarchy = val;
2698 struct mem_cgroup_idx_data {
2700 enum mem_cgroup_stat_index idx;
2704 mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2706 struct mem_cgroup_idx_data *d = data;
2707 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2712 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2713 enum mem_cgroup_stat_index idx, s64 *val)
2715 struct mem_cgroup_idx_data d;
2718 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2722 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2724 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2728 type = MEMFILE_TYPE(cft->private);
2729 name = MEMFILE_ATTR(cft->private);
2732 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2733 mem_cgroup_get_recursive_idx_stat(mem,
2734 MEM_CGROUP_STAT_CACHE, &idx_val);
2736 mem_cgroup_get_recursive_idx_stat(mem,
2737 MEM_CGROUP_STAT_RSS, &idx_val);
2741 val = res_counter_read_u64(&mem->res, name);
2744 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2745 mem_cgroup_get_recursive_idx_stat(mem,
2746 MEM_CGROUP_STAT_CACHE, &idx_val);
2748 mem_cgroup_get_recursive_idx_stat(mem,
2749 MEM_CGROUP_STAT_RSS, &idx_val);
2751 mem_cgroup_get_recursive_idx_stat(mem,
2752 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2756 val = res_counter_read_u64(&mem->memsw, name);
2765 * The user of this function is...
2768 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2771 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2773 unsigned long long val;
2776 type = MEMFILE_TYPE(cft->private);
2777 name = MEMFILE_ATTR(cft->private);
2780 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2784 /* This function does all necessary parse...reuse it */
2785 ret = res_counter_memparse_write_strategy(buffer, &val);
2789 ret = mem_cgroup_resize_limit(memcg, val);
2791 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2793 case RES_SOFT_LIMIT:
2794 ret = res_counter_memparse_write_strategy(buffer, &val);
2798 * For memsw, soft limits are hard to implement in terms
2799 * of semantics, for now, we support soft limits for
2800 * control without swap
2803 ret = res_counter_set_soft_limit(&memcg->res, val);
2808 ret = -EINVAL; /* should be BUG() ? */
2814 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2815 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2817 struct cgroup *cgroup;
2818 unsigned long long min_limit, min_memsw_limit, tmp;
2820 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2821 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2822 cgroup = memcg->css.cgroup;
2823 if (!memcg->use_hierarchy)
2826 while (cgroup->parent) {
2827 cgroup = cgroup->parent;
2828 memcg = mem_cgroup_from_cont(cgroup);
2829 if (!memcg->use_hierarchy)
2831 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2832 min_limit = min(min_limit, tmp);
2833 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2834 min_memsw_limit = min(min_memsw_limit, tmp);
2837 *mem_limit = min_limit;
2838 *memsw_limit = min_memsw_limit;
2842 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2844 struct mem_cgroup *mem;
2847 mem = mem_cgroup_from_cont(cont);
2848 type = MEMFILE_TYPE(event);
2849 name = MEMFILE_ATTR(event);
2853 res_counter_reset_max(&mem->res);
2855 res_counter_reset_max(&mem->memsw);
2859 res_counter_reset_failcnt(&mem->res);
2861 res_counter_reset_failcnt(&mem->memsw);
2869 /* For read statistics */
2885 struct mcs_total_stat {
2886 s64 stat[NR_MCS_STAT];
2892 } memcg_stat_strings[NR_MCS_STAT] = {
2893 {"cache", "total_cache"},
2894 {"rss", "total_rss"},
2895 {"mapped_file", "total_mapped_file"},
2896 {"pgpgin", "total_pgpgin"},
2897 {"pgpgout", "total_pgpgout"},
2898 {"swap", "total_swap"},
2899 {"inactive_anon", "total_inactive_anon"},
2900 {"active_anon", "total_active_anon"},
2901 {"inactive_file", "total_inactive_file"},
2902 {"active_file", "total_active_file"},
2903 {"unevictable", "total_unevictable"}
2907 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2909 struct mcs_total_stat *s = data;
2913 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2914 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2915 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2916 s->stat[MCS_RSS] += val * PAGE_SIZE;
2917 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
2918 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
2919 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2920 s->stat[MCS_PGPGIN] += val;
2921 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2922 s->stat[MCS_PGPGOUT] += val;
2923 if (do_swap_account) {
2924 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2925 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2929 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2930 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2931 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2932 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2933 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2934 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2935 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2936 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2937 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2938 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2943 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2945 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2948 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2949 struct cgroup_map_cb *cb)
2951 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2952 struct mcs_total_stat mystat;
2955 memset(&mystat, 0, sizeof(mystat));
2956 mem_cgroup_get_local_stat(mem_cont, &mystat);
2958 for (i = 0; i < NR_MCS_STAT; i++) {
2959 if (i == MCS_SWAP && !do_swap_account)
2961 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2964 /* Hierarchical information */
2966 unsigned long long limit, memsw_limit;
2967 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2968 cb->fill(cb, "hierarchical_memory_limit", limit);
2969 if (do_swap_account)
2970 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2973 memset(&mystat, 0, sizeof(mystat));
2974 mem_cgroup_get_total_stat(mem_cont, &mystat);
2975 for (i = 0; i < NR_MCS_STAT; i++) {
2976 if (i == MCS_SWAP && !do_swap_account)
2978 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2981 #ifdef CONFIG_DEBUG_VM
2982 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2986 struct mem_cgroup_per_zone *mz;
2987 unsigned long recent_rotated[2] = {0, 0};
2988 unsigned long recent_scanned[2] = {0, 0};
2990 for_each_online_node(nid)
2991 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2992 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2994 recent_rotated[0] +=
2995 mz->reclaim_stat.recent_rotated[0];
2996 recent_rotated[1] +=
2997 mz->reclaim_stat.recent_rotated[1];
2998 recent_scanned[0] +=
2999 mz->reclaim_stat.recent_scanned[0];
3000 recent_scanned[1] +=
3001 mz->reclaim_stat.recent_scanned[1];
3003 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3004 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3005 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3006 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3013 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3015 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3017 return get_swappiness(memcg);
3020 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3023 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3024 struct mem_cgroup *parent;
3029 if (cgrp->parent == NULL)
3032 parent = mem_cgroup_from_cont(cgrp->parent);
3036 /* If under hierarchy, only empty-root can set this value */
3037 if ((parent->use_hierarchy) ||
3038 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3043 spin_lock(&memcg->reclaim_param_lock);
3044 memcg->swappiness = val;
3045 spin_unlock(&memcg->reclaim_param_lock);
3053 static struct cftype mem_cgroup_files[] = {
3055 .name = "usage_in_bytes",
3056 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
3057 .read_u64 = mem_cgroup_read,
3060 .name = "max_usage_in_bytes",
3061 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
3062 .trigger = mem_cgroup_reset,
3063 .read_u64 = mem_cgroup_read,
3066 .name = "limit_in_bytes",
3067 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
3068 .write_string = mem_cgroup_write,
3069 .read_u64 = mem_cgroup_read,
3072 .name = "soft_limit_in_bytes",
3073 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3074 .write_string = mem_cgroup_write,
3075 .read_u64 = mem_cgroup_read,
3079 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
3080 .trigger = mem_cgroup_reset,
3081 .read_u64 = mem_cgroup_read,
3085 .read_map = mem_control_stat_show,
3088 .name = "force_empty",
3089 .trigger = mem_cgroup_force_empty_write,
3092 .name = "use_hierarchy",
3093 .write_u64 = mem_cgroup_hierarchy_write,
3094 .read_u64 = mem_cgroup_hierarchy_read,
3097 .name = "swappiness",
3098 .read_u64 = mem_cgroup_swappiness_read,
3099 .write_u64 = mem_cgroup_swappiness_write,
3103 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3104 static struct cftype memsw_cgroup_files[] = {
3106 .name = "memsw.usage_in_bytes",
3107 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3108 .read_u64 = mem_cgroup_read,
3111 .name = "memsw.max_usage_in_bytes",
3112 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3113 .trigger = mem_cgroup_reset,
3114 .read_u64 = mem_cgroup_read,
3117 .name = "memsw.limit_in_bytes",
3118 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3119 .write_string = mem_cgroup_write,
3120 .read_u64 = mem_cgroup_read,
3123 .name = "memsw.failcnt",
3124 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3125 .trigger = mem_cgroup_reset,
3126 .read_u64 = mem_cgroup_read,
3130 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3132 if (!do_swap_account)
3134 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3135 ARRAY_SIZE(memsw_cgroup_files));
3138 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3144 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3146 struct mem_cgroup_per_node *pn;
3147 struct mem_cgroup_per_zone *mz;
3149 int zone, tmp = node;
3151 * This routine is called against possible nodes.
3152 * But it's BUG to call kmalloc() against offline node.
3154 * TODO: this routine can waste much memory for nodes which will
3155 * never be onlined. It's better to use memory hotplug callback
3158 if (!node_state(node, N_NORMAL_MEMORY))
3160 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
3164 mem->info.nodeinfo[node] = pn;
3165 memset(pn, 0, sizeof(*pn));
3167 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3168 mz = &pn->zoneinfo[zone];
3170 INIT_LIST_HEAD(&mz->lists[l]);
3171 mz->usage_in_excess = 0;
3172 mz->on_tree = false;
3178 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3180 kfree(mem->info.nodeinfo[node]);
3183 static int mem_cgroup_size(void)
3185 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3186 return sizeof(struct mem_cgroup) + cpustat_size;
3189 static struct mem_cgroup *mem_cgroup_alloc(void)
3191 struct mem_cgroup *mem;
3192 int size = mem_cgroup_size();
3194 if (size < PAGE_SIZE)
3195 mem = kmalloc(size, GFP_KERNEL);
3197 mem = vmalloc(size);
3200 memset(mem, 0, size);
3205 * At destroying mem_cgroup, references from swap_cgroup can remain.
3206 * (scanning all at force_empty is too costly...)
3208 * Instead of clearing all references at force_empty, we remember
3209 * the number of reference from swap_cgroup and free mem_cgroup when
3210 * it goes down to 0.
3212 * Removal of cgroup itself succeeds regardless of refs from swap.
3215 static void __mem_cgroup_free(struct mem_cgroup *mem)
3219 mem_cgroup_remove_from_trees(mem);
3220 free_css_id(&mem_cgroup_subsys, &mem->css);
3222 for_each_node_state(node, N_POSSIBLE)
3223 free_mem_cgroup_per_zone_info(mem, node);
3225 if (mem_cgroup_size() < PAGE_SIZE)
3231 static void mem_cgroup_get(struct mem_cgroup *mem)
3233 atomic_inc(&mem->refcnt);
3236 static void mem_cgroup_put(struct mem_cgroup *mem)
3238 if (atomic_dec_and_test(&mem->refcnt)) {
3239 struct mem_cgroup *parent = parent_mem_cgroup(mem);
3240 __mem_cgroup_free(mem);
3242 mem_cgroup_put(parent);
3247 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3249 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3251 if (!mem->res.parent)
3253 return mem_cgroup_from_res_counter(mem->res.parent, res);
3256 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3257 static void __init enable_swap_cgroup(void)
3259 if (!mem_cgroup_disabled() && really_do_swap_account)
3260 do_swap_account = 1;
3263 static void __init enable_swap_cgroup(void)
3268 static int mem_cgroup_soft_limit_tree_init(void)
3270 struct mem_cgroup_tree_per_node *rtpn;
3271 struct mem_cgroup_tree_per_zone *rtpz;
3272 int tmp, node, zone;
3274 for_each_node_state(node, N_POSSIBLE) {
3276 if (!node_state(node, N_NORMAL_MEMORY))
3278 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3282 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3284 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3285 rtpz = &rtpn->rb_tree_per_zone[zone];
3286 rtpz->rb_root = RB_ROOT;
3287 spin_lock_init(&rtpz->lock);
3293 static struct cgroup_subsys_state * __ref
3294 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3296 struct mem_cgroup *mem, *parent;
3297 long error = -ENOMEM;
3300 mem = mem_cgroup_alloc();
3302 return ERR_PTR(error);
3304 for_each_node_state(node, N_POSSIBLE)
3305 if (alloc_mem_cgroup_per_zone_info(mem, node))
3309 if (cont->parent == NULL) {
3311 enable_swap_cgroup();
3313 root_mem_cgroup = mem;
3314 if (mem_cgroup_soft_limit_tree_init())
3316 for_each_possible_cpu(cpu) {
3317 struct memcg_stock_pcp *stock =
3318 &per_cpu(memcg_stock, cpu);
3319 INIT_WORK(&stock->work, drain_local_stock);
3321 hotcpu_notifier(memcg_stock_cpu_callback, 0);
3324 parent = mem_cgroup_from_cont(cont->parent);
3325 mem->use_hierarchy = parent->use_hierarchy;
3328 if (parent && parent->use_hierarchy) {
3329 res_counter_init(&mem->res, &parent->res);
3330 res_counter_init(&mem->memsw, &parent->memsw);
3332 * We increment refcnt of the parent to ensure that we can
3333 * safely access it on res_counter_charge/uncharge.
3334 * This refcnt will be decremented when freeing this
3335 * mem_cgroup(see mem_cgroup_put).
3337 mem_cgroup_get(parent);
3339 res_counter_init(&mem->res, NULL);
3340 res_counter_init(&mem->memsw, NULL);
3342 mem->last_scanned_child = 0;
3343 spin_lock_init(&mem->reclaim_param_lock);
3346 mem->swappiness = get_swappiness(parent);
3347 atomic_set(&mem->refcnt, 1);
3350 __mem_cgroup_free(mem);
3351 root_mem_cgroup = NULL;
3352 return ERR_PTR(error);
3355 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
3356 struct cgroup *cont)
3358 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3360 return mem_cgroup_force_empty(mem, false);
3363 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3364 struct cgroup *cont)
3366 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3368 mem_cgroup_put(mem);
3371 static int mem_cgroup_populate(struct cgroup_subsys *ss,
3372 struct cgroup *cont)
3376 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3377 ARRAY_SIZE(mem_cgroup_files));
3380 ret = register_memsw_files(cont, ss);
3384 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3385 struct cgroup *cont,
3386 struct cgroup *old_cont,
3387 struct task_struct *p,
3391 * FIXME: It's better to move charges of this process from old
3392 * memcg to new memcg. But it's just on TODO-List now.
3396 struct cgroup_subsys mem_cgroup_subsys = {
3398 .subsys_id = mem_cgroup_subsys_id,
3399 .create = mem_cgroup_create,
3400 .pre_destroy = mem_cgroup_pre_destroy,
3401 .destroy = mem_cgroup_destroy,
3402 .populate = mem_cgroup_populate,
3403 .attach = mem_cgroup_move_task,
3408 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3410 static int __init disable_swap_account(char *s)
3412 really_do_swap_account = 0;
3415 __setup("noswapaccount", disable_swap_account);