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/hugetlb.h>
25 #include <linux/pagemap.h>
26 #include <linux/smp.h>
27 #include <linux/page-flags.h>
28 #include <linux/backing-dev.h>
29 #include <linux/bit_spinlock.h>
30 #include <linux/rcupdate.h>
31 #include <linux/limits.h>
32 #include <linux/mutex.h>
33 #include <linux/rbtree.h>
34 #include <linux/slab.h>
35 #include <linux/swap.h>
36 #include <linux/spinlock.h>
38 #include <linux/seq_file.h>
39 #include <linux/vmalloc.h>
40 #include <linux/mm_inline.h>
41 #include <linux/page_cgroup.h>
42 #include <linux/cpu.h>
45 #include <asm/uaccess.h>
47 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
48 #define MEM_CGROUP_RECLAIM_RETRIES 5
49 struct mem_cgroup *root_mem_cgroup __read_mostly;
51 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
52 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
53 int do_swap_account __read_mostly;
54 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
56 #define do_swap_account (0)
59 #define SOFTLIMIT_EVENTS_THRESH (1000)
62 * Statistics for memory cgroup.
64 enum mem_cgroup_stat_index {
66 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
68 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
69 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
70 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
71 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
72 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
73 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
74 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
76 MEM_CGROUP_STAT_NSTATS,
79 struct mem_cgroup_stat_cpu {
80 s64 count[MEM_CGROUP_STAT_NSTATS];
81 } ____cacheline_aligned_in_smp;
83 struct mem_cgroup_stat {
84 struct mem_cgroup_stat_cpu cpustat[0];
88 __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
89 enum mem_cgroup_stat_index idx)
95 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
96 enum mem_cgroup_stat_index idx)
98 return stat->count[idx];
102 * For accounting under irq disable, no need for increment preempt count.
104 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
105 enum mem_cgroup_stat_index idx, int val)
107 stat->count[idx] += val;
110 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
111 enum mem_cgroup_stat_index idx)
115 for_each_possible_cpu(cpu)
116 ret += stat->cpustat[cpu].count[idx];
120 static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
124 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
125 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
130 * per-zone information in memory controller.
132 struct mem_cgroup_per_zone {
134 * spin_lock to protect the per cgroup LRU
136 struct list_head lists[NR_LRU_LISTS];
137 unsigned long count[NR_LRU_LISTS];
139 struct zone_reclaim_stat reclaim_stat;
140 struct rb_node tree_node; /* RB tree node */
141 unsigned long long usage_in_excess;/* Set to the value by which */
142 /* the soft limit is exceeded*/
144 struct mem_cgroup *mem; /* Back pointer, we cannot */
145 /* use container_of */
147 /* Macro for accessing counter */
148 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
150 struct mem_cgroup_per_node {
151 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
154 struct mem_cgroup_lru_info {
155 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
159 * Cgroups above their limits are maintained in a RB-Tree, independent of
160 * their hierarchy representation
163 struct mem_cgroup_tree_per_zone {
164 struct rb_root rb_root;
168 struct mem_cgroup_tree_per_node {
169 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
172 struct mem_cgroup_tree {
173 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
176 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
179 * The memory controller data structure. The memory controller controls both
180 * page cache and RSS per cgroup. We would eventually like to provide
181 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
182 * to help the administrator determine what knobs to tune.
184 * TODO: Add a water mark for the memory controller. Reclaim will begin when
185 * we hit the water mark. May be even add a low water mark, such that
186 * no reclaim occurs from a cgroup at it's low water mark, this is
187 * a feature that will be implemented much later in the future.
190 struct cgroup_subsys_state css;
192 * the counter to account for memory usage
194 struct res_counter res;
196 * the counter to account for mem+swap usage.
198 struct res_counter memsw;
200 * Per cgroup active and inactive list, similar to the
201 * per zone LRU lists.
203 struct mem_cgroup_lru_info info;
206 protect against reclaim related member.
208 spinlock_t reclaim_param_lock;
210 int prev_priority; /* for recording reclaim priority */
213 * While reclaiming in a hierarchy, we cache the last child we
216 int last_scanned_child;
218 * Should the accounting and control be hierarchical, per subtree?
221 unsigned long last_oom_jiffies;
224 unsigned int swappiness;
226 /* set when res.limit == memsw.limit */
227 bool memsw_is_minimum;
230 * Should we move charges of a task when a task is moved into this
231 * mem_cgroup ? And what type of charges should we move ?
233 unsigned long move_charge_at_immigrate;
236 * statistics. This must be placed at the end of memcg.
238 struct mem_cgroup_stat stat;
241 /* Stuffs for move charges at task migration. */
243 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
244 * left-shifted bitmap of these types.
247 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
251 /* "mc" and its members are protected by cgroup_mutex */
252 static struct move_charge_struct {
253 struct mem_cgroup *from;
254 struct mem_cgroup *to;
255 unsigned long precharge;
256 unsigned long moved_charge;
260 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
261 * limit reclaim to prevent infinite loops, if they ever occur.
263 #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
264 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
267 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
268 MEM_CGROUP_CHARGE_TYPE_MAPPED,
269 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
270 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
271 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
272 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
276 /* only for here (for easy reading.) */
277 #define PCGF_CACHE (1UL << PCG_CACHE)
278 #define PCGF_USED (1UL << PCG_USED)
279 #define PCGF_LOCK (1UL << PCG_LOCK)
280 /* Not used, but added here for completeness */
281 #define PCGF_ACCT (1UL << PCG_ACCT)
283 /* for encoding cft->private value on file */
286 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
287 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
288 #define MEMFILE_ATTR(val) ((val) & 0xffff)
291 * Reclaim flags for mem_cgroup_hierarchical_reclaim
293 #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
294 #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
295 #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
296 #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
297 #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
298 #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
300 static void mem_cgroup_get(struct mem_cgroup *mem);
301 static void mem_cgroup_put(struct mem_cgroup *mem);
302 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
303 static void drain_all_stock_async(void);
305 static struct mem_cgroup_per_zone *
306 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
308 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
311 struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
316 static struct mem_cgroup_per_zone *
317 page_cgroup_zoneinfo(struct page_cgroup *pc)
319 struct mem_cgroup *mem = pc->mem_cgroup;
320 int nid = page_cgroup_nid(pc);
321 int zid = page_cgroup_zid(pc);
326 return mem_cgroup_zoneinfo(mem, nid, zid);
329 static struct mem_cgroup_tree_per_zone *
330 soft_limit_tree_node_zone(int nid, int zid)
332 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
335 static struct mem_cgroup_tree_per_zone *
336 soft_limit_tree_from_page(struct page *page)
338 int nid = page_to_nid(page);
339 int zid = page_zonenum(page);
341 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
345 __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
346 struct mem_cgroup_per_zone *mz,
347 struct mem_cgroup_tree_per_zone *mctz,
348 unsigned long long new_usage_in_excess)
350 struct rb_node **p = &mctz->rb_root.rb_node;
351 struct rb_node *parent = NULL;
352 struct mem_cgroup_per_zone *mz_node;
357 mz->usage_in_excess = new_usage_in_excess;
358 if (!mz->usage_in_excess)
362 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
364 if (mz->usage_in_excess < mz_node->usage_in_excess)
367 * We can't avoid mem cgroups that are over their soft
368 * limit by the same amount
370 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
373 rb_link_node(&mz->tree_node, parent, p);
374 rb_insert_color(&mz->tree_node, &mctz->rb_root);
379 __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
380 struct mem_cgroup_per_zone *mz,
381 struct mem_cgroup_tree_per_zone *mctz)
385 rb_erase(&mz->tree_node, &mctz->rb_root);
390 mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
391 struct mem_cgroup_per_zone *mz,
392 struct mem_cgroup_tree_per_zone *mctz)
394 spin_lock(&mctz->lock);
395 __mem_cgroup_remove_exceeded(mem, mz, mctz);
396 spin_unlock(&mctz->lock);
399 static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
404 struct mem_cgroup_stat_cpu *cpustat;
407 cpustat = &mem->stat.cpustat[cpu];
408 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
409 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
410 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
417 static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
419 unsigned long long excess;
420 struct mem_cgroup_per_zone *mz;
421 struct mem_cgroup_tree_per_zone *mctz;
422 int nid = page_to_nid(page);
423 int zid = page_zonenum(page);
424 mctz = soft_limit_tree_from_page(page);
427 * Necessary to update all ancestors when hierarchy is used.
428 * because their event counter is not touched.
430 for (; mem; mem = parent_mem_cgroup(mem)) {
431 mz = mem_cgroup_zoneinfo(mem, nid, zid);
432 excess = res_counter_soft_limit_excess(&mem->res);
434 * We have to update the tree if mz is on RB-tree or
435 * mem is over its softlimit.
437 if (excess || mz->on_tree) {
438 spin_lock(&mctz->lock);
439 /* if on-tree, remove it */
441 __mem_cgroup_remove_exceeded(mem, mz, mctz);
443 * Insert again. mz->usage_in_excess will be updated.
444 * If excess is 0, no tree ops.
446 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
447 spin_unlock(&mctz->lock);
452 static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
455 struct mem_cgroup_per_zone *mz;
456 struct mem_cgroup_tree_per_zone *mctz;
458 for_each_node_state(node, N_POSSIBLE) {
459 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
460 mz = mem_cgroup_zoneinfo(mem, node, zone);
461 mctz = soft_limit_tree_node_zone(node, zone);
462 mem_cgroup_remove_exceeded(mem, mz, mctz);
467 static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
469 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
472 static struct mem_cgroup_per_zone *
473 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
475 struct rb_node *rightmost = NULL;
476 struct mem_cgroup_per_zone *mz;
480 rightmost = rb_last(&mctz->rb_root);
482 goto done; /* Nothing to reclaim from */
484 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
486 * Remove the node now but someone else can add it back,
487 * we will to add it back at the end of reclaim to its correct
488 * position in the tree.
490 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
491 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
492 !css_tryget(&mz->mem->css))
498 static struct mem_cgroup_per_zone *
499 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
501 struct mem_cgroup_per_zone *mz;
503 spin_lock(&mctz->lock);
504 mz = __mem_cgroup_largest_soft_limit_node(mctz);
505 spin_unlock(&mctz->lock);
509 static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
512 int val = (charge) ? 1 : -1;
513 struct mem_cgroup_stat *stat = &mem->stat;
514 struct mem_cgroup_stat_cpu *cpustat;
517 cpustat = &stat->cpustat[cpu];
518 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_SWAPOUT, val);
522 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
523 struct page_cgroup *pc,
526 int val = (charge) ? 1 : -1;
527 struct mem_cgroup_stat *stat = &mem->stat;
528 struct mem_cgroup_stat_cpu *cpustat;
531 cpustat = &stat->cpustat[cpu];
532 if (PageCgroupCache(pc))
533 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
535 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
538 __mem_cgroup_stat_add_safe(cpustat,
539 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
541 __mem_cgroup_stat_add_safe(cpustat,
542 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
543 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
547 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
551 struct mem_cgroup_per_zone *mz;
554 for_each_online_node(nid)
555 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
556 mz = mem_cgroup_zoneinfo(mem, nid, zid);
557 total += MEM_CGROUP_ZSTAT(mz, idx);
562 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
564 return container_of(cgroup_subsys_state(cont,
565 mem_cgroup_subsys_id), struct mem_cgroup,
569 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
572 * mm_update_next_owner() may clear mm->owner to NULL
573 * if it races with swapoff, page migration, etc.
574 * So this can be called with p == NULL.
579 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
580 struct mem_cgroup, css);
583 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
585 struct mem_cgroup *mem = NULL;
590 * Because we have no locks, mm->owner's may be being moved to other
591 * cgroup. We use css_tryget() here even if this looks
592 * pessimistic (rather than adding locks here).
596 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
599 } while (!css_tryget(&mem->css));
605 * Call callback function against all cgroup under hierarchy tree.
607 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
608 int (*func)(struct mem_cgroup *, void *))
610 int found, ret, nextid;
611 struct cgroup_subsys_state *css;
612 struct mem_cgroup *mem;
614 if (!root->use_hierarchy)
615 return (*func)(root, data);
623 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
625 if (css && css_tryget(css))
626 mem = container_of(css, struct mem_cgroup, css);
630 ret = (*func)(mem, data);
634 } while (!ret && css);
639 static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
641 return (mem == root_mem_cgroup);
645 * Following LRU functions are allowed to be used without PCG_LOCK.
646 * Operations are called by routine of global LRU independently from memcg.
647 * What we have to take care of here is validness of pc->mem_cgroup.
649 * Changes to pc->mem_cgroup happens when
652 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
653 * It is added to LRU before charge.
654 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
655 * When moving account, the page is not on LRU. It's isolated.
658 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
660 struct page_cgroup *pc;
661 struct mem_cgroup_per_zone *mz;
663 if (mem_cgroup_disabled())
665 pc = lookup_page_cgroup(page);
666 /* can happen while we handle swapcache. */
667 if (!TestClearPageCgroupAcctLRU(pc))
669 VM_BUG_ON(!pc->mem_cgroup);
671 * We don't check PCG_USED bit. It's cleared when the "page" is finally
672 * removed from global LRU.
674 mz = page_cgroup_zoneinfo(pc);
675 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
676 if (mem_cgroup_is_root(pc->mem_cgroup))
678 VM_BUG_ON(list_empty(&pc->lru));
679 list_del_init(&pc->lru);
683 void mem_cgroup_del_lru(struct page *page)
685 mem_cgroup_del_lru_list(page, page_lru(page));
688 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
690 struct mem_cgroup_per_zone *mz;
691 struct page_cgroup *pc;
693 if (mem_cgroup_disabled())
696 pc = lookup_page_cgroup(page);
698 * Used bit is set without atomic ops but after smp_wmb().
699 * For making pc->mem_cgroup visible, insert smp_rmb() here.
702 /* unused or root page is not rotated. */
703 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
705 mz = page_cgroup_zoneinfo(pc);
706 list_move(&pc->lru, &mz->lists[lru]);
709 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
711 struct page_cgroup *pc;
712 struct mem_cgroup_per_zone *mz;
714 if (mem_cgroup_disabled())
716 pc = lookup_page_cgroup(page);
717 VM_BUG_ON(PageCgroupAcctLRU(pc));
719 * Used bit is set without atomic ops but after smp_wmb().
720 * For making pc->mem_cgroup visible, insert smp_rmb() here.
723 if (!PageCgroupUsed(pc))
726 mz = page_cgroup_zoneinfo(pc);
727 MEM_CGROUP_ZSTAT(mz, lru) += 1;
728 SetPageCgroupAcctLRU(pc);
729 if (mem_cgroup_is_root(pc->mem_cgroup))
731 list_add(&pc->lru, &mz->lists[lru]);
735 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
736 * lru because the page may.be reused after it's fully uncharged (because of
737 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
738 * it again. This function is only used to charge SwapCache. It's done under
739 * lock_page and expected that zone->lru_lock is never held.
741 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
744 struct zone *zone = page_zone(page);
745 struct page_cgroup *pc = lookup_page_cgroup(page);
747 spin_lock_irqsave(&zone->lru_lock, flags);
749 * Forget old LRU when this page_cgroup is *not* used. This Used bit
750 * is guarded by lock_page() because the page is SwapCache.
752 if (!PageCgroupUsed(pc))
753 mem_cgroup_del_lru_list(page, page_lru(page));
754 spin_unlock_irqrestore(&zone->lru_lock, flags);
757 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
760 struct zone *zone = page_zone(page);
761 struct page_cgroup *pc = lookup_page_cgroup(page);
763 spin_lock_irqsave(&zone->lru_lock, flags);
764 /* link when the page is linked to LRU but page_cgroup isn't */
765 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
766 mem_cgroup_add_lru_list(page, page_lru(page));
767 spin_unlock_irqrestore(&zone->lru_lock, flags);
771 void mem_cgroup_move_lists(struct page *page,
772 enum lru_list from, enum lru_list to)
774 if (mem_cgroup_disabled())
776 mem_cgroup_del_lru_list(page, from);
777 mem_cgroup_add_lru_list(page, to);
780 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
783 struct mem_cgroup *curr = NULL;
787 curr = try_get_mem_cgroup_from_mm(task->mm);
793 * We should check use_hierarchy of "mem" not "curr". Because checking
794 * use_hierarchy of "curr" here make this function true if hierarchy is
795 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
796 * hierarchy(even if use_hierarchy is disabled in "mem").
798 if (mem->use_hierarchy)
799 ret = css_is_ancestor(&curr->css, &mem->css);
807 * prev_priority control...this will be used in memory reclaim path.
809 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
813 spin_lock(&mem->reclaim_param_lock);
814 prev_priority = mem->prev_priority;
815 spin_unlock(&mem->reclaim_param_lock);
817 return prev_priority;
820 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
822 spin_lock(&mem->reclaim_param_lock);
823 if (priority < mem->prev_priority)
824 mem->prev_priority = priority;
825 spin_unlock(&mem->reclaim_param_lock);
828 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
830 spin_lock(&mem->reclaim_param_lock);
831 mem->prev_priority = priority;
832 spin_unlock(&mem->reclaim_param_lock);
835 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
837 unsigned long active;
838 unsigned long inactive;
840 unsigned long inactive_ratio;
842 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
843 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
845 gb = (inactive + active) >> (30 - PAGE_SHIFT);
847 inactive_ratio = int_sqrt(10 * gb);
852 present_pages[0] = inactive;
853 present_pages[1] = active;
856 return inactive_ratio;
859 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
861 unsigned long active;
862 unsigned long inactive;
863 unsigned long present_pages[2];
864 unsigned long inactive_ratio;
866 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
868 inactive = present_pages[0];
869 active = present_pages[1];
871 if (inactive * inactive_ratio < active)
877 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
879 unsigned long active;
880 unsigned long inactive;
882 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
883 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
885 return (active > inactive);
888 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
892 int nid = zone->zone_pgdat->node_id;
893 int zid = zone_idx(zone);
894 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
896 return MEM_CGROUP_ZSTAT(mz, lru);
899 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
902 int nid = zone->zone_pgdat->node_id;
903 int zid = zone_idx(zone);
904 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
906 return &mz->reclaim_stat;
909 struct zone_reclaim_stat *
910 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
912 struct page_cgroup *pc;
913 struct mem_cgroup_per_zone *mz;
915 if (mem_cgroup_disabled())
918 pc = lookup_page_cgroup(page);
920 * Used bit is set without atomic ops but after smp_wmb().
921 * For making pc->mem_cgroup visible, insert smp_rmb() here.
924 if (!PageCgroupUsed(pc))
927 mz = page_cgroup_zoneinfo(pc);
931 return &mz->reclaim_stat;
934 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
935 struct list_head *dst,
936 unsigned long *scanned, int order,
937 int mode, struct zone *z,
938 struct mem_cgroup *mem_cont,
939 int active, int file)
941 unsigned long nr_taken = 0;
945 struct list_head *src;
946 struct page_cgroup *pc, *tmp;
947 int nid = z->zone_pgdat->node_id;
948 int zid = zone_idx(z);
949 struct mem_cgroup_per_zone *mz;
950 int lru = LRU_FILE * file + active;
954 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
955 src = &mz->lists[lru];
958 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
959 if (scan >= nr_to_scan)
963 if (unlikely(!PageCgroupUsed(pc)))
965 if (unlikely(!PageLRU(page)))
969 ret = __isolate_lru_page(page, mode, file);
972 list_move(&page->lru, dst);
973 mem_cgroup_del_lru(page);
977 /* we don't affect global LRU but rotate in our LRU */
978 mem_cgroup_rotate_lru_list(page, page_lru(page));
989 #define mem_cgroup_from_res_counter(counter, member) \
990 container_of(counter, struct mem_cgroup, member)
992 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
994 if (do_swap_account) {
995 if (res_counter_check_under_limit(&mem->res) &&
996 res_counter_check_under_limit(&mem->memsw))
999 if (res_counter_check_under_limit(&mem->res))
1004 static unsigned int get_swappiness(struct mem_cgroup *memcg)
1006 struct cgroup *cgrp = memcg->css.cgroup;
1007 unsigned int swappiness;
1010 if (cgrp->parent == NULL)
1011 return vm_swappiness;
1013 spin_lock(&memcg->reclaim_param_lock);
1014 swappiness = memcg->swappiness;
1015 spin_unlock(&memcg->reclaim_param_lock);
1020 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1028 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
1029 * @memcg: The memory cgroup that went over limit
1030 * @p: Task that is going to be killed
1032 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1035 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1037 struct cgroup *task_cgrp;
1038 struct cgroup *mem_cgrp;
1040 * Need a buffer in BSS, can't rely on allocations. The code relies
1041 * on the assumption that OOM is serialized for memory controller.
1042 * If this assumption is broken, revisit this code.
1044 static char memcg_name[PATH_MAX];
1053 mem_cgrp = memcg->css.cgroup;
1054 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1056 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1059 * Unfortunately, we are unable to convert to a useful name
1060 * But we'll still print out the usage information
1067 printk(KERN_INFO "Task in %s killed", memcg_name);
1070 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1078 * Continues from above, so we don't need an KERN_ level
1080 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1083 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1084 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1085 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1086 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1087 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1089 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1090 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1091 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1095 * This function returns the number of memcg under hierarchy tree. Returns
1096 * 1(self count) if no children.
1098 static int mem_cgroup_count_children(struct mem_cgroup *mem)
1101 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1106 * Visit the first child (need not be the first child as per the ordering
1107 * of the cgroup list, since we track last_scanned_child) of @mem and use
1108 * that to reclaim free pages from.
1110 static struct mem_cgroup *
1111 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1113 struct mem_cgroup *ret = NULL;
1114 struct cgroup_subsys_state *css;
1117 if (!root_mem->use_hierarchy) {
1118 css_get(&root_mem->css);
1124 nextid = root_mem->last_scanned_child + 1;
1125 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1127 if (css && css_tryget(css))
1128 ret = container_of(css, struct mem_cgroup, css);
1131 /* Updates scanning parameter */
1132 spin_lock(&root_mem->reclaim_param_lock);
1134 /* this means start scan from ID:1 */
1135 root_mem->last_scanned_child = 0;
1137 root_mem->last_scanned_child = found;
1138 spin_unlock(&root_mem->reclaim_param_lock);
1145 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1146 * we reclaimed from, so that we don't end up penalizing one child extensively
1147 * based on its position in the children list.
1149 * root_mem is the original ancestor that we've been reclaim from.
1151 * We give up and return to the caller when we visit root_mem twice.
1152 * (other groups can be removed while we're walking....)
1154 * If shrink==true, for avoiding to free too much, this returns immedieately.
1156 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1159 unsigned long reclaim_options)
1161 struct mem_cgroup *victim;
1164 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1165 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
1166 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1167 unsigned long excess = mem_cgroup_get_excess(root_mem);
1169 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1170 if (root_mem->memsw_is_minimum)
1174 victim = mem_cgroup_select_victim(root_mem);
1175 if (victim == root_mem) {
1178 drain_all_stock_async();
1181 * If we have not been able to reclaim
1182 * anything, it might because there are
1183 * no reclaimable pages under this hierarchy
1185 if (!check_soft || !total) {
1186 css_put(&victim->css);
1190 * We want to do more targetted reclaim.
1191 * excess >> 2 is not to excessive so as to
1192 * reclaim too much, nor too less that we keep
1193 * coming back to reclaim from this cgroup
1195 if (total >= (excess >> 2) ||
1196 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1197 css_put(&victim->css);
1202 if (!mem_cgroup_local_usage(&victim->stat)) {
1203 /* this cgroup's local usage == 0 */
1204 css_put(&victim->css);
1207 /* we use swappiness of local cgroup */
1209 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1210 noswap, get_swappiness(victim), zone,
1211 zone->zone_pgdat->node_id);
1213 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1214 noswap, get_swappiness(victim));
1215 css_put(&victim->css);
1217 * At shrinking usage, we can't check we should stop here or
1218 * reclaim more. It's depends on callers. last_scanned_child
1219 * will work enough for keeping fairness under tree.
1225 if (res_counter_check_under_soft_limit(&root_mem->res))
1227 } else if (mem_cgroup_check_under_limit(root_mem))
1233 bool mem_cgroup_oom_called(struct task_struct *task)
1236 struct mem_cgroup *mem;
1237 struct mm_struct *mm;
1243 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1244 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1250 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1252 mem->last_oom_jiffies = jiffies;
1256 static void record_last_oom(struct mem_cgroup *mem)
1258 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1262 * Currently used to update mapped file statistics, but the routine can be
1263 * generalized to update other statistics as well.
1265 void mem_cgroup_update_file_mapped(struct page *page, int val)
1267 struct mem_cgroup *mem;
1268 struct mem_cgroup_stat *stat;
1269 struct mem_cgroup_stat_cpu *cpustat;
1271 struct page_cgroup *pc;
1273 pc = lookup_page_cgroup(page);
1277 lock_page_cgroup(pc);
1278 mem = pc->mem_cgroup;
1282 if (!PageCgroupUsed(pc))
1286 * Preemption is already disabled, we don't need get_cpu()
1288 cpu = smp_processor_id();
1290 cpustat = &stat->cpustat[cpu];
1292 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED, val);
1294 unlock_page_cgroup(pc);
1298 * size of first charge trial. "32" comes from vmscan.c's magic value.
1299 * TODO: maybe necessary to use big numbers in big irons.
1301 #define CHARGE_SIZE (32 * PAGE_SIZE)
1302 struct memcg_stock_pcp {
1303 struct mem_cgroup *cached; /* this never be root cgroup */
1305 struct work_struct work;
1307 static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1308 static atomic_t memcg_drain_count;
1311 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1312 * from local stock and true is returned. If the stock is 0 or charges from a
1313 * cgroup which is not current target, returns false. This stock will be
1316 static bool consume_stock(struct mem_cgroup *mem)
1318 struct memcg_stock_pcp *stock;
1321 stock = &get_cpu_var(memcg_stock);
1322 if (mem == stock->cached && stock->charge)
1323 stock->charge -= PAGE_SIZE;
1324 else /* need to call res_counter_charge */
1326 put_cpu_var(memcg_stock);
1331 * Returns stocks cached in percpu to res_counter and reset cached information.
1333 static void drain_stock(struct memcg_stock_pcp *stock)
1335 struct mem_cgroup *old = stock->cached;
1337 if (stock->charge) {
1338 res_counter_uncharge(&old->res, stock->charge);
1339 if (do_swap_account)
1340 res_counter_uncharge(&old->memsw, stock->charge);
1342 stock->cached = NULL;
1347 * This must be called under preempt disabled or must be called by
1348 * a thread which is pinned to local cpu.
1350 static void drain_local_stock(struct work_struct *dummy)
1352 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1357 * Cache charges(val) which is from res_counter, to local per_cpu area.
1358 * This will be consumed by consumt_stock() function, later.
1360 static void refill_stock(struct mem_cgroup *mem, int val)
1362 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1364 if (stock->cached != mem) { /* reset if necessary */
1366 stock->cached = mem;
1368 stock->charge += val;
1369 put_cpu_var(memcg_stock);
1373 * Tries to drain stocked charges in other cpus. This function is asynchronous
1374 * and just put a work per cpu for draining localy on each cpu. Caller can
1375 * expects some charges will be back to res_counter later but cannot wait for
1378 static void drain_all_stock_async(void)
1381 /* This function is for scheduling "drain" in asynchronous way.
1382 * The result of "drain" is not directly handled by callers. Then,
1383 * if someone is calling drain, we don't have to call drain more.
1384 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1385 * there is a race. We just do loose check here.
1387 if (atomic_read(&memcg_drain_count))
1389 /* Notify other cpus that system-wide "drain" is running */
1390 atomic_inc(&memcg_drain_count);
1392 for_each_online_cpu(cpu) {
1393 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1394 schedule_work_on(cpu, &stock->work);
1397 atomic_dec(&memcg_drain_count);
1398 /* We don't wait for flush_work */
1401 /* This is a synchronous drain interface. */
1402 static void drain_all_stock_sync(void)
1404 /* called when force_empty is called */
1405 atomic_inc(&memcg_drain_count);
1406 schedule_on_each_cpu(drain_local_stock);
1407 atomic_dec(&memcg_drain_count);
1410 static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1411 unsigned long action,
1414 int cpu = (unsigned long)hcpu;
1415 struct memcg_stock_pcp *stock;
1417 if (action != CPU_DEAD)
1419 stock = &per_cpu(memcg_stock, cpu);
1425 * Unlike exported interface, "oom" parameter is added. if oom==true,
1426 * oom-killer can be invoked.
1428 static int __mem_cgroup_try_charge(struct mm_struct *mm,
1429 gfp_t gfp_mask, struct mem_cgroup **memcg,
1430 bool oom, struct page *page)
1432 struct mem_cgroup *mem, *mem_over_limit;
1433 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1434 struct res_counter *fail_res;
1435 int csize = CHARGE_SIZE;
1437 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1438 /* Don't account this! */
1444 * We always charge the cgroup the mm_struct belongs to.
1445 * The mm_struct's mem_cgroup changes on task migration if the
1446 * thread group leader migrates. It's possible that mm is not
1447 * set, if so charge the init_mm (happens for pagecache usage).
1451 mem = try_get_mem_cgroup_from_mm(mm);
1459 VM_BUG_ON(css_is_removed(&mem->css));
1460 if (mem_cgroup_is_root(mem))
1465 unsigned long flags = 0;
1467 if (consume_stock(mem))
1470 ret = res_counter_charge(&mem->res, csize, &fail_res);
1472 if (!do_swap_account)
1474 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
1477 /* mem+swap counter fails */
1478 res_counter_uncharge(&mem->res, csize);
1479 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1480 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1483 /* mem counter fails */
1484 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1487 /* reduce request size and retry */
1488 if (csize > PAGE_SIZE) {
1492 if (!(gfp_mask & __GFP_WAIT))
1495 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1501 * try_to_free_mem_cgroup_pages() might not give us a full
1502 * picture of reclaim. Some pages are reclaimed and might be
1503 * moved to swap cache or just unmapped from the cgroup.
1504 * Check the limit again to see if the reclaim reduced the
1505 * current usage of the cgroup before giving up
1508 if (mem_cgroup_check_under_limit(mem_over_limit))
1511 if (!nr_retries--) {
1513 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1514 record_last_oom(mem_over_limit);
1519 if (csize > PAGE_SIZE)
1520 refill_stock(mem, csize - PAGE_SIZE);
1523 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1524 * if they exceeds softlimit.
1526 if (page && mem_cgroup_soft_limit_check(mem))
1527 mem_cgroup_update_tree(mem, page);
1536 * Somemtimes we have to undo a charge we got by try_charge().
1537 * This function is for that and do uncharge, put css's refcnt.
1538 * gotten by try_charge().
1540 static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1541 unsigned long count)
1543 if (!mem_cgroup_is_root(mem)) {
1544 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
1545 if (do_swap_account)
1546 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1547 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
1548 WARN_ON_ONCE(count > INT_MAX);
1549 __css_put(&mem->css, (int)count);
1551 /* we don't need css_put for root */
1554 static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1556 __mem_cgroup_cancel_charge(mem, 1);
1560 * A helper function to get mem_cgroup from ID. must be called under
1561 * rcu_read_lock(). The caller must check css_is_removed() or some if
1562 * it's concern. (dropping refcnt from swap can be called against removed
1565 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1567 struct cgroup_subsys_state *css;
1569 /* ID 0 is unused ID */
1572 css = css_lookup(&mem_cgroup_subsys, id);
1575 return container_of(css, struct mem_cgroup, css);
1578 struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
1580 struct mem_cgroup *mem = NULL;
1581 struct page_cgroup *pc;
1585 VM_BUG_ON(!PageLocked(page));
1587 pc = lookup_page_cgroup(page);
1588 lock_page_cgroup(pc);
1589 if (PageCgroupUsed(pc)) {
1590 mem = pc->mem_cgroup;
1591 if (mem && !css_tryget(&mem->css))
1593 } else if (PageSwapCache(page)) {
1594 ent.val = page_private(page);
1595 id = lookup_swap_cgroup(ent);
1597 mem = mem_cgroup_lookup(id);
1598 if (mem && !css_tryget(&mem->css))
1602 unlock_page_cgroup(pc);
1607 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1608 * USED state. If already USED, uncharge and return.
1611 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1612 struct page_cgroup *pc,
1613 enum charge_type ctype)
1615 /* try_charge() can return NULL to *memcg, taking care of it. */
1619 lock_page_cgroup(pc);
1620 if (unlikely(PageCgroupUsed(pc))) {
1621 unlock_page_cgroup(pc);
1622 mem_cgroup_cancel_charge(mem);
1626 pc->mem_cgroup = mem;
1628 * We access a page_cgroup asynchronously without lock_page_cgroup().
1629 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1630 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1631 * before USED bit, we need memory barrier here.
1632 * See mem_cgroup_add_lru_list(), etc.
1636 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1637 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1638 SetPageCgroupCache(pc);
1639 SetPageCgroupUsed(pc);
1641 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1642 ClearPageCgroupCache(pc);
1643 SetPageCgroupUsed(pc);
1649 mem_cgroup_charge_statistics(mem, pc, true);
1651 unlock_page_cgroup(pc);
1655 * __mem_cgroup_move_account - move account of the page
1656 * @pc: page_cgroup of the page.
1657 * @from: mem_cgroup which the page is moved from.
1658 * @to: mem_cgroup which the page is moved to. @from != @to.
1659 * @uncharge: whether we should call uncharge and css_put against @from.
1661 * The caller must confirm following.
1662 * - page is not on LRU (isolate_page() is useful.)
1663 * - the pc is locked, used, and ->mem_cgroup points to @from.
1665 * This function doesn't do "charge" nor css_get to new cgroup. It should be
1666 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
1667 * true, this function does "uncharge" from old cgroup, but it doesn't if
1668 * @uncharge is false, so a caller should do "uncharge".
1671 static void __mem_cgroup_move_account(struct page_cgroup *pc,
1672 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
1676 struct mem_cgroup_stat *stat;
1677 struct mem_cgroup_stat_cpu *cpustat;
1679 VM_BUG_ON(from == to);
1680 VM_BUG_ON(PageLRU(pc->page));
1681 VM_BUG_ON(!PageCgroupLocked(pc));
1682 VM_BUG_ON(!PageCgroupUsed(pc));
1683 VM_BUG_ON(pc->mem_cgroup != from);
1686 if (page_mapped(page) && !PageAnon(page)) {
1687 cpu = smp_processor_id();
1688 /* Update mapped_file data for mem_cgroup "from" */
1690 cpustat = &stat->cpustat[cpu];
1691 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
1694 /* Update mapped_file data for mem_cgroup "to" */
1696 cpustat = &stat->cpustat[cpu];
1697 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_FILE_MAPPED,
1700 mem_cgroup_charge_statistics(from, pc, false);
1702 /* This is not "cancel", but cancel_charge does all we need. */
1703 mem_cgroup_cancel_charge(from);
1705 /* caller should have done css_get */
1706 pc->mem_cgroup = to;
1707 mem_cgroup_charge_statistics(to, pc, true);
1709 * We charges against "to" which may not have any tasks. Then, "to"
1710 * can be under rmdir(). But in current implementation, caller of
1711 * this function is just force_empty() and move charge, so it's
1712 * garanteed that "to" is never removed. So, we don't check rmdir
1718 * check whether the @pc is valid for moving account and call
1719 * __mem_cgroup_move_account()
1721 static int mem_cgroup_move_account(struct page_cgroup *pc,
1722 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
1725 lock_page_cgroup(pc);
1726 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
1727 __mem_cgroup_move_account(pc, from, to, uncharge);
1730 unlock_page_cgroup(pc);
1735 * move charges to its parent.
1738 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1739 struct mem_cgroup *child,
1742 struct page *page = pc->page;
1743 struct cgroup *cg = child->css.cgroup;
1744 struct cgroup *pcg = cg->parent;
1745 struct mem_cgroup *parent;
1753 if (!get_page_unless_zero(page))
1755 if (isolate_lru_page(page))
1758 parent = mem_cgroup_from_cont(pcg);
1759 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
1763 ret = mem_cgroup_move_account(pc, child, parent, true);
1765 mem_cgroup_cancel_charge(parent);
1767 putback_lru_page(page);
1775 * Charge the memory controller for page usage.
1777 * 0 if the charge was successful
1778 * < 0 if the cgroup is over its limit
1780 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1781 gfp_t gfp_mask, enum charge_type ctype,
1782 struct mem_cgroup *memcg)
1784 struct mem_cgroup *mem;
1785 struct page_cgroup *pc;
1788 pc = lookup_page_cgroup(page);
1789 /* can happen at boot */
1795 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
1799 __mem_cgroup_commit_charge(mem, pc, ctype);
1803 int mem_cgroup_newpage_charge(struct page *page,
1804 struct mm_struct *mm, gfp_t gfp_mask)
1806 if (mem_cgroup_disabled())
1808 if (PageCompound(page))
1811 * If already mapped, we don't have to account.
1812 * If page cache, page->mapping has address_space.
1813 * But page->mapping may have out-of-use anon_vma pointer,
1814 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1817 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1821 return mem_cgroup_charge_common(page, mm, gfp_mask,
1822 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1826 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1827 enum charge_type ctype);
1829 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1832 struct mem_cgroup *mem = NULL;
1835 if (mem_cgroup_disabled())
1837 if (PageCompound(page))
1840 * Corner case handling. This is called from add_to_page_cache()
1841 * in usual. But some FS (shmem) precharges this page before calling it
1842 * and call add_to_page_cache() with GFP_NOWAIT.
1844 * For GFP_NOWAIT case, the page may be pre-charged before calling
1845 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1846 * charge twice. (It works but has to pay a bit larger cost.)
1847 * And when the page is SwapCache, it should take swap information
1848 * into account. This is under lock_page() now.
1850 if (!(gfp_mask & __GFP_WAIT)) {
1851 struct page_cgroup *pc;
1854 pc = lookup_page_cgroup(page);
1857 lock_page_cgroup(pc);
1858 if (PageCgroupUsed(pc)) {
1859 unlock_page_cgroup(pc);
1862 unlock_page_cgroup(pc);
1865 if (unlikely(!mm && !mem))
1868 if (page_is_file_cache(page))
1869 return mem_cgroup_charge_common(page, mm, gfp_mask,
1870 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1873 if (PageSwapCache(page)) {
1874 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1876 __mem_cgroup_commit_charge_swapin(page, mem,
1877 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1879 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1880 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1886 * While swap-in, try_charge -> commit or cancel, the page is locked.
1887 * And when try_charge() successfully returns, one refcnt to memcg without
1888 * struct page_cgroup is acquired. This refcnt will be consumed by
1889 * "commit()" or removed by "cancel()"
1891 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1893 gfp_t mask, struct mem_cgroup **ptr)
1895 struct mem_cgroup *mem;
1898 if (mem_cgroup_disabled())
1901 if (!do_swap_account)
1904 * A racing thread's fault, or swapoff, may have already updated
1905 * the pte, and even removed page from swap cache: in those cases
1906 * do_swap_page()'s pte_same() test will fail; but there's also a
1907 * KSM case which does need to charge the page.
1909 if (!PageSwapCache(page))
1911 mem = try_get_mem_cgroup_from_page(page);
1915 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
1916 /* drop extra refcnt from tryget */
1922 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
1926 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1927 enum charge_type ctype)
1929 struct page_cgroup *pc;
1931 if (mem_cgroup_disabled())
1935 cgroup_exclude_rmdir(&ptr->css);
1936 pc = lookup_page_cgroup(page);
1937 mem_cgroup_lru_del_before_commit_swapcache(page);
1938 __mem_cgroup_commit_charge(ptr, pc, ctype);
1939 mem_cgroup_lru_add_after_commit_swapcache(page);
1941 * Now swap is on-memory. This means this page may be
1942 * counted both as mem and swap....double count.
1943 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1944 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1945 * may call delete_from_swap_cache() before reach here.
1947 if (do_swap_account && PageSwapCache(page)) {
1948 swp_entry_t ent = {.val = page_private(page)};
1950 struct mem_cgroup *memcg;
1952 id = swap_cgroup_record(ent, 0);
1954 memcg = mem_cgroup_lookup(id);
1957 * This recorded memcg can be obsolete one. So, avoid
1958 * calling css_tryget
1960 if (!mem_cgroup_is_root(memcg))
1961 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1962 mem_cgroup_swap_statistics(memcg, false);
1963 mem_cgroup_put(memcg);
1968 * At swapin, we may charge account against cgroup which has no tasks.
1969 * So, rmdir()->pre_destroy() can be called while we do this charge.
1970 * In that case, we need to call pre_destroy() again. check it here.
1972 cgroup_release_and_wakeup_rmdir(&ptr->css);
1975 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1977 __mem_cgroup_commit_charge_swapin(page, ptr,
1978 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1981 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1983 if (mem_cgroup_disabled())
1987 mem_cgroup_cancel_charge(mem);
1991 __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
1993 struct memcg_batch_info *batch = NULL;
1994 bool uncharge_memsw = true;
1995 /* If swapout, usage of swap doesn't decrease */
1996 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1997 uncharge_memsw = false;
1999 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2000 * In those cases, all pages freed continously can be expected to be in
2001 * the same cgroup and we have chance to coalesce uncharges.
2002 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2003 * because we want to do uncharge as soon as possible.
2005 if (!current->memcg_batch.do_batch || test_thread_flag(TIF_MEMDIE))
2006 goto direct_uncharge;
2008 batch = ¤t->memcg_batch;
2010 * In usual, we do css_get() when we remember memcg pointer.
2011 * But in this case, we keep res->usage until end of a series of
2012 * uncharges. Then, it's ok to ignore memcg's refcnt.
2017 * In typical case, batch->memcg == mem. This means we can
2018 * merge a series of uncharges to an uncharge of res_counter.
2019 * If not, we uncharge res_counter ony by one.
2021 if (batch->memcg != mem)
2022 goto direct_uncharge;
2023 /* remember freed charge and uncharge it later */
2024 batch->bytes += PAGE_SIZE;
2026 batch->memsw_bytes += PAGE_SIZE;
2029 res_counter_uncharge(&mem->res, PAGE_SIZE);
2031 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2036 * uncharge if !page_mapped(page)
2038 static struct mem_cgroup *
2039 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
2041 struct page_cgroup *pc;
2042 struct mem_cgroup *mem = NULL;
2043 struct mem_cgroup_per_zone *mz;
2045 if (mem_cgroup_disabled())
2048 if (PageSwapCache(page))
2052 * Check if our page_cgroup is valid
2054 pc = lookup_page_cgroup(page);
2055 if (unlikely(!pc || !PageCgroupUsed(pc)))
2058 lock_page_cgroup(pc);
2060 mem = pc->mem_cgroup;
2062 if (!PageCgroupUsed(pc))
2066 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2067 case MEM_CGROUP_CHARGE_TYPE_DROP:
2068 if (page_mapped(page))
2071 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2072 if (!PageAnon(page)) { /* Shared memory */
2073 if (page->mapping && !page_is_file_cache(page))
2075 } else if (page_mapped(page)) /* Anon */
2082 if (!mem_cgroup_is_root(mem))
2083 __do_uncharge(mem, ctype);
2084 if (ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2085 mem_cgroup_swap_statistics(mem, true);
2086 mem_cgroup_charge_statistics(mem, pc, false);
2088 ClearPageCgroupUsed(pc);
2090 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2091 * freed from LRU. This is safe because uncharged page is expected not
2092 * to be reused (freed soon). Exception is SwapCache, it's handled by
2093 * special functions.
2096 mz = page_cgroup_zoneinfo(pc);
2097 unlock_page_cgroup(pc);
2099 if (mem_cgroup_soft_limit_check(mem))
2100 mem_cgroup_update_tree(mem, page);
2101 /* at swapout, this memcg will be accessed to record to swap */
2102 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2108 unlock_page_cgroup(pc);
2112 void mem_cgroup_uncharge_page(struct page *page)
2115 if (page_mapped(page))
2117 if (page->mapping && !PageAnon(page))
2119 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2122 void mem_cgroup_uncharge_cache_page(struct page *page)
2124 VM_BUG_ON(page_mapped(page));
2125 VM_BUG_ON(page->mapping);
2126 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2130 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2131 * In that cases, pages are freed continuously and we can expect pages
2132 * are in the same memcg. All these calls itself limits the number of
2133 * pages freed at once, then uncharge_start/end() is called properly.
2134 * This may be called prural(2) times in a context,
2137 void mem_cgroup_uncharge_start(void)
2139 current->memcg_batch.do_batch++;
2140 /* We can do nest. */
2141 if (current->memcg_batch.do_batch == 1) {
2142 current->memcg_batch.memcg = NULL;
2143 current->memcg_batch.bytes = 0;
2144 current->memcg_batch.memsw_bytes = 0;
2148 void mem_cgroup_uncharge_end(void)
2150 struct memcg_batch_info *batch = ¤t->memcg_batch;
2152 if (!batch->do_batch)
2156 if (batch->do_batch) /* If stacked, do nothing. */
2162 * This "batch->memcg" is valid without any css_get/put etc...
2163 * bacause we hide charges behind us.
2166 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2167 if (batch->memsw_bytes)
2168 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2169 /* forget this pointer (for sanity check) */
2170 batch->memcg = NULL;
2175 * called after __delete_from_swap_cache() and drop "page" account.
2176 * memcg information is recorded to swap_cgroup of "ent"
2179 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
2181 struct mem_cgroup *memcg;
2182 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
2184 if (!swapout) /* this was a swap cache but the swap is unused ! */
2185 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2187 memcg = __mem_cgroup_uncharge_common(page, ctype);
2189 /* record memcg information */
2190 if (do_swap_account && swapout && memcg) {
2191 swap_cgroup_record(ent, css_id(&memcg->css));
2192 mem_cgroup_get(memcg);
2194 if (swapout && memcg)
2195 css_put(&memcg->css);
2199 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2201 * called from swap_entry_free(). remove record in swap_cgroup and
2202 * uncharge "memsw" account.
2204 void mem_cgroup_uncharge_swap(swp_entry_t ent)
2206 struct mem_cgroup *memcg;
2209 if (!do_swap_account)
2212 id = swap_cgroup_record(ent, 0);
2214 memcg = mem_cgroup_lookup(id);
2217 * We uncharge this because swap is freed.
2218 * This memcg can be obsolete one. We avoid calling css_tryget
2220 if (!mem_cgroup_is_root(memcg))
2221 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
2222 mem_cgroup_swap_statistics(memcg, false);
2223 mem_cgroup_put(memcg);
2230 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2233 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
2235 struct page_cgroup *pc;
2236 struct mem_cgroup *mem = NULL;
2239 if (mem_cgroup_disabled())
2242 pc = lookup_page_cgroup(page);
2243 lock_page_cgroup(pc);
2244 if (PageCgroupUsed(pc)) {
2245 mem = pc->mem_cgroup;
2248 unlock_page_cgroup(pc);
2251 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
2259 /* remove redundant charge if migration failed*/
2260 void mem_cgroup_end_migration(struct mem_cgroup *mem,
2261 struct page *oldpage, struct page *newpage)
2263 struct page *target, *unused;
2264 struct page_cgroup *pc;
2265 enum charge_type ctype;
2269 cgroup_exclude_rmdir(&mem->css);
2270 /* at migration success, oldpage->mapping is NULL. */
2271 if (oldpage->mapping) {
2279 if (PageAnon(target))
2280 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2281 else if (page_is_file_cache(target))
2282 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2284 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2286 /* unused page is not on radix-tree now. */
2288 __mem_cgroup_uncharge_common(unused, ctype);
2290 pc = lookup_page_cgroup(target);
2292 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2293 * So, double-counting is effectively avoided.
2295 __mem_cgroup_commit_charge(mem, pc, ctype);
2298 * Both of oldpage and newpage are still under lock_page().
2299 * Then, we don't have to care about race in radix-tree.
2300 * But we have to be careful that this page is unmapped or not.
2302 * There is a case for !page_mapped(). At the start of
2303 * migration, oldpage was mapped. But now, it's zapped.
2304 * But we know *target* page is not freed/reused under us.
2305 * mem_cgroup_uncharge_page() does all necessary checks.
2307 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
2308 mem_cgroup_uncharge_page(target);
2310 * At migration, we may charge account against cgroup which has no tasks
2311 * So, rmdir()->pre_destroy() can be called while we do this charge.
2312 * In that case, we need to call pre_destroy() again. check it here.
2314 cgroup_release_and_wakeup_rmdir(&mem->css);
2318 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2319 * Calling hierarchical_reclaim is not enough because we should update
2320 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2321 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2322 * not from the memcg which this page would be charged to.
2323 * try_charge_swapin does all of these works properly.
2325 int mem_cgroup_shmem_charge_fallback(struct page *page,
2326 struct mm_struct *mm,
2329 struct mem_cgroup *mem = NULL;
2332 if (mem_cgroup_disabled())
2335 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2337 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
2342 static DEFINE_MUTEX(set_limit_mutex);
2344 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
2345 unsigned long long val)
2350 int children = mem_cgroup_count_children(memcg);
2351 u64 curusage, oldusage;
2354 * For keeping hierarchical_reclaim simple, how long we should retry
2355 * is depends on callers. We set our retry-count to be function
2356 * of # of children which we should visit in this loop.
2358 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2360 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2362 while (retry_count) {
2363 if (signal_pending(current)) {
2368 * Rather than hide all in some function, I do this in
2369 * open coded manner. You see what this really does.
2370 * We have to guarantee mem->res.limit < mem->memsw.limit.
2372 mutex_lock(&set_limit_mutex);
2373 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2374 if (memswlimit < val) {
2376 mutex_unlock(&set_limit_mutex);
2379 ret = res_counter_set_limit(&memcg->res, val);
2381 if (memswlimit == val)
2382 memcg->memsw_is_minimum = true;
2384 memcg->memsw_is_minimum = false;
2386 mutex_unlock(&set_limit_mutex);
2391 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2392 MEM_CGROUP_RECLAIM_SHRINK);
2393 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2394 /* Usage is reduced ? */
2395 if (curusage >= oldusage)
2398 oldusage = curusage;
2404 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2405 unsigned long long val)
2408 u64 memlimit, oldusage, curusage;
2409 int children = mem_cgroup_count_children(memcg);
2412 /* see mem_cgroup_resize_res_limit */
2413 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2414 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2415 while (retry_count) {
2416 if (signal_pending(current)) {
2421 * Rather than hide all in some function, I do this in
2422 * open coded manner. You see what this really does.
2423 * We have to guarantee mem->res.limit < mem->memsw.limit.
2425 mutex_lock(&set_limit_mutex);
2426 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2427 if (memlimit > val) {
2429 mutex_unlock(&set_limit_mutex);
2432 ret = res_counter_set_limit(&memcg->memsw, val);
2434 if (memlimit == val)
2435 memcg->memsw_is_minimum = true;
2437 memcg->memsw_is_minimum = false;
2439 mutex_unlock(&set_limit_mutex);
2444 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2445 MEM_CGROUP_RECLAIM_NOSWAP |
2446 MEM_CGROUP_RECLAIM_SHRINK);
2447 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2448 /* Usage is reduced ? */
2449 if (curusage >= oldusage)
2452 oldusage = curusage;
2457 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2458 gfp_t gfp_mask, int nid,
2461 unsigned long nr_reclaimed = 0;
2462 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2463 unsigned long reclaimed;
2465 struct mem_cgroup_tree_per_zone *mctz;
2466 unsigned long long excess;
2471 mctz = soft_limit_tree_node_zone(nid, zid);
2473 * This loop can run a while, specially if mem_cgroup's continuously
2474 * keep exceeding their soft limit and putting the system under
2481 mz = mem_cgroup_largest_soft_limit_node(mctz);
2485 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2487 MEM_CGROUP_RECLAIM_SOFT);
2488 nr_reclaimed += reclaimed;
2489 spin_lock(&mctz->lock);
2492 * If we failed to reclaim anything from this memory cgroup
2493 * it is time to move on to the next cgroup
2499 * Loop until we find yet another one.
2501 * By the time we get the soft_limit lock
2502 * again, someone might have aded the
2503 * group back on the RB tree. Iterate to
2504 * make sure we get a different mem.
2505 * mem_cgroup_largest_soft_limit_node returns
2506 * NULL if no other cgroup is present on
2510 __mem_cgroup_largest_soft_limit_node(mctz);
2511 if (next_mz == mz) {
2512 css_put(&next_mz->mem->css);
2514 } else /* next_mz == NULL or other memcg */
2518 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
2519 excess = res_counter_soft_limit_excess(&mz->mem->res);
2521 * One school of thought says that we should not add
2522 * back the node to the tree if reclaim returns 0.
2523 * But our reclaim could return 0, simply because due
2524 * to priority we are exposing a smaller subset of
2525 * memory to reclaim from. Consider this as a longer
2528 /* If excess == 0, no tree ops */
2529 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
2530 spin_unlock(&mctz->lock);
2531 css_put(&mz->mem->css);
2534 * Could not reclaim anything and there are no more
2535 * mem cgroups to try or we seem to be looping without
2536 * reclaiming anything.
2538 if (!nr_reclaimed &&
2540 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
2542 } while (!nr_reclaimed);
2544 css_put(&next_mz->mem->css);
2545 return nr_reclaimed;
2549 * This routine traverse page_cgroup in given list and drop them all.
2550 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2552 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
2553 int node, int zid, enum lru_list lru)
2556 struct mem_cgroup_per_zone *mz;
2557 struct page_cgroup *pc, *busy;
2558 unsigned long flags, loop;
2559 struct list_head *list;
2562 zone = &NODE_DATA(node)->node_zones[zid];
2563 mz = mem_cgroup_zoneinfo(mem, node, zid);
2564 list = &mz->lists[lru];
2566 loop = MEM_CGROUP_ZSTAT(mz, lru);
2567 /* give some margin against EBUSY etc...*/
2572 spin_lock_irqsave(&zone->lru_lock, flags);
2573 if (list_empty(list)) {
2574 spin_unlock_irqrestore(&zone->lru_lock, flags);
2577 pc = list_entry(list->prev, struct page_cgroup, lru);
2579 list_move(&pc->lru, list);
2581 spin_unlock_irqrestore(&zone->lru_lock, flags);
2584 spin_unlock_irqrestore(&zone->lru_lock, flags);
2586 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
2590 if (ret == -EBUSY || ret == -EINVAL) {
2591 /* found lock contention or "pc" is obsolete. */
2598 if (!ret && !list_empty(list))
2604 * make mem_cgroup's charge to be 0 if there is no task.
2605 * This enables deleting this mem_cgroup.
2607 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
2610 int node, zid, shrink;
2611 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
2612 struct cgroup *cgrp = mem->css.cgroup;
2617 /* should free all ? */
2623 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
2626 if (signal_pending(current))
2628 /* This is for making all *used* pages to be on LRU. */
2629 lru_add_drain_all();
2630 drain_all_stock_sync();
2632 for_each_node_state(node, N_HIGH_MEMORY) {
2633 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
2636 ret = mem_cgroup_force_empty_list(mem,
2645 /* it seems parent cgroup doesn't have enough mem */
2649 /* "ret" should also be checked to ensure all lists are empty. */
2650 } while (mem->res.usage > 0 || ret);
2656 /* returns EBUSY if there is a task or if we come here twice. */
2657 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
2661 /* we call try-to-free pages for make this cgroup empty */
2662 lru_add_drain_all();
2663 /* try to free all pages in this cgroup */
2665 while (nr_retries && mem->res.usage > 0) {
2668 if (signal_pending(current)) {
2672 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2673 false, get_swappiness(mem));
2676 /* maybe some writeback is necessary */
2677 congestion_wait(BLK_RW_ASYNC, HZ/10);
2682 /* try move_account...there may be some *locked* pages. */
2686 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2688 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2692 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2694 return mem_cgroup_from_cont(cont)->use_hierarchy;
2697 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2701 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2702 struct cgroup *parent = cont->parent;
2703 struct mem_cgroup *parent_mem = NULL;
2706 parent_mem = mem_cgroup_from_cont(parent);
2710 * If parent's use_hierarchy is set, we can't make any modifications
2711 * in the child subtrees. If it is unset, then the change can
2712 * occur, provided the current cgroup has no children.
2714 * For the root cgroup, parent_mem is NULL, we allow value to be
2715 * set if there are no children.
2717 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2718 (val == 1 || val == 0)) {
2719 if (list_empty(&cont->children))
2720 mem->use_hierarchy = val;
2730 struct mem_cgroup_idx_data {
2732 enum mem_cgroup_stat_index idx;
2736 mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
2738 struct mem_cgroup_idx_data *d = data;
2739 d->val += mem_cgroup_read_stat(&mem->stat, d->idx);
2744 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
2745 enum mem_cgroup_stat_index idx, s64 *val)
2747 struct mem_cgroup_idx_data d;
2750 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
2754 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2756 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2760 type = MEMFILE_TYPE(cft->private);
2761 name = MEMFILE_ATTR(cft->private);
2764 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2765 mem_cgroup_get_recursive_idx_stat(mem,
2766 MEM_CGROUP_STAT_CACHE, &idx_val);
2768 mem_cgroup_get_recursive_idx_stat(mem,
2769 MEM_CGROUP_STAT_RSS, &idx_val);
2773 val = res_counter_read_u64(&mem->res, name);
2776 if (name == RES_USAGE && mem_cgroup_is_root(mem)) {
2777 mem_cgroup_get_recursive_idx_stat(mem,
2778 MEM_CGROUP_STAT_CACHE, &idx_val);
2780 mem_cgroup_get_recursive_idx_stat(mem,
2781 MEM_CGROUP_STAT_RSS, &idx_val);
2783 mem_cgroup_get_recursive_idx_stat(mem,
2784 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
2788 val = res_counter_read_u64(&mem->memsw, name);
2797 * The user of this function is...
2800 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2803 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2805 unsigned long long val;
2808 type = MEMFILE_TYPE(cft->private);
2809 name = MEMFILE_ATTR(cft->private);
2812 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2816 /* This function does all necessary parse...reuse it */
2817 ret = res_counter_memparse_write_strategy(buffer, &val);
2821 ret = mem_cgroup_resize_limit(memcg, val);
2823 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2825 case RES_SOFT_LIMIT:
2826 ret = res_counter_memparse_write_strategy(buffer, &val);
2830 * For memsw, soft limits are hard to implement in terms
2831 * of semantics, for now, we support soft limits for
2832 * control without swap
2835 ret = res_counter_set_soft_limit(&memcg->res, val);
2840 ret = -EINVAL; /* should be BUG() ? */
2846 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2847 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2849 struct cgroup *cgroup;
2850 unsigned long long min_limit, min_memsw_limit, tmp;
2852 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2853 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2854 cgroup = memcg->css.cgroup;
2855 if (!memcg->use_hierarchy)
2858 while (cgroup->parent) {
2859 cgroup = cgroup->parent;
2860 memcg = mem_cgroup_from_cont(cgroup);
2861 if (!memcg->use_hierarchy)
2863 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2864 min_limit = min(min_limit, tmp);
2865 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2866 min_memsw_limit = min(min_memsw_limit, tmp);
2869 *mem_limit = min_limit;
2870 *memsw_limit = min_memsw_limit;
2874 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2876 struct mem_cgroup *mem;
2879 mem = mem_cgroup_from_cont(cont);
2880 type = MEMFILE_TYPE(event);
2881 name = MEMFILE_ATTR(event);
2885 res_counter_reset_max(&mem->res);
2887 res_counter_reset_max(&mem->memsw);
2891 res_counter_reset_failcnt(&mem->res);
2893 res_counter_reset_failcnt(&mem->memsw);
2900 static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
2903 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
2906 static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
2907 struct cftype *cft, u64 val)
2909 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
2911 if (val >= (1 << NR_MOVE_TYPE))
2914 * We check this value several times in both in can_attach() and
2915 * attach(), so we need cgroup lock to prevent this value from being
2919 mem->move_charge_at_immigrate = val;
2926 /* For read statistics */
2942 struct mcs_total_stat {
2943 s64 stat[NR_MCS_STAT];
2949 } memcg_stat_strings[NR_MCS_STAT] = {
2950 {"cache", "total_cache"},
2951 {"rss", "total_rss"},
2952 {"mapped_file", "total_mapped_file"},
2953 {"pgpgin", "total_pgpgin"},
2954 {"pgpgout", "total_pgpgout"},
2955 {"swap", "total_swap"},
2956 {"inactive_anon", "total_inactive_anon"},
2957 {"active_anon", "total_active_anon"},
2958 {"inactive_file", "total_inactive_file"},
2959 {"active_file", "total_active_file"},
2960 {"unevictable", "total_unevictable"}
2964 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2966 struct mcs_total_stat *s = data;
2970 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2971 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2972 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2973 s->stat[MCS_RSS] += val * PAGE_SIZE;
2974 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_FILE_MAPPED);
2975 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
2976 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2977 s->stat[MCS_PGPGIN] += val;
2978 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2979 s->stat[MCS_PGPGOUT] += val;
2980 if (do_swap_account) {
2981 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_SWAPOUT);
2982 s->stat[MCS_SWAP] += val * PAGE_SIZE;
2986 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2987 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2988 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2989 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2990 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2991 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2992 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2993 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2994 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2995 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3000 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3002 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3005 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3006 struct cgroup_map_cb *cb)
3008 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
3009 struct mcs_total_stat mystat;
3012 memset(&mystat, 0, sizeof(mystat));
3013 mem_cgroup_get_local_stat(mem_cont, &mystat);
3015 for (i = 0; i < NR_MCS_STAT; i++) {
3016 if (i == MCS_SWAP && !do_swap_account)
3018 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
3021 /* Hierarchical information */
3023 unsigned long long limit, memsw_limit;
3024 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3025 cb->fill(cb, "hierarchical_memory_limit", limit);
3026 if (do_swap_account)
3027 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3030 memset(&mystat, 0, sizeof(mystat));
3031 mem_cgroup_get_total_stat(mem_cont, &mystat);
3032 for (i = 0; i < NR_MCS_STAT; i++) {
3033 if (i == MCS_SWAP && !do_swap_account)
3035 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
3038 #ifdef CONFIG_DEBUG_VM
3039 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
3043 struct mem_cgroup_per_zone *mz;
3044 unsigned long recent_rotated[2] = {0, 0};
3045 unsigned long recent_scanned[2] = {0, 0};
3047 for_each_online_node(nid)
3048 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3049 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3051 recent_rotated[0] +=
3052 mz->reclaim_stat.recent_rotated[0];
3053 recent_rotated[1] +=
3054 mz->reclaim_stat.recent_rotated[1];
3055 recent_scanned[0] +=
3056 mz->reclaim_stat.recent_scanned[0];
3057 recent_scanned[1] +=
3058 mz->reclaim_stat.recent_scanned[1];
3060 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3061 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3062 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3063 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3070 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3072 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3074 return get_swappiness(memcg);
3077 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3080 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3081 struct mem_cgroup *parent;
3086 if (cgrp->parent == NULL)
3089 parent = mem_cgroup_from_cont(cgrp->parent);
3093 /* If under hierarchy, only empty-root can set this value */
3094 if ((parent->use_hierarchy) ||
3095 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3100 spin_lock(&memcg->reclaim_param_lock);
3101 memcg->swappiness = val;
3102 spin_unlock(&memcg->reclaim_param_lock);
3110 static struct cftype mem_cgroup_files[] = {
3112 .name = "usage_in_bytes",
3113 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
3114 .read_u64 = mem_cgroup_read,
3117 .name = "max_usage_in_bytes",
3118 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
3119 .trigger = mem_cgroup_reset,
3120 .read_u64 = mem_cgroup_read,
3123 .name = "limit_in_bytes",
3124 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
3125 .write_string = mem_cgroup_write,
3126 .read_u64 = mem_cgroup_read,
3129 .name = "soft_limit_in_bytes",
3130 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3131 .write_string = mem_cgroup_write,
3132 .read_u64 = mem_cgroup_read,
3136 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
3137 .trigger = mem_cgroup_reset,
3138 .read_u64 = mem_cgroup_read,
3142 .read_map = mem_control_stat_show,
3145 .name = "force_empty",
3146 .trigger = mem_cgroup_force_empty_write,
3149 .name = "use_hierarchy",
3150 .write_u64 = mem_cgroup_hierarchy_write,
3151 .read_u64 = mem_cgroup_hierarchy_read,
3154 .name = "swappiness",
3155 .read_u64 = mem_cgroup_swappiness_read,
3156 .write_u64 = mem_cgroup_swappiness_write,
3159 .name = "move_charge_at_immigrate",
3160 .read_u64 = mem_cgroup_move_charge_read,
3161 .write_u64 = mem_cgroup_move_charge_write,
3165 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3166 static struct cftype memsw_cgroup_files[] = {
3168 .name = "memsw.usage_in_bytes",
3169 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
3170 .read_u64 = mem_cgroup_read,
3173 .name = "memsw.max_usage_in_bytes",
3174 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
3175 .trigger = mem_cgroup_reset,
3176 .read_u64 = mem_cgroup_read,
3179 .name = "memsw.limit_in_bytes",
3180 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
3181 .write_string = mem_cgroup_write,
3182 .read_u64 = mem_cgroup_read,
3185 .name = "memsw.failcnt",
3186 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
3187 .trigger = mem_cgroup_reset,
3188 .read_u64 = mem_cgroup_read,
3192 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3194 if (!do_swap_account)
3196 return cgroup_add_files(cont, ss, memsw_cgroup_files,
3197 ARRAY_SIZE(memsw_cgroup_files));
3200 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
3206 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3208 struct mem_cgroup_per_node *pn;
3209 struct mem_cgroup_per_zone *mz;
3211 int zone, tmp = node;
3213 * This routine is called against possible nodes.
3214 * But it's BUG to call kmalloc() against offline node.
3216 * TODO: this routine can waste much memory for nodes which will
3217 * never be onlined. It's better to use memory hotplug callback
3220 if (!node_state(node, N_NORMAL_MEMORY))
3222 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
3226 mem->info.nodeinfo[node] = pn;
3227 memset(pn, 0, sizeof(*pn));
3229 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3230 mz = &pn->zoneinfo[zone];
3232 INIT_LIST_HEAD(&mz->lists[l]);
3233 mz->usage_in_excess = 0;
3234 mz->on_tree = false;
3240 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
3242 kfree(mem->info.nodeinfo[node]);
3245 static int mem_cgroup_size(void)
3247 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
3248 return sizeof(struct mem_cgroup) + cpustat_size;
3251 static struct mem_cgroup *mem_cgroup_alloc(void)
3253 struct mem_cgroup *mem;
3254 int size = mem_cgroup_size();
3256 if (size < PAGE_SIZE)
3257 mem = kmalloc(size, GFP_KERNEL);
3259 mem = vmalloc(size);
3262 memset(mem, 0, size);
3267 * At destroying mem_cgroup, references from swap_cgroup can remain.
3268 * (scanning all at force_empty is too costly...)
3270 * Instead of clearing all references at force_empty, we remember
3271 * the number of reference from swap_cgroup and free mem_cgroup when
3272 * it goes down to 0.
3274 * Removal of cgroup itself succeeds regardless of refs from swap.
3277 static void __mem_cgroup_free(struct mem_cgroup *mem)
3281 mem_cgroup_remove_from_trees(mem);
3282 free_css_id(&mem_cgroup_subsys, &mem->css);
3284 for_each_node_state(node, N_POSSIBLE)
3285 free_mem_cgroup_per_zone_info(mem, node);
3287 if (mem_cgroup_size() < PAGE_SIZE)
3293 static void mem_cgroup_get(struct mem_cgroup *mem)
3295 atomic_inc(&mem->refcnt);
3298 static void mem_cgroup_put(struct mem_cgroup *mem)
3300 if (atomic_dec_and_test(&mem->refcnt)) {
3301 struct mem_cgroup *parent = parent_mem_cgroup(mem);
3302 __mem_cgroup_free(mem);
3304 mem_cgroup_put(parent);
3309 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3311 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
3313 if (!mem->res.parent)
3315 return mem_cgroup_from_res_counter(mem->res.parent, res);
3318 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3319 static void __init enable_swap_cgroup(void)
3321 if (!mem_cgroup_disabled() && really_do_swap_account)
3322 do_swap_account = 1;
3325 static void __init enable_swap_cgroup(void)
3330 static int mem_cgroup_soft_limit_tree_init(void)
3332 struct mem_cgroup_tree_per_node *rtpn;
3333 struct mem_cgroup_tree_per_zone *rtpz;
3334 int tmp, node, zone;
3336 for_each_node_state(node, N_POSSIBLE) {
3338 if (!node_state(node, N_NORMAL_MEMORY))
3340 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
3344 soft_limit_tree.rb_tree_per_node[node] = rtpn;
3346 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
3347 rtpz = &rtpn->rb_tree_per_zone[zone];
3348 rtpz->rb_root = RB_ROOT;
3349 spin_lock_init(&rtpz->lock);
3355 static struct cgroup_subsys_state * __ref
3356 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
3358 struct mem_cgroup *mem, *parent;
3359 long error = -ENOMEM;
3362 mem = mem_cgroup_alloc();
3364 return ERR_PTR(error);
3366 for_each_node_state(node, N_POSSIBLE)
3367 if (alloc_mem_cgroup_per_zone_info(mem, node))
3371 if (cont->parent == NULL) {
3373 enable_swap_cgroup();
3375 root_mem_cgroup = mem;
3376 if (mem_cgroup_soft_limit_tree_init())
3378 for_each_possible_cpu(cpu) {
3379 struct memcg_stock_pcp *stock =
3380 &per_cpu(memcg_stock, cpu);
3381 INIT_WORK(&stock->work, drain_local_stock);
3383 hotcpu_notifier(memcg_stock_cpu_callback, 0);
3386 parent = mem_cgroup_from_cont(cont->parent);
3387 mem->use_hierarchy = parent->use_hierarchy;
3390 if (parent && parent->use_hierarchy) {
3391 res_counter_init(&mem->res, &parent->res);
3392 res_counter_init(&mem->memsw, &parent->memsw);
3394 * We increment refcnt of the parent to ensure that we can
3395 * safely access it on res_counter_charge/uncharge.
3396 * This refcnt will be decremented when freeing this
3397 * mem_cgroup(see mem_cgroup_put).
3399 mem_cgroup_get(parent);
3401 res_counter_init(&mem->res, NULL);
3402 res_counter_init(&mem->memsw, NULL);
3404 mem->last_scanned_child = 0;
3405 spin_lock_init(&mem->reclaim_param_lock);
3408 mem->swappiness = get_swappiness(parent);
3409 atomic_set(&mem->refcnt, 1);
3410 mem->move_charge_at_immigrate = 0;
3413 __mem_cgroup_free(mem);
3414 root_mem_cgroup = NULL;
3415 return ERR_PTR(error);
3418 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
3419 struct cgroup *cont)
3421 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3423 return mem_cgroup_force_empty(mem, false);
3426 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
3427 struct cgroup *cont)
3429 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3431 mem_cgroup_put(mem);
3434 static int mem_cgroup_populate(struct cgroup_subsys *ss,
3435 struct cgroup *cont)
3439 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
3440 ARRAY_SIZE(mem_cgroup_files));
3443 ret = register_memsw_files(cont, ss);
3447 /* Handlers for move charge at task migration. */
3448 #define PRECHARGE_COUNT_AT_ONCE 256
3449 static int mem_cgroup_do_precharge(unsigned long count)
3452 int batch_count = PRECHARGE_COUNT_AT_ONCE;
3453 struct mem_cgroup *mem = mc.to;
3455 if (mem_cgroup_is_root(mem)) {
3456 mc.precharge += count;
3457 /* we don't need css_get for root */
3460 /* try to charge at once */
3462 struct res_counter *dummy;
3464 * "mem" cannot be under rmdir() because we've already checked
3465 * by cgroup_lock_live_cgroup() that it is not removed and we
3466 * are still under the same cgroup_mutex. So we can postpone
3469 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
3471 if (do_swap_account && res_counter_charge(&mem->memsw,
3472 PAGE_SIZE * count, &dummy)) {
3473 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
3476 mc.precharge += count;
3477 VM_BUG_ON(test_bit(CSS_ROOT, &mem->css.flags));
3478 WARN_ON_ONCE(count > INT_MAX);
3479 __css_get(&mem->css, (int)count);
3483 /* fall back to one by one charge */
3485 if (signal_pending(current)) {
3489 if (!batch_count--) {
3490 batch_count = PRECHARGE_COUNT_AT_ONCE;
3493 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem,
3496 /* mem_cgroup_clear_mc() will do uncharge later */
3504 * is_target_pte_for_mc - check a pte whether it is valid for move charge
3505 * @vma: the vma the pte to be checked belongs
3506 * @addr: the address corresponding to the pte to be checked
3507 * @ptent: the pte to be checked
3508 * @target: the pointer the target page will be stored(can be NULL)
3511 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
3512 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
3513 * move charge. if @target is not NULL, the page is stored in target->page
3514 * with extra refcnt got(Callers should handle it).
3516 * Called with pte lock held.
3518 /* We add a new member later. */
3523 /* We add a new type later. */
3524 enum mc_target_type {
3525 MC_TARGET_NONE, /* not used */
3529 static int is_target_pte_for_mc(struct vm_area_struct *vma,
3530 unsigned long addr, pte_t ptent, union mc_target *target)
3533 struct page_cgroup *pc;
3535 bool move_anon = test_bit(MOVE_CHARGE_TYPE_ANON,
3536 &mc.to->move_charge_at_immigrate);
3538 if (!pte_present(ptent))
3541 page = vm_normal_page(vma, addr, ptent);
3542 if (!page || !page_mapped(page))
3545 * TODO: We don't move charges of file(including shmem/tmpfs) pages for
3548 if (!move_anon || !PageAnon(page))
3551 * TODO: We don't move charges of shared(used by multiple processes)
3554 if (page_mapcount(page) > 1)
3556 if (!get_page_unless_zero(page))
3559 pc = lookup_page_cgroup(page);
3561 * Do only loose check w/o page_cgroup lock. mem_cgroup_move_account()
3562 * checks the pc is valid or not under the lock.
3564 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
3565 ret = MC_TARGET_PAGE;
3567 target->page = page;
3570 if (!ret || !target)
3576 static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
3577 unsigned long addr, unsigned long end,
3578 struct mm_walk *walk)
3580 struct vm_area_struct *vma = walk->private;
3584 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
3585 for (; addr != end; pte++, addr += PAGE_SIZE)
3586 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
3587 mc.precharge++; /* increment precharge temporarily */
3588 pte_unmap_unlock(pte - 1, ptl);
3594 static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
3596 unsigned long precharge;
3597 struct vm_area_struct *vma;
3599 down_read(&mm->mmap_sem);
3600 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3601 struct mm_walk mem_cgroup_count_precharge_walk = {
3602 .pmd_entry = mem_cgroup_count_precharge_pte_range,
3606 if (is_vm_hugetlb_page(vma))
3608 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
3609 if (vma->vm_flags & VM_SHARED)
3611 walk_page_range(vma->vm_start, vma->vm_end,
3612 &mem_cgroup_count_precharge_walk);
3614 up_read(&mm->mmap_sem);
3616 precharge = mc.precharge;
3622 static int mem_cgroup_precharge_mc(struct mm_struct *mm)
3624 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
3627 static void mem_cgroup_clear_mc(void)
3629 /* we must uncharge all the leftover precharges from mc.to */
3631 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
3635 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
3636 * we must uncharge here.
3638 if (mc.moved_charge) {
3639 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
3640 mc.moved_charge = 0;
3646 static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
3647 struct cgroup *cgroup,
3648 struct task_struct *p,
3652 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
3654 if (mem->move_charge_at_immigrate) {
3655 struct mm_struct *mm;
3656 struct mem_cgroup *from = mem_cgroup_from_task(p);
3658 VM_BUG_ON(from == mem);
3660 mm = get_task_mm(p);
3663 /* We move charges only when we move a owner of the mm */
3664 if (mm->owner == p) {
3667 VM_BUG_ON(mc.precharge);
3668 VM_BUG_ON(mc.moved_charge);
3672 mc.moved_charge = 0;
3674 ret = mem_cgroup_precharge_mc(mm);
3676 mem_cgroup_clear_mc();
3683 static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
3684 struct cgroup *cgroup,
3685 struct task_struct *p,
3688 mem_cgroup_clear_mc();
3691 static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
3692 unsigned long addr, unsigned long end,
3693 struct mm_walk *walk)
3696 struct vm_area_struct *vma = walk->private;
3701 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
3702 for (; addr != end; addr += PAGE_SIZE) {
3703 pte_t ptent = *(pte++);
3704 union mc_target target;
3707 struct page_cgroup *pc;
3712 type = is_target_pte_for_mc(vma, addr, ptent, &target);
3714 case MC_TARGET_PAGE:
3716 if (isolate_lru_page(page))
3718 pc = lookup_page_cgroup(page);
3719 if (!mem_cgroup_move_account(pc,
3720 mc.from, mc.to, false)) {
3722 /* we uncharge from mc.from later. */
3725 putback_lru_page(page);
3726 put: /* is_target_pte_for_mc() gets the page */
3733 pte_unmap_unlock(pte - 1, ptl);
3738 * We have consumed all precharges we got in can_attach().
3739 * We try charge one by one, but don't do any additional
3740 * charges to mc.to if we have failed in charge once in attach()
3743 ret = mem_cgroup_do_precharge(1);
3751 static void mem_cgroup_move_charge(struct mm_struct *mm)
3753 struct vm_area_struct *vma;
3755 lru_add_drain_all();
3756 down_read(&mm->mmap_sem);
3757 for (vma = mm->mmap; vma; vma = vma->vm_next) {
3759 struct mm_walk mem_cgroup_move_charge_walk = {
3760 .pmd_entry = mem_cgroup_move_charge_pte_range,
3764 if (is_vm_hugetlb_page(vma))
3766 /* TODO: We don't move charges of shmem/tmpfs pages for now. */
3767 if (vma->vm_flags & VM_SHARED)
3769 ret = walk_page_range(vma->vm_start, vma->vm_end,
3770 &mem_cgroup_move_charge_walk);
3773 * means we have consumed all precharges and failed in
3774 * doing additional charge. Just abandon here.
3778 up_read(&mm->mmap_sem);
3781 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
3782 struct cgroup *cont,
3783 struct cgroup *old_cont,
3784 struct task_struct *p,
3787 struct mm_struct *mm;
3790 /* no need to move charge */
3793 mm = get_task_mm(p);
3795 mem_cgroup_move_charge(mm);
3798 mem_cgroup_clear_mc();
3801 struct cgroup_subsys mem_cgroup_subsys = {
3803 .subsys_id = mem_cgroup_subsys_id,
3804 .create = mem_cgroup_create,
3805 .pre_destroy = mem_cgroup_pre_destroy,
3806 .destroy = mem_cgroup_destroy,
3807 .populate = mem_cgroup_populate,
3808 .can_attach = mem_cgroup_can_attach,
3809 .cancel_attach = mem_cgroup_cancel_attach,
3810 .attach = mem_cgroup_move_task,
3815 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3817 static int __init disable_swap_account(char *s)
3819 really_do_swap_account = 0;
3822 __setup("noswapaccount", disable_swap_account);