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 static struct mem_cgroup_per_zone *
286 page_cgroup_zoneinfo(struct page_cgroup *pc)
288 struct mem_cgroup *mem = pc->mem_cgroup;
289 int nid = page_cgroup_nid(pc);
290 int zid = page_cgroup_zid(pc);
295 return mem_cgroup_zoneinfo(mem, nid, zid);
298 static struct mem_cgroup_tree_per_zone *
299 soft_limit_tree_node_zone(int nid, int zid)
301 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
304 static struct mem_cgroup_tree_per_zone *
305 soft_limit_tree_from_page(struct page *page)
307 int nid = page_to_nid(page);
308 int zid = page_zonenum(page);
310 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
314 __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
315 struct mem_cgroup_per_zone *mz,
316 struct mem_cgroup_tree_per_zone *mctz,
317 unsigned long long new_usage_in_excess)
319 struct rb_node **p = &mctz->rb_root.rb_node;
320 struct rb_node *parent = NULL;
321 struct mem_cgroup_per_zone *mz_node;
326 mz->usage_in_excess = new_usage_in_excess;
327 if (!mz->usage_in_excess)
331 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
333 if (mz->usage_in_excess < mz_node->usage_in_excess)
336 * We can't avoid mem cgroups that are over their soft
337 * limit by the same amount
339 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
342 rb_link_node(&mz->tree_node, parent, p);
343 rb_insert_color(&mz->tree_node, &mctz->rb_root);
348 __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
349 struct mem_cgroup_per_zone *mz,
350 struct mem_cgroup_tree_per_zone *mctz)
354 rb_erase(&mz->tree_node, &mctz->rb_root);
359 mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
360 struct mem_cgroup_per_zone *mz,
361 struct mem_cgroup_tree_per_zone *mctz)
363 spin_lock(&mctz->lock);
364 __mem_cgroup_remove_exceeded(mem, mz, mctz);
365 spin_unlock(&mctz->lock);
368 static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
373 struct mem_cgroup_stat_cpu *cpustat;
376 cpustat = &mem->stat.cpustat[cpu];
377 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
378 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
379 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
386 static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
388 unsigned long long excess;
389 struct mem_cgroup_per_zone *mz;
390 struct mem_cgroup_tree_per_zone *mctz;
391 int nid = page_to_nid(page);
392 int zid = page_zonenum(page);
393 mctz = soft_limit_tree_from_page(page);
396 * Necessary to update all ancestors when hierarchy is used.
397 * because their event counter is not touched.
399 for (; mem; mem = parent_mem_cgroup(mem)) {
400 mz = mem_cgroup_zoneinfo(mem, nid, zid);
401 excess = res_counter_soft_limit_excess(&mem->res);
403 * We have to update the tree if mz is on RB-tree or
404 * mem is over its softlimit.
406 if (excess || mz->on_tree) {
407 spin_lock(&mctz->lock);
408 /* if on-tree, remove it */
410 __mem_cgroup_remove_exceeded(mem, mz, mctz);
412 * Insert again. mz->usage_in_excess will be updated.
413 * If excess is 0, no tree ops.
415 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
416 spin_unlock(&mctz->lock);
421 static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
424 struct mem_cgroup_per_zone *mz;
425 struct mem_cgroup_tree_per_zone *mctz;
427 for_each_node_state(node, N_POSSIBLE) {
428 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
429 mz = mem_cgroup_zoneinfo(mem, node, zone);
430 mctz = soft_limit_tree_node_zone(node, zone);
431 mem_cgroup_remove_exceeded(mem, mz, mctz);
436 static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
438 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
441 static struct mem_cgroup_per_zone *
442 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
444 struct rb_node *rightmost = NULL;
445 struct mem_cgroup_per_zone *mz;
449 rightmost = rb_last(&mctz->rb_root);
451 goto done; /* Nothing to reclaim from */
453 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
455 * Remove the node now but someone else can add it back,
456 * we will to add it back at the end of reclaim to its correct
457 * position in the tree.
459 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
460 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
461 !css_tryget(&mz->mem->css))
467 static struct mem_cgroup_per_zone *
468 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
470 struct mem_cgroup_per_zone *mz;
472 spin_lock(&mctz->lock);
473 mz = __mem_cgroup_largest_soft_limit_node(mctz);
474 spin_unlock(&mctz->lock);
478 static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
481 int val = (charge) ? 1 : -1;
482 struct mem_cgroup_stat *stat = &mem->stat;
483 struct mem_cgroup_stat_cpu *cpustat;
486 cpustat = &stat->cpustat[cpu];
487 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
491 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
492 struct page_cgroup *pc,
495 int val = (charge) ? 1 : -1;
496 struct mem_cgroup_stat *stat = &mem->stat;
497 struct mem_cgroup_stat_cpu *cpustat;
500 cpustat = &stat->cpustat[cpu];
501 if (PageCgroupCache(pc))
502 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
504 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
507 __mem_cgroup_stat_add_safe(cpustat,
508 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
510 __mem_cgroup_stat_add_safe(cpustat,
511 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
512 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
516 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
520 struct mem_cgroup_per_zone *mz;
523 for_each_online_node(nid)
524 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
525 mz = mem_cgroup_zoneinfo(mem, nid, zid);
526 total += MEM_CGROUP_ZSTAT(mz, idx);
531 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
533 return container_of(cgroup_subsys_state(cont,
534 mem_cgroup_subsys_id), struct mem_cgroup,
538 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
541 * mm_update_next_owner() may clear mm->owner to NULL
542 * if it races with swapoff, page migration, etc.
543 * So this can be called with p == NULL.
548 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
549 struct mem_cgroup, css);
552 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
554 struct mem_cgroup *mem = NULL;
559 * Because we have no locks, mm->owner's may be being moved to other
560 * cgroup. We use css_tryget() here even if this looks
561 * pessimistic (rather than adding locks here).
565 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
568 } while (!css_tryget(&mem->css));
574 * Call callback function against all cgroup under hierarchy tree.
576 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
577 int (*func)(struct mem_cgroup *, void *))
579 int found, ret, nextid;
580 struct cgroup_subsys_state *css;
581 struct mem_cgroup *mem;
583 if (!root->use_hierarchy)
584 return (*func)(root, data);
592 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
594 if (css && css_tryget(css))
595 mem = container_of(css, struct mem_cgroup, css);
599 ret = (*func)(mem, data);
603 } while (!ret && css);
608 static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
610 return (mem == root_mem_cgroup);
614 * Following LRU functions are allowed to be used without PCG_LOCK.
615 * Operations are called by routine of global LRU independently from memcg.
616 * What we have to take care of here is validness of pc->mem_cgroup.
618 * Changes to pc->mem_cgroup happens when
621 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
622 * It is added to LRU before charge.
623 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
624 * When moving account, the page is not on LRU. It's isolated.
627 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
629 struct page_cgroup *pc;
630 struct mem_cgroup_per_zone *mz;
632 if (mem_cgroup_disabled())
634 pc = lookup_page_cgroup(page);
635 /* can happen while we handle swapcache. */
636 if (!TestClearPageCgroupAcctLRU(pc))
638 VM_BUG_ON(!pc->mem_cgroup);
640 * We don't check PCG_USED bit. It's cleared when the "page" is finally
641 * removed from global LRU.
643 mz = page_cgroup_zoneinfo(pc);
644 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
645 if (mem_cgroup_is_root(pc->mem_cgroup))
647 VM_BUG_ON(list_empty(&pc->lru));
648 list_del_init(&pc->lru);
652 void mem_cgroup_del_lru(struct page *page)
654 mem_cgroup_del_lru_list(page, page_lru(page));
657 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
659 struct mem_cgroup_per_zone *mz;
660 struct page_cgroup *pc;
662 if (mem_cgroup_disabled())
665 pc = lookup_page_cgroup(page);
667 * Used bit is set without atomic ops but after smp_wmb().
668 * For making pc->mem_cgroup visible, insert smp_rmb() here.
671 /* unused or root page is not rotated. */
672 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
674 mz = page_cgroup_zoneinfo(pc);
675 list_move(&pc->lru, &mz->lists[lru]);
678 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
680 struct page_cgroup *pc;
681 struct mem_cgroup_per_zone *mz;
683 if (mem_cgroup_disabled())
685 pc = lookup_page_cgroup(page);
686 VM_BUG_ON(PageCgroupAcctLRU(pc));
688 * Used bit is set without atomic ops but after smp_wmb().
689 * For making pc->mem_cgroup visible, insert smp_rmb() here.
692 if (!PageCgroupUsed(pc))
695 mz = page_cgroup_zoneinfo(pc);
696 MEM_CGROUP_ZSTAT(mz, lru) += 1;
697 SetPageCgroupAcctLRU(pc);
698 if (mem_cgroup_is_root(pc->mem_cgroup))
700 list_add(&pc->lru, &mz->lists[lru]);
704 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
705 * lru because the page may.be reused after it's fully uncharged (because of
706 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
707 * it again. This function is only used to charge SwapCache. It's done under
708 * lock_page and expected that zone->lru_lock is never held.
710 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
713 struct zone *zone = page_zone(page);
714 struct page_cgroup *pc = lookup_page_cgroup(page);
716 spin_lock_irqsave(&zone->lru_lock, flags);
718 * Forget old LRU when this page_cgroup is *not* used. This Used bit
719 * is guarded by lock_page() because the page is SwapCache.
721 if (!PageCgroupUsed(pc))
722 mem_cgroup_del_lru_list(page, page_lru(page));
723 spin_unlock_irqrestore(&zone->lru_lock, flags);
726 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
729 struct zone *zone = page_zone(page);
730 struct page_cgroup *pc = lookup_page_cgroup(page);
732 spin_lock_irqsave(&zone->lru_lock, flags);
733 /* link when the page is linked to LRU but page_cgroup isn't */
734 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
735 mem_cgroup_add_lru_list(page, page_lru(page));
736 spin_unlock_irqrestore(&zone->lru_lock, flags);
740 void mem_cgroup_move_lists(struct page *page,
741 enum lru_list from, enum lru_list to)
743 if (mem_cgroup_disabled())
745 mem_cgroup_del_lru_list(page, from);
746 mem_cgroup_add_lru_list(page, to);
749 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
752 struct mem_cgroup *curr = NULL;
756 curr = try_get_mem_cgroup_from_mm(task->mm);
761 if (curr->use_hierarchy)
762 ret = css_is_ancestor(&curr->css, &mem->css);
770 * prev_priority control...this will be used in memory reclaim path.
772 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
776 spin_lock(&mem->reclaim_param_lock);
777 prev_priority = mem->prev_priority;
778 spin_unlock(&mem->reclaim_param_lock);
780 return prev_priority;
783 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
785 spin_lock(&mem->reclaim_param_lock);
786 if (priority < mem->prev_priority)
787 mem->prev_priority = priority;
788 spin_unlock(&mem->reclaim_param_lock);
791 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
793 spin_lock(&mem->reclaim_param_lock);
794 mem->prev_priority = priority;
795 spin_unlock(&mem->reclaim_param_lock);
798 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
800 unsigned long active;
801 unsigned long inactive;
803 unsigned long inactive_ratio;
805 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
806 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
808 gb = (inactive + active) >> (30 - PAGE_SHIFT);
810 inactive_ratio = int_sqrt(10 * gb);
815 present_pages[0] = inactive;
816 present_pages[1] = active;
819 return inactive_ratio;
822 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
824 unsigned long active;
825 unsigned long inactive;
826 unsigned long present_pages[2];
827 unsigned long inactive_ratio;
829 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
831 inactive = present_pages[0];
832 active = present_pages[1];
834 if (inactive * inactive_ratio < active)
840 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
842 unsigned long active;
843 unsigned long inactive;
845 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
846 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
848 return (active > inactive);
851 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
855 int nid = zone->zone_pgdat->node_id;
856 int zid = zone_idx(zone);
857 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
859 return MEM_CGROUP_ZSTAT(mz, lru);
862 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
865 int nid = zone->zone_pgdat->node_id;
866 int zid = zone_idx(zone);
867 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
869 return &mz->reclaim_stat;
872 struct zone_reclaim_stat *
873 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
875 struct page_cgroup *pc;
876 struct mem_cgroup_per_zone *mz;
878 if (mem_cgroup_disabled())
881 pc = lookup_page_cgroup(page);
883 * Used bit is set without atomic ops but after smp_wmb().
884 * For making pc->mem_cgroup visible, insert smp_rmb() here.
887 if (!PageCgroupUsed(pc))
890 mz = page_cgroup_zoneinfo(pc);
894 return &mz->reclaim_stat;
897 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
898 struct list_head *dst,
899 unsigned long *scanned, int order,
900 int mode, struct zone *z,
901 struct mem_cgroup *mem_cont,
902 int active, int file)
904 unsigned long nr_taken = 0;
908 struct list_head *src;
909 struct page_cgroup *pc, *tmp;
910 int nid = z->zone_pgdat->node_id;
911 int zid = zone_idx(z);
912 struct mem_cgroup_per_zone *mz;
913 int lru = LRU_FILE * file + active;
917 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
918 src = &mz->lists[lru];
921 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
922 if (scan >= nr_to_scan)
926 if (unlikely(!PageCgroupUsed(pc)))
928 if (unlikely(!PageLRU(page)))
932 ret = __isolate_lru_page(page, mode, file);
935 list_move(&page->lru, dst);
936 mem_cgroup_del_lru(page);
940 /* we don't affect global LRU but rotate in our LRU */
941 mem_cgroup_rotate_lru_list(page, page_lru(page));
952 #define mem_cgroup_from_res_counter(counter, member) \
953 container_of(counter, struct mem_cgroup, member)
955 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
957 if (do_swap_account) {
958 if (res_counter_check_under_limit(&mem->res) &&
959 res_counter_check_under_limit(&mem->memsw))
962 if (res_counter_check_under_limit(&mem->res))
967 static unsigned int get_swappiness(struct mem_cgroup *memcg)
969 struct cgroup *cgrp = memcg->css.cgroup;
970 unsigned int swappiness;
973 if (cgrp->parent == NULL)
974 return vm_swappiness;
976 spin_lock(&memcg->reclaim_param_lock);
977 swappiness = memcg->swappiness;
978 spin_unlock(&memcg->reclaim_param_lock);
983 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
991 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
992 * @memcg: The memory cgroup that went over limit
993 * @p: Task that is going to be killed
995 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
998 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1000 struct cgroup *task_cgrp;
1001 struct cgroup *mem_cgrp;
1003 * Need a buffer in BSS, can't rely on allocations. The code relies
1004 * on the assumption that OOM is serialized for memory controller.
1005 * If this assumption is broken, revisit this code.
1007 static char memcg_name[PATH_MAX];
1016 mem_cgrp = memcg->css.cgroup;
1017 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1019 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1022 * Unfortunately, we are unable to convert to a useful name
1023 * But we'll still print out the usage information
1030 printk(KERN_INFO "Task in %s killed", memcg_name);
1033 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1041 * Continues from above, so we don't need an KERN_ level
1043 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1046 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1047 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1048 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1049 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1050 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1052 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1053 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1054 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1058 * This function returns the number of memcg under hierarchy tree. Returns
1059 * 1(self count) if no children.
1061 static int mem_cgroup_count_children(struct mem_cgroup *mem)
1064 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1069 * Visit the first child (need not be the first child as per the ordering
1070 * of the cgroup list, since we track last_scanned_child) of @mem and use
1071 * that to reclaim free pages from.
1073 static struct mem_cgroup *
1074 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1076 struct mem_cgroup *ret = NULL;
1077 struct cgroup_subsys_state *css;
1080 if (!root_mem->use_hierarchy) {
1081 css_get(&root_mem->css);
1087 nextid = root_mem->last_scanned_child + 1;
1088 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1090 if (css && css_tryget(css))
1091 ret = container_of(css, struct mem_cgroup, css);
1094 /* Updates scanning parameter */
1095 spin_lock(&root_mem->reclaim_param_lock);
1097 /* this means start scan from ID:1 */
1098 root_mem->last_scanned_child = 0;
1100 root_mem->last_scanned_child = found;
1101 spin_unlock(&root_mem->reclaim_param_lock);
1108 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1109 * we reclaimed from, so that we don't end up penalizing one child extensively
1110 * based on its position in the children list.
1112 * root_mem is the original ancestor that we've been reclaim from.
1114 * We give up and return to the caller when we visit root_mem twice.
1115 * (other groups can be removed while we're walking....)
1117 * If shrink==true, for avoiding to free too much, this returns immedieately.
1119 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1122 unsigned long reclaim_options)
1124 struct mem_cgroup *victim;
1127 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1128 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
1129 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1130 unsigned long excess = mem_cgroup_get_excess(root_mem);
1132 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1133 if (root_mem->memsw_is_minimum)
1137 victim = mem_cgroup_select_victim(root_mem);
1138 if (victim == root_mem) {
1142 * If we have not been able to reclaim
1143 * anything, it might because there are
1144 * no reclaimable pages under this hierarchy
1146 if (!check_soft || !total) {
1147 css_put(&victim->css);
1151 * We want to do more targetted reclaim.
1152 * excess >> 2 is not to excessive so as to
1153 * reclaim too much, nor too less that we keep
1154 * coming back to reclaim from this cgroup
1156 if (total >= (excess >> 2) ||
1157 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1158 css_put(&victim->css);
1163 if (!mem_cgroup_local_usage(&victim->stat)) {
1164 /* this cgroup's local usage == 0 */
1165 css_put(&victim->css);
1168 /* we use swappiness of local cgroup */
1170 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1171 noswap, get_swappiness(victim), zone,
1172 zone->zone_pgdat->node_id);
1174 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1175 noswap, get_swappiness(victim));
1176 css_put(&victim->css);
1178 * At shrinking usage, we can't check we should stop here or
1179 * reclaim more. It's depends on callers. last_scanned_child
1180 * will work enough for keeping fairness under tree.
1186 if (res_counter_check_under_soft_limit(&root_mem->res))
1188 } else if (mem_cgroup_check_under_limit(root_mem))
1194 bool mem_cgroup_oom_called(struct task_struct *task)
1197 struct mem_cgroup *mem;
1198 struct mm_struct *mm;
1204 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1205 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1211 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1213 mem->last_oom_jiffies = jiffies;
1217 static void record_last_oom(struct mem_cgroup *mem)
1219 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1223 * Currently used to update mapped file statistics, but the routine can be
1224 * generalized to update other statistics as well.
1226 void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1228 struct mem_cgroup *mem;
1229 struct mem_cgroup_stat *stat;
1230 struct mem_cgroup_stat_cpu *cpustat;
1232 struct page_cgroup *pc;
1234 if (!page_is_file_cache(page))
1237 pc = lookup_page_cgroup(page);
1241 lock_page_cgroup(pc);
1242 mem = pc->mem_cgroup;
1246 if (!PageCgroupUsed(pc))
1250 * Preemption is already disabled, we don't need get_cpu()
1252 cpu = smp_processor_id();
1254 cpustat = &stat->cpustat[cpu];
1256 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1258 unlock_page_cgroup(pc);
1262 * Unlike exported interface, "oom" parameter is added. if oom==true,
1263 * oom-killer can be invoked.
1265 static int __mem_cgroup_try_charge(struct mm_struct *mm,
1266 gfp_t gfp_mask, struct mem_cgroup **memcg,
1267 bool oom, struct page *page)
1269 struct mem_cgroup *mem, *mem_over_limit;
1270 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1271 struct res_counter *fail_res;
1273 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1274 /* Don't account this! */
1280 * We always charge the cgroup the mm_struct belongs to.
1281 * The mm_struct's mem_cgroup changes on task migration if the
1282 * thread group leader migrates. It's possible that mm is not
1283 * set, if so charge the init_mm (happens for pagecache usage).
1287 mem = try_get_mem_cgroup_from_mm(mm);
1295 VM_BUG_ON(css_is_removed(&mem->css));
1299 unsigned long flags = 0;
1301 if (mem_cgroup_is_root(mem))
1303 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
1305 if (!do_swap_account)
1307 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
1311 /* mem+swap counter fails */
1312 res_counter_uncharge(&mem->res, PAGE_SIZE);
1313 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1314 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1317 /* mem counter fails */
1318 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1321 if (!(gfp_mask & __GFP_WAIT))
1324 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1330 * try_to_free_mem_cgroup_pages() might not give us a full
1331 * picture of reclaim. Some pages are reclaimed and might be
1332 * moved to swap cache or just unmapped from the cgroup.
1333 * Check the limit again to see if the reclaim reduced the
1334 * current usage of the cgroup before giving up
1337 if (mem_cgroup_check_under_limit(mem_over_limit))
1340 if (!nr_retries--) {
1342 mutex_lock(&memcg_tasklist);
1343 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1344 mutex_unlock(&memcg_tasklist);
1345 record_last_oom(mem_over_limit);
1351 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1352 * if they exceeds softlimit.
1354 if (mem_cgroup_soft_limit_check(mem))
1355 mem_cgroup_update_tree(mem, page);
1364 * A helper function to get mem_cgroup from ID. must be called under
1365 * rcu_read_lock(). The caller must check css_is_removed() or some if
1366 * it's concern. (dropping refcnt from swap can be called against removed
1369 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1371 struct cgroup_subsys_state *css;
1373 /* ID 0 is unused ID */
1376 css = css_lookup(&mem_cgroup_subsys, id);
1379 return container_of(css, struct mem_cgroup, css);
1382 struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
1384 struct mem_cgroup *mem = NULL;
1385 struct page_cgroup *pc;
1389 VM_BUG_ON(!PageLocked(page));
1391 pc = lookup_page_cgroup(page);
1392 lock_page_cgroup(pc);
1393 if (PageCgroupUsed(pc)) {
1394 mem = pc->mem_cgroup;
1395 if (mem && !css_tryget(&mem->css))
1397 } else if (PageSwapCache(page)) {
1398 ent.val = page_private(page);
1399 id = lookup_swap_cgroup(ent);
1401 mem = mem_cgroup_lookup(id);
1402 if (mem && !css_tryget(&mem->css))
1406 unlock_page_cgroup(pc);
1411 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1412 * USED state. If already USED, uncharge and return.
1415 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1416 struct page_cgroup *pc,
1417 enum charge_type ctype)
1419 /* try_charge() can return NULL to *memcg, taking care of it. */
1423 lock_page_cgroup(pc);
1424 if (unlikely(PageCgroupUsed(pc))) {
1425 unlock_page_cgroup(pc);
1426 if (!mem_cgroup_is_root(mem)) {
1427 res_counter_uncharge(&mem->res, PAGE_SIZE);
1428 if (do_swap_account)
1429 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1435 pc->mem_cgroup = mem;
1437 * We access a page_cgroup asynchronously without lock_page_cgroup().
1438 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1439 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1440 * before USED bit, we need memory barrier here.
1441 * See mem_cgroup_add_lru_list(), etc.
1445 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1446 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1447 SetPageCgroupCache(pc);
1448 SetPageCgroupUsed(pc);
1450 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1451 ClearPageCgroupCache(pc);
1452 SetPageCgroupUsed(pc);
1458 mem_cgroup_charge_statistics(mem, pc, true);
1460 unlock_page_cgroup(pc);
1464 * mem_cgroup_move_account - move account of the page
1465 * @pc: page_cgroup of the page.
1466 * @from: mem_cgroup which the page is moved from.
1467 * @to: mem_cgroup which the page is moved to. @from != @to.
1469 * The caller must confirm following.
1470 * - page is not on LRU (isolate_page() is useful.)
1472 * returns 0 at success,
1473 * returns -EBUSY when lock is busy or "pc" is unstable.
1475 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1476 * new cgroup. It should be done by a caller.
1479 static int mem_cgroup_move_account(struct page_cgroup *pc,
1480 struct mem_cgroup *from, struct mem_cgroup *to)
1482 struct mem_cgroup_per_zone *from_mz, *to_mz;
1487 struct mem_cgroup_stat *stat;
1488 struct mem_cgroup_stat_cpu *cpustat;
1490 VM_BUG_ON(from == to);
1491 VM_BUG_ON(PageLRU(pc->page));
1493 nid = page_cgroup_nid(pc);
1494 zid = page_cgroup_zid(pc);
1495 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1496 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1498 if (!trylock_page_cgroup(pc))
1501 if (!PageCgroupUsed(pc))
1504 if (pc->mem_cgroup != from)
1507 if (!mem_cgroup_is_root(from))
1508 res_counter_uncharge(&from->res, PAGE_SIZE);
1509 mem_cgroup_charge_statistics(from, pc, false);
1512 if (page_is_file_cache(page) && page_mapped(page)) {
1513 cpu = smp_processor_id();
1514 /* Update mapped_file data for mem_cgroup "from" */
1516 cpustat = &stat->cpustat[cpu];
1517 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1520 /* Update mapped_file data for mem_cgroup "to" */
1522 cpustat = &stat->cpustat[cpu];
1523 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1527 if (do_swap_account && !mem_cgroup_is_root(from))
1528 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1529 css_put(&from->css);
1532 pc->mem_cgroup = to;
1533 mem_cgroup_charge_statistics(to, pc, true);
1536 unlock_page_cgroup(pc);
1538 * We charges against "to" which may not have any tasks. Then, "to"
1539 * can be under rmdir(). But in current implementation, caller of
1540 * this function is just force_empty() and it's garanteed that
1541 * "to" is never removed. So, we don't check rmdir status here.
1547 * move charges to its parent.
1550 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1551 struct mem_cgroup *child,
1554 struct page *page = pc->page;
1555 struct cgroup *cg = child->css.cgroup;
1556 struct cgroup *pcg = cg->parent;
1557 struct mem_cgroup *parent;
1565 parent = mem_cgroup_from_cont(pcg);
1568 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
1572 if (!get_page_unless_zero(page)) {
1577 ret = isolate_lru_page(page);
1582 ret = mem_cgroup_move_account(pc, child, parent);
1584 putback_lru_page(page);
1587 /* drop extra refcnt by try_charge() */
1588 css_put(&parent->css);
1595 /* drop extra refcnt by try_charge() */
1596 css_put(&parent->css);
1597 /* uncharge if move fails */
1598 if (!mem_cgroup_is_root(parent)) {
1599 res_counter_uncharge(&parent->res, PAGE_SIZE);
1600 if (do_swap_account)
1601 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
1607 * Charge the memory controller for page usage.
1609 * 0 if the charge was successful
1610 * < 0 if the cgroup is over its limit
1612 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1613 gfp_t gfp_mask, enum charge_type ctype,
1614 struct mem_cgroup *memcg)
1616 struct mem_cgroup *mem;
1617 struct page_cgroup *pc;
1620 pc = lookup_page_cgroup(page);
1621 /* can happen at boot */
1627 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
1631 __mem_cgroup_commit_charge(mem, pc, ctype);
1635 int mem_cgroup_newpage_charge(struct page *page,
1636 struct mm_struct *mm, gfp_t gfp_mask)
1638 if (mem_cgroup_disabled())
1640 if (PageCompound(page))
1643 * If already mapped, we don't have to account.
1644 * If page cache, page->mapping has address_space.
1645 * But page->mapping may have out-of-use anon_vma pointer,
1646 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1649 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1653 return mem_cgroup_charge_common(page, mm, gfp_mask,
1654 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1658 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1659 enum charge_type ctype);
1661 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1664 struct mem_cgroup *mem = NULL;
1667 if (mem_cgroup_disabled())
1669 if (PageCompound(page))
1672 * Corner case handling. This is called from add_to_page_cache()
1673 * in usual. But some FS (shmem) precharges this page before calling it
1674 * and call add_to_page_cache() with GFP_NOWAIT.
1676 * For GFP_NOWAIT case, the page may be pre-charged before calling
1677 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1678 * charge twice. (It works but has to pay a bit larger cost.)
1679 * And when the page is SwapCache, it should take swap information
1680 * into account. This is under lock_page() now.
1682 if (!(gfp_mask & __GFP_WAIT)) {
1683 struct page_cgroup *pc;
1686 pc = lookup_page_cgroup(page);
1689 lock_page_cgroup(pc);
1690 if (PageCgroupUsed(pc)) {
1691 unlock_page_cgroup(pc);
1694 unlock_page_cgroup(pc);
1697 if (unlikely(!mm && !mem))
1700 if (page_is_file_cache(page))
1701 return mem_cgroup_charge_common(page, mm, gfp_mask,
1702 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1705 if (PageSwapCache(page)) {
1706 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1708 __mem_cgroup_commit_charge_swapin(page, mem,
1709 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1711 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1712 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1718 * While swap-in, try_charge -> commit or cancel, the page is locked.
1719 * And when try_charge() successfully returns, one refcnt to memcg without
1720 * struct page_cgroup is acquired. This refcnt will be consumed by
1721 * "commit()" or removed by "cancel()"
1723 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1725 gfp_t mask, struct mem_cgroup **ptr)
1727 struct mem_cgroup *mem;
1730 if (mem_cgroup_disabled())
1733 if (!do_swap_account)
1736 * A racing thread's fault, or swapoff, may have already updated
1737 * the pte, and even removed page from swap cache: in those cases
1738 * do_swap_page()'s pte_same() test will fail; but there's also a
1739 * KSM case which does need to charge the page.
1741 if (!PageSwapCache(page))
1743 mem = try_get_mem_cgroup_from_page(page);
1747 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
1748 /* drop extra refcnt from tryget */
1754 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
1758 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1759 enum charge_type ctype)
1761 struct page_cgroup *pc;
1763 if (mem_cgroup_disabled())
1767 cgroup_exclude_rmdir(&ptr->css);
1768 pc = lookup_page_cgroup(page);
1769 mem_cgroup_lru_del_before_commit_swapcache(page);
1770 __mem_cgroup_commit_charge(ptr, pc, ctype);
1771 mem_cgroup_lru_add_after_commit_swapcache(page);
1773 * Now swap is on-memory. This means this page may be
1774 * counted both as mem and swap....double count.
1775 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1776 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1777 * may call delete_from_swap_cache() before reach here.
1779 if (do_swap_account && PageSwapCache(page)) {
1780 swp_entry_t ent = {.val = page_private(page)};
1782 struct mem_cgroup *memcg;
1784 id = swap_cgroup_record(ent, 0);
1786 memcg = mem_cgroup_lookup(id);
1789 * This recorded memcg can be obsolete one. So, avoid
1790 * calling css_tryget
1792 if (!mem_cgroup_is_root(memcg))
1793 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1794 mem_cgroup_swap_statistics(memcg, false);
1795 mem_cgroup_put(memcg);
1800 * At swapin, we may charge account against cgroup which has no tasks.
1801 * So, rmdir()->pre_destroy() can be called while we do this charge.
1802 * In that case, we need to call pre_destroy() again. check it here.
1804 cgroup_release_and_wakeup_rmdir(&ptr->css);
1807 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1809 __mem_cgroup_commit_charge_swapin(page, ptr,
1810 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1813 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1815 if (mem_cgroup_disabled())
1819 if (!mem_cgroup_is_root(mem)) {
1820 res_counter_uncharge(&mem->res, PAGE_SIZE);
1821 if (do_swap_account)
1822 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1829 * uncharge if !page_mapped(page)
1831 static struct mem_cgroup *
1832 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1834 struct page_cgroup *pc;
1835 struct mem_cgroup *mem = NULL;
1836 struct mem_cgroup_per_zone *mz;
1838 if (mem_cgroup_disabled())
1841 if (PageSwapCache(page))
1845 * Check if our page_cgroup is valid
1847 pc = lookup_page_cgroup(page);
1848 if (unlikely(!pc || !PageCgroupUsed(pc)))
1851 lock_page_cgroup(pc);
1853 mem = pc->mem_cgroup;
1855 if (!PageCgroupUsed(pc))
1859 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1860 case MEM_CGROUP_CHARGE_TYPE_DROP:
1861 if (page_mapped(page))
1864 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1865 if (!PageAnon(page)) { /* Shared memory */
1866 if (page->mapping && !page_is_file_cache(page))
1868 } else if (page_mapped(page)) /* Anon */
1875 if (!mem_cgroup_is_root(mem)) {
1876 res_counter_uncharge(&mem->res, PAGE_SIZE);
1877 if (do_swap_account &&
1878 (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1879 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1881 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1882 mem_cgroup_swap_statistics(mem, true);
1883 mem_cgroup_charge_statistics(mem, pc, false);
1885 ClearPageCgroupUsed(pc);
1887 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1888 * freed from LRU. This is safe because uncharged page is expected not
1889 * to be reused (freed soon). Exception is SwapCache, it's handled by
1890 * special functions.
1893 mz = page_cgroup_zoneinfo(pc);
1894 unlock_page_cgroup(pc);
1896 if (mem_cgroup_soft_limit_check(mem))
1897 mem_cgroup_update_tree(mem, page);
1898 /* at swapout, this memcg will be accessed to record to swap */
1899 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1905 unlock_page_cgroup(pc);
1909 void mem_cgroup_uncharge_page(struct page *page)
1912 if (page_mapped(page))
1914 if (page->mapping && !PageAnon(page))
1916 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1919 void mem_cgroup_uncharge_cache_page(struct page *page)
1921 VM_BUG_ON(page_mapped(page));
1922 VM_BUG_ON(page->mapping);
1923 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1928 * called after __delete_from_swap_cache() and drop "page" account.
1929 * memcg information is recorded to swap_cgroup of "ent"
1932 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
1934 struct mem_cgroup *memcg;
1935 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1937 if (!swapout) /* this was a swap cache but the swap is unused ! */
1938 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1940 memcg = __mem_cgroup_uncharge_common(page, ctype);
1942 /* record memcg information */
1943 if (do_swap_account && swapout && memcg) {
1944 swap_cgroup_record(ent, css_id(&memcg->css));
1945 mem_cgroup_get(memcg);
1947 if (swapout && memcg)
1948 css_put(&memcg->css);
1952 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1954 * called from swap_entry_free(). remove record in swap_cgroup and
1955 * uncharge "memsw" account.
1957 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1959 struct mem_cgroup *memcg;
1962 if (!do_swap_account)
1965 id = swap_cgroup_record(ent, 0);
1967 memcg = mem_cgroup_lookup(id);
1970 * We uncharge this because swap is freed.
1971 * This memcg can be obsolete one. We avoid calling css_tryget
1973 if (!mem_cgroup_is_root(memcg))
1974 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1975 mem_cgroup_swap_statistics(memcg, false);
1976 mem_cgroup_put(memcg);
1983 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1986 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1988 struct page_cgroup *pc;
1989 struct mem_cgroup *mem = NULL;
1992 if (mem_cgroup_disabled())
1995 pc = lookup_page_cgroup(page);
1996 lock_page_cgroup(pc);
1997 if (PageCgroupUsed(pc)) {
1998 mem = pc->mem_cgroup;
2001 unlock_page_cgroup(pc);
2004 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2012 /* remove redundant charge if migration failed*/
2013 void mem_cgroup_end_migration(struct mem_cgroup *mem,
2014 struct page *oldpage, struct page *newpage)
2016 struct page *target, *unused;
2017 struct page_cgroup *pc;
2018 enum charge_type ctype;
2022 cgroup_exclude_rmdir(&mem->css);
2023 /* at migration success, oldpage->mapping is NULL. */
2024 if (oldpage->mapping) {
2032 if (PageAnon(target))
2033 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2034 else if (page_is_file_cache(target))
2035 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2037 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2039 /* unused page is not on radix-tree now. */
2041 __mem_cgroup_uncharge_common(unused, ctype);
2043 pc = lookup_page_cgroup(target);
2045 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2046 * So, double-counting is effectively avoided.
2048 __mem_cgroup_commit_charge(mem, pc, ctype);
2051 * Both of oldpage and newpage are still under lock_page().
2052 * Then, we don't have to care about race in radix-tree.
2053 * But we have to be careful that this page is unmapped or not.
2055 * There is a case for !page_mapped(). At the start of
2056 * migration, oldpage was mapped. But now, it's zapped.
2057 * But we know *target* page is not freed/reused under us.
2058 * mem_cgroup_uncharge_page() does all necessary checks.
2060 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2061 mem_cgroup_uncharge_page(target);
2063 * At migration, we may charge account against cgroup which has no tasks
2064 * So, rmdir()->pre_destroy() can be called while we do this charge.
2065 * In that case, we need to call pre_destroy() again. check it here.
2067 cgroup_release_and_wakeup_rmdir(&mem->css);
2071 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2072 * Calling hierarchical_reclaim is not enough because we should update
2073 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2074 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2075 * not from the memcg which this page would be charged to.
2076 * try_charge_swapin does all of these works properly.
2078 int mem_cgroup_shmem_charge_fallback(struct page *page,
2079 struct mm_struct *mm,
2082 struct mem_cgroup *mem = NULL;
2085 if (mem_cgroup_disabled())
2088 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2090 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
2095 static DEFINE_MUTEX(set_limit_mutex);
2097 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
2098 unsigned long long val)
2104 int children = mem_cgroup_count_children(memcg);
2105 u64 curusage, oldusage;
2108 * For keeping hierarchical_reclaim simple, how long we should retry
2109 * is depends on callers. We set our retry-count to be function
2110 * of # of children which we should visit in this loop.
2112 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2114 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2116 while (retry_count) {
2117 if (signal_pending(current)) {
2122 * Rather than hide all in some function, I do this in
2123 * open coded manner. You see what this really does.
2124 * We have to guarantee mem->res.limit < mem->memsw.limit.
2126 mutex_lock(&set_limit_mutex);
2127 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2128 if (memswlimit < val) {
2130 mutex_unlock(&set_limit_mutex);
2133 ret = res_counter_set_limit(&memcg->res, val);
2135 if (memswlimit == val)
2136 memcg->memsw_is_minimum = true;
2138 memcg->memsw_is_minimum = false;
2140 mutex_unlock(&set_limit_mutex);
2145 progress = mem_cgroup_hierarchical_reclaim(memcg, NULL,
2147 MEM_CGROUP_RECLAIM_SHRINK);
2148 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2149 /* Usage is reduced ? */
2150 if (curusage >= oldusage)
2153 oldusage = curusage;
2159 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2160 unsigned long long val)
2163 u64 memlimit, oldusage, curusage;
2164 int children = mem_cgroup_count_children(memcg);
2167 /* see mem_cgroup_resize_res_limit */
2168 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2169 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2170 while (retry_count) {
2171 if (signal_pending(current)) {
2176 * Rather than hide all in some function, I do this in
2177 * open coded manner. You see what this really does.
2178 * We have to guarantee mem->res.limit < mem->memsw.limit.
2180 mutex_lock(&set_limit_mutex);
2181 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2182 if (memlimit > val) {
2184 mutex_unlock(&set_limit_mutex);
2187 ret = res_counter_set_limit(&memcg->memsw, val);
2189 if (memlimit == val)
2190 memcg->memsw_is_minimum = true;
2192 memcg->memsw_is_minimum = false;
2194 mutex_unlock(&set_limit_mutex);
2199 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2200 MEM_CGROUP_RECLAIM_NOSWAP |
2201 MEM_CGROUP_RECLAIM_SHRINK);
2202 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2203 /* Usage is reduced ? */
2204 if (curusage >= oldusage)
2207 oldusage = curusage;
2212 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2213 gfp_t gfp_mask, int nid,
2216 unsigned long nr_reclaimed = 0;
2217 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2218 unsigned long reclaimed;
2220 struct mem_cgroup_tree_per_zone *mctz;
2221 unsigned long long excess;
2226 mctz = soft_limit_tree_node_zone(nid, zid);
2228 * This loop can run a while, specially if mem_cgroup's continuously
2229 * keep exceeding their soft limit and putting the system under
2236 mz = mem_cgroup_largest_soft_limit_node(mctz);
2240 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2242 MEM_CGROUP_RECLAIM_SOFT);
2243 nr_reclaimed += reclaimed;
2244 spin_lock(&mctz->lock);
2247 * If we failed to reclaim anything from this memory cgroup
2248 * it is time to move on to the next cgroup
2254 * Loop until we find yet another one.
2256 * By the time we get the soft_limit lock
2257 * again, someone might have aded the
2258 * group back on the RB tree. Iterate to
2259 * make sure we get a different mem.
2260 * mem_cgroup_largest_soft_limit_node returns
2261 * NULL if no other cgroup is present on
2265 __mem_cgroup_largest_soft_limit_node(mctz);
2266 if (next_mz == mz) {
2267 css_put(&next_mz->mem->css);
2269 } else /* next_mz == NULL or other memcg */
2273 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
2274 excess = res_counter_soft_limit_excess(&mz->mem->res);
2276 * One school of thought says that we should not add
2277 * back the node to the tree if reclaim returns 0.
2278 * But our reclaim could return 0, simply because due
2279 * to priority we are exposing a smaller subset of
2280 * memory to reclaim from. Consider this as a longer
2283 /* If excess == 0, no tree ops */
2284 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
2285 spin_unlock(&mctz->lock);
2286 css_put(&mz->mem->css);
2289 * Could not reclaim anything and there are no more
2290 * mem cgroups to try or we seem to be looping without
2291 * reclaiming anything.
2293 if (!nr_reclaimed &&
2295 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2297 } while (!nr_reclaimed);
2299 css_put(&next_mz->mem->css);
2300 return nr_reclaimed;
2304 * This routine traverse page_cgroup in given list and drop them all.
2305 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2307 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
2308 int node, int zid, enum lru_list lru)
2311 struct mem_cgroup_per_zone *mz;
2312 struct page_cgroup *pc, *busy;
2313 unsigned long flags, loop;
2314 struct list_head *list;
2317 zone = &NODE_DATA(node)->node_zones[zid];
2318 mz = mem_cgroup_zoneinfo(mem, node, zid);
2319 list = &mz->lists[lru];
2321 loop = MEM_CGROUP_ZSTAT(mz, lru);
2322 /* give some margin against EBUSY etc...*/
2327 spin_lock_irqsave(&zone->lru_lock, flags);
2328 if (list_empty(list)) {
2329 spin_unlock_irqrestore(&zone->lru_lock, flags);
2332 pc = list_entry(list->prev, struct page_cgroup, lru);
2334 list_move(&pc->lru, list);
2336 spin_unlock_irqrestore(&zone->lru_lock, flags);
2339 spin_unlock_irqrestore(&zone->lru_lock, flags);
2341 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
2345 if (ret == -EBUSY || ret == -EINVAL) {
2346 /* found lock contention or "pc" is obsolete. */
2353 if (!ret && !list_empty(list))
2359 * make mem_cgroup's charge to be 0 if there is no task.
2360 * This enables deleting this mem_cgroup.
2362 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
2365 int node, zid, shrink;
2366 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
2367 struct cgroup *cgrp = mem->css.cgroup;
2372 /* should free all ? */
2376 while (mem->res.usage > 0) {
2378 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
2381 if (signal_pending(current))
2383 /* This is for making all *used* pages to be on LRU. */
2384 lru_add_drain_all();
2386 for_each_node_state(node, N_HIGH_MEMORY) {
2387 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
2390 ret = mem_cgroup_force_empty_list(mem,
2399 /* it seems parent cgroup doesn't have enough mem */
2410 /* returns EBUSY if there is a task or if we come here twice. */
2411 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
2415 /* we call try-to-free pages for make this cgroup empty */
2416 lru_add_drain_all();
2417 /* try to free all pages in this cgroup */
2419 while (nr_retries && mem->res.usage > 0) {
2422 if (signal_pending(current)) {
2426 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2427 false, get_swappiness(mem));
2430 /* maybe some writeback is necessary */
2431 congestion_wait(BLK_RW_ASYNC, HZ/10);
2436 /* try move_account...there may be some *locked* pages. */
2443 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2445 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2449 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2451 return mem_cgroup_from_cont(cont)->use_hierarchy;
2454 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2458 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2459 struct cgroup *parent = cont->parent;
2460 struct mem_cgroup *parent_mem = NULL;
2463 parent_mem = mem_cgroup_from_cont(parent);
2467 * If parent's use_hierarchy is set, we can't make any modifications
2468 * in the child subtrees. If it is unset, then the change can
2469 * occur, provided the current cgroup has no children.
2471 * For the root cgroup, parent_mem is NULL, we allow value to be
2472 * set if there are no children.
2474 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2475 (val == 1 || val == 0)) {
2476 if (list_empty(&cont->children))
2477 mem->use_hierarchy = val;
2487 struct mem_cgroup_idx_data {
2489 enum mem_cgroup_stat_index idx;
2493 mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2495 struct mem_cgroup_idx_data *d = data;
2496 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2501 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2502 enum mem_cgroup_stat_index idx, s64 *val)
2504 struct mem_cgroup_idx_data d;
2507 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2511 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2513 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2517 type = MEMFILE_TYPE(cft->private);
2518 name = MEMFILE_ATTR(cft->private);
2521 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2522 mem_cgroup_get_recursive_idx_stat(mem,
2523 MEM_CGROUP_STAT_CACHE, &idx_val);
2525 mem_cgroup_get_recursive_idx_stat(mem,
2526 MEM_CGROUP_STAT_RSS, &idx_val);
2530 val = res_counter_read_u64(&mem->res, name);
2533 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2534 mem_cgroup_get_recursive_idx_stat(mem,
2535 MEM_CGROUP_STAT_CACHE, &idx_val);
2537 mem_cgroup_get_recursive_idx_stat(mem,
2538 MEM_CGROUP_STAT_RSS, &idx_val);
2540 mem_cgroup_get_recursive_idx_stat(mem,
2541 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2544 val = res_counter_read_u64(&mem->memsw, name);
2553 * The user of this function is...
2556 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2559 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2561 unsigned long long val;
2564 type = MEMFILE_TYPE(cft->private);
2565 name = MEMFILE_ATTR(cft->private);
2568 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2572 /* This function does all necessary parse...reuse it */
2573 ret = res_counter_memparse_write_strategy(buffer, &val);
2577 ret = mem_cgroup_resize_limit(memcg, val);
2579 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2581 case RES_SOFT_LIMIT:
2582 ret = res_counter_memparse_write_strategy(buffer, &val);
2586 * For memsw, soft limits are hard to implement in terms
2587 * of semantics, for now, we support soft limits for
2588 * control without swap
2591 ret = res_counter_set_soft_limit(&memcg->res, val);
2596 ret = -EINVAL; /* should be BUG() ? */
2602 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2603 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2605 struct cgroup *cgroup;
2606 unsigned long long min_limit, min_memsw_limit, tmp;
2608 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2609 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2610 cgroup = memcg->css.cgroup;
2611 if (!memcg->use_hierarchy)
2614 while (cgroup->parent) {
2615 cgroup = cgroup->parent;
2616 memcg = mem_cgroup_from_cont(cgroup);
2617 if (!memcg->use_hierarchy)
2619 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2620 min_limit = min(min_limit, tmp);
2621 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2622 min_memsw_limit = min(min_memsw_limit, tmp);
2625 *mem_limit = min_limit;
2626 *memsw_limit = min_memsw_limit;
2630 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2632 struct mem_cgroup *mem;
2635 mem = mem_cgroup_from_cont(cont);
2636 type = MEMFILE_TYPE(event);
2637 name = MEMFILE_ATTR(event);
2641 res_counter_reset_max(&mem->res);
2643 res_counter_reset_max(&mem->memsw);
2647 res_counter_reset_failcnt(&mem->res);
2649 res_counter_reset_failcnt(&mem->memsw);
2657 /* For read statistics */
2673 struct mcs_total_stat {
2674 s64 stat[NR_MCS_STAT];
2680 } memcg_stat_strings[NR_MCS_STAT] = {
2681 {"cache", "total_cache"},
2682 {"rss", "total_rss"},
2683 {"mapped_file", "total_mapped_file"},
2684 {"pgpgin", "total_pgpgin"},
2685 {"pgpgout", "total_pgpgout"},
2686 {"swap", "total_swap"},
2687 {"inactive_anon", "total_inactive_anon"},
2688 {"active_anon", "total_active_anon"},
2689 {"inactive_file", "total_inactive_file"},
2690 {"active_file", "total_active_file"},
2691 {"unevictable", "total_unevictable"}
2695 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2697 struct mcs_total_stat *s = data;
2701 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2702 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2703 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2704 s->stat[MCS_RSS] += val * PAGE_SIZE;
2705 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2706 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
2707 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2708 s->stat[MCS_PGPGIN] += val;
2709 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2710 s->stat[MCS_PGPGOUT] += val;
2711 if (do_swap_account) {
2712 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2713 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2717 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2718 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2719 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2720 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2721 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2722 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2723 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2724 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2725 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2726 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2731 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2733 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2736 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2737 struct cgroup_map_cb *cb)
2739 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2740 struct mcs_total_stat mystat;
2743 memset(&mystat, 0, sizeof(mystat));
2744 mem_cgroup_get_local_stat(mem_cont, &mystat);
2746 for (i = 0; i < NR_MCS_STAT; i++) {
2747 if (i == MCS_SWAP && !do_swap_account)
2749 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2752 /* Hierarchical information */
2754 unsigned long long limit, memsw_limit;
2755 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2756 cb->fill(cb, "hierarchical_memory_limit", limit);
2757 if (do_swap_account)
2758 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2761 memset(&mystat, 0, sizeof(mystat));
2762 mem_cgroup_get_total_stat(mem_cont, &mystat);
2763 for (i = 0; i < NR_MCS_STAT; i++) {
2764 if (i == MCS_SWAP && !do_swap_account)
2766 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2769 #ifdef CONFIG_DEBUG_VM
2770 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2774 struct mem_cgroup_per_zone *mz;
2775 unsigned long recent_rotated[2] = {0, 0};
2776 unsigned long recent_scanned[2] = {0, 0};
2778 for_each_online_node(nid)
2779 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2780 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2782 recent_rotated[0] +=
2783 mz->reclaim_stat.recent_rotated[0];
2784 recent_rotated[1] +=
2785 mz->reclaim_stat.recent_rotated[1];
2786 recent_scanned[0] +=
2787 mz->reclaim_stat.recent_scanned[0];
2788 recent_scanned[1] +=
2789 mz->reclaim_stat.recent_scanned[1];
2791 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2792 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2793 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2794 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2801 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2803 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2805 return get_swappiness(memcg);
2808 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2811 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2812 struct mem_cgroup *parent;
2817 if (cgrp->parent == NULL)
2820 parent = mem_cgroup_from_cont(cgrp->parent);
2824 /* If under hierarchy, only empty-root can set this value */
2825 if ((parent->use_hierarchy) ||
2826 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2831 spin_lock(&memcg->reclaim_param_lock);
2832 memcg->swappiness = val;
2833 spin_unlock(&memcg->reclaim_param_lock);
2841 static struct cftype mem_cgroup_files[] = {
2843 .name = "usage_in_bytes",
2844 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2845 .read_u64 = mem_cgroup_read,
2848 .name = "max_usage_in_bytes",
2849 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2850 .trigger = mem_cgroup_reset,
2851 .read_u64 = mem_cgroup_read,
2854 .name = "limit_in_bytes",
2855 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2856 .write_string = mem_cgroup_write,
2857 .read_u64 = mem_cgroup_read,
2860 .name = "soft_limit_in_bytes",
2861 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2862 .write_string = mem_cgroup_write,
2863 .read_u64 = mem_cgroup_read,
2867 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2868 .trigger = mem_cgroup_reset,
2869 .read_u64 = mem_cgroup_read,
2873 .read_map = mem_control_stat_show,
2876 .name = "force_empty",
2877 .trigger = mem_cgroup_force_empty_write,
2880 .name = "use_hierarchy",
2881 .write_u64 = mem_cgroup_hierarchy_write,
2882 .read_u64 = mem_cgroup_hierarchy_read,
2885 .name = "swappiness",
2886 .read_u64 = mem_cgroup_swappiness_read,
2887 .write_u64 = mem_cgroup_swappiness_write,
2891 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2892 static struct cftype memsw_cgroup_files[] = {
2894 .name = "memsw.usage_in_bytes",
2895 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2896 .read_u64 = mem_cgroup_read,
2899 .name = "memsw.max_usage_in_bytes",
2900 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2901 .trigger = mem_cgroup_reset,
2902 .read_u64 = mem_cgroup_read,
2905 .name = "memsw.limit_in_bytes",
2906 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2907 .write_string = mem_cgroup_write,
2908 .read_u64 = mem_cgroup_read,
2911 .name = "memsw.failcnt",
2912 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2913 .trigger = mem_cgroup_reset,
2914 .read_u64 = mem_cgroup_read,
2918 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2920 if (!do_swap_account)
2922 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2923 ARRAY_SIZE(memsw_cgroup_files));
2926 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2932 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2934 struct mem_cgroup_per_node *pn;
2935 struct mem_cgroup_per_zone *mz;
2937 int zone, tmp = node;
2939 * This routine is called against possible nodes.
2940 * But it's BUG to call kmalloc() against offline node.
2942 * TODO: this routine can waste much memory for nodes which will
2943 * never be onlined. It's better to use memory hotplug callback
2946 if (!node_state(node, N_NORMAL_MEMORY))
2948 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
2952 mem->info.nodeinfo[node] = pn;
2953 memset(pn, 0, sizeof(*pn));
2955 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2956 mz = &pn->zoneinfo[zone];
2958 INIT_LIST_HEAD(&mz->lists[l]);
2959 mz->usage_in_excess = 0;
2960 mz->on_tree = false;
2966 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2968 kfree(mem->info.nodeinfo[node]);
2971 static int mem_cgroup_size(void)
2973 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2974 return sizeof(struct mem_cgroup) + cpustat_size;
2977 static struct mem_cgroup *mem_cgroup_alloc(void)
2979 struct mem_cgroup *mem;
2980 int size = mem_cgroup_size();
2982 if (size < PAGE_SIZE)
2983 mem = kmalloc(size, GFP_KERNEL);
2985 mem = vmalloc(size);
2988 memset(mem, 0, size);
2993 * At destroying mem_cgroup, references from swap_cgroup can remain.
2994 * (scanning all at force_empty is too costly...)
2996 * Instead of clearing all references at force_empty, we remember
2997 * the number of reference from swap_cgroup and free mem_cgroup when
2998 * it goes down to 0.
3000 * Removal of cgroup itself succeeds regardless of refs from swap.
3003 static void __mem_cgroup_free(struct mem_cgroup *mem)
3007 mem_cgroup_remove_from_trees(mem);
3008 free_css_id(&mem_cgroup_subsys, &mem->css);
3010 for_each_node_state(node, N_POSSIBLE)
3011 free_mem_cgroup_per_zone_info(mem, node);
3013 if (mem_cgroup_size() < PAGE_SIZE)
3019 static void mem_cgroup_get(struct mem_cgroup *mem)
3021 atomic_inc(&mem->refcnt);
3024 static void mem_cgroup_put(struct mem_cgroup *mem)
3026 if (atomic_dec_and_test(&mem->refcnt)) {
3027 struct mem_cgroup *parent = parent_mem_cgroup(mem);
3028 __mem_cgroup_free(mem);
3030 mem_cgroup_put(parent);
3035 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3037 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3039 if (!mem->res.parent)
3041 return mem_cgroup_from_res_counter(mem->res.parent, res);
3044 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3045 static void __init enable_swap_cgroup(void)
3047 if (!mem_cgroup_disabled() && really_do_swap_account)
3048 do_swap_account = 1;
3051 static void __init enable_swap_cgroup(void)
3056 static int mem_cgroup_soft_limit_tree_init(void)
3058 struct mem_cgroup_tree_per_node *rtpn;
3059 struct mem_cgroup_tree_per_zone *rtpz;
3060 int tmp, node, zone;
3062 for_each_node_state(node, N_POSSIBLE) {
3064 if (!node_state(node, N_NORMAL_MEMORY))
3066 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3070 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3072 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3073 rtpz = &rtpn->rb_tree_per_zone[zone];
3074 rtpz->rb_root = RB_ROOT;
3075 spin_lock_init(&rtpz->lock);
3081 static struct cgroup_subsys_state * __ref
3082 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3084 struct mem_cgroup *mem, *parent;
3085 long error = -ENOMEM;
3088 mem = mem_cgroup_alloc();
3090 return ERR_PTR(error);
3092 for_each_node_state(node, N_POSSIBLE)
3093 if (alloc_mem_cgroup_per_zone_info(mem, node))
3097 if (cont->parent == NULL) {
3098 enable_swap_cgroup();
3100 root_mem_cgroup = mem;
3101 if (mem_cgroup_soft_limit_tree_init())
3105 parent = mem_cgroup_from_cont(cont->parent);
3106 mem->use_hierarchy = parent->use_hierarchy;
3109 if (parent && parent->use_hierarchy) {
3110 res_counter_init(&mem->res, &parent->res);
3111 res_counter_init(&mem->memsw, &parent->memsw);
3113 * We increment refcnt of the parent to ensure that we can
3114 * safely access it on res_counter_charge/uncharge.
3115 * This refcnt will be decremented when freeing this
3116 * mem_cgroup(see mem_cgroup_put).
3118 mem_cgroup_get(parent);
3120 res_counter_init(&mem->res, NULL);
3121 res_counter_init(&mem->memsw, NULL);
3123 mem->last_scanned_child = 0;
3124 spin_lock_init(&mem->reclaim_param_lock);
3127 mem->swappiness = get_swappiness(parent);
3128 atomic_set(&mem->refcnt, 1);
3131 __mem_cgroup_free(mem);
3132 root_mem_cgroup = NULL;
3133 return ERR_PTR(error);
3136 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
3137 struct cgroup *cont)
3139 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3141 return mem_cgroup_force_empty(mem, false);
3144 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3145 struct cgroup *cont)
3147 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3149 mem_cgroup_put(mem);
3152 static int mem_cgroup_populate(struct cgroup_subsys *ss,
3153 struct cgroup *cont)
3157 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3158 ARRAY_SIZE(mem_cgroup_files));
3161 ret = register_memsw_files(cont, ss);
3165 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3166 struct cgroup *cont,
3167 struct cgroup *old_cont,
3168 struct task_struct *p,
3171 mutex_lock(&memcg_tasklist);
3173 * FIXME: It's better to move charges of this process from old
3174 * memcg to new memcg. But it's just on TODO-List now.
3176 mutex_unlock(&memcg_tasklist);
3179 struct cgroup_subsys mem_cgroup_subsys = {
3181 .subsys_id = mem_cgroup_subsys_id,
3182 .create = mem_cgroup_create,
3183 .pre_destroy = mem_cgroup_pre_destroy,
3184 .destroy = mem_cgroup_destroy,
3185 .populate = mem_cgroup_populate,
3186 .attach = mem_cgroup_move_task,
3191 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3193 static int __init disable_swap_account(char *s)
3195 really_do_swap_account = 0;
3198 __setup("noswapaccount", disable_swap_account);