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>
43 #include <asm/uaccess.h>
45 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
46 #define MEM_CGROUP_RECLAIM_RETRIES 5
47 struct mem_cgroup *root_mem_cgroup __read_mostly;
49 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
50 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
51 int do_swap_account __read_mostly;
52 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
54 #define do_swap_account (0)
57 static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
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_MAPPED_FILE, /* # 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);
279 static struct mem_cgroup_per_zone *
280 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
282 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
285 struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
290 static struct mem_cgroup_per_zone *
291 page_cgroup_zoneinfo(struct page_cgroup *pc)
293 struct mem_cgroup *mem = pc->mem_cgroup;
294 int nid = page_cgroup_nid(pc);
295 int zid = page_cgroup_zid(pc);
300 return mem_cgroup_zoneinfo(mem, nid, zid);
303 static struct mem_cgroup_tree_per_zone *
304 soft_limit_tree_node_zone(int nid, int zid)
306 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
309 static struct mem_cgroup_tree_per_zone *
310 soft_limit_tree_from_page(struct page *page)
312 int nid = page_to_nid(page);
313 int zid = page_zonenum(page);
315 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
319 __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
320 struct mem_cgroup_per_zone *mz,
321 struct mem_cgroup_tree_per_zone *mctz,
322 unsigned long long new_usage_in_excess)
324 struct rb_node **p = &mctz->rb_root.rb_node;
325 struct rb_node *parent = NULL;
326 struct mem_cgroup_per_zone *mz_node;
331 mz->usage_in_excess = new_usage_in_excess;
332 if (!mz->usage_in_excess)
336 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
338 if (mz->usage_in_excess < mz_node->usage_in_excess)
341 * We can't avoid mem cgroups that are over their soft
342 * limit by the same amount
344 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
347 rb_link_node(&mz->tree_node, parent, p);
348 rb_insert_color(&mz->tree_node, &mctz->rb_root);
353 __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
354 struct mem_cgroup_per_zone *mz,
355 struct mem_cgroup_tree_per_zone *mctz)
359 rb_erase(&mz->tree_node, &mctz->rb_root);
364 mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
365 struct mem_cgroup_per_zone *mz,
366 struct mem_cgroup_tree_per_zone *mctz)
368 spin_lock(&mctz->lock);
369 __mem_cgroup_remove_exceeded(mem, mz, mctz);
370 spin_unlock(&mctz->lock);
373 static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
378 struct mem_cgroup_stat_cpu *cpustat;
381 cpustat = &mem->stat.cpustat[cpu];
382 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
383 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
384 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
391 static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
393 unsigned long long excess;
394 struct mem_cgroup_per_zone *mz;
395 struct mem_cgroup_tree_per_zone *mctz;
396 int nid = page_to_nid(page);
397 int zid = page_zonenum(page);
398 mctz = soft_limit_tree_from_page(page);
401 * Necessary to update all ancestors when hierarchy is used.
402 * because their event counter is not touched.
404 for (; mem; mem = parent_mem_cgroup(mem)) {
405 mz = mem_cgroup_zoneinfo(mem, nid, zid);
406 excess = res_counter_soft_limit_excess(&mem->res);
408 * We have to update the tree if mz is on RB-tree or
409 * mem is over its softlimit.
411 if (excess || mz->on_tree) {
412 spin_lock(&mctz->lock);
413 /* if on-tree, remove it */
415 __mem_cgroup_remove_exceeded(mem, mz, mctz);
417 * Insert again. mz->usage_in_excess will be updated.
418 * If excess is 0, no tree ops.
420 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
421 spin_unlock(&mctz->lock);
426 static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
429 struct mem_cgroup_per_zone *mz;
430 struct mem_cgroup_tree_per_zone *mctz;
432 for_each_node_state(node, N_POSSIBLE) {
433 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
434 mz = mem_cgroup_zoneinfo(mem, node, zone);
435 mctz = soft_limit_tree_node_zone(node, zone);
436 mem_cgroup_remove_exceeded(mem, mz, mctz);
441 static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
443 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
446 static struct mem_cgroup_per_zone *
447 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
449 struct rb_node *rightmost = NULL;
450 struct mem_cgroup_per_zone *mz;
454 rightmost = rb_last(&mctz->rb_root);
456 goto done; /* Nothing to reclaim from */
458 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
460 * Remove the node now but someone else can add it back,
461 * we will to add it back at the end of reclaim to its correct
462 * position in the tree.
464 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
465 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
466 !css_tryget(&mz->mem->css))
472 static struct mem_cgroup_per_zone *
473 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
475 struct mem_cgroup_per_zone *mz;
477 spin_lock(&mctz->lock);
478 mz = __mem_cgroup_largest_soft_limit_node(mctz);
479 spin_unlock(&mctz->lock);
483 static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
486 int val = (charge) ? 1 : -1;
487 struct mem_cgroup_stat *stat = &mem->stat;
488 struct mem_cgroup_stat_cpu *cpustat;
491 cpustat = &stat->cpustat[cpu];
492 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
496 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
497 struct page_cgroup *pc,
500 int val = (charge) ? 1 : -1;
501 struct mem_cgroup_stat *stat = &mem->stat;
502 struct mem_cgroup_stat_cpu *cpustat;
505 cpustat = &stat->cpustat[cpu];
506 if (PageCgroupCache(pc))
507 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
509 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
512 __mem_cgroup_stat_add_safe(cpustat,
513 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
515 __mem_cgroup_stat_add_safe(cpustat,
516 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
517 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
521 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
525 struct mem_cgroup_per_zone *mz;
528 for_each_online_node(nid)
529 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
530 mz = mem_cgroup_zoneinfo(mem, nid, zid);
531 total += MEM_CGROUP_ZSTAT(mz, idx);
536 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
538 return container_of(cgroup_subsys_state(cont,
539 mem_cgroup_subsys_id), struct mem_cgroup,
543 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
546 * mm_update_next_owner() may clear mm->owner to NULL
547 * if it races with swapoff, page migration, etc.
548 * So this can be called with p == NULL.
553 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
554 struct mem_cgroup, css);
557 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
559 struct mem_cgroup *mem = NULL;
564 * Because we have no locks, mm->owner's may be being moved to other
565 * cgroup. We use css_tryget() here even if this looks
566 * pessimistic (rather than adding locks here).
570 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
573 } while (!css_tryget(&mem->css));
579 * Call callback function against all cgroup under hierarchy tree.
581 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
582 int (*func)(struct mem_cgroup *, void *))
584 int found, ret, nextid;
585 struct cgroup_subsys_state *css;
586 struct mem_cgroup *mem;
588 if (!root->use_hierarchy)
589 return (*func)(root, data);
597 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
599 if (css && css_tryget(css))
600 mem = container_of(css, struct mem_cgroup, css);
604 ret = (*func)(mem, data);
608 } while (!ret && css);
613 static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
615 return (mem == root_mem_cgroup);
619 * Following LRU functions are allowed to be used without PCG_LOCK.
620 * Operations are called by routine of global LRU independently from memcg.
621 * What we have to take care of here is validness of pc->mem_cgroup.
623 * Changes to pc->mem_cgroup happens when
626 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
627 * It is added to LRU before charge.
628 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
629 * When moving account, the page is not on LRU. It's isolated.
632 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
634 struct page_cgroup *pc;
635 struct mem_cgroup_per_zone *mz;
637 if (mem_cgroup_disabled())
639 pc = lookup_page_cgroup(page);
640 /* can happen while we handle swapcache. */
641 if (!TestClearPageCgroupAcctLRU(pc))
643 VM_BUG_ON(!pc->mem_cgroup);
645 * We don't check PCG_USED bit. It's cleared when the "page" is finally
646 * removed from global LRU.
648 mz = page_cgroup_zoneinfo(pc);
649 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
650 if (mem_cgroup_is_root(pc->mem_cgroup))
652 VM_BUG_ON(list_empty(&pc->lru));
653 list_del_init(&pc->lru);
657 void mem_cgroup_del_lru(struct page *page)
659 mem_cgroup_del_lru_list(page, page_lru(page));
662 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
664 struct mem_cgroup_per_zone *mz;
665 struct page_cgroup *pc;
667 if (mem_cgroup_disabled())
670 pc = lookup_page_cgroup(page);
672 * Used bit is set without atomic ops but after smp_wmb().
673 * For making pc->mem_cgroup visible, insert smp_rmb() here.
676 /* unused or root page is not rotated. */
677 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
679 mz = page_cgroup_zoneinfo(pc);
680 list_move(&pc->lru, &mz->lists[lru]);
683 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
685 struct page_cgroup *pc;
686 struct mem_cgroup_per_zone *mz;
688 if (mem_cgroup_disabled())
690 pc = lookup_page_cgroup(page);
691 VM_BUG_ON(PageCgroupAcctLRU(pc));
693 * Used bit is set without atomic ops but after smp_wmb().
694 * For making pc->mem_cgroup visible, insert smp_rmb() here.
697 if (!PageCgroupUsed(pc))
700 mz = page_cgroup_zoneinfo(pc);
701 MEM_CGROUP_ZSTAT(mz, lru) += 1;
702 SetPageCgroupAcctLRU(pc);
703 if (mem_cgroup_is_root(pc->mem_cgroup))
705 list_add(&pc->lru, &mz->lists[lru]);
709 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
710 * lru because the page may.be reused after it's fully uncharged (because of
711 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
712 * it again. This function is only used to charge SwapCache. It's done under
713 * lock_page and expected that zone->lru_lock is never held.
715 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
718 struct zone *zone = page_zone(page);
719 struct page_cgroup *pc = lookup_page_cgroup(page);
721 spin_lock_irqsave(&zone->lru_lock, flags);
723 * Forget old LRU when this page_cgroup is *not* used. This Used bit
724 * is guarded by lock_page() because the page is SwapCache.
726 if (!PageCgroupUsed(pc))
727 mem_cgroup_del_lru_list(page, page_lru(page));
728 spin_unlock_irqrestore(&zone->lru_lock, flags);
731 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
734 struct zone *zone = page_zone(page);
735 struct page_cgroup *pc = lookup_page_cgroup(page);
737 spin_lock_irqsave(&zone->lru_lock, flags);
738 /* link when the page is linked to LRU but page_cgroup isn't */
739 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
740 mem_cgroup_add_lru_list(page, page_lru(page));
741 spin_unlock_irqrestore(&zone->lru_lock, flags);
745 void mem_cgroup_move_lists(struct page *page,
746 enum lru_list from, enum lru_list to)
748 if (mem_cgroup_disabled())
750 mem_cgroup_del_lru_list(page, from);
751 mem_cgroup_add_lru_list(page, to);
754 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
757 struct mem_cgroup *curr = NULL;
761 curr = try_get_mem_cgroup_from_mm(task->mm);
766 if (curr->use_hierarchy)
767 ret = css_is_ancestor(&curr->css, &mem->css);
775 * prev_priority control...this will be used in memory reclaim path.
777 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
781 spin_lock(&mem->reclaim_param_lock);
782 prev_priority = mem->prev_priority;
783 spin_unlock(&mem->reclaim_param_lock);
785 return prev_priority;
788 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
790 spin_lock(&mem->reclaim_param_lock);
791 if (priority < mem->prev_priority)
792 mem->prev_priority = priority;
793 spin_unlock(&mem->reclaim_param_lock);
796 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
798 spin_lock(&mem->reclaim_param_lock);
799 mem->prev_priority = priority;
800 spin_unlock(&mem->reclaim_param_lock);
803 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
805 unsigned long active;
806 unsigned long inactive;
808 unsigned long inactive_ratio;
810 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
811 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
813 gb = (inactive + active) >> (30 - PAGE_SHIFT);
815 inactive_ratio = int_sqrt(10 * gb);
820 present_pages[0] = inactive;
821 present_pages[1] = active;
824 return inactive_ratio;
827 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
829 unsigned long active;
830 unsigned long inactive;
831 unsigned long present_pages[2];
832 unsigned long inactive_ratio;
834 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
836 inactive = present_pages[0];
837 active = present_pages[1];
839 if (inactive * inactive_ratio < active)
845 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
847 unsigned long active;
848 unsigned long inactive;
850 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
851 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
853 return (active > inactive);
856 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
860 int nid = zone->zone_pgdat->node_id;
861 int zid = zone_idx(zone);
862 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
864 return MEM_CGROUP_ZSTAT(mz, lru);
867 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
870 int nid = zone->zone_pgdat->node_id;
871 int zid = zone_idx(zone);
872 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
874 return &mz->reclaim_stat;
877 struct zone_reclaim_stat *
878 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
880 struct page_cgroup *pc;
881 struct mem_cgroup_per_zone *mz;
883 if (mem_cgroup_disabled())
886 pc = lookup_page_cgroup(page);
888 * Used bit is set without atomic ops but after smp_wmb().
889 * For making pc->mem_cgroup visible, insert smp_rmb() here.
892 if (!PageCgroupUsed(pc))
895 mz = page_cgroup_zoneinfo(pc);
899 return &mz->reclaim_stat;
902 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
903 struct list_head *dst,
904 unsigned long *scanned, int order,
905 int mode, struct zone *z,
906 struct mem_cgroup *mem_cont,
907 int active, int file)
909 unsigned long nr_taken = 0;
913 struct list_head *src;
914 struct page_cgroup *pc, *tmp;
915 int nid = z->zone_pgdat->node_id;
916 int zid = zone_idx(z);
917 struct mem_cgroup_per_zone *mz;
918 int lru = LRU_FILE * file + active;
922 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
923 src = &mz->lists[lru];
926 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
927 if (scan >= nr_to_scan)
931 if (unlikely(!PageCgroupUsed(pc)))
933 if (unlikely(!PageLRU(page)))
937 ret = __isolate_lru_page(page, mode, file);
940 list_move(&page->lru, dst);
941 mem_cgroup_del_lru(page);
945 /* we don't affect global LRU but rotate in our LRU */
946 mem_cgroup_rotate_lru_list(page, page_lru(page));
957 #define mem_cgroup_from_res_counter(counter, member) \
958 container_of(counter, struct mem_cgroup, member)
960 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
962 if (do_swap_account) {
963 if (res_counter_check_under_limit(&mem->res) &&
964 res_counter_check_under_limit(&mem->memsw))
967 if (res_counter_check_under_limit(&mem->res))
972 static unsigned int get_swappiness(struct mem_cgroup *memcg)
974 struct cgroup *cgrp = memcg->css.cgroup;
975 unsigned int swappiness;
978 if (cgrp->parent == NULL)
979 return vm_swappiness;
981 spin_lock(&memcg->reclaim_param_lock);
982 swappiness = memcg->swappiness;
983 spin_unlock(&memcg->reclaim_param_lock);
988 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
996 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
997 * @memcg: The memory cgroup that went over limit
998 * @p: Task that is going to be killed
1000 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1003 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1005 struct cgroup *task_cgrp;
1006 struct cgroup *mem_cgrp;
1008 * Need a buffer in BSS, can't rely on allocations. The code relies
1009 * on the assumption that OOM is serialized for memory controller.
1010 * If this assumption is broken, revisit this code.
1012 static char memcg_name[PATH_MAX];
1021 mem_cgrp = memcg->css.cgroup;
1022 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1024 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1027 * Unfortunately, we are unable to convert to a useful name
1028 * But we'll still print out the usage information
1035 printk(KERN_INFO "Task in %s killed", memcg_name);
1038 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1046 * Continues from above, so we don't need an KERN_ level
1048 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1051 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1052 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1053 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1054 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1055 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1057 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1058 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1059 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1063 * This function returns the number of memcg under hierarchy tree. Returns
1064 * 1(self count) if no children.
1066 static int mem_cgroup_count_children(struct mem_cgroup *mem)
1069 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1074 * Visit the first child (need not be the first child as per the ordering
1075 * of the cgroup list, since we track last_scanned_child) of @mem and use
1076 * that to reclaim free pages from.
1078 static struct mem_cgroup *
1079 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1081 struct mem_cgroup *ret = NULL;
1082 struct cgroup_subsys_state *css;
1085 if (!root_mem->use_hierarchy) {
1086 css_get(&root_mem->css);
1092 nextid = root_mem->last_scanned_child + 1;
1093 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1095 if (css && css_tryget(css))
1096 ret = container_of(css, struct mem_cgroup, css);
1099 /* Updates scanning parameter */
1100 spin_lock(&root_mem->reclaim_param_lock);
1102 /* this means start scan from ID:1 */
1103 root_mem->last_scanned_child = 0;
1105 root_mem->last_scanned_child = found;
1106 spin_unlock(&root_mem->reclaim_param_lock);
1113 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1114 * we reclaimed from, so that we don't end up penalizing one child extensively
1115 * based on its position in the children list.
1117 * root_mem is the original ancestor that we've been reclaim from.
1119 * We give up and return to the caller when we visit root_mem twice.
1120 * (other groups can be removed while we're walking....)
1122 * If shrink==true, for avoiding to free too much, this returns immedieately.
1124 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1127 unsigned long reclaim_options)
1129 struct mem_cgroup *victim;
1132 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1133 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
1134 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1135 unsigned long excess = mem_cgroup_get_excess(root_mem);
1137 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1138 if (root_mem->memsw_is_minimum)
1142 victim = mem_cgroup_select_victim(root_mem);
1143 if (victim == root_mem) {
1147 * If we have not been able to reclaim
1148 * anything, it might because there are
1149 * no reclaimable pages under this hierarchy
1151 if (!check_soft || !total) {
1152 css_put(&victim->css);
1156 * We want to do more targetted reclaim.
1157 * excess >> 2 is not to excessive so as to
1158 * reclaim too much, nor too less that we keep
1159 * coming back to reclaim from this cgroup
1161 if (total >= (excess >> 2) ||
1162 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1163 css_put(&victim->css);
1168 if (!mem_cgroup_local_usage(&victim->stat)) {
1169 /* this cgroup's local usage == 0 */
1170 css_put(&victim->css);
1173 /* we use swappiness of local cgroup */
1175 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1176 noswap, get_swappiness(victim), zone,
1177 zone->zone_pgdat->node_id);
1179 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1180 noswap, get_swappiness(victim));
1181 css_put(&victim->css);
1183 * At shrinking usage, we can't check we should stop here or
1184 * reclaim more. It's depends on callers. last_scanned_child
1185 * will work enough for keeping fairness under tree.
1191 if (res_counter_check_under_soft_limit(&root_mem->res))
1193 } else if (mem_cgroup_check_under_limit(root_mem))
1199 bool mem_cgroup_oom_called(struct task_struct *task)
1202 struct mem_cgroup *mem;
1203 struct mm_struct *mm;
1209 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1210 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1216 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1218 mem->last_oom_jiffies = jiffies;
1222 static void record_last_oom(struct mem_cgroup *mem)
1224 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1228 * Currently used to update mapped file statistics, but the routine can be
1229 * generalized to update other statistics as well.
1231 void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1233 struct mem_cgroup *mem;
1234 struct mem_cgroup_stat *stat;
1235 struct mem_cgroup_stat_cpu *cpustat;
1237 struct page_cgroup *pc;
1239 if (!page_is_file_cache(page))
1242 pc = lookup_page_cgroup(page);
1246 lock_page_cgroup(pc);
1247 mem = pc->mem_cgroup;
1251 if (!PageCgroupUsed(pc))
1255 * Preemption is already disabled, we don't need get_cpu()
1257 cpu = smp_processor_id();
1259 cpustat = &stat->cpustat[cpu];
1261 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1263 unlock_page_cgroup(pc);
1267 * Unlike exported interface, "oom" parameter is added. if oom==true,
1268 * oom-killer can be invoked.
1270 static int __mem_cgroup_try_charge(struct mm_struct *mm,
1271 gfp_t gfp_mask, struct mem_cgroup **memcg,
1272 bool oom, struct page *page)
1274 struct mem_cgroup *mem, *mem_over_limit;
1275 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1276 struct res_counter *fail_res;
1278 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1279 /* Don't account this! */
1285 * We always charge the cgroup the mm_struct belongs to.
1286 * The mm_struct's mem_cgroup changes on task migration if the
1287 * thread group leader migrates. It's possible that mm is not
1288 * set, if so charge the init_mm (happens for pagecache usage).
1292 mem = try_get_mem_cgroup_from_mm(mm);
1300 VM_BUG_ON(css_is_removed(&mem->css));
1304 unsigned long flags = 0;
1306 if (mem_cgroup_is_root(mem))
1308 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
1310 if (!do_swap_account)
1312 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
1316 /* mem+swap counter fails */
1317 res_counter_uncharge(&mem->res, PAGE_SIZE);
1318 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1319 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1322 /* mem counter fails */
1323 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1326 if (!(gfp_mask & __GFP_WAIT))
1329 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1335 * try_to_free_mem_cgroup_pages() might not give us a full
1336 * picture of reclaim. Some pages are reclaimed and might be
1337 * moved to swap cache or just unmapped from the cgroup.
1338 * Check the limit again to see if the reclaim reduced the
1339 * current usage of the cgroup before giving up
1342 if (mem_cgroup_check_under_limit(mem_over_limit))
1345 if (!nr_retries--) {
1347 mutex_lock(&memcg_tasklist);
1348 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1349 mutex_unlock(&memcg_tasklist);
1350 record_last_oom(mem_over_limit);
1356 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1357 * if they exceeds softlimit.
1359 if (mem_cgroup_soft_limit_check(mem))
1360 mem_cgroup_update_tree(mem, page);
1369 * A helper function to get mem_cgroup from ID. must be called under
1370 * rcu_read_lock(). The caller must check css_is_removed() or some if
1371 * it's concern. (dropping refcnt from swap can be called against removed
1374 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1376 struct cgroup_subsys_state *css;
1378 /* ID 0 is unused ID */
1381 css = css_lookup(&mem_cgroup_subsys, id);
1384 return container_of(css, struct mem_cgroup, css);
1387 struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
1389 struct mem_cgroup *mem = NULL;
1390 struct page_cgroup *pc;
1394 VM_BUG_ON(!PageLocked(page));
1396 pc = lookup_page_cgroup(page);
1397 lock_page_cgroup(pc);
1398 if (PageCgroupUsed(pc)) {
1399 mem = pc->mem_cgroup;
1400 if (mem && !css_tryget(&mem->css))
1402 } else if (PageSwapCache(page)) {
1403 ent.val = page_private(page);
1404 id = lookup_swap_cgroup(ent);
1406 mem = mem_cgroup_lookup(id);
1407 if (mem && !css_tryget(&mem->css))
1411 unlock_page_cgroup(pc);
1416 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1417 * USED state. If already USED, uncharge and return.
1420 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1421 struct page_cgroup *pc,
1422 enum charge_type ctype)
1424 /* try_charge() can return NULL to *memcg, taking care of it. */
1428 lock_page_cgroup(pc);
1429 if (unlikely(PageCgroupUsed(pc))) {
1430 unlock_page_cgroup(pc);
1431 if (!mem_cgroup_is_root(mem)) {
1432 res_counter_uncharge(&mem->res, PAGE_SIZE);
1433 if (do_swap_account)
1434 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1440 pc->mem_cgroup = mem;
1442 * We access a page_cgroup asynchronously without lock_page_cgroup().
1443 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1444 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1445 * before USED bit, we need memory barrier here.
1446 * See mem_cgroup_add_lru_list(), etc.
1450 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1451 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1452 SetPageCgroupCache(pc);
1453 SetPageCgroupUsed(pc);
1455 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1456 ClearPageCgroupCache(pc);
1457 SetPageCgroupUsed(pc);
1463 mem_cgroup_charge_statistics(mem, pc, true);
1465 unlock_page_cgroup(pc);
1469 * mem_cgroup_move_account - move account of the page
1470 * @pc: page_cgroup of the page.
1471 * @from: mem_cgroup which the page is moved from.
1472 * @to: mem_cgroup which the page is moved to. @from != @to.
1474 * The caller must confirm following.
1475 * - page is not on LRU (isolate_page() is useful.)
1477 * returns 0 at success,
1478 * returns -EBUSY when lock is busy or "pc" is unstable.
1480 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1481 * new cgroup. It should be done by a caller.
1484 static int mem_cgroup_move_account(struct page_cgroup *pc,
1485 struct mem_cgroup *from, struct mem_cgroup *to)
1487 struct mem_cgroup_per_zone *from_mz, *to_mz;
1492 struct mem_cgroup_stat *stat;
1493 struct mem_cgroup_stat_cpu *cpustat;
1495 VM_BUG_ON(from == to);
1496 VM_BUG_ON(PageLRU(pc->page));
1498 nid = page_cgroup_nid(pc);
1499 zid = page_cgroup_zid(pc);
1500 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1501 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1503 if (!trylock_page_cgroup(pc))
1506 if (!PageCgroupUsed(pc))
1509 if (pc->mem_cgroup != from)
1512 if (!mem_cgroup_is_root(from))
1513 res_counter_uncharge(&from->res, PAGE_SIZE);
1514 mem_cgroup_charge_statistics(from, pc, false);
1517 if (page_is_file_cache(page) && page_mapped(page)) {
1518 cpu = smp_processor_id();
1519 /* Update mapped_file data for mem_cgroup "from" */
1521 cpustat = &stat->cpustat[cpu];
1522 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1525 /* Update mapped_file data for mem_cgroup "to" */
1527 cpustat = &stat->cpustat[cpu];
1528 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1532 if (do_swap_account && !mem_cgroup_is_root(from))
1533 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1534 css_put(&from->css);
1537 pc->mem_cgroup = to;
1538 mem_cgroup_charge_statistics(to, pc, true);
1541 unlock_page_cgroup(pc);
1543 * We charges against "to" which may not have any tasks. Then, "to"
1544 * can be under rmdir(). But in current implementation, caller of
1545 * this function is just force_empty() and it's garanteed that
1546 * "to" is never removed. So, we don't check rmdir status here.
1552 * move charges to its parent.
1555 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1556 struct mem_cgroup *child,
1559 struct page *page = pc->page;
1560 struct cgroup *cg = child->css.cgroup;
1561 struct cgroup *pcg = cg->parent;
1562 struct mem_cgroup *parent;
1570 parent = mem_cgroup_from_cont(pcg);
1573 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
1577 if (!get_page_unless_zero(page)) {
1582 ret = isolate_lru_page(page);
1587 ret = mem_cgroup_move_account(pc, child, parent);
1589 putback_lru_page(page);
1592 /* drop extra refcnt by try_charge() */
1593 css_put(&parent->css);
1600 /* drop extra refcnt by try_charge() */
1601 css_put(&parent->css);
1602 /* uncharge if move fails */
1603 if (!mem_cgroup_is_root(parent)) {
1604 res_counter_uncharge(&parent->res, PAGE_SIZE);
1605 if (do_swap_account)
1606 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
1612 * Charge the memory controller for page usage.
1614 * 0 if the charge was successful
1615 * < 0 if the cgroup is over its limit
1617 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1618 gfp_t gfp_mask, enum charge_type ctype,
1619 struct mem_cgroup *memcg)
1621 struct mem_cgroup *mem;
1622 struct page_cgroup *pc;
1625 pc = lookup_page_cgroup(page);
1626 /* can happen at boot */
1632 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
1636 __mem_cgroup_commit_charge(mem, pc, ctype);
1640 int mem_cgroup_newpage_charge(struct page *page,
1641 struct mm_struct *mm, gfp_t gfp_mask)
1643 if (mem_cgroup_disabled())
1645 if (PageCompound(page))
1648 * If already mapped, we don't have to account.
1649 * If page cache, page->mapping has address_space.
1650 * But page->mapping may have out-of-use anon_vma pointer,
1651 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1654 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1658 return mem_cgroup_charge_common(page, mm, gfp_mask,
1659 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1663 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1664 enum charge_type ctype);
1666 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1669 struct mem_cgroup *mem = NULL;
1672 if (mem_cgroup_disabled())
1674 if (PageCompound(page))
1677 * Corner case handling. This is called from add_to_page_cache()
1678 * in usual. But some FS (shmem) precharges this page before calling it
1679 * and call add_to_page_cache() with GFP_NOWAIT.
1681 * For GFP_NOWAIT case, the page may be pre-charged before calling
1682 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1683 * charge twice. (It works but has to pay a bit larger cost.)
1684 * And when the page is SwapCache, it should take swap information
1685 * into account. This is under lock_page() now.
1687 if (!(gfp_mask & __GFP_WAIT)) {
1688 struct page_cgroup *pc;
1691 pc = lookup_page_cgroup(page);
1694 lock_page_cgroup(pc);
1695 if (PageCgroupUsed(pc)) {
1696 unlock_page_cgroup(pc);
1699 unlock_page_cgroup(pc);
1702 if (unlikely(!mm && !mem))
1705 if (page_is_file_cache(page))
1706 return mem_cgroup_charge_common(page, mm, gfp_mask,
1707 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1710 if (PageSwapCache(page)) {
1711 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1713 __mem_cgroup_commit_charge_swapin(page, mem,
1714 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1716 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1717 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1723 * While swap-in, try_charge -> commit or cancel, the page is locked.
1724 * And when try_charge() successfully returns, one refcnt to memcg without
1725 * struct page_cgroup is acquired. This refcnt will be consumed by
1726 * "commit()" or removed by "cancel()"
1728 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1730 gfp_t mask, struct mem_cgroup **ptr)
1732 struct mem_cgroup *mem;
1735 if (mem_cgroup_disabled())
1738 if (!do_swap_account)
1741 * A racing thread's fault, or swapoff, may have already updated
1742 * the pte, and even removed page from swap cache: in those cases
1743 * do_swap_page()'s pte_same() test will fail; but there's also a
1744 * KSM case which does need to charge the page.
1746 if (!PageSwapCache(page))
1748 mem = try_get_mem_cgroup_from_page(page);
1752 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
1753 /* drop extra refcnt from tryget */
1759 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
1763 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1764 enum charge_type ctype)
1766 struct page_cgroup *pc;
1768 if (mem_cgroup_disabled())
1772 cgroup_exclude_rmdir(&ptr->css);
1773 pc = lookup_page_cgroup(page);
1774 mem_cgroup_lru_del_before_commit_swapcache(page);
1775 __mem_cgroup_commit_charge(ptr, pc, ctype);
1776 mem_cgroup_lru_add_after_commit_swapcache(page);
1778 * Now swap is on-memory. This means this page may be
1779 * counted both as mem and swap....double count.
1780 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1781 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1782 * may call delete_from_swap_cache() before reach here.
1784 if (do_swap_account && PageSwapCache(page)) {
1785 swp_entry_t ent = {.val = page_private(page)};
1787 struct mem_cgroup *memcg;
1789 id = swap_cgroup_record(ent, 0);
1791 memcg = mem_cgroup_lookup(id);
1794 * This recorded memcg can be obsolete one. So, avoid
1795 * calling css_tryget
1797 if (!mem_cgroup_is_root(memcg))
1798 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1799 mem_cgroup_swap_statistics(memcg, false);
1800 mem_cgroup_put(memcg);
1805 * At swapin, we may charge account against cgroup which has no tasks.
1806 * So, rmdir()->pre_destroy() can be called while we do this charge.
1807 * In that case, we need to call pre_destroy() again. check it here.
1809 cgroup_release_and_wakeup_rmdir(&ptr->css);
1812 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1814 __mem_cgroup_commit_charge_swapin(page, ptr,
1815 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1818 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1820 if (mem_cgroup_disabled())
1824 if (!mem_cgroup_is_root(mem)) {
1825 res_counter_uncharge(&mem->res, PAGE_SIZE);
1826 if (do_swap_account)
1827 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1834 * uncharge if !page_mapped(page)
1836 static struct mem_cgroup *
1837 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1839 struct page_cgroup *pc;
1840 struct mem_cgroup *mem = NULL;
1841 struct mem_cgroup_per_zone *mz;
1843 if (mem_cgroup_disabled())
1846 if (PageSwapCache(page))
1850 * Check if our page_cgroup is valid
1852 pc = lookup_page_cgroup(page);
1853 if (unlikely(!pc || !PageCgroupUsed(pc)))
1856 lock_page_cgroup(pc);
1858 mem = pc->mem_cgroup;
1860 if (!PageCgroupUsed(pc))
1864 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1865 case MEM_CGROUP_CHARGE_TYPE_DROP:
1866 if (page_mapped(page))
1869 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1870 if (!PageAnon(page)) { /* Shared memory */
1871 if (page->mapping && !page_is_file_cache(page))
1873 } else if (page_mapped(page)) /* Anon */
1880 if (!mem_cgroup_is_root(mem)) {
1881 res_counter_uncharge(&mem->res, PAGE_SIZE);
1882 if (do_swap_account &&
1883 (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1884 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1886 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1887 mem_cgroup_swap_statistics(mem, true);
1888 mem_cgroup_charge_statistics(mem, pc, false);
1890 ClearPageCgroupUsed(pc);
1892 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1893 * freed from LRU. This is safe because uncharged page is expected not
1894 * to be reused (freed soon). Exception is SwapCache, it's handled by
1895 * special functions.
1898 mz = page_cgroup_zoneinfo(pc);
1899 unlock_page_cgroup(pc);
1901 if (mem_cgroup_soft_limit_check(mem))
1902 mem_cgroup_update_tree(mem, page);
1903 /* at swapout, this memcg will be accessed to record to swap */
1904 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1910 unlock_page_cgroup(pc);
1914 void mem_cgroup_uncharge_page(struct page *page)
1917 if (page_mapped(page))
1919 if (page->mapping && !PageAnon(page))
1921 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1924 void mem_cgroup_uncharge_cache_page(struct page *page)
1926 VM_BUG_ON(page_mapped(page));
1927 VM_BUG_ON(page->mapping);
1928 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1933 * called after __delete_from_swap_cache() and drop "page" account.
1934 * memcg information is recorded to swap_cgroup of "ent"
1937 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
1939 struct mem_cgroup *memcg;
1940 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1942 if (!swapout) /* this was a swap cache but the swap is unused ! */
1943 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1945 memcg = __mem_cgroup_uncharge_common(page, ctype);
1947 /* record memcg information */
1948 if (do_swap_account && swapout && memcg) {
1949 swap_cgroup_record(ent, css_id(&memcg->css));
1950 mem_cgroup_get(memcg);
1952 if (swapout && memcg)
1953 css_put(&memcg->css);
1957 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1959 * called from swap_entry_free(). remove record in swap_cgroup and
1960 * uncharge "memsw" account.
1962 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1964 struct mem_cgroup *memcg;
1967 if (!do_swap_account)
1970 id = swap_cgroup_record(ent, 0);
1972 memcg = mem_cgroup_lookup(id);
1975 * We uncharge this because swap is freed.
1976 * This memcg can be obsolete one. We avoid calling css_tryget
1978 if (!mem_cgroup_is_root(memcg))
1979 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1980 mem_cgroup_swap_statistics(memcg, false);
1981 mem_cgroup_put(memcg);
1988 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1991 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1993 struct page_cgroup *pc;
1994 struct mem_cgroup *mem = NULL;
1997 if (mem_cgroup_disabled())
2000 pc = lookup_page_cgroup(page);
2001 lock_page_cgroup(pc);
2002 if (PageCgroupUsed(pc)) {
2003 mem = pc->mem_cgroup;
2006 unlock_page_cgroup(pc);
2009 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2017 /* remove redundant charge if migration failed*/
2018 void mem_cgroup_end_migration(struct mem_cgroup *mem,
2019 struct page *oldpage, struct page *newpage)
2021 struct page *target, *unused;
2022 struct page_cgroup *pc;
2023 enum charge_type ctype;
2027 cgroup_exclude_rmdir(&mem->css);
2028 /* at migration success, oldpage->mapping is NULL. */
2029 if (oldpage->mapping) {
2037 if (PageAnon(target))
2038 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2039 else if (page_is_file_cache(target))
2040 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2042 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2044 /* unused page is not on radix-tree now. */
2046 __mem_cgroup_uncharge_common(unused, ctype);
2048 pc = lookup_page_cgroup(target);
2050 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2051 * So, double-counting is effectively avoided.
2053 __mem_cgroup_commit_charge(mem, pc, ctype);
2056 * Both of oldpage and newpage are still under lock_page().
2057 * Then, we don't have to care about race in radix-tree.
2058 * But we have to be careful that this page is unmapped or not.
2060 * There is a case for !page_mapped(). At the start of
2061 * migration, oldpage was mapped. But now, it's zapped.
2062 * But we know *target* page is not freed/reused under us.
2063 * mem_cgroup_uncharge_page() does all necessary checks.
2065 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2066 mem_cgroup_uncharge_page(target);
2068 * At migration, we may charge account against cgroup which has no tasks
2069 * So, rmdir()->pre_destroy() can be called while we do this charge.
2070 * In that case, we need to call pre_destroy() again. check it here.
2072 cgroup_release_and_wakeup_rmdir(&mem->css);
2076 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2077 * Calling hierarchical_reclaim is not enough because we should update
2078 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2079 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2080 * not from the memcg which this page would be charged to.
2081 * try_charge_swapin does all of these works properly.
2083 int mem_cgroup_shmem_charge_fallback(struct page *page,
2084 struct mm_struct *mm,
2087 struct mem_cgroup *mem = NULL;
2090 if (mem_cgroup_disabled())
2093 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2095 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
2100 static DEFINE_MUTEX(set_limit_mutex);
2102 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
2103 unsigned long long val)
2109 int children = mem_cgroup_count_children(memcg);
2110 u64 curusage, oldusage;
2113 * For keeping hierarchical_reclaim simple, how long we should retry
2114 * is depends on callers. We set our retry-count to be function
2115 * of # of children which we should visit in this loop.
2117 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2119 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2121 while (retry_count) {
2122 if (signal_pending(current)) {
2127 * Rather than hide all in some function, I do this in
2128 * open coded manner. You see what this really does.
2129 * We have to guarantee mem->res.limit < mem->memsw.limit.
2131 mutex_lock(&set_limit_mutex);
2132 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2133 if (memswlimit < val) {
2135 mutex_unlock(&set_limit_mutex);
2138 ret = res_counter_set_limit(&memcg->res, val);
2140 if (memswlimit == val)
2141 memcg->memsw_is_minimum = true;
2143 memcg->memsw_is_minimum = false;
2145 mutex_unlock(&set_limit_mutex);
2150 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2152 MEM_CGROUP_RECLAIM_SHRINK);
2153 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2154 /* Usage is reduced ? */
2155 if (curusage >= oldusage)
2158 oldusage = curusage;
2164 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2165 unsigned long long val)
2168 u64 memlimit, oldusage, curusage;
2169 int children = mem_cgroup_count_children(memcg);
2172 /* see mem_cgroup_resize_res_limit */
2173 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2174 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2175 while (retry_count) {
2176 if (signal_pending(current)) {
2181 * Rather than hide all in some function, I do this in
2182 * open coded manner. You see what this really does.
2183 * We have to guarantee mem->res.limit < mem->memsw.limit.
2185 mutex_lock(&set_limit_mutex);
2186 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2187 if (memlimit > val) {
2189 mutex_unlock(&set_limit_mutex);
2192 ret = res_counter_set_limit(&memcg->memsw, val);
2194 if (memlimit == val)
2195 memcg->memsw_is_minimum = true;
2197 memcg->memsw_is_minimum = false;
2199 mutex_unlock(&set_limit_mutex);
2204 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2205 MEM_CGROUP_RECLAIM_NOSWAP |
2206 MEM_CGROUP_RECLAIM_SHRINK);
2207 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2208 /* Usage is reduced ? */
2209 if (curusage >= oldusage)
2212 oldusage = curusage;
2217 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2218 gfp_t gfp_mask, int nid,
2221 unsigned long nr_reclaimed = 0;
2222 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2223 unsigned long reclaimed;
2225 struct mem_cgroup_tree_per_zone *mctz;
2226 unsigned long long excess;
2231 mctz = soft_limit_tree_node_zone(nid, zid);
2233 * This loop can run a while, specially if mem_cgroup's continuously
2234 * keep exceeding their soft limit and putting the system under
2241 mz = mem_cgroup_largest_soft_limit_node(mctz);
2245 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2247 MEM_CGROUP_RECLAIM_SOFT);
2248 nr_reclaimed += reclaimed;
2249 spin_lock(&mctz->lock);
2252 * If we failed to reclaim anything from this memory cgroup
2253 * it is time to move on to the next cgroup
2259 * Loop until we find yet another one.
2261 * By the time we get the soft_limit lock
2262 * again, someone might have aded the
2263 * group back on the RB tree. Iterate to
2264 * make sure we get a different mem.
2265 * mem_cgroup_largest_soft_limit_node returns
2266 * NULL if no other cgroup is present on
2270 __mem_cgroup_largest_soft_limit_node(mctz);
2271 if (next_mz == mz) {
2272 css_put(&next_mz->mem->css);
2274 } else /* next_mz == NULL or other memcg */
2278 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
2279 excess = res_counter_soft_limit_excess(&mz->mem->res);
2281 * One school of thought says that we should not add
2282 * back the node to the tree if reclaim returns 0.
2283 * But our reclaim could return 0, simply because due
2284 * to priority we are exposing a smaller subset of
2285 * memory to reclaim from. Consider this as a longer
2288 /* If excess == 0, no tree ops */
2289 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
2290 spin_unlock(&mctz->lock);
2291 css_put(&mz->mem->css);
2294 * Could not reclaim anything and there are no more
2295 * mem cgroups to try or we seem to be looping without
2296 * reclaiming anything.
2298 if (!nr_reclaimed &&
2300 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2302 } while (!nr_reclaimed);
2304 css_put(&next_mz->mem->css);
2305 return nr_reclaimed;
2309 * This routine traverse page_cgroup in given list and drop them all.
2310 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2312 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
2313 int node, int zid, enum lru_list lru)
2316 struct mem_cgroup_per_zone *mz;
2317 struct page_cgroup *pc, *busy;
2318 unsigned long flags, loop;
2319 struct list_head *list;
2322 zone = &NODE_DATA(node)->node_zones[zid];
2323 mz = mem_cgroup_zoneinfo(mem, node, zid);
2324 list = &mz->lists[lru];
2326 loop = MEM_CGROUP_ZSTAT(mz, lru);
2327 /* give some margin against EBUSY etc...*/
2332 spin_lock_irqsave(&zone->lru_lock, flags);
2333 if (list_empty(list)) {
2334 spin_unlock_irqrestore(&zone->lru_lock, flags);
2337 pc = list_entry(list->prev, struct page_cgroup, lru);
2339 list_move(&pc->lru, list);
2341 spin_unlock_irqrestore(&zone->lru_lock, flags);
2344 spin_unlock_irqrestore(&zone->lru_lock, flags);
2346 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
2350 if (ret == -EBUSY || ret == -EINVAL) {
2351 /* found lock contention or "pc" is obsolete. */
2358 if (!ret && !list_empty(list))
2364 * make mem_cgroup's charge to be 0 if there is no task.
2365 * This enables deleting this mem_cgroup.
2367 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
2370 int node, zid, shrink;
2371 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
2372 struct cgroup *cgrp = mem->css.cgroup;
2377 /* should free all ? */
2381 while (mem->res.usage > 0) {
2383 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
2386 if (signal_pending(current))
2388 /* This is for making all *used* pages to be on LRU. */
2389 lru_add_drain_all();
2391 for_each_node_state(node, N_HIGH_MEMORY) {
2392 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
2395 ret = mem_cgroup_force_empty_list(mem,
2404 /* it seems parent cgroup doesn't have enough mem */
2415 /* returns EBUSY if there is a task or if we come here twice. */
2416 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
2420 /* we call try-to-free pages for make this cgroup empty */
2421 lru_add_drain_all();
2422 /* try to free all pages in this cgroup */
2424 while (nr_retries && mem->res.usage > 0) {
2427 if (signal_pending(current)) {
2431 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2432 false, get_swappiness(mem));
2435 /* maybe some writeback is necessary */
2436 congestion_wait(BLK_RW_ASYNC, HZ/10);
2441 /* try move_account...there may be some *locked* pages. */
2448 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2450 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2454 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2456 return mem_cgroup_from_cont(cont)->use_hierarchy;
2459 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2463 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2464 struct cgroup *parent = cont->parent;
2465 struct mem_cgroup *parent_mem = NULL;
2468 parent_mem = mem_cgroup_from_cont(parent);
2472 * If parent's use_hierarchy is set, we can't make any modifications
2473 * in the child subtrees. If it is unset, then the change can
2474 * occur, provided the current cgroup has no children.
2476 * For the root cgroup, parent_mem is NULL, we allow value to be
2477 * set if there are no children.
2479 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2480 (val == 1 || val == 0)) {
2481 if (list_empty(&cont->children))
2482 mem->use_hierarchy = val;
2492 struct mem_cgroup_idx_data {
2494 enum mem_cgroup_stat_index idx;
2498 mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2500 struct mem_cgroup_idx_data *d = data;
2501 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2506 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2507 enum mem_cgroup_stat_index idx, s64 *val)
2509 struct mem_cgroup_idx_data d;
2512 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2516 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2518 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2522 type = MEMFILE_TYPE(cft->private);
2523 name = MEMFILE_ATTR(cft->private);
2526 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2527 mem_cgroup_get_recursive_idx_stat(mem,
2528 MEM_CGROUP_STAT_CACHE, &idx_val);
2530 mem_cgroup_get_recursive_idx_stat(mem,
2531 MEM_CGROUP_STAT_RSS, &idx_val);
2535 val = res_counter_read_u64(&mem->res, name);
2538 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2539 mem_cgroup_get_recursive_idx_stat(mem,
2540 MEM_CGROUP_STAT_CACHE, &idx_val);
2542 mem_cgroup_get_recursive_idx_stat(mem,
2543 MEM_CGROUP_STAT_RSS, &idx_val);
2545 mem_cgroup_get_recursive_idx_stat(mem,
2546 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2549 val = res_counter_read_u64(&mem->memsw, name);
2558 * The user of this function is...
2561 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2564 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2566 unsigned long long val;
2569 type = MEMFILE_TYPE(cft->private);
2570 name = MEMFILE_ATTR(cft->private);
2573 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2577 /* This function does all necessary parse...reuse it */
2578 ret = res_counter_memparse_write_strategy(buffer, &val);
2582 ret = mem_cgroup_resize_limit(memcg, val);
2584 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2586 case RES_SOFT_LIMIT:
2587 ret = res_counter_memparse_write_strategy(buffer, &val);
2591 * For memsw, soft limits are hard to implement in terms
2592 * of semantics, for now, we support soft limits for
2593 * control without swap
2596 ret = res_counter_set_soft_limit(&memcg->res, val);
2601 ret = -EINVAL; /* should be BUG() ? */
2607 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2608 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2610 struct cgroup *cgroup;
2611 unsigned long long min_limit, min_memsw_limit, tmp;
2613 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2614 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2615 cgroup = memcg->css.cgroup;
2616 if (!memcg->use_hierarchy)
2619 while (cgroup->parent) {
2620 cgroup = cgroup->parent;
2621 memcg = mem_cgroup_from_cont(cgroup);
2622 if (!memcg->use_hierarchy)
2624 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2625 min_limit = min(min_limit, tmp);
2626 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2627 min_memsw_limit = min(min_memsw_limit, tmp);
2630 *mem_limit = min_limit;
2631 *memsw_limit = min_memsw_limit;
2635 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2637 struct mem_cgroup *mem;
2640 mem = mem_cgroup_from_cont(cont);
2641 type = MEMFILE_TYPE(event);
2642 name = MEMFILE_ATTR(event);
2646 res_counter_reset_max(&mem->res);
2648 res_counter_reset_max(&mem->memsw);
2652 res_counter_reset_failcnt(&mem->res);
2654 res_counter_reset_failcnt(&mem->memsw);
2662 /* For read statistics */
2678 struct mcs_total_stat {
2679 s64 stat[NR_MCS_STAT];
2685 } memcg_stat_strings[NR_MCS_STAT] = {
2686 {"cache", "total_cache"},
2687 {"rss", "total_rss"},
2688 {"mapped_file", "total_mapped_file"},
2689 {"pgpgin", "total_pgpgin"},
2690 {"pgpgout", "total_pgpgout"},
2691 {"swap", "total_swap"},
2692 {"inactive_anon", "total_inactive_anon"},
2693 {"active_anon", "total_active_anon"},
2694 {"inactive_file", "total_inactive_file"},
2695 {"active_file", "total_active_file"},
2696 {"unevictable", "total_unevictable"}
2700 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2702 struct mcs_total_stat *s = data;
2706 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2707 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2708 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2709 s->stat[MCS_RSS] += val * PAGE_SIZE;
2710 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2711 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
2712 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2713 s->stat[MCS_PGPGIN] += val;
2714 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2715 s->stat[MCS_PGPGOUT] += val;
2716 if (do_swap_account) {
2717 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2718 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2722 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2723 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2724 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2725 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2726 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2727 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2728 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2729 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2730 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2731 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2736 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2738 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2741 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2742 struct cgroup_map_cb *cb)
2744 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2745 struct mcs_total_stat mystat;
2748 memset(&mystat, 0, sizeof(mystat));
2749 mem_cgroup_get_local_stat(mem_cont, &mystat);
2751 for (i = 0; i < NR_MCS_STAT; i++) {
2752 if (i == MCS_SWAP && !do_swap_account)
2754 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2757 /* Hierarchical information */
2759 unsigned long long limit, memsw_limit;
2760 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2761 cb->fill(cb, "hierarchical_memory_limit", limit);
2762 if (do_swap_account)
2763 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2766 memset(&mystat, 0, sizeof(mystat));
2767 mem_cgroup_get_total_stat(mem_cont, &mystat);
2768 for (i = 0; i < NR_MCS_STAT; i++) {
2769 if (i == MCS_SWAP && !do_swap_account)
2771 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2774 #ifdef CONFIG_DEBUG_VM
2775 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2779 struct mem_cgroup_per_zone *mz;
2780 unsigned long recent_rotated[2] = {0, 0};
2781 unsigned long recent_scanned[2] = {0, 0};
2783 for_each_online_node(nid)
2784 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2785 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2787 recent_rotated[0] +=
2788 mz->reclaim_stat.recent_rotated[0];
2789 recent_rotated[1] +=
2790 mz->reclaim_stat.recent_rotated[1];
2791 recent_scanned[0] +=
2792 mz->reclaim_stat.recent_scanned[0];
2793 recent_scanned[1] +=
2794 mz->reclaim_stat.recent_scanned[1];
2796 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2797 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2798 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2799 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2806 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2808 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2810 return get_swappiness(memcg);
2813 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2816 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2817 struct mem_cgroup *parent;
2822 if (cgrp->parent == NULL)
2825 parent = mem_cgroup_from_cont(cgrp->parent);
2829 /* If under hierarchy, only empty-root can set this value */
2830 if ((parent->use_hierarchy) ||
2831 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2836 spin_lock(&memcg->reclaim_param_lock);
2837 memcg->swappiness = val;
2838 spin_unlock(&memcg->reclaim_param_lock);
2846 static struct cftype mem_cgroup_files[] = {
2848 .name = "usage_in_bytes",
2849 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2850 .read_u64 = mem_cgroup_read,
2853 .name = "max_usage_in_bytes",
2854 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2855 .trigger = mem_cgroup_reset,
2856 .read_u64 = mem_cgroup_read,
2859 .name = "limit_in_bytes",
2860 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2861 .write_string = mem_cgroup_write,
2862 .read_u64 = mem_cgroup_read,
2865 .name = "soft_limit_in_bytes",
2866 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2867 .write_string = mem_cgroup_write,
2868 .read_u64 = mem_cgroup_read,
2872 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2873 .trigger = mem_cgroup_reset,
2874 .read_u64 = mem_cgroup_read,
2878 .read_map = mem_control_stat_show,
2881 .name = "force_empty",
2882 .trigger = mem_cgroup_force_empty_write,
2885 .name = "use_hierarchy",
2886 .write_u64 = mem_cgroup_hierarchy_write,
2887 .read_u64 = mem_cgroup_hierarchy_read,
2890 .name = "swappiness",
2891 .read_u64 = mem_cgroup_swappiness_read,
2892 .write_u64 = mem_cgroup_swappiness_write,
2896 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2897 static struct cftype memsw_cgroup_files[] = {
2899 .name = "memsw.usage_in_bytes",
2900 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2901 .read_u64 = mem_cgroup_read,
2904 .name = "memsw.max_usage_in_bytes",
2905 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2906 .trigger = mem_cgroup_reset,
2907 .read_u64 = mem_cgroup_read,
2910 .name = "memsw.limit_in_bytes",
2911 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2912 .write_string = mem_cgroup_write,
2913 .read_u64 = mem_cgroup_read,
2916 .name = "memsw.failcnt",
2917 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2918 .trigger = mem_cgroup_reset,
2919 .read_u64 = mem_cgroup_read,
2923 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2925 if (!do_swap_account)
2927 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2928 ARRAY_SIZE(memsw_cgroup_files));
2931 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2937 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2939 struct mem_cgroup_per_node *pn;
2940 struct mem_cgroup_per_zone *mz;
2942 int zone, tmp = node;
2944 * This routine is called against possible nodes.
2945 * But it's BUG to call kmalloc() against offline node.
2947 * TODO: this routine can waste much memory for nodes which will
2948 * never be onlined. It's better to use memory hotplug callback
2951 if (!node_state(node, N_NORMAL_MEMORY))
2953 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
2957 mem->info.nodeinfo[node] = pn;
2958 memset(pn, 0, sizeof(*pn));
2960 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2961 mz = &pn->zoneinfo[zone];
2963 INIT_LIST_HEAD(&mz->lists[l]);
2964 mz->usage_in_excess = 0;
2965 mz->on_tree = false;
2971 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2973 kfree(mem->info.nodeinfo[node]);
2976 static int mem_cgroup_size(void)
2978 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2979 return sizeof(struct mem_cgroup) + cpustat_size;
2982 static struct mem_cgroup *mem_cgroup_alloc(void)
2984 struct mem_cgroup *mem;
2985 int size = mem_cgroup_size();
2987 if (size < PAGE_SIZE)
2988 mem = kmalloc(size, GFP_KERNEL);
2990 mem = vmalloc(size);
2993 memset(mem, 0, size);
2998 * At destroying mem_cgroup, references from swap_cgroup can remain.
2999 * (scanning all at force_empty is too costly...)
3001 * Instead of clearing all references at force_empty, we remember
3002 * the number of reference from swap_cgroup and free mem_cgroup when
3003 * it goes down to 0.
3005 * Removal of cgroup itself succeeds regardless of refs from swap.
3008 static void __mem_cgroup_free(struct mem_cgroup *mem)
3012 mem_cgroup_remove_from_trees(mem);
3013 free_css_id(&mem_cgroup_subsys, &mem->css);
3015 for_each_node_state(node, N_POSSIBLE)
3016 free_mem_cgroup_per_zone_info(mem, node);
3018 if (mem_cgroup_size() < PAGE_SIZE)
3024 static void mem_cgroup_get(struct mem_cgroup *mem)
3026 atomic_inc(&mem->refcnt);
3029 static void mem_cgroup_put(struct mem_cgroup *mem)
3031 if (atomic_dec_and_test(&mem->refcnt)) {
3032 struct mem_cgroup *parent = parent_mem_cgroup(mem);
3033 __mem_cgroup_free(mem);
3035 mem_cgroup_put(parent);
3040 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3042 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3044 if (!mem->res.parent)
3046 return mem_cgroup_from_res_counter(mem->res.parent, res);
3049 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3050 static void __init enable_swap_cgroup(void)
3052 if (!mem_cgroup_disabled() && really_do_swap_account)
3053 do_swap_account = 1;
3056 static void __init enable_swap_cgroup(void)
3061 static int mem_cgroup_soft_limit_tree_init(void)
3063 struct mem_cgroup_tree_per_node *rtpn;
3064 struct mem_cgroup_tree_per_zone *rtpz;
3065 int tmp, node, zone;
3067 for_each_node_state(node, N_POSSIBLE) {
3069 if (!node_state(node, N_NORMAL_MEMORY))
3071 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3075 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3077 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3078 rtpz = &rtpn->rb_tree_per_zone[zone];
3079 rtpz->rb_root = RB_ROOT;
3080 spin_lock_init(&rtpz->lock);
3086 static struct cgroup_subsys_state * __ref
3087 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3089 struct mem_cgroup *mem, *parent;
3090 long error = -ENOMEM;
3093 mem = mem_cgroup_alloc();
3095 return ERR_PTR(error);
3097 for_each_node_state(node, N_POSSIBLE)
3098 if (alloc_mem_cgroup_per_zone_info(mem, node))
3102 if (cont->parent == NULL) {
3103 enable_swap_cgroup();
3105 root_mem_cgroup = mem;
3106 if (mem_cgroup_soft_limit_tree_init())
3110 parent = mem_cgroup_from_cont(cont->parent);
3111 mem->use_hierarchy = parent->use_hierarchy;
3114 if (parent && parent->use_hierarchy) {
3115 res_counter_init(&mem->res, &parent->res);
3116 res_counter_init(&mem->memsw, &parent->memsw);
3118 * We increment refcnt of the parent to ensure that we can
3119 * safely access it on res_counter_charge/uncharge.
3120 * This refcnt will be decremented when freeing this
3121 * mem_cgroup(see mem_cgroup_put).
3123 mem_cgroup_get(parent);
3125 res_counter_init(&mem->res, NULL);
3126 res_counter_init(&mem->memsw, NULL);
3128 mem->last_scanned_child = 0;
3129 spin_lock_init(&mem->reclaim_param_lock);
3132 mem->swappiness = get_swappiness(parent);
3133 atomic_set(&mem->refcnt, 1);
3136 __mem_cgroup_free(mem);
3137 root_mem_cgroup = NULL;
3138 return ERR_PTR(error);
3141 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
3142 struct cgroup *cont)
3144 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3146 return mem_cgroup_force_empty(mem, false);
3149 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3150 struct cgroup *cont)
3152 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3154 mem_cgroup_put(mem);
3157 static int mem_cgroup_populate(struct cgroup_subsys *ss,
3158 struct cgroup *cont)
3162 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3163 ARRAY_SIZE(mem_cgroup_files));
3166 ret = register_memsw_files(cont, ss);
3170 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3171 struct cgroup *cont,
3172 struct cgroup *old_cont,
3173 struct task_struct *p,
3176 mutex_lock(&memcg_tasklist);
3178 * FIXME: It's better to move charges of this process from old
3179 * memcg to new memcg. But it's just on TODO-List now.
3181 mutex_unlock(&memcg_tasklist);
3184 struct cgroup_subsys mem_cgroup_subsys = {
3186 .subsys_id = mem_cgroup_subsys_id,
3187 .create = mem_cgroup_create,
3188 .pre_destroy = mem_cgroup_pre_destroy,
3189 .destroy = mem_cgroup_destroy,
3190 .populate = mem_cgroup_populate,
3191 .attach = mem_cgroup_move_task,
3196 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3198 static int __init disable_swap_account(char *s)
3200 really_do_swap_account = 0;
3203 __setup("noswapaccount", disable_swap_account);