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>
10 * Copyright (C) 2009 Nokia Corporation
11 * Author: Kirill A. Shutemov
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
24 #include <linux/res_counter.h>
25 #include <linux/memcontrol.h>
26 #include <linux/cgroup.h>
28 #include <linux/hugetlb.h>
29 #include <linux/pagemap.h>
30 #include <linux/smp.h>
31 #include <linux/page-flags.h>
32 #include <linux/backing-dev.h>
33 #include <linux/bit_spinlock.h>
34 #include <linux/rcupdate.h>
35 #include <linux/limits.h>
36 #include <linux/mutex.h>
37 #include <linux/rbtree.h>
38 #include <linux/slab.h>
39 #include <linux/swap.h>
40 #include <linux/swapops.h>
41 #include <linux/spinlock.h>
42 #include <linux/eventfd.h>
43 #include <linux/sort.h>
45 #include <linux/seq_file.h>
46 #include <linux/vmalloc.h>
47 #include <linux/mm_inline.h>
48 #include <linux/page_cgroup.h>
49 #include <linux/cpu.h>
50 #include <linux/oom.h>
53 #include <asm/uaccess.h>
55 #include <trace/events/vmscan.h>
57 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
58 #define MEM_CGROUP_RECLAIM_RETRIES 5
59 struct mem_cgroup *root_mem_cgroup __read_mostly;
61 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
62 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
63 int do_swap_account __read_mostly;
64 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
66 #define do_swap_account (0)
70 * Per memcg event counter is incremented at every pagein/pageout. This counter
71 * is used for trigger some periodic events. This is straightforward and better
72 * than using jiffies etc. to handle periodic memcg event.
74 * These values will be used as !((event) & ((1 <<(thresh)) - 1))
76 #define THRESHOLDS_EVENTS_THRESH (7) /* once in 128 */
77 #define SOFTLIMIT_EVENTS_THRESH (10) /* once in 1024 */
80 * Statistics for memory cgroup.
82 enum mem_cgroup_stat_index {
84 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
86 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
87 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
88 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
89 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
90 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
91 MEM_CGROUP_STAT_SWAPOUT, /* # of pages, swapped out */
92 MEM_CGROUP_EVENTS, /* incremented at every pagein/pageout */
93 MEM_CGROUP_ON_MOVE, /* someone is moving account between groups */
95 MEM_CGROUP_STAT_NSTATS,
98 struct mem_cgroup_stat_cpu {
99 s64 count[MEM_CGROUP_STAT_NSTATS];
103 * per-zone information in memory controller.
105 struct mem_cgroup_per_zone {
107 * spin_lock to protect the per cgroup LRU
109 struct list_head lists[NR_LRU_LISTS];
110 unsigned long count[NR_LRU_LISTS];
112 struct zone_reclaim_stat reclaim_stat;
113 struct rb_node tree_node; /* RB tree node */
114 unsigned long long usage_in_excess;/* Set to the value by which */
115 /* the soft limit is exceeded*/
117 struct mem_cgroup *mem; /* Back pointer, we cannot */
118 /* use container_of */
120 /* Macro for accessing counter */
121 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
123 struct mem_cgroup_per_node {
124 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
127 struct mem_cgroup_lru_info {
128 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
132 * Cgroups above their limits are maintained in a RB-Tree, independent of
133 * their hierarchy representation
136 struct mem_cgroup_tree_per_zone {
137 struct rb_root rb_root;
141 struct mem_cgroup_tree_per_node {
142 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
145 struct mem_cgroup_tree {
146 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
149 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
151 struct mem_cgroup_threshold {
152 struct eventfd_ctx *eventfd;
157 struct mem_cgroup_threshold_ary {
158 /* An array index points to threshold just below usage. */
159 int current_threshold;
160 /* Size of entries[] */
162 /* Array of thresholds */
163 struct mem_cgroup_threshold entries[0];
166 struct mem_cgroup_thresholds {
167 /* Primary thresholds array */
168 struct mem_cgroup_threshold_ary *primary;
170 * Spare threshold array.
171 * This is needed to make mem_cgroup_unregister_event() "never fail".
172 * It must be able to store at least primary->size - 1 entries.
174 struct mem_cgroup_threshold_ary *spare;
178 struct mem_cgroup_eventfd_list {
179 struct list_head list;
180 struct eventfd_ctx *eventfd;
183 static void mem_cgroup_threshold(struct mem_cgroup *mem);
184 static void mem_cgroup_oom_notify(struct mem_cgroup *mem);
187 * The memory controller data structure. The memory controller controls both
188 * page cache and RSS per cgroup. We would eventually like to provide
189 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
190 * to help the administrator determine what knobs to tune.
192 * TODO: Add a water mark for the memory controller. Reclaim will begin when
193 * we hit the water mark. May be even add a low water mark, such that
194 * no reclaim occurs from a cgroup at it's low water mark, this is
195 * a feature that will be implemented much later in the future.
198 struct cgroup_subsys_state css;
200 * the counter to account for memory usage
202 struct res_counter res;
204 * the counter to account for mem+swap usage.
206 struct res_counter memsw;
208 * Per cgroup active and inactive list, similar to the
209 * per zone LRU lists.
211 struct mem_cgroup_lru_info info;
214 protect against reclaim related member.
216 spinlock_t reclaim_param_lock;
219 * While reclaiming in a hierarchy, we cache the last child we
222 int last_scanned_child;
224 * Should the accounting and control be hierarchical, per subtree?
230 unsigned int swappiness;
231 /* OOM-Killer disable */
232 int oom_kill_disable;
234 /* set when res.limit == memsw.limit */
235 bool memsw_is_minimum;
237 /* protect arrays of thresholds */
238 struct mutex thresholds_lock;
240 /* thresholds for memory usage. RCU-protected */
241 struct mem_cgroup_thresholds thresholds;
243 /* thresholds for mem+swap usage. RCU-protected */
244 struct mem_cgroup_thresholds memsw_thresholds;
246 /* For oom notifier event fd */
247 struct list_head oom_notify;
250 * Should we move charges of a task when a task is moved into this
251 * mem_cgroup ? And what type of charges should we move ?
253 unsigned long move_charge_at_immigrate;
257 struct mem_cgroup_stat_cpu *stat;
260 /* Stuffs for move charges at task migration. */
262 * Types of charges to be moved. "move_charge_at_immitgrate" is treated as a
263 * left-shifted bitmap of these types.
266 MOVE_CHARGE_TYPE_ANON, /* private anonymous page and swap of it */
267 MOVE_CHARGE_TYPE_FILE, /* file page(including tmpfs) and swap of it */
271 /* "mc" and its members are protected by cgroup_mutex */
272 static struct move_charge_struct {
273 spinlock_t lock; /* for from, to, moving_task */
274 struct mem_cgroup *from;
275 struct mem_cgroup *to;
276 unsigned long precharge;
277 unsigned long moved_charge;
278 unsigned long moved_swap;
279 struct task_struct *moving_task; /* a task moving charges */
280 wait_queue_head_t waitq; /* a waitq for other context */
282 .lock = __SPIN_LOCK_UNLOCKED(mc.lock),
283 .waitq = __WAIT_QUEUE_HEAD_INITIALIZER(mc.waitq),
286 static bool move_anon(void)
288 return test_bit(MOVE_CHARGE_TYPE_ANON,
289 &mc.to->move_charge_at_immigrate);
292 static bool move_file(void)
294 return test_bit(MOVE_CHARGE_TYPE_FILE,
295 &mc.to->move_charge_at_immigrate);
299 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
300 * limit reclaim to prevent infinite loops, if they ever occur.
302 #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
303 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
306 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
307 MEM_CGROUP_CHARGE_TYPE_MAPPED,
308 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
309 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
310 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
311 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
315 /* only for here (for easy reading.) */
316 #define PCGF_CACHE (1UL << PCG_CACHE)
317 #define PCGF_USED (1UL << PCG_USED)
318 #define PCGF_LOCK (1UL << PCG_LOCK)
319 /* Not used, but added here for completeness */
320 #define PCGF_ACCT (1UL << PCG_ACCT)
322 /* for encoding cft->private value on file */
325 #define _OOM_TYPE (2)
326 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
327 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
328 #define MEMFILE_ATTR(val) ((val) & 0xffff)
329 /* Used for OOM nofiier */
330 #define OOM_CONTROL (0)
333 * Reclaim flags for mem_cgroup_hierarchical_reclaim
335 #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
336 #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
337 #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
338 #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
339 #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
340 #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
342 static void mem_cgroup_get(struct mem_cgroup *mem);
343 static void mem_cgroup_put(struct mem_cgroup *mem);
344 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
345 static void drain_all_stock_async(void);
347 static struct mem_cgroup_per_zone *
348 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
350 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
353 struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *mem)
358 static struct mem_cgroup_per_zone *
359 page_cgroup_zoneinfo(struct page_cgroup *pc)
361 struct mem_cgroup *mem = pc->mem_cgroup;
362 int nid = page_cgroup_nid(pc);
363 int zid = page_cgroup_zid(pc);
368 return mem_cgroup_zoneinfo(mem, nid, zid);
371 static struct mem_cgroup_tree_per_zone *
372 soft_limit_tree_node_zone(int nid, int zid)
374 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
377 static struct mem_cgroup_tree_per_zone *
378 soft_limit_tree_from_page(struct page *page)
380 int nid = page_to_nid(page);
381 int zid = page_zonenum(page);
383 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
387 __mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
388 struct mem_cgroup_per_zone *mz,
389 struct mem_cgroup_tree_per_zone *mctz,
390 unsigned long long new_usage_in_excess)
392 struct rb_node **p = &mctz->rb_root.rb_node;
393 struct rb_node *parent = NULL;
394 struct mem_cgroup_per_zone *mz_node;
399 mz->usage_in_excess = new_usage_in_excess;
400 if (!mz->usage_in_excess)
404 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
406 if (mz->usage_in_excess < mz_node->usage_in_excess)
409 * We can't avoid mem cgroups that are over their soft
410 * limit by the same amount
412 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
415 rb_link_node(&mz->tree_node, parent, p);
416 rb_insert_color(&mz->tree_node, &mctz->rb_root);
421 __mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
422 struct mem_cgroup_per_zone *mz,
423 struct mem_cgroup_tree_per_zone *mctz)
427 rb_erase(&mz->tree_node, &mctz->rb_root);
432 mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
433 struct mem_cgroup_per_zone *mz,
434 struct mem_cgroup_tree_per_zone *mctz)
436 spin_lock(&mctz->lock);
437 __mem_cgroup_remove_exceeded(mem, mz, mctz);
438 spin_unlock(&mctz->lock);
442 static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
444 unsigned long long excess;
445 struct mem_cgroup_per_zone *mz;
446 struct mem_cgroup_tree_per_zone *mctz;
447 int nid = page_to_nid(page);
448 int zid = page_zonenum(page);
449 mctz = soft_limit_tree_from_page(page);
452 * Necessary to update all ancestors when hierarchy is used.
453 * because their event counter is not touched.
455 for (; mem; mem = parent_mem_cgroup(mem)) {
456 mz = mem_cgroup_zoneinfo(mem, nid, zid);
457 excess = res_counter_soft_limit_excess(&mem->res);
459 * We have to update the tree if mz is on RB-tree or
460 * mem is over its softlimit.
462 if (excess || mz->on_tree) {
463 spin_lock(&mctz->lock);
464 /* if on-tree, remove it */
466 __mem_cgroup_remove_exceeded(mem, mz, mctz);
468 * Insert again. mz->usage_in_excess will be updated.
469 * If excess is 0, no tree ops.
471 __mem_cgroup_insert_exceeded(mem, mz, mctz, excess);
472 spin_unlock(&mctz->lock);
477 static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
480 struct mem_cgroup_per_zone *mz;
481 struct mem_cgroup_tree_per_zone *mctz;
483 for_each_node_state(node, N_POSSIBLE) {
484 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
485 mz = mem_cgroup_zoneinfo(mem, node, zone);
486 mctz = soft_limit_tree_node_zone(node, zone);
487 mem_cgroup_remove_exceeded(mem, mz, mctz);
492 static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup *mem)
494 return res_counter_soft_limit_excess(&mem->res) >> PAGE_SHIFT;
497 static struct mem_cgroup_per_zone *
498 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
500 struct rb_node *rightmost = NULL;
501 struct mem_cgroup_per_zone *mz;
505 rightmost = rb_last(&mctz->rb_root);
507 goto done; /* Nothing to reclaim from */
509 mz = rb_entry(rightmost, struct mem_cgroup_per_zone, tree_node);
511 * Remove the node now but someone else can add it back,
512 * we will to add it back at the end of reclaim to its correct
513 * position in the tree.
515 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
516 if (!res_counter_soft_limit_excess(&mz->mem->res) ||
517 !css_tryget(&mz->mem->css))
523 static struct mem_cgroup_per_zone *
524 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone *mctz)
526 struct mem_cgroup_per_zone *mz;
528 spin_lock(&mctz->lock);
529 mz = __mem_cgroup_largest_soft_limit_node(mctz);
530 spin_unlock(&mctz->lock);
534 static s64 mem_cgroup_read_stat(struct mem_cgroup *mem,
535 enum mem_cgroup_stat_index idx)
540 for_each_possible_cpu(cpu)
541 val += per_cpu(mem->stat->count[idx], cpu);
545 static s64 mem_cgroup_local_usage(struct mem_cgroup *mem)
549 ret = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
550 ret += mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
554 static void mem_cgroup_swap_statistics(struct mem_cgroup *mem,
557 int val = (charge) ? 1 : -1;
558 this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_SWAPOUT], val);
561 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
562 struct page_cgroup *pc,
565 int val = (charge) ? 1 : -1;
569 if (PageCgroupCache(pc))
570 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_CACHE], val);
572 __this_cpu_add(mem->stat->count[MEM_CGROUP_STAT_RSS], val);
575 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGIN_COUNT]);
577 __this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_PGPGOUT_COUNT]);
578 __this_cpu_inc(mem->stat->count[MEM_CGROUP_EVENTS]);
583 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
587 struct mem_cgroup_per_zone *mz;
590 for_each_online_node(nid)
591 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
592 mz = mem_cgroup_zoneinfo(mem, nid, zid);
593 total += MEM_CGROUP_ZSTAT(mz, idx);
598 static bool __memcg_event_check(struct mem_cgroup *mem, int event_mask_shift)
602 val = this_cpu_read(mem->stat->count[MEM_CGROUP_EVENTS]);
604 return !(val & ((1 << event_mask_shift) - 1));
608 * Check events in order.
611 static void memcg_check_events(struct mem_cgroup *mem, struct page *page)
613 /* threshold event is triggered in finer grain than soft limit */
614 if (unlikely(__memcg_event_check(mem, THRESHOLDS_EVENTS_THRESH))) {
615 mem_cgroup_threshold(mem);
616 if (unlikely(__memcg_event_check(mem, SOFTLIMIT_EVENTS_THRESH)))
617 mem_cgroup_update_tree(mem, page);
621 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
623 return container_of(cgroup_subsys_state(cont,
624 mem_cgroup_subsys_id), struct mem_cgroup,
628 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
631 * mm_update_next_owner() may clear mm->owner to NULL
632 * if it races with swapoff, page migration, etc.
633 * So this can be called with p == NULL.
638 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
639 struct mem_cgroup, css);
642 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
644 struct mem_cgroup *mem = NULL;
649 * Because we have no locks, mm->owner's may be being moved to other
650 * cgroup. We use css_tryget() here even if this looks
651 * pessimistic (rather than adding locks here).
655 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
658 } while (!css_tryget(&mem->css));
664 * Call callback function against all cgroup under hierarchy tree.
666 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
667 int (*func)(struct mem_cgroup *, void *))
669 int found, ret, nextid;
670 struct cgroup_subsys_state *css;
671 struct mem_cgroup *mem;
673 if (!root->use_hierarchy)
674 return (*func)(root, data);
682 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
684 if (css && css_tryget(css))
685 mem = container_of(css, struct mem_cgroup, css);
689 ret = (*func)(mem, data);
693 } while (!ret && css);
698 static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
700 return (mem == root_mem_cgroup);
704 * Following LRU functions are allowed to be used without PCG_LOCK.
705 * Operations are called by routine of global LRU independently from memcg.
706 * What we have to take care of here is validness of pc->mem_cgroup.
708 * Changes to pc->mem_cgroup happens when
711 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
712 * It is added to LRU before charge.
713 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
714 * When moving account, the page is not on LRU. It's isolated.
717 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
719 struct page_cgroup *pc;
720 struct mem_cgroup_per_zone *mz;
722 if (mem_cgroup_disabled())
724 pc = lookup_page_cgroup(page);
725 /* can happen while we handle swapcache. */
726 if (!TestClearPageCgroupAcctLRU(pc))
728 VM_BUG_ON(!pc->mem_cgroup);
730 * We don't check PCG_USED bit. It's cleared when the "page" is finally
731 * removed from global LRU.
733 mz = page_cgroup_zoneinfo(pc);
734 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
735 if (mem_cgroup_is_root(pc->mem_cgroup))
737 VM_BUG_ON(list_empty(&pc->lru));
738 list_del_init(&pc->lru);
742 void mem_cgroup_del_lru(struct page *page)
744 mem_cgroup_del_lru_list(page, page_lru(page));
747 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
749 struct mem_cgroup_per_zone *mz;
750 struct page_cgroup *pc;
752 if (mem_cgroup_disabled())
755 pc = lookup_page_cgroup(page);
757 * Used bit is set without atomic ops but after smp_wmb().
758 * For making pc->mem_cgroup visible, insert smp_rmb() here.
761 /* unused or root page is not rotated. */
762 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
764 mz = page_cgroup_zoneinfo(pc);
765 list_move(&pc->lru, &mz->lists[lru]);
768 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
770 struct page_cgroup *pc;
771 struct mem_cgroup_per_zone *mz;
773 if (mem_cgroup_disabled())
775 pc = lookup_page_cgroup(page);
776 VM_BUG_ON(PageCgroupAcctLRU(pc));
778 * Used bit is set without atomic ops but after smp_wmb().
779 * For making pc->mem_cgroup visible, insert smp_rmb() here.
782 if (!PageCgroupUsed(pc))
785 mz = page_cgroup_zoneinfo(pc);
786 MEM_CGROUP_ZSTAT(mz, lru) += 1;
787 SetPageCgroupAcctLRU(pc);
788 if (mem_cgroup_is_root(pc->mem_cgroup))
790 list_add(&pc->lru, &mz->lists[lru]);
794 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
795 * lru because the page may.be reused after it's fully uncharged (because of
796 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
797 * it again. This function is only used to charge SwapCache. It's done under
798 * lock_page and expected that zone->lru_lock is never held.
800 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
803 struct zone *zone = page_zone(page);
804 struct page_cgroup *pc = lookup_page_cgroup(page);
806 spin_lock_irqsave(&zone->lru_lock, flags);
808 * Forget old LRU when this page_cgroup is *not* used. This Used bit
809 * is guarded by lock_page() because the page is SwapCache.
811 if (!PageCgroupUsed(pc))
812 mem_cgroup_del_lru_list(page, page_lru(page));
813 spin_unlock_irqrestore(&zone->lru_lock, flags);
816 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
819 struct zone *zone = page_zone(page);
820 struct page_cgroup *pc = lookup_page_cgroup(page);
822 spin_lock_irqsave(&zone->lru_lock, flags);
823 /* link when the page is linked to LRU but page_cgroup isn't */
824 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
825 mem_cgroup_add_lru_list(page, page_lru(page));
826 spin_unlock_irqrestore(&zone->lru_lock, flags);
830 void mem_cgroup_move_lists(struct page *page,
831 enum lru_list from, enum lru_list to)
833 if (mem_cgroup_disabled())
835 mem_cgroup_del_lru_list(page, from);
836 mem_cgroup_add_lru_list(page, to);
839 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
842 struct mem_cgroup *curr = NULL;
843 struct task_struct *p;
845 p = find_lock_task_mm(task);
848 curr = try_get_mem_cgroup_from_mm(p->mm);
853 * We should check use_hierarchy of "mem" not "curr". Because checking
854 * use_hierarchy of "curr" here make this function true if hierarchy is
855 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
856 * hierarchy(even if use_hierarchy is disabled in "mem").
858 if (mem->use_hierarchy)
859 ret = css_is_ancestor(&curr->css, &mem->css);
866 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
868 unsigned long active;
869 unsigned long inactive;
871 unsigned long inactive_ratio;
873 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
874 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
876 gb = (inactive + active) >> (30 - PAGE_SHIFT);
878 inactive_ratio = int_sqrt(10 * gb);
883 present_pages[0] = inactive;
884 present_pages[1] = active;
887 return inactive_ratio;
890 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
892 unsigned long active;
893 unsigned long inactive;
894 unsigned long present_pages[2];
895 unsigned long inactive_ratio;
897 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
899 inactive = present_pages[0];
900 active = present_pages[1];
902 if (inactive * inactive_ratio < active)
908 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
910 unsigned long active;
911 unsigned long inactive;
913 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
914 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
916 return (active > inactive);
919 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
923 int nid = zone_to_nid(zone);
924 int zid = zone_idx(zone);
925 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
927 return MEM_CGROUP_ZSTAT(mz, lru);
930 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
933 int nid = zone_to_nid(zone);
934 int zid = zone_idx(zone);
935 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
937 return &mz->reclaim_stat;
940 struct zone_reclaim_stat *
941 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
943 struct page_cgroup *pc;
944 struct mem_cgroup_per_zone *mz;
946 if (mem_cgroup_disabled())
949 pc = lookup_page_cgroup(page);
951 * Used bit is set without atomic ops but after smp_wmb().
952 * For making pc->mem_cgroup visible, insert smp_rmb() here.
955 if (!PageCgroupUsed(pc))
958 mz = page_cgroup_zoneinfo(pc);
962 return &mz->reclaim_stat;
965 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
966 struct list_head *dst,
967 unsigned long *scanned, int order,
968 int mode, struct zone *z,
969 struct mem_cgroup *mem_cont,
970 int active, int file)
972 unsigned long nr_taken = 0;
976 struct list_head *src;
977 struct page_cgroup *pc, *tmp;
978 int nid = zone_to_nid(z);
979 int zid = zone_idx(z);
980 struct mem_cgroup_per_zone *mz;
981 int lru = LRU_FILE * file + active;
985 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
986 src = &mz->lists[lru];
989 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
990 if (scan >= nr_to_scan)
994 if (unlikely(!PageCgroupUsed(pc)))
996 if (unlikely(!PageLRU(page)))
1000 ret = __isolate_lru_page(page, mode, file);
1003 list_move(&page->lru, dst);
1004 mem_cgroup_del_lru(page);
1008 /* we don't affect global LRU but rotate in our LRU */
1009 mem_cgroup_rotate_lru_list(page, page_lru(page));
1018 trace_mm_vmscan_memcg_isolate(0, nr_to_scan, scan, nr_taken,
1024 #define mem_cgroup_from_res_counter(counter, member) \
1025 container_of(counter, struct mem_cgroup, member)
1027 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
1029 if (do_swap_account) {
1030 if (res_counter_check_under_limit(&mem->res) &&
1031 res_counter_check_under_limit(&mem->memsw))
1034 if (res_counter_check_under_limit(&mem->res))
1039 static unsigned int get_swappiness(struct mem_cgroup *memcg)
1041 struct cgroup *cgrp = memcg->css.cgroup;
1042 unsigned int swappiness;
1045 if (cgrp->parent == NULL)
1046 return vm_swappiness;
1048 spin_lock(&memcg->reclaim_param_lock);
1049 swappiness = memcg->swappiness;
1050 spin_unlock(&memcg->reclaim_param_lock);
1055 static void mem_cgroup_start_move(struct mem_cgroup *mem)
1058 /* Because this is for moving account, reuse mc.lock */
1059 spin_lock(&mc.lock);
1060 for_each_possible_cpu(cpu)
1061 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) += 1;
1062 spin_unlock(&mc.lock);
1067 static void mem_cgroup_end_move(struct mem_cgroup *mem)
1073 spin_lock(&mc.lock);
1074 for_each_possible_cpu(cpu)
1075 per_cpu(mem->stat->count[MEM_CGROUP_ON_MOVE], cpu) -= 1;
1076 spin_unlock(&mc.lock);
1079 * 2 routines for checking "mem" is under move_account() or not.
1081 * mem_cgroup_stealed() - checking a cgroup is mc.from or not. This is used
1082 * for avoiding race in accounting. If true,
1083 * pc->mem_cgroup may be overwritten.
1085 * mem_cgroup_under_move() - checking a cgroup is mc.from or mc.to or
1086 * under hierarchy of moving cgroups. This is for
1087 * waiting at hith-memory prressure caused by "move".
1090 static bool mem_cgroup_stealed(struct mem_cgroup *mem)
1092 VM_BUG_ON(!rcu_read_lock_held());
1093 return this_cpu_read(mem->stat->count[MEM_CGROUP_ON_MOVE]) > 0;
1096 static bool mem_cgroup_under_move(struct mem_cgroup *mem)
1098 struct mem_cgroup *from;
1099 struct mem_cgroup *to;
1102 * Unlike task_move routines, we access mc.to, mc.from not under
1103 * mutual exclusion by cgroup_mutex. Here, we take spinlock instead.
1105 spin_lock(&mc.lock);
1110 if (from == mem || to == mem
1111 || (mem->use_hierarchy && css_is_ancestor(&from->css, &mem->css))
1112 || (mem->use_hierarchy && css_is_ancestor(&to->css, &mem->css)))
1115 spin_unlock(&mc.lock);
1119 static bool mem_cgroup_wait_acct_move(struct mem_cgroup *mem)
1121 if (mc.moving_task && current != mc.moving_task) {
1122 if (mem_cgroup_under_move(mem)) {
1124 prepare_to_wait(&mc.waitq, &wait, TASK_INTERRUPTIBLE);
1125 /* moving charge context might have finished. */
1128 finish_wait(&mc.waitq, &wait);
1135 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
1143 * mem_cgroup_print_oom_info: Called from OOM with tasklist_lock held in read mode.
1144 * @memcg: The memory cgroup that went over limit
1145 * @p: Task that is going to be killed
1147 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1150 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
1152 struct cgroup *task_cgrp;
1153 struct cgroup *mem_cgrp;
1155 * Need a buffer in BSS, can't rely on allocations. The code relies
1156 * on the assumption that OOM is serialized for memory controller.
1157 * If this assumption is broken, revisit this code.
1159 static char memcg_name[PATH_MAX];
1168 mem_cgrp = memcg->css.cgroup;
1169 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
1171 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
1174 * Unfortunately, we are unable to convert to a useful name
1175 * But we'll still print out the usage information
1182 printk(KERN_INFO "Task in %s killed", memcg_name);
1185 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
1193 * Continues from above, so we don't need an KERN_ level
1195 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
1198 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
1199 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
1200 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
1201 res_counter_read_u64(&memcg->res, RES_FAILCNT));
1202 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
1204 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
1205 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
1206 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
1210 * This function returns the number of memcg under hierarchy tree. Returns
1211 * 1(self count) if no children.
1213 static int mem_cgroup_count_children(struct mem_cgroup *mem)
1216 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
1221 * Return the memory (and swap, if configured) limit for a memcg.
1223 u64 mem_cgroup_get_limit(struct mem_cgroup *memcg)
1228 limit = res_counter_read_u64(&memcg->res, RES_LIMIT) +
1230 memsw = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1232 * If memsw is finite and limits the amount of swap space available
1233 * to this memcg, return that limit.
1235 return min(limit, memsw);
1239 * Visit the first child (need not be the first child as per the ordering
1240 * of the cgroup list, since we track last_scanned_child) of @mem and use
1241 * that to reclaim free pages from.
1243 static struct mem_cgroup *
1244 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
1246 struct mem_cgroup *ret = NULL;
1247 struct cgroup_subsys_state *css;
1250 if (!root_mem->use_hierarchy) {
1251 css_get(&root_mem->css);
1257 nextid = root_mem->last_scanned_child + 1;
1258 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1260 if (css && css_tryget(css))
1261 ret = container_of(css, struct mem_cgroup, css);
1264 /* Updates scanning parameter */
1265 spin_lock(&root_mem->reclaim_param_lock);
1267 /* this means start scan from ID:1 */
1268 root_mem->last_scanned_child = 0;
1270 root_mem->last_scanned_child = found;
1271 spin_unlock(&root_mem->reclaim_param_lock);
1278 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1279 * we reclaimed from, so that we don't end up penalizing one child extensively
1280 * based on its position in the children list.
1282 * root_mem is the original ancestor that we've been reclaim from.
1284 * We give up and return to the caller when we visit root_mem twice.
1285 * (other groups can be removed while we're walking....)
1287 * If shrink==true, for avoiding to free too much, this returns immedieately.
1289 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1292 unsigned long reclaim_options)
1294 struct mem_cgroup *victim;
1297 bool noswap = reclaim_options & MEM_CGROUP_RECLAIM_NOSWAP;
1298 bool shrink = reclaim_options & MEM_CGROUP_RECLAIM_SHRINK;
1299 bool check_soft = reclaim_options & MEM_CGROUP_RECLAIM_SOFT;
1300 unsigned long excess = mem_cgroup_get_excess(root_mem);
1302 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1303 if (root_mem->memsw_is_minimum)
1307 victim = mem_cgroup_select_victim(root_mem);
1308 if (victim == root_mem) {
1311 drain_all_stock_async();
1314 * If we have not been able to reclaim
1315 * anything, it might because there are
1316 * no reclaimable pages under this hierarchy
1318 if (!check_soft || !total) {
1319 css_put(&victim->css);
1323 * We want to do more targetted reclaim.
1324 * excess >> 2 is not to excessive so as to
1325 * reclaim too much, nor too less that we keep
1326 * coming back to reclaim from this cgroup
1328 if (total >= (excess >> 2) ||
1329 (loop > MEM_CGROUP_MAX_RECLAIM_LOOPS)) {
1330 css_put(&victim->css);
1335 if (!mem_cgroup_local_usage(victim)) {
1336 /* this cgroup's local usage == 0 */
1337 css_put(&victim->css);
1340 /* we use swappiness of local cgroup */
1342 ret = mem_cgroup_shrink_node_zone(victim, gfp_mask,
1343 noswap, get_swappiness(victim), zone);
1345 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask,
1346 noswap, get_swappiness(victim));
1347 css_put(&victim->css);
1349 * At shrinking usage, we can't check we should stop here or
1350 * reclaim more. It's depends on callers. last_scanned_child
1351 * will work enough for keeping fairness under tree.
1357 if (res_counter_check_under_soft_limit(&root_mem->res))
1359 } else if (mem_cgroup_check_under_limit(root_mem))
1365 static int mem_cgroup_oom_lock_cb(struct mem_cgroup *mem, void *data)
1367 int *val = (int *)data;
1370 * Logically, we can stop scanning immediately when we find
1371 * a memcg is already locked. But condidering unlock ops and
1372 * creation/removal of memcg, scan-all is simple operation.
1374 x = atomic_inc_return(&mem->oom_lock);
1375 *val = max(x, *val);
1379 * Check OOM-Killer is already running under our hierarchy.
1380 * If someone is running, return false.
1382 static bool mem_cgroup_oom_lock(struct mem_cgroup *mem)
1386 mem_cgroup_walk_tree(mem, &lock_count, mem_cgroup_oom_lock_cb);
1388 if (lock_count == 1)
1393 static int mem_cgroup_oom_unlock_cb(struct mem_cgroup *mem, void *data)
1396 * When a new child is created while the hierarchy is under oom,
1397 * mem_cgroup_oom_lock() may not be called. We have to use
1398 * atomic_add_unless() here.
1400 atomic_add_unless(&mem->oom_lock, -1, 0);
1404 static void mem_cgroup_oom_unlock(struct mem_cgroup *mem)
1406 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_unlock_cb);
1409 static DEFINE_MUTEX(memcg_oom_mutex);
1410 static DECLARE_WAIT_QUEUE_HEAD(memcg_oom_waitq);
1412 struct oom_wait_info {
1413 struct mem_cgroup *mem;
1417 static int memcg_oom_wake_function(wait_queue_t *wait,
1418 unsigned mode, int sync, void *arg)
1420 struct mem_cgroup *wake_mem = (struct mem_cgroup *)arg;
1421 struct oom_wait_info *oom_wait_info;
1423 oom_wait_info = container_of(wait, struct oom_wait_info, wait);
1425 if (oom_wait_info->mem == wake_mem)
1427 /* if no hierarchy, no match */
1428 if (!oom_wait_info->mem->use_hierarchy || !wake_mem->use_hierarchy)
1431 * Both of oom_wait_info->mem and wake_mem are stable under us.
1432 * Then we can use css_is_ancestor without taking care of RCU.
1434 if (!css_is_ancestor(&oom_wait_info->mem->css, &wake_mem->css) &&
1435 !css_is_ancestor(&wake_mem->css, &oom_wait_info->mem->css))
1439 return autoremove_wake_function(wait, mode, sync, arg);
1442 static void memcg_wakeup_oom(struct mem_cgroup *mem)
1444 /* for filtering, pass "mem" as argument. */
1445 __wake_up(&memcg_oom_waitq, TASK_NORMAL, 0, mem);
1448 static void memcg_oom_recover(struct mem_cgroup *mem)
1450 if (mem && atomic_read(&mem->oom_lock))
1451 memcg_wakeup_oom(mem);
1455 * try to call OOM killer. returns false if we should exit memory-reclaim loop.
1457 bool mem_cgroup_handle_oom(struct mem_cgroup *mem, gfp_t mask)
1459 struct oom_wait_info owait;
1460 bool locked, need_to_kill;
1463 owait.wait.flags = 0;
1464 owait.wait.func = memcg_oom_wake_function;
1465 owait.wait.private = current;
1466 INIT_LIST_HEAD(&owait.wait.task_list);
1467 need_to_kill = true;
1468 /* At first, try to OOM lock hierarchy under mem.*/
1469 mutex_lock(&memcg_oom_mutex);
1470 locked = mem_cgroup_oom_lock(mem);
1472 * Even if signal_pending(), we can't quit charge() loop without
1473 * accounting. So, UNINTERRUPTIBLE is appropriate. But SIGKILL
1474 * under OOM is always welcomed, use TASK_KILLABLE here.
1476 prepare_to_wait(&memcg_oom_waitq, &owait.wait, TASK_KILLABLE);
1477 if (!locked || mem->oom_kill_disable)
1478 need_to_kill = false;
1480 mem_cgroup_oom_notify(mem);
1481 mutex_unlock(&memcg_oom_mutex);
1484 finish_wait(&memcg_oom_waitq, &owait.wait);
1485 mem_cgroup_out_of_memory(mem, mask);
1488 finish_wait(&memcg_oom_waitq, &owait.wait);
1490 mutex_lock(&memcg_oom_mutex);
1491 mem_cgroup_oom_unlock(mem);
1492 memcg_wakeup_oom(mem);
1493 mutex_unlock(&memcg_oom_mutex);
1495 if (test_thread_flag(TIF_MEMDIE) || fatal_signal_pending(current))
1497 /* Give chance to dying process */
1498 schedule_timeout(1);
1503 * Currently used to update mapped file statistics, but the routine can be
1504 * generalized to update other statistics as well.
1506 * Notes: Race condition
1508 * We usually use page_cgroup_lock() for accessing page_cgroup member but
1509 * it tends to be costly. But considering some conditions, we doesn't need
1510 * to do so _always_.
1512 * Considering "charge", lock_page_cgroup() is not required because all
1513 * file-stat operations happen after a page is attached to radix-tree. There
1514 * are no race with "charge".
1516 * Considering "uncharge", we know that memcg doesn't clear pc->mem_cgroup
1517 * at "uncharge" intentionally. So, we always see valid pc->mem_cgroup even
1518 * if there are race with "uncharge". Statistics itself is properly handled
1521 * Considering "move", this is an only case we see a race. To make the race
1522 * small, we check MEM_CGROUP_ON_MOVE percpu value and detect there are
1523 * possibility of race condition. If there is, we take a lock.
1525 void mem_cgroup_update_file_mapped(struct page *page, int val)
1527 struct mem_cgroup *mem;
1528 struct page_cgroup *pc = lookup_page_cgroup(page);
1529 bool need_unlock = false;
1535 mem = pc->mem_cgroup;
1536 if (unlikely(!mem || !PageCgroupUsed(pc)))
1538 /* pc->mem_cgroup is unstable ? */
1539 if (unlikely(mem_cgroup_stealed(mem))) {
1540 /* take a lock against to access pc->mem_cgroup */
1541 lock_page_cgroup(pc);
1543 mem = pc->mem_cgroup;
1544 if (!mem || !PageCgroupUsed(pc))
1548 this_cpu_inc(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1549 SetPageCgroupFileMapped(pc);
1551 this_cpu_dec(mem->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
1552 if (!page_mapped(page)) /* for race between dec->inc counter */
1553 ClearPageCgroupFileMapped(pc);
1557 if (unlikely(need_unlock))
1558 unlock_page_cgroup(pc);
1564 * size of first charge trial. "32" comes from vmscan.c's magic value.
1565 * TODO: maybe necessary to use big numbers in big irons.
1567 #define CHARGE_SIZE (32 * PAGE_SIZE)
1568 struct memcg_stock_pcp {
1569 struct mem_cgroup *cached; /* this never be root cgroup */
1571 struct work_struct work;
1573 static DEFINE_PER_CPU(struct memcg_stock_pcp, memcg_stock);
1574 static atomic_t memcg_drain_count;
1577 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1578 * from local stock and true is returned. If the stock is 0 or charges from a
1579 * cgroup which is not current target, returns false. This stock will be
1582 static bool consume_stock(struct mem_cgroup *mem)
1584 struct memcg_stock_pcp *stock;
1587 stock = &get_cpu_var(memcg_stock);
1588 if (mem == stock->cached && stock->charge)
1589 stock->charge -= PAGE_SIZE;
1590 else /* need to call res_counter_charge */
1592 put_cpu_var(memcg_stock);
1597 * Returns stocks cached in percpu to res_counter and reset cached information.
1599 static void drain_stock(struct memcg_stock_pcp *stock)
1601 struct mem_cgroup *old = stock->cached;
1603 if (stock->charge) {
1604 res_counter_uncharge(&old->res, stock->charge);
1605 if (do_swap_account)
1606 res_counter_uncharge(&old->memsw, stock->charge);
1608 stock->cached = NULL;
1613 * This must be called under preempt disabled or must be called by
1614 * a thread which is pinned to local cpu.
1616 static void drain_local_stock(struct work_struct *dummy)
1618 struct memcg_stock_pcp *stock = &__get_cpu_var(memcg_stock);
1623 * Cache charges(val) which is from res_counter, to local per_cpu area.
1624 * This will be consumed by consume_stock() function, later.
1626 static void refill_stock(struct mem_cgroup *mem, int val)
1628 struct memcg_stock_pcp *stock = &get_cpu_var(memcg_stock);
1630 if (stock->cached != mem) { /* reset if necessary */
1632 stock->cached = mem;
1634 stock->charge += val;
1635 put_cpu_var(memcg_stock);
1639 * Tries to drain stocked charges in other cpus. This function is asynchronous
1640 * and just put a work per cpu for draining localy on each cpu. Caller can
1641 * expects some charges will be back to res_counter later but cannot wait for
1644 static void drain_all_stock_async(void)
1647 /* This function is for scheduling "drain" in asynchronous way.
1648 * The result of "drain" is not directly handled by callers. Then,
1649 * if someone is calling drain, we don't have to call drain more.
1650 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1651 * there is a race. We just do loose check here.
1653 if (atomic_read(&memcg_drain_count))
1655 /* Notify other cpus that system-wide "drain" is running */
1656 atomic_inc(&memcg_drain_count);
1658 for_each_online_cpu(cpu) {
1659 struct memcg_stock_pcp *stock = &per_cpu(memcg_stock, cpu);
1660 schedule_work_on(cpu, &stock->work);
1663 atomic_dec(&memcg_drain_count);
1664 /* We don't wait for flush_work */
1667 /* This is a synchronous drain interface. */
1668 static void drain_all_stock_sync(void)
1670 /* called when force_empty is called */
1671 atomic_inc(&memcg_drain_count);
1672 schedule_on_each_cpu(drain_local_stock);
1673 atomic_dec(&memcg_drain_count);
1676 static int __cpuinit memcg_stock_cpu_callback(struct notifier_block *nb,
1677 unsigned long action,
1680 int cpu = (unsigned long)hcpu;
1681 struct memcg_stock_pcp *stock;
1683 if (action != CPU_DEAD)
1685 stock = &per_cpu(memcg_stock, cpu);
1691 /* See __mem_cgroup_try_charge() for details */
1693 CHARGE_OK, /* success */
1694 CHARGE_RETRY, /* need to retry but retry is not bad */
1695 CHARGE_NOMEM, /* we can't do more. return -ENOMEM */
1696 CHARGE_WOULDBLOCK, /* GFP_WAIT wasn't set and no enough res. */
1697 CHARGE_OOM_DIE, /* the current is killed because of OOM */
1700 static int __mem_cgroup_do_charge(struct mem_cgroup *mem, gfp_t gfp_mask,
1701 int csize, bool oom_check)
1703 struct mem_cgroup *mem_over_limit;
1704 struct res_counter *fail_res;
1705 unsigned long flags = 0;
1708 ret = res_counter_charge(&mem->res, csize, &fail_res);
1711 if (!do_swap_account)
1713 ret = res_counter_charge(&mem->memsw, csize, &fail_res);
1717 mem_over_limit = mem_cgroup_from_res_counter(fail_res, memsw);
1718 flags |= MEM_CGROUP_RECLAIM_NOSWAP;
1720 mem_over_limit = mem_cgroup_from_res_counter(fail_res, res);
1722 if (csize > PAGE_SIZE) /* change csize and retry */
1723 return CHARGE_RETRY;
1725 if (!(gfp_mask & __GFP_WAIT))
1726 return CHARGE_WOULDBLOCK;
1728 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, NULL,
1731 * try_to_free_mem_cgroup_pages() might not give us a full
1732 * picture of reclaim. Some pages are reclaimed and might be
1733 * moved to swap cache or just unmapped from the cgroup.
1734 * Check the limit again to see if the reclaim reduced the
1735 * current usage of the cgroup before giving up
1737 if (ret || mem_cgroup_check_under_limit(mem_over_limit))
1738 return CHARGE_RETRY;
1741 * At task move, charge accounts can be doubly counted. So, it's
1742 * better to wait until the end of task_move if something is going on.
1744 if (mem_cgroup_wait_acct_move(mem_over_limit))
1745 return CHARGE_RETRY;
1747 /* If we don't need to call oom-killer at el, return immediately */
1749 return CHARGE_NOMEM;
1751 if (!mem_cgroup_handle_oom(mem_over_limit, gfp_mask))
1752 return CHARGE_OOM_DIE;
1754 return CHARGE_RETRY;
1758 * Unlike exported interface, "oom" parameter is added. if oom==true,
1759 * oom-killer can be invoked.
1761 static int __mem_cgroup_try_charge(struct mm_struct *mm,
1762 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
1764 int nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1765 struct mem_cgroup *mem = NULL;
1767 int csize = CHARGE_SIZE;
1770 * Unlike gloval-vm's OOM-kill, we're not in memory shortage
1771 * in system level. So, allow to go ahead dying process in addition to
1774 if (unlikely(test_thread_flag(TIF_MEMDIE)
1775 || fatal_signal_pending(current)))
1779 * We always charge the cgroup the mm_struct belongs to.
1780 * The mm_struct's mem_cgroup changes on task migration if the
1781 * thread group leader migrates. It's possible that mm is not
1782 * set, if so charge the init_mm (happens for pagecache usage).
1787 if (*memcg) { /* css should be a valid one */
1789 VM_BUG_ON(css_is_removed(&mem->css));
1790 if (mem_cgroup_is_root(mem))
1792 if (consume_stock(mem))
1796 struct task_struct *p;
1799 p = rcu_dereference(mm->owner);
1802 * because we don't have task_lock(), "p" can exit while
1803 * we're here. In that case, "mem" can point to root
1804 * cgroup but never be NULL. (and task_struct itself is freed
1805 * by RCU, cgroup itself is RCU safe.) Then, we have small
1806 * risk here to get wrong cgroup. But such kind of mis-account
1807 * by race always happens because we don't have cgroup_mutex().
1808 * It's overkill and we allow that small race, here.
1810 mem = mem_cgroup_from_task(p);
1812 if (mem_cgroup_is_root(mem)) {
1816 if (consume_stock(mem)) {
1818 * It seems dagerous to access memcg without css_get().
1819 * But considering how consume_stok works, it's not
1820 * necessary. If consume_stock success, some charges
1821 * from this memcg are cached on this cpu. So, we
1822 * don't need to call css_get()/css_tryget() before
1823 * calling consume_stock().
1828 /* after here, we may be blocked. we need to get refcnt */
1829 if (!css_tryget(&mem->css)) {
1839 /* If killed, bypass charge */
1840 if (fatal_signal_pending(current)) {
1846 if (oom && !nr_oom_retries) {
1848 nr_oom_retries = MEM_CGROUP_RECLAIM_RETRIES;
1851 ret = __mem_cgroup_do_charge(mem, gfp_mask, csize, oom_check);
1856 case CHARGE_RETRY: /* not in OOM situation but retry */
1861 case CHARGE_WOULDBLOCK: /* !__GFP_WAIT */
1864 case CHARGE_NOMEM: /* OOM routine works */
1869 /* If oom, we never return -ENOMEM */
1872 case CHARGE_OOM_DIE: /* Killed by OOM Killer */
1876 } while (ret != CHARGE_OK);
1878 if (csize > PAGE_SIZE)
1879 refill_stock(mem, csize - PAGE_SIZE);
1893 * Somemtimes we have to undo a charge we got by try_charge().
1894 * This function is for that and do uncharge, put css's refcnt.
1895 * gotten by try_charge().
1897 static void __mem_cgroup_cancel_charge(struct mem_cgroup *mem,
1898 unsigned long count)
1900 if (!mem_cgroup_is_root(mem)) {
1901 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
1902 if (do_swap_account)
1903 res_counter_uncharge(&mem->memsw, PAGE_SIZE * count);
1907 static void mem_cgroup_cancel_charge(struct mem_cgroup *mem)
1909 __mem_cgroup_cancel_charge(mem, 1);
1913 * A helper function to get mem_cgroup from ID. must be called under
1914 * rcu_read_lock(). The caller must check css_is_removed() or some if
1915 * it's concern. (dropping refcnt from swap can be called against removed
1918 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1920 struct cgroup_subsys_state *css;
1922 /* ID 0 is unused ID */
1925 css = css_lookup(&mem_cgroup_subsys, id);
1928 return container_of(css, struct mem_cgroup, css);
1931 struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
1933 struct mem_cgroup *mem = NULL;
1934 struct page_cgroup *pc;
1938 VM_BUG_ON(!PageLocked(page));
1940 pc = lookup_page_cgroup(page);
1941 lock_page_cgroup(pc);
1942 if (PageCgroupUsed(pc)) {
1943 mem = pc->mem_cgroup;
1944 if (mem && !css_tryget(&mem->css))
1946 } else if (PageSwapCache(page)) {
1947 ent.val = page_private(page);
1948 id = lookup_swap_cgroup(ent);
1950 mem = mem_cgroup_lookup(id);
1951 if (mem && !css_tryget(&mem->css))
1955 unlock_page_cgroup(pc);
1960 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1961 * USED state. If already USED, uncharge and return.
1964 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1965 struct page_cgroup *pc,
1966 enum charge_type ctype)
1968 /* try_charge() can return NULL to *memcg, taking care of it. */
1972 lock_page_cgroup(pc);
1973 if (unlikely(PageCgroupUsed(pc))) {
1974 unlock_page_cgroup(pc);
1975 mem_cgroup_cancel_charge(mem);
1979 pc->mem_cgroup = mem;
1981 * We access a page_cgroup asynchronously without lock_page_cgroup().
1982 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1983 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1984 * before USED bit, we need memory barrier here.
1985 * See mem_cgroup_add_lru_list(), etc.
1989 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1990 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1991 SetPageCgroupCache(pc);
1992 SetPageCgroupUsed(pc);
1994 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1995 ClearPageCgroupCache(pc);
1996 SetPageCgroupUsed(pc);
2002 mem_cgroup_charge_statistics(mem, pc, true);
2004 unlock_page_cgroup(pc);
2006 * "charge_statistics" updated event counter. Then, check it.
2007 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
2008 * if they exceeds softlimit.
2010 memcg_check_events(mem, pc->page);
2014 * __mem_cgroup_move_account - move account of the page
2015 * @pc: page_cgroup of the page.
2016 * @from: mem_cgroup which the page is moved from.
2017 * @to: mem_cgroup which the page is moved to. @from != @to.
2018 * @uncharge: whether we should call uncharge and css_put against @from.
2020 * The caller must confirm following.
2021 * - page is not on LRU (isolate_page() is useful.)
2022 * - the pc is locked, used, and ->mem_cgroup points to @from.
2024 * This function doesn't do "charge" nor css_get to new cgroup. It should be
2025 * done by a caller(__mem_cgroup_try_charge would be usefull). If @uncharge is
2026 * true, this function does "uncharge" from old cgroup, but it doesn't if
2027 * @uncharge is false, so a caller should do "uncharge".
2030 static void __mem_cgroup_move_account(struct page_cgroup *pc,
2031 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
2033 VM_BUG_ON(from == to);
2034 VM_BUG_ON(PageLRU(pc->page));
2035 VM_BUG_ON(!PageCgroupLocked(pc));
2036 VM_BUG_ON(!PageCgroupUsed(pc));
2037 VM_BUG_ON(pc->mem_cgroup != from);
2039 if (PageCgroupFileMapped(pc)) {
2040 /* Update mapped_file data for mem_cgroup */
2042 __this_cpu_dec(from->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2043 __this_cpu_inc(to->stat->count[MEM_CGROUP_STAT_FILE_MAPPED]);
2046 mem_cgroup_charge_statistics(from, pc, false);
2048 /* This is not "cancel", but cancel_charge does all we need. */
2049 mem_cgroup_cancel_charge(from);
2051 /* caller should have done css_get */
2052 pc->mem_cgroup = to;
2053 mem_cgroup_charge_statistics(to, pc, true);
2055 * We charges against "to" which may not have any tasks. Then, "to"
2056 * can be under rmdir(). But in current implementation, caller of
2057 * this function is just force_empty() and move charge, so it's
2058 * garanteed that "to" is never removed. So, we don't check rmdir
2064 * check whether the @pc is valid for moving account and call
2065 * __mem_cgroup_move_account()
2067 static int mem_cgroup_move_account(struct page_cgroup *pc,
2068 struct mem_cgroup *from, struct mem_cgroup *to, bool uncharge)
2071 lock_page_cgroup(pc);
2072 if (PageCgroupUsed(pc) && pc->mem_cgroup == from) {
2073 __mem_cgroup_move_account(pc, from, to, uncharge);
2076 unlock_page_cgroup(pc);
2080 memcg_check_events(to, pc->page);
2081 memcg_check_events(from, pc->page);
2086 * move charges to its parent.
2089 static int mem_cgroup_move_parent(struct page_cgroup *pc,
2090 struct mem_cgroup *child,
2093 struct page *page = pc->page;
2094 struct cgroup *cg = child->css.cgroup;
2095 struct cgroup *pcg = cg->parent;
2096 struct mem_cgroup *parent;
2104 if (!get_page_unless_zero(page))
2106 if (isolate_lru_page(page))
2109 parent = mem_cgroup_from_cont(pcg);
2110 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
2114 ret = mem_cgroup_move_account(pc, child, parent, true);
2116 mem_cgroup_cancel_charge(parent);
2118 putback_lru_page(page);
2126 * Charge the memory controller for page usage.
2128 * 0 if the charge was successful
2129 * < 0 if the cgroup is over its limit
2131 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
2132 gfp_t gfp_mask, enum charge_type ctype)
2134 struct mem_cgroup *mem = NULL;
2135 struct page_cgroup *pc;
2138 pc = lookup_page_cgroup(page);
2139 /* can happen at boot */
2144 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
2148 __mem_cgroup_commit_charge(mem, pc, ctype);
2152 int mem_cgroup_newpage_charge(struct page *page,
2153 struct mm_struct *mm, gfp_t gfp_mask)
2155 if (mem_cgroup_disabled())
2157 if (PageCompound(page))
2160 * If already mapped, we don't have to account.
2161 * If page cache, page->mapping has address_space.
2162 * But page->mapping may have out-of-use anon_vma pointer,
2163 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
2166 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
2170 return mem_cgroup_charge_common(page, mm, gfp_mask,
2171 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2175 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2176 enum charge_type ctype);
2178 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
2183 if (mem_cgroup_disabled())
2185 if (PageCompound(page))
2188 * Corner case handling. This is called from add_to_page_cache()
2189 * in usual. But some FS (shmem) precharges this page before calling it
2190 * and call add_to_page_cache() with GFP_NOWAIT.
2192 * For GFP_NOWAIT case, the page may be pre-charged before calling
2193 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
2194 * charge twice. (It works but has to pay a bit larger cost.)
2195 * And when the page is SwapCache, it should take swap information
2196 * into account. This is under lock_page() now.
2198 if (!(gfp_mask & __GFP_WAIT)) {
2199 struct page_cgroup *pc;
2201 pc = lookup_page_cgroup(page);
2204 lock_page_cgroup(pc);
2205 if (PageCgroupUsed(pc)) {
2206 unlock_page_cgroup(pc);
2209 unlock_page_cgroup(pc);
2215 if (page_is_file_cache(page))
2216 return mem_cgroup_charge_common(page, mm, gfp_mask,
2217 MEM_CGROUP_CHARGE_TYPE_CACHE);
2220 if (PageSwapCache(page)) {
2221 struct mem_cgroup *mem = NULL;
2223 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2225 __mem_cgroup_commit_charge_swapin(page, mem,
2226 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2228 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
2229 MEM_CGROUP_CHARGE_TYPE_SHMEM);
2235 * While swap-in, try_charge -> commit or cancel, the page is locked.
2236 * And when try_charge() successfully returns, one refcnt to memcg without
2237 * struct page_cgroup is acquired. This refcnt will be consumed by
2238 * "commit()" or removed by "cancel()"
2240 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
2242 gfp_t mask, struct mem_cgroup **ptr)
2244 struct mem_cgroup *mem;
2247 if (mem_cgroup_disabled())
2250 if (!do_swap_account)
2253 * A racing thread's fault, or swapoff, may have already updated
2254 * the pte, and even removed page from swap cache: in those cases
2255 * do_swap_page()'s pte_same() test will fail; but there's also a
2256 * KSM case which does need to charge the page.
2258 if (!PageSwapCache(page))
2260 mem = try_get_mem_cgroup_from_page(page);
2264 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
2270 return __mem_cgroup_try_charge(mm, mask, ptr, true);
2274 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
2275 enum charge_type ctype)
2277 struct page_cgroup *pc;
2279 if (mem_cgroup_disabled())
2283 cgroup_exclude_rmdir(&ptr->css);
2284 pc = lookup_page_cgroup(page);
2285 mem_cgroup_lru_del_before_commit_swapcache(page);
2286 __mem_cgroup_commit_charge(ptr, pc, ctype);
2287 mem_cgroup_lru_add_after_commit_swapcache(page);
2289 * Now swap is on-memory. This means this page may be
2290 * counted both as mem and swap....double count.
2291 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
2292 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
2293 * may call delete_from_swap_cache() before reach here.
2295 if (do_swap_account && PageSwapCache(page)) {
2296 swp_entry_t ent = {.val = page_private(page)};
2298 struct mem_cgroup *memcg;
2300 id = swap_cgroup_record(ent, 0);
2302 memcg = mem_cgroup_lookup(id);
2305 * This recorded memcg can be obsolete one. So, avoid
2306 * calling css_tryget
2308 if (!mem_cgroup_is_root(memcg))
2309 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
2310 mem_cgroup_swap_statistics(memcg, false);
2311 mem_cgroup_put(memcg);
2316 * At swapin, we may charge account against cgroup which has no tasks.
2317 * So, rmdir()->pre_destroy() can be called while we do this charge.
2318 * In that case, we need to call pre_destroy() again. check it here.
2320 cgroup_release_and_wakeup_rmdir(&ptr->css);
2323 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
2325 __mem_cgroup_commit_charge_swapin(page, ptr,
2326 MEM_CGROUP_CHARGE_TYPE_MAPPED);
2329 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
2331 if (mem_cgroup_disabled())
2335 mem_cgroup_cancel_charge(mem);
2339 __do_uncharge(struct mem_cgroup *mem, const enum charge_type ctype)
2341 struct memcg_batch_info *batch = NULL;
2342 bool uncharge_memsw = true;
2343 /* If swapout, usage of swap doesn't decrease */
2344 if (!do_swap_account || ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
2345 uncharge_memsw = false;
2347 batch = ¤t->memcg_batch;
2349 * In usual, we do css_get() when we remember memcg pointer.
2350 * But in this case, we keep res->usage until end of a series of
2351 * uncharges. Then, it's ok to ignore memcg's refcnt.
2356 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
2357 * In those cases, all pages freed continously can be expected to be in
2358 * the same cgroup and we have chance to coalesce uncharges.
2359 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
2360 * because we want to do uncharge as soon as possible.
2363 if (!batch->do_batch || test_thread_flag(TIF_MEMDIE))
2364 goto direct_uncharge;
2367 * In typical case, batch->memcg == mem. This means we can
2368 * merge a series of uncharges to an uncharge of res_counter.
2369 * If not, we uncharge res_counter ony by one.
2371 if (batch->memcg != mem)
2372 goto direct_uncharge;
2373 /* remember freed charge and uncharge it later */
2374 batch->bytes += PAGE_SIZE;
2376 batch->memsw_bytes += PAGE_SIZE;
2379 res_counter_uncharge(&mem->res, PAGE_SIZE);
2381 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
2382 if (unlikely(batch->memcg != mem))
2383 memcg_oom_recover(mem);
2388 * uncharge if !page_mapped(page)
2390 static struct mem_cgroup *
2391 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
2393 struct page_cgroup *pc;
2394 struct mem_cgroup *mem = NULL;
2396 if (mem_cgroup_disabled())
2399 if (PageSwapCache(page))
2403 * Check if our page_cgroup is valid
2405 pc = lookup_page_cgroup(page);
2406 if (unlikely(!pc || !PageCgroupUsed(pc)))
2409 lock_page_cgroup(pc);
2411 mem = pc->mem_cgroup;
2413 if (!PageCgroupUsed(pc))
2417 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
2418 case MEM_CGROUP_CHARGE_TYPE_DROP:
2419 /* See mem_cgroup_prepare_migration() */
2420 if (page_mapped(page) || PageCgroupMigration(pc))
2423 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
2424 if (!PageAnon(page)) { /* Shared memory */
2425 if (page->mapping && !page_is_file_cache(page))
2427 } else if (page_mapped(page)) /* Anon */
2434 mem_cgroup_charge_statistics(mem, pc, false);
2436 ClearPageCgroupUsed(pc);
2438 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2439 * freed from LRU. This is safe because uncharged page is expected not
2440 * to be reused (freed soon). Exception is SwapCache, it's handled by
2441 * special functions.
2444 unlock_page_cgroup(pc);
2446 * even after unlock, we have mem->res.usage here and this memcg
2447 * will never be freed.
2449 memcg_check_events(mem, page);
2450 if (do_swap_account && ctype == MEM_CGROUP_CHARGE_TYPE_SWAPOUT) {
2451 mem_cgroup_swap_statistics(mem, true);
2452 mem_cgroup_get(mem);
2454 if (!mem_cgroup_is_root(mem))
2455 __do_uncharge(mem, ctype);
2460 unlock_page_cgroup(pc);
2464 void mem_cgroup_uncharge_page(struct page *page)
2467 if (page_mapped(page))
2469 if (page->mapping && !PageAnon(page))
2471 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
2474 void mem_cgroup_uncharge_cache_page(struct page *page)
2476 VM_BUG_ON(page_mapped(page));
2477 VM_BUG_ON(page->mapping);
2478 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
2482 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2483 * In that cases, pages are freed continuously and we can expect pages
2484 * are in the same memcg. All these calls itself limits the number of
2485 * pages freed at once, then uncharge_start/end() is called properly.
2486 * This may be called prural(2) times in a context,
2489 void mem_cgroup_uncharge_start(void)
2491 current->memcg_batch.do_batch++;
2492 /* We can do nest. */
2493 if (current->memcg_batch.do_batch == 1) {
2494 current->memcg_batch.memcg = NULL;
2495 current->memcg_batch.bytes = 0;
2496 current->memcg_batch.memsw_bytes = 0;
2500 void mem_cgroup_uncharge_end(void)
2502 struct memcg_batch_info *batch = ¤t->memcg_batch;
2504 if (!batch->do_batch)
2508 if (batch->do_batch) /* If stacked, do nothing. */
2514 * This "batch->memcg" is valid without any css_get/put etc...
2515 * bacause we hide charges behind us.
2518 res_counter_uncharge(&batch->memcg->res, batch->bytes);
2519 if (batch->memsw_bytes)
2520 res_counter_uncharge(&batch->memcg->memsw, batch->memsw_bytes);
2521 memcg_oom_recover(batch->memcg);
2522 /* forget this pointer (for sanity check) */
2523 batch->memcg = NULL;
2528 * called after __delete_from_swap_cache() and drop "page" account.
2529 * memcg information is recorded to swap_cgroup of "ent"
2532 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
2534 struct mem_cgroup *memcg;
2535 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
2537 if (!swapout) /* this was a swap cache but the swap is unused ! */
2538 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
2540 memcg = __mem_cgroup_uncharge_common(page, ctype);
2543 * record memcg information, if swapout && memcg != NULL,
2544 * mem_cgroup_get() was called in uncharge().
2546 if (do_swap_account && swapout && memcg)
2547 swap_cgroup_record(ent, css_id(&memcg->css));
2551 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2553 * called from swap_entry_free(). remove record in swap_cgroup and
2554 * uncharge "memsw" account.
2556 void mem_cgroup_uncharge_swap(swp_entry_t ent)
2558 struct mem_cgroup *memcg;
2561 if (!do_swap_account)
2564 id = swap_cgroup_record(ent, 0);
2566 memcg = mem_cgroup_lookup(id);
2569 * We uncharge this because swap is freed.
2570 * This memcg can be obsolete one. We avoid calling css_tryget
2572 if (!mem_cgroup_is_root(memcg))
2573 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
2574 mem_cgroup_swap_statistics(memcg, false);
2575 mem_cgroup_put(memcg);
2581 * mem_cgroup_move_swap_account - move swap charge and swap_cgroup's record.
2582 * @entry: swap entry to be moved
2583 * @from: mem_cgroup which the entry is moved from
2584 * @to: mem_cgroup which the entry is moved to
2585 * @need_fixup: whether we should fixup res_counters and refcounts.
2587 * It succeeds only when the swap_cgroup's record for this entry is the same
2588 * as the mem_cgroup's id of @from.
2590 * Returns 0 on success, -EINVAL on failure.
2592 * The caller must have charged to @to, IOW, called res_counter_charge() about
2593 * both res and memsw, and called css_get().
2595 static int mem_cgroup_move_swap_account(swp_entry_t entry,
2596 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
2598 unsigned short old_id, new_id;
2600 old_id = css_id(&from->css);
2601 new_id = css_id(&to->css);
2603 if (swap_cgroup_cmpxchg(entry, old_id, new_id) == old_id) {
2604 mem_cgroup_swap_statistics(from, false);
2605 mem_cgroup_swap_statistics(to, true);
2607 * This function is only called from task migration context now.
2608 * It postpones res_counter and refcount handling till the end
2609 * of task migration(mem_cgroup_clear_mc()) for performance
2610 * improvement. But we cannot postpone mem_cgroup_get(to)
2611 * because if the process that has been moved to @to does
2612 * swap-in, the refcount of @to might be decreased to 0.
2616 if (!mem_cgroup_is_root(from))
2617 res_counter_uncharge(&from->memsw, PAGE_SIZE);
2618 mem_cgroup_put(from);
2620 * we charged both to->res and to->memsw, so we should
2623 if (!mem_cgroup_is_root(to))
2624 res_counter_uncharge(&to->res, PAGE_SIZE);
2631 static inline int mem_cgroup_move_swap_account(swp_entry_t entry,
2632 struct mem_cgroup *from, struct mem_cgroup *to, bool need_fixup)
2639 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2642 int mem_cgroup_prepare_migration(struct page *page,
2643 struct page *newpage, struct mem_cgroup **ptr)
2645 struct page_cgroup *pc;
2646 struct mem_cgroup *mem = NULL;
2647 enum charge_type ctype;
2650 if (mem_cgroup_disabled())
2653 pc = lookup_page_cgroup(page);
2654 lock_page_cgroup(pc);
2655 if (PageCgroupUsed(pc)) {
2656 mem = pc->mem_cgroup;
2659 * At migrating an anonymous page, its mapcount goes down
2660 * to 0 and uncharge() will be called. But, even if it's fully
2661 * unmapped, migration may fail and this page has to be
2662 * charged again. We set MIGRATION flag here and delay uncharge
2663 * until end_migration() is called
2665 * Corner Case Thinking
2667 * When the old page was mapped as Anon and it's unmap-and-freed
2668 * while migration was ongoing.
2669 * If unmap finds the old page, uncharge() of it will be delayed
2670 * until end_migration(). If unmap finds a new page, it's
2671 * uncharged when it make mapcount to be 1->0. If unmap code
2672 * finds swap_migration_entry, the new page will not be mapped
2673 * and end_migration() will find it(mapcount==0).
2676 * When the old page was mapped but migraion fails, the kernel
2677 * remaps it. A charge for it is kept by MIGRATION flag even
2678 * if mapcount goes down to 0. We can do remap successfully
2679 * without charging it again.
2682 * The "old" page is under lock_page() until the end of
2683 * migration, so, the old page itself will not be swapped-out.
2684 * If the new page is swapped out before end_migraton, our
2685 * hook to usual swap-out path will catch the event.
2688 SetPageCgroupMigration(pc);
2690 unlock_page_cgroup(pc);
2692 * If the page is not charged at this point,
2699 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, ptr, false);
2700 css_put(&mem->css);/* drop extra refcnt */
2701 if (ret || *ptr == NULL) {
2702 if (PageAnon(page)) {
2703 lock_page_cgroup(pc);
2704 ClearPageCgroupMigration(pc);
2705 unlock_page_cgroup(pc);
2707 * The old page may be fully unmapped while we kept it.
2709 mem_cgroup_uncharge_page(page);
2714 * We charge new page before it's used/mapped. So, even if unlock_page()
2715 * is called before end_migration, we can catch all events on this new
2716 * page. In the case new page is migrated but not remapped, new page's
2717 * mapcount will be finally 0 and we call uncharge in end_migration().
2719 pc = lookup_page_cgroup(newpage);
2721 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
2722 else if (page_is_file_cache(page))
2723 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
2725 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
2726 __mem_cgroup_commit_charge(mem, pc, ctype);
2730 /* remove redundant charge if migration failed*/
2731 void mem_cgroup_end_migration(struct mem_cgroup *mem,
2732 struct page *oldpage, struct page *newpage)
2734 struct page *used, *unused;
2735 struct page_cgroup *pc;
2739 /* blocks rmdir() */
2740 cgroup_exclude_rmdir(&mem->css);
2741 /* at migration success, oldpage->mapping is NULL. */
2742 if (oldpage->mapping) {
2750 * We disallowed uncharge of pages under migration because mapcount
2751 * of the page goes down to zero, temporarly.
2752 * Clear the flag and check the page should be charged.
2754 pc = lookup_page_cgroup(oldpage);
2755 lock_page_cgroup(pc);
2756 ClearPageCgroupMigration(pc);
2757 unlock_page_cgroup(pc);
2759 __mem_cgroup_uncharge_common(unused, MEM_CGROUP_CHARGE_TYPE_FORCE);
2762 * If a page is a file cache, radix-tree replacement is very atomic
2763 * and we can skip this check. When it was an Anon page, its mapcount
2764 * goes down to 0. But because we added MIGRATION flage, it's not
2765 * uncharged yet. There are several case but page->mapcount check
2766 * and USED bit check in mem_cgroup_uncharge_page() will do enough
2767 * check. (see prepare_charge() also)
2770 mem_cgroup_uncharge_page(used);
2772 * At migration, we may charge account against cgroup which has no
2774 * So, rmdir()->pre_destroy() can be called while we do this charge.
2775 * In that case, we need to call pre_destroy() again. check it here.
2777 cgroup_release_and_wakeup_rmdir(&mem->css);
2781 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2782 * Calling hierarchical_reclaim is not enough because we should update
2783 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2784 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2785 * not from the memcg which this page would be charged to.
2786 * try_charge_swapin does all of these works properly.
2788 int mem_cgroup_shmem_charge_fallback(struct page *page,
2789 struct mm_struct *mm,
2792 struct mem_cgroup *mem = NULL;
2795 if (mem_cgroup_disabled())
2798 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
2800 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
2805 static DEFINE_MUTEX(set_limit_mutex);
2807 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
2808 unsigned long long val)
2811 u64 memswlimit, memlimit;
2813 int children = mem_cgroup_count_children(memcg);
2814 u64 curusage, oldusage;
2818 * For keeping hierarchical_reclaim simple, how long we should retry
2819 * is depends on callers. We set our retry-count to be function
2820 * of # of children which we should visit in this loop.
2822 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
2824 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2827 while (retry_count) {
2828 if (signal_pending(current)) {
2833 * Rather than hide all in some function, I do this in
2834 * open coded manner. You see what this really does.
2835 * We have to guarantee mem->res.limit < mem->memsw.limit.
2837 mutex_lock(&set_limit_mutex);
2838 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2839 if (memswlimit < val) {
2841 mutex_unlock(&set_limit_mutex);
2845 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2849 ret = res_counter_set_limit(&memcg->res, val);
2851 if (memswlimit == val)
2852 memcg->memsw_is_minimum = true;
2854 memcg->memsw_is_minimum = false;
2856 mutex_unlock(&set_limit_mutex);
2861 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2862 MEM_CGROUP_RECLAIM_SHRINK);
2863 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2864 /* Usage is reduced ? */
2865 if (curusage >= oldusage)
2868 oldusage = curusage;
2870 if (!ret && enlarge)
2871 memcg_oom_recover(memcg);
2876 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2877 unsigned long long val)
2880 u64 memlimit, memswlimit, oldusage, curusage;
2881 int children = mem_cgroup_count_children(memcg);
2885 /* see mem_cgroup_resize_res_limit */
2886 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2887 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2888 while (retry_count) {
2889 if (signal_pending(current)) {
2894 * Rather than hide all in some function, I do this in
2895 * open coded manner. You see what this really does.
2896 * We have to guarantee mem->res.limit < mem->memsw.limit.
2898 mutex_lock(&set_limit_mutex);
2899 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2900 if (memlimit > val) {
2902 mutex_unlock(&set_limit_mutex);
2905 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2906 if (memswlimit < val)
2908 ret = res_counter_set_limit(&memcg->memsw, val);
2910 if (memlimit == val)
2911 memcg->memsw_is_minimum = true;
2913 memcg->memsw_is_minimum = false;
2915 mutex_unlock(&set_limit_mutex);
2920 mem_cgroup_hierarchical_reclaim(memcg, NULL, GFP_KERNEL,
2921 MEM_CGROUP_RECLAIM_NOSWAP |
2922 MEM_CGROUP_RECLAIM_SHRINK);
2923 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2924 /* Usage is reduced ? */
2925 if (curusage >= oldusage)
2928 oldusage = curusage;
2930 if (!ret && enlarge)
2931 memcg_oom_recover(memcg);
2935 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
2938 unsigned long nr_reclaimed = 0;
2939 struct mem_cgroup_per_zone *mz, *next_mz = NULL;
2940 unsigned long reclaimed;
2942 struct mem_cgroup_tree_per_zone *mctz;
2943 unsigned long long excess;
2948 mctz = soft_limit_tree_node_zone(zone_to_nid(zone), zone_idx(zone));
2950 * This loop can run a while, specially if mem_cgroup's continuously
2951 * keep exceeding their soft limit and putting the system under
2958 mz = mem_cgroup_largest_soft_limit_node(mctz);
2962 reclaimed = mem_cgroup_hierarchical_reclaim(mz->mem, zone,
2964 MEM_CGROUP_RECLAIM_SOFT);
2965 nr_reclaimed += reclaimed;
2966 spin_lock(&mctz->lock);
2969 * If we failed to reclaim anything from this memory cgroup
2970 * it is time to move on to the next cgroup
2976 * Loop until we find yet another one.
2978 * By the time we get the soft_limit lock
2979 * again, someone might have aded the
2980 * group back on the RB tree. Iterate to
2981 * make sure we get a different mem.
2982 * mem_cgroup_largest_soft_limit_node returns
2983 * NULL if no other cgroup is present on
2987 __mem_cgroup_largest_soft_limit_node(mctz);
2988 if (next_mz == mz) {
2989 css_put(&next_mz->mem->css);
2991 } else /* next_mz == NULL or other memcg */
2995 __mem_cgroup_remove_exceeded(mz->mem, mz, mctz);
2996 excess = res_counter_soft_limit_excess(&mz->mem->res);
2998 * One school of thought says that we should not add
2999 * back the node to the tree if reclaim returns 0.
3000 * But our reclaim could return 0, simply because due
3001 * to priority we are exposing a smaller subset of
3002 * memory to reclaim from. Consider this as a longer
3005 /* If excess == 0, no tree ops */
3006 __mem_cgroup_insert_exceeded(mz->mem, mz, mctz, excess);
3007 spin_unlock(&mctz->lock);
3008 css_put(&mz->mem->css);
3011 * Could not reclaim anything and there are no more
3012 * mem cgroups to try or we seem to be looping without
3013 * reclaiming anything.
3015 if (!nr_reclaimed &&
3017 loop > MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS))
3019 } while (!nr_reclaimed);
3021 css_put(&next_mz->mem->css);
3022 return nr_reclaimed;
3026 * This routine traverse page_cgroup in given list and drop them all.
3027 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
3029 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
3030 int node, int zid, enum lru_list lru)
3033 struct mem_cgroup_per_zone *mz;
3034 struct page_cgroup *pc, *busy;
3035 unsigned long flags, loop;
3036 struct list_head *list;
3039 zone = &NODE_DATA(node)->node_zones[zid];
3040 mz = mem_cgroup_zoneinfo(mem, node, zid);
3041 list = &mz->lists[lru];
3043 loop = MEM_CGROUP_ZSTAT(mz, lru);
3044 /* give some margin against EBUSY etc...*/
3049 spin_lock_irqsave(&zone->lru_lock, flags);
3050 if (list_empty(list)) {
3051 spin_unlock_irqrestore(&zone->lru_lock, flags);
3054 pc = list_entry(list->prev, struct page_cgroup, lru);
3056 list_move(&pc->lru, list);
3058 spin_unlock_irqrestore(&zone->lru_lock, flags);
3061 spin_unlock_irqrestore(&zone->lru_lock, flags);
3063 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
3067 if (ret == -EBUSY || ret == -EINVAL) {
3068 /* found lock contention or "pc" is obsolete. */
3075 if (!ret && !list_empty(list))
3081 * make mem_cgroup's charge to be 0 if there is no task.
3082 * This enables deleting this mem_cgroup.
3084 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
3087 int node, zid, shrink;
3088 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
3089 struct cgroup *cgrp = mem->css.cgroup;
3094 /* should free all ? */
3100 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
3103 if (signal_pending(current))
3105 /* This is for making all *used* pages to be on LRU. */
3106 lru_add_drain_all();
3107 drain_all_stock_sync();
3109 mem_cgroup_start_move(mem);
3110 for_each_node_state(node, N_HIGH_MEMORY) {
3111 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
3114 ret = mem_cgroup_force_empty_list(mem,
3123 mem_cgroup_end_move(mem);
3124 memcg_oom_recover(mem);
3125 /* it seems parent cgroup doesn't have enough mem */
3129 /* "ret" should also be checked to ensure all lists are empty. */
3130 } while (mem->res.usage > 0 || ret);
3136 /* returns EBUSY if there is a task or if we come here twice. */
3137 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
3141 /* we call try-to-free pages for make this cgroup empty */
3142 lru_add_drain_all();
3143 /* try to free all pages in this cgroup */
3145 while (nr_retries && mem->res.usage > 0) {
3148 if (signal_pending(current)) {
3152 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
3153 false, get_swappiness(mem));
3156 /* maybe some writeback is necessary */
3157 congestion_wait(BLK_RW_ASYNC, HZ/10);
3162 /* try move_account...there may be some *locked* pages. */
3166 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
3168 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
3172 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
3174 return mem_cgroup_from_cont(cont)->use_hierarchy;
3177 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
3181 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3182 struct cgroup *parent = cont->parent;
3183 struct mem_cgroup *parent_mem = NULL;
3186 parent_mem = mem_cgroup_from_cont(parent);
3190 * If parent's use_hierarchy is set, we can't make any modifications
3191 * in the child subtrees. If it is unset, then the change can
3192 * occur, provided the current cgroup has no children.
3194 * For the root cgroup, parent_mem is NULL, we allow value to be
3195 * set if there are no children.
3197 if ((!parent_mem || !parent_mem->use_hierarchy) &&
3198 (val == 1 || val == 0)) {
3199 if (list_empty(&cont->children))
3200 mem->use_hierarchy = val;
3210 struct mem_cgroup_idx_data {
3212 enum mem_cgroup_stat_index idx;
3216 mem_cgroup_get_idx_stat(struct mem_cgroup *mem, void *data)
3218 struct mem_cgroup_idx_data *d = data;
3219 d->val += mem_cgroup_read_stat(mem, d->idx);
3224 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup *mem,
3225 enum mem_cgroup_stat_index idx, s64 *val)
3227 struct mem_cgroup_idx_data d;
3230 mem_cgroup_walk_tree(mem, &d, mem_cgroup_get_idx_stat);
3234 static inline u64 mem_cgroup_usage(struct mem_cgroup *mem, bool swap)
3238 if (!mem_cgroup_is_root(mem)) {
3240 return res_counter_read_u64(&mem->res, RES_USAGE);
3242 return res_counter_read_u64(&mem->memsw, RES_USAGE);
3245 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_CACHE, &idx_val);
3247 mem_cgroup_get_recursive_idx_stat(mem, MEM_CGROUP_STAT_RSS, &idx_val);
3251 mem_cgroup_get_recursive_idx_stat(mem,
3252 MEM_CGROUP_STAT_SWAPOUT, &idx_val);
3256 return val << PAGE_SHIFT;
3259 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
3261 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
3265 type = MEMFILE_TYPE(cft->private);
3266 name = MEMFILE_ATTR(cft->private);
3269 if (name == RES_USAGE)
3270 val = mem_cgroup_usage(mem, false);
3272 val = res_counter_read_u64(&mem->res, name);
3275 if (name == RES_USAGE)
3276 val = mem_cgroup_usage(mem, true);
3278 val = res_counter_read_u64(&mem->memsw, name);
3287 * The user of this function is...
3290 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
3293 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
3295 unsigned long long val;
3298 type = MEMFILE_TYPE(cft->private);
3299 name = MEMFILE_ATTR(cft->private);
3302 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
3306 /* This function does all necessary parse...reuse it */
3307 ret = res_counter_memparse_write_strategy(buffer, &val);
3311 ret = mem_cgroup_resize_limit(memcg, val);
3313 ret = mem_cgroup_resize_memsw_limit(memcg, val);
3315 case RES_SOFT_LIMIT:
3316 ret = res_counter_memparse_write_strategy(buffer, &val);
3320 * For memsw, soft limits are hard to implement in terms
3321 * of semantics, for now, we support soft limits for
3322 * control without swap
3325 ret = res_counter_set_soft_limit(&memcg->res, val);
3330 ret = -EINVAL; /* should be BUG() ? */
3336 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
3337 unsigned long long *mem_limit, unsigned long long *memsw_limit)
3339 struct cgroup *cgroup;
3340 unsigned long long min_limit, min_memsw_limit, tmp;
3342 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
3343 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3344 cgroup = memcg->css.cgroup;
3345 if (!memcg->use_hierarchy)
3348 while (cgroup->parent) {
3349 cgroup = cgroup->parent;
3350 memcg = mem_cgroup_from_cont(cgroup);
3351 if (!memcg->use_hierarchy)
3353 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
3354 min_limit = min(min_limit, tmp);
3355 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
3356 min_memsw_limit = min(min_memsw_limit, tmp);
3359 *mem_limit = min_limit;
3360 *memsw_limit = min_memsw_limit;
3364 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
3366 struct mem_cgroup *mem;
3369 mem = mem_cgroup_from_cont(cont);
3370 type = MEMFILE_TYPE(event);
3371 name = MEMFILE_ATTR(event);
3375 res_counter_reset_max(&mem->res);
3377 res_counter_reset_max(&mem->memsw);
3381 res_counter_reset_failcnt(&mem->res);
3383 res_counter_reset_failcnt(&mem->memsw);
3390 static u64 mem_cgroup_move_charge_read(struct cgroup *cgrp,
3393 return mem_cgroup_from_cont(cgrp)->move_charge_at_immigrate;
3397 static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3398 struct cftype *cft, u64 val)
3400 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3402 if (val >= (1 << NR_MOVE_TYPE))
3405 * We check this value several times in both in can_attach() and
3406 * attach(), so we need cgroup lock to prevent this value from being
3410 mem->move_charge_at_immigrate = val;
3416 static int mem_cgroup_move_charge_write(struct cgroup *cgrp,
3417 struct cftype *cft, u64 val)
3424 /* For read statistics */
3440 struct mcs_total_stat {
3441 s64 stat[NR_MCS_STAT];
3447 } memcg_stat_strings[NR_MCS_STAT] = {
3448 {"cache", "total_cache"},
3449 {"rss", "total_rss"},
3450 {"mapped_file", "total_mapped_file"},
3451 {"pgpgin", "total_pgpgin"},
3452 {"pgpgout", "total_pgpgout"},
3453 {"swap", "total_swap"},
3454 {"inactive_anon", "total_inactive_anon"},
3455 {"active_anon", "total_active_anon"},
3456 {"inactive_file", "total_inactive_file"},
3457 {"active_file", "total_active_file"},
3458 {"unevictable", "total_unevictable"}
3462 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
3464 struct mcs_total_stat *s = data;
3468 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_CACHE);
3469 s->stat[MCS_CACHE] += val * PAGE_SIZE;
3470 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_RSS);
3471 s->stat[MCS_RSS] += val * PAGE_SIZE;
3472 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_FILE_MAPPED);
3473 s->stat[MCS_FILE_MAPPED] += val * PAGE_SIZE;
3474 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGIN_COUNT);
3475 s->stat[MCS_PGPGIN] += val;
3476 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_PGPGOUT_COUNT);
3477 s->stat[MCS_PGPGOUT] += val;
3478 if (do_swap_account) {
3479 val = mem_cgroup_read_stat(mem, MEM_CGROUP_STAT_SWAPOUT);
3480 s->stat[MCS_SWAP] += val * PAGE_SIZE;
3484 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
3485 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
3486 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
3487 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
3488 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
3489 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
3490 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
3491 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
3492 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
3493 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
3498 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
3500 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
3503 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
3504 struct cgroup_map_cb *cb)
3506 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
3507 struct mcs_total_stat mystat;
3510 memset(&mystat, 0, sizeof(mystat));
3511 mem_cgroup_get_local_stat(mem_cont, &mystat);
3513 for (i = 0; i < NR_MCS_STAT; i++) {
3514 if (i == MCS_SWAP && !do_swap_account)
3516 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
3519 /* Hierarchical information */
3521 unsigned long long limit, memsw_limit;
3522 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
3523 cb->fill(cb, "hierarchical_memory_limit", limit);
3524 if (do_swap_account)
3525 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
3528 memset(&mystat, 0, sizeof(mystat));
3529 mem_cgroup_get_total_stat(mem_cont, &mystat);
3530 for (i = 0; i < NR_MCS_STAT; i++) {
3531 if (i == MCS_SWAP && !do_swap_account)
3533 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
3536 #ifdef CONFIG_DEBUG_VM
3537 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
3541 struct mem_cgroup_per_zone *mz;
3542 unsigned long recent_rotated[2] = {0, 0};
3543 unsigned long recent_scanned[2] = {0, 0};
3545 for_each_online_node(nid)
3546 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
3547 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
3549 recent_rotated[0] +=
3550 mz->reclaim_stat.recent_rotated[0];
3551 recent_rotated[1] +=
3552 mz->reclaim_stat.recent_rotated[1];
3553 recent_scanned[0] +=
3554 mz->reclaim_stat.recent_scanned[0];
3555 recent_scanned[1] +=
3556 mz->reclaim_stat.recent_scanned[1];
3558 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
3559 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
3560 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
3561 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
3568 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
3570 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3572 return get_swappiness(memcg);
3575 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
3578 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3579 struct mem_cgroup *parent;
3584 if (cgrp->parent == NULL)
3587 parent = mem_cgroup_from_cont(cgrp->parent);
3591 /* If under hierarchy, only empty-root can set this value */
3592 if ((parent->use_hierarchy) ||
3593 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
3598 spin_lock(&memcg->reclaim_param_lock);
3599 memcg->swappiness = val;
3600 spin_unlock(&memcg->reclaim_param_lock);
3607 static void __mem_cgroup_threshold(struct mem_cgroup *memcg, bool swap)
3609 struct mem_cgroup_threshold_ary *t;
3615 t = rcu_dereference(memcg->thresholds.primary);
3617 t = rcu_dereference(memcg->memsw_thresholds.primary);
3622 usage = mem_cgroup_usage(memcg, swap);
3625 * current_threshold points to threshold just below usage.
3626 * If it's not true, a threshold was crossed after last
3627 * call of __mem_cgroup_threshold().
3629 i = t->current_threshold;
3632 * Iterate backward over array of thresholds starting from
3633 * current_threshold and check if a threshold is crossed.
3634 * If none of thresholds below usage is crossed, we read
3635 * only one element of the array here.
3637 for (; i >= 0 && unlikely(t->entries[i].threshold > usage); i--)
3638 eventfd_signal(t->entries[i].eventfd, 1);
3640 /* i = current_threshold + 1 */
3644 * Iterate forward over array of thresholds starting from
3645 * current_threshold+1 and check if a threshold is crossed.
3646 * If none of thresholds above usage is crossed, we read
3647 * only one element of the array here.
3649 for (; i < t->size && unlikely(t->entries[i].threshold <= usage); i++)
3650 eventfd_signal(t->entries[i].eventfd, 1);
3652 /* Update current_threshold */
3653 t->current_threshold = i - 1;
3658 static void mem_cgroup_threshold(struct mem_cgroup *memcg)
3661 __mem_cgroup_threshold(memcg, false);
3662 if (do_swap_account)
3663 __mem_cgroup_threshold(memcg, true);
3665 memcg = parent_mem_cgroup(memcg);
3669 static int compare_thresholds(const void *a, const void *b)
3671 const struct mem_cgroup_threshold *_a = a;
3672 const struct mem_cgroup_threshold *_b = b;
3674 return _a->threshold - _b->threshold;
3677 static int mem_cgroup_oom_notify_cb(struct mem_cgroup *mem, void *data)
3679 struct mem_cgroup_eventfd_list *ev;
3681 list_for_each_entry(ev, &mem->oom_notify, list)
3682 eventfd_signal(ev->eventfd, 1);
3686 static void mem_cgroup_oom_notify(struct mem_cgroup *mem)
3688 mem_cgroup_walk_tree(mem, NULL, mem_cgroup_oom_notify_cb);
3691 static int mem_cgroup_usage_register_event(struct cgroup *cgrp,
3692 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3694 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3695 struct mem_cgroup_thresholds *thresholds;
3696 struct mem_cgroup_threshold_ary *new;
3697 int type = MEMFILE_TYPE(cft->private);
3698 u64 threshold, usage;
3701 ret = res_counter_memparse_write_strategy(args, &threshold);
3705 mutex_lock(&memcg->thresholds_lock);
3708 thresholds = &memcg->thresholds;
3709 else if (type == _MEMSWAP)
3710 thresholds = &memcg->memsw_thresholds;
3714 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3716 /* Check if a threshold crossed before adding a new one */
3717 if (thresholds->primary)
3718 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3720 size = thresholds->primary ? thresholds->primary->size + 1 : 1;
3722 /* Allocate memory for new array of thresholds */
3723 new = kmalloc(sizeof(*new) + size * sizeof(struct mem_cgroup_threshold),
3731 /* Copy thresholds (if any) to new array */
3732 if (thresholds->primary) {
3733 memcpy(new->entries, thresholds->primary->entries, (size - 1) *
3734 sizeof(struct mem_cgroup_threshold));
3737 /* Add new threshold */
3738 new->entries[size - 1].eventfd = eventfd;
3739 new->entries[size - 1].threshold = threshold;
3741 /* Sort thresholds. Registering of new threshold isn't time-critical */
3742 sort(new->entries, size, sizeof(struct mem_cgroup_threshold),
3743 compare_thresholds, NULL);
3745 /* Find current threshold */
3746 new->current_threshold = -1;
3747 for (i = 0; i < size; i++) {
3748 if (new->entries[i].threshold < usage) {
3750 * new->current_threshold will not be used until
3751 * rcu_assign_pointer(), so it's safe to increment
3754 ++new->current_threshold;
3758 /* Free old spare buffer and save old primary buffer as spare */
3759 kfree(thresholds->spare);
3760 thresholds->spare = thresholds->primary;
3762 rcu_assign_pointer(thresholds->primary, new);
3764 /* To be sure that nobody uses thresholds */
3768 mutex_unlock(&memcg->thresholds_lock);
3773 static void mem_cgroup_usage_unregister_event(struct cgroup *cgrp,
3774 struct cftype *cft, struct eventfd_ctx *eventfd)
3776 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3777 struct mem_cgroup_thresholds *thresholds;
3778 struct mem_cgroup_threshold_ary *new;
3779 int type = MEMFILE_TYPE(cft->private);
3783 mutex_lock(&memcg->thresholds_lock);
3785 thresholds = &memcg->thresholds;
3786 else if (type == _MEMSWAP)
3787 thresholds = &memcg->memsw_thresholds;
3792 * Something went wrong if we trying to unregister a threshold
3793 * if we don't have thresholds
3795 BUG_ON(!thresholds);
3797 usage = mem_cgroup_usage(memcg, type == _MEMSWAP);
3799 /* Check if a threshold crossed before removing */
3800 __mem_cgroup_threshold(memcg, type == _MEMSWAP);
3802 /* Calculate new number of threshold */
3804 for (i = 0; i < thresholds->primary->size; i++) {
3805 if (thresholds->primary->entries[i].eventfd != eventfd)
3809 new = thresholds->spare;
3811 /* Set thresholds array to NULL if we don't have thresholds */
3820 /* Copy thresholds and find current threshold */
3821 new->current_threshold = -1;
3822 for (i = 0, j = 0; i < thresholds->primary->size; i++) {
3823 if (thresholds->primary->entries[i].eventfd == eventfd)
3826 new->entries[j] = thresholds->primary->entries[i];
3827 if (new->entries[j].threshold < usage) {
3829 * new->current_threshold will not be used
3830 * until rcu_assign_pointer(), so it's safe to increment
3833 ++new->current_threshold;
3839 /* Swap primary and spare array */
3840 thresholds->spare = thresholds->primary;
3841 rcu_assign_pointer(thresholds->primary, new);
3843 /* To be sure that nobody uses thresholds */
3846 mutex_unlock(&memcg->thresholds_lock);
3849 static int mem_cgroup_oom_register_event(struct cgroup *cgrp,
3850 struct cftype *cft, struct eventfd_ctx *eventfd, const char *args)
3852 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
3853 struct mem_cgroup_eventfd_list *event;
3854 int type = MEMFILE_TYPE(cft->private);
3856 BUG_ON(type != _OOM_TYPE);
3857 event = kmalloc(sizeof(*event), GFP_KERNEL);
3861 mutex_lock(&memcg_oom_mutex);
3863 event->eventfd = eventfd;
3864 list_add(&event->list, &memcg->oom_notify);
3866 /* already in OOM ? */
3867 if (atomic_read(&memcg->oom_lock))
3868 eventfd_signal(eventfd, 1);
3869 mutex_unlock(&memcg_oom_mutex);
3874 static void mem_cgroup_oom_unregister_event(struct cgroup *cgrp,
3875 struct cftype *cft, struct eventfd_ctx *eventfd)
3877 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3878 struct mem_cgroup_eventfd_list *ev, *tmp;
3879 int type = MEMFILE_TYPE(cft->private);
3881 BUG_ON(type != _OOM_TYPE);
3883 mutex_lock(&memcg_oom_mutex);
3885 list_for_each_entry_safe(ev, tmp, &mem->oom_notify, list) {
3886 if (ev->eventfd == eventfd) {
3887 list_del(&ev->list);
3892 mutex_unlock(&memcg_oom_mutex);
3895 static int mem_cgroup_oom_control_read(struct cgroup *cgrp,
3896 struct cftype *cft, struct cgroup_map_cb *cb)
3898 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3900 cb->fill(cb, "oom_kill_disable", mem->oom_kill_disable);
3902 if (atomic_read(&mem->oom_lock))
3903 cb->fill(cb, "under_oom", 1);
3905 cb->fill(cb, "under_oom", 0);
3909 static int mem_cgroup_oom_control_write(struct cgroup *cgrp,
3910 struct cftype *cft, u64 val)
3912 struct mem_cgroup *mem = mem_cgroup_from_cont(cgrp);
3913 struct mem_cgroup *parent;
3915 /* cannot set to root cgroup and only 0 and 1 are allowed */
3916 if (!cgrp->parent || !((val == 0) || (val == 1)))
3919 parent = mem_cgroup_from_cont(cgrp->parent);
3922 /* oom-kill-disable is a flag for subhierarchy. */
3923 if ((parent->use_hierarchy) ||
3924 (mem->use_hierarchy && !list_empty(&cgrp->children))) {
3928 mem->oom_kill_disable = val;
3930 memcg_oom_recover(mem);
3935 static struct cftype mem_cgroup_files[] = {
3937 .name = "usage_in_bytes",
3938 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
3939 .read_u64 = mem_cgroup_read,
3940 .register_event = mem_cgroup_usage_register_event,
3941 .unregister_event = mem_cgroup_usage_unregister_event,
3944 .name = "max_usage_in_bytes",
3945 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
3946 .trigger = mem_cgroup_reset,
3947 .read_u64 = mem_cgroup_read,
3950 .name = "limit_in_bytes",
3951 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
3952 .write_string = mem_cgroup_write,
3953 .read_u64 = mem_cgroup_read,
3956 .name = "soft_limit_in_bytes",
3957 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
3958 .write_string = mem_cgroup_write,
3959 .read_u64 = mem_cgroup_read,
3963 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
3964 .trigger = mem_cgroup_reset,
3965 .read_u64 = mem_cgroup_read,
3969 .read_map = mem_control_stat_show,
3972 .name = "force_empty",
3973 .trigger = mem_cgroup_force_empty_write,
3976 .name = "use_hierarchy",
3977 .write_u64 = mem_cgroup_hierarchy_write,
3978 .read_u64 = mem_cgroup_hierarchy_read,
3981 .name = "swappiness",
3982 .read_u64 = mem_cgroup_swappiness_read,
3983 .write_u64 = mem_cgroup_swappiness_write,
3986 .name = "move_charge_at_immigrate",
3987 .read_u64 = mem_cgroup_move_charge_read,
3988 .write_u64 = mem_cgroup_move_charge_write,
3991 .name = "oom_control",
3992 .read_map = mem_cgroup_oom_control_read,
3993 .write_u64 = mem_cgroup_oom_control_write,
3994 .register_event = mem_cgroup_oom_register_event,
3995 .unregister_event = mem_cgroup_oom_unregister_event,
3996 .private = MEMFILE_PRIVATE(_OOM_TYPE, OOM_CONTROL),
4000 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4001 static struct cftype memsw_cgroup_files[] = {
4003 .name = "memsw.usage_in_bytes",
4004 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
4005 .read_u64 = mem_cgroup_read,
4006 .register_event = mem_cgroup_usage_register_event,
4007 .unregister_event = mem_cgroup_usage_unregister_event,
4010 .name = "memsw.max_usage_in_bytes",
4011 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
4012 .trigger = mem_cgroup_reset,
4013 .read_u64 = mem_cgroup_read,
4016 .name = "memsw.limit_in_bytes",
4017 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
4018 .write_string = mem_cgroup_write,
4019 .read_u64 = mem_cgroup_read,
4022 .name = "memsw.failcnt",
4023 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
4024 .trigger = mem_cgroup_reset,
4025 .read_u64 = mem_cgroup_read,
4029 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4031 if (!do_swap_account)
4033 return cgroup_add_files(cont, ss, memsw_cgroup_files,
4034 ARRAY_SIZE(memsw_cgroup_files));
4037 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
4043 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4045 struct mem_cgroup_per_node *pn;
4046 struct mem_cgroup_per_zone *mz;
4048 int zone, tmp = node;
4050 * This routine is called against possible nodes.
4051 * But it's BUG to call kmalloc() against offline node.
4053 * TODO: this routine can waste much memory for nodes which will
4054 * never be onlined. It's better to use memory hotplug callback
4057 if (!node_state(node, N_NORMAL_MEMORY))
4059 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
4063 mem->info.nodeinfo[node] = pn;
4064 memset(pn, 0, sizeof(*pn));
4066 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4067 mz = &pn->zoneinfo[zone];
4069 INIT_LIST_HEAD(&mz->lists[l]);
4070 mz->usage_in_excess = 0;
4071 mz->on_tree = false;
4077 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
4079 kfree(mem->info.nodeinfo[node]);
4082 static struct mem_cgroup *mem_cgroup_alloc(void)
4084 struct mem_cgroup *mem;
4085 int size = sizeof(struct mem_cgroup);
4087 /* Can be very big if MAX_NUMNODES is very big */
4088 if (size < PAGE_SIZE)
4089 mem = kmalloc(size, GFP_KERNEL);
4091 mem = vmalloc(size);
4096 memset(mem, 0, size);
4097 mem->stat = alloc_percpu(struct mem_cgroup_stat_cpu);
4099 if (size < PAGE_SIZE)
4109 * At destroying mem_cgroup, references from swap_cgroup can remain.
4110 * (scanning all at force_empty is too costly...)
4112 * Instead of clearing all references at force_empty, we remember
4113 * the number of reference from swap_cgroup and free mem_cgroup when
4114 * it goes down to 0.
4116 * Removal of cgroup itself succeeds regardless of refs from swap.
4119 static void __mem_cgroup_free(struct mem_cgroup *mem)
4123 mem_cgroup_remove_from_trees(mem);
4124 free_css_id(&mem_cgroup_subsys, &mem->css);
4126 for_each_node_state(node, N_POSSIBLE)
4127 free_mem_cgroup_per_zone_info(mem, node);
4129 free_percpu(mem->stat);
4130 if (sizeof(struct mem_cgroup) < PAGE_SIZE)
4136 static void mem_cgroup_get(struct mem_cgroup *mem)
4138 atomic_inc(&mem->refcnt);
4141 static void __mem_cgroup_put(struct mem_cgroup *mem, int count)
4143 if (atomic_sub_and_test(count, &mem->refcnt)) {
4144 struct mem_cgroup *parent = parent_mem_cgroup(mem);
4145 __mem_cgroup_free(mem);
4147 mem_cgroup_put(parent);
4151 static void mem_cgroup_put(struct mem_cgroup *mem)
4153 __mem_cgroup_put(mem, 1);
4157 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
4159 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
4161 if (!mem->res.parent)
4163 return mem_cgroup_from_res_counter(mem->res.parent, res);
4166 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4167 static void __init enable_swap_cgroup(void)
4169 if (!mem_cgroup_disabled() && really_do_swap_account)
4170 do_swap_account = 1;
4173 static void __init enable_swap_cgroup(void)
4178 static int mem_cgroup_soft_limit_tree_init(void)
4180 struct mem_cgroup_tree_per_node *rtpn;
4181 struct mem_cgroup_tree_per_zone *rtpz;
4182 int tmp, node, zone;
4184 for_each_node_state(node, N_POSSIBLE) {
4186 if (!node_state(node, N_NORMAL_MEMORY))
4188 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
4192 soft_limit_tree.rb_tree_per_node[node] = rtpn;
4194 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
4195 rtpz = &rtpn->rb_tree_per_zone[zone];
4196 rtpz->rb_root = RB_ROOT;
4197 spin_lock_init(&rtpz->lock);
4203 static struct cgroup_subsys_state * __ref
4204 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
4206 struct mem_cgroup *mem, *parent;
4207 long error = -ENOMEM;
4210 mem = mem_cgroup_alloc();
4212 return ERR_PTR(error);
4214 for_each_node_state(node, N_POSSIBLE)
4215 if (alloc_mem_cgroup_per_zone_info(mem, node))
4219 if (cont->parent == NULL) {
4221 enable_swap_cgroup();
4223 root_mem_cgroup = mem;
4224 if (mem_cgroup_soft_limit_tree_init())
4226 for_each_possible_cpu(cpu) {
4227 struct memcg_stock_pcp *stock =
4228 &per_cpu(memcg_stock, cpu);
4229 INIT_WORK(&stock->work, drain_local_stock);
4231 hotcpu_notifier(memcg_stock_cpu_callback, 0);
4233 parent = mem_cgroup_from_cont(cont->parent);
4234 mem->use_hierarchy = parent->use_hierarchy;
4235 mem->oom_kill_disable = parent->oom_kill_disable;
4238 if (parent && parent->use_hierarchy) {
4239 res_counter_init(&mem->res, &parent->res);
4240 res_counter_init(&mem->memsw, &parent->memsw);
4242 * We increment refcnt of the parent to ensure that we can
4243 * safely access it on res_counter_charge/uncharge.
4244 * This refcnt will be decremented when freeing this
4245 * mem_cgroup(see mem_cgroup_put).
4247 mem_cgroup_get(parent);
4249 res_counter_init(&mem->res, NULL);
4250 res_counter_init(&mem->memsw, NULL);
4252 mem->last_scanned_child = 0;
4253 spin_lock_init(&mem->reclaim_param_lock);
4254 INIT_LIST_HEAD(&mem->oom_notify);
4257 mem->swappiness = get_swappiness(parent);
4258 atomic_set(&mem->refcnt, 1);
4259 mem->move_charge_at_immigrate = 0;
4260 mutex_init(&mem->thresholds_lock);
4263 __mem_cgroup_free(mem);
4264 root_mem_cgroup = NULL;
4265 return ERR_PTR(error);
4268 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
4269 struct cgroup *cont)
4271 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
4273 return mem_cgroup_force_empty(mem, false);
4276 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
4277 struct cgroup *cont)
4279 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
4281 mem_cgroup_put(mem);
4284 static int mem_cgroup_populate(struct cgroup_subsys *ss,
4285 struct cgroup *cont)
4289 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
4290 ARRAY_SIZE(mem_cgroup_files));
4293 ret = register_memsw_files(cont, ss);
4298 /* Handlers for move charge at task migration. */
4299 #define PRECHARGE_COUNT_AT_ONCE 256
4300 static int mem_cgroup_do_precharge(unsigned long count)
4303 int batch_count = PRECHARGE_COUNT_AT_ONCE;
4304 struct mem_cgroup *mem = mc.to;
4306 if (mem_cgroup_is_root(mem)) {
4307 mc.precharge += count;
4308 /* we don't need css_get for root */
4311 /* try to charge at once */
4313 struct res_counter *dummy;
4315 * "mem" cannot be under rmdir() because we've already checked
4316 * by cgroup_lock_live_cgroup() that it is not removed and we
4317 * are still under the same cgroup_mutex. So we can postpone
4320 if (res_counter_charge(&mem->res, PAGE_SIZE * count, &dummy))
4322 if (do_swap_account && res_counter_charge(&mem->memsw,
4323 PAGE_SIZE * count, &dummy)) {
4324 res_counter_uncharge(&mem->res, PAGE_SIZE * count);
4327 mc.precharge += count;
4331 /* fall back to one by one charge */
4333 if (signal_pending(current)) {
4337 if (!batch_count--) {
4338 batch_count = PRECHARGE_COUNT_AT_ONCE;
4341 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
4343 /* mem_cgroup_clear_mc() will do uncharge later */
4351 * is_target_pte_for_mc - check a pte whether it is valid for move charge
4352 * @vma: the vma the pte to be checked belongs
4353 * @addr: the address corresponding to the pte to be checked
4354 * @ptent: the pte to be checked
4355 * @target: the pointer the target page or swap ent will be stored(can be NULL)
4358 * 0(MC_TARGET_NONE): if the pte is not a target for move charge.
4359 * 1(MC_TARGET_PAGE): if the page corresponding to this pte is a target for
4360 * move charge. if @target is not NULL, the page is stored in target->page
4361 * with extra refcnt got(Callers should handle it).
4362 * 2(MC_TARGET_SWAP): if the swap entry corresponding to this pte is a
4363 * target for charge migration. if @target is not NULL, the entry is stored
4366 * Called with pte lock held.
4373 enum mc_target_type {
4374 MC_TARGET_NONE, /* not used */
4379 static struct page *mc_handle_present_pte(struct vm_area_struct *vma,
4380 unsigned long addr, pte_t ptent)
4382 struct page *page = vm_normal_page(vma, addr, ptent);
4384 if (!page || !page_mapped(page))
4386 if (PageAnon(page)) {
4387 /* we don't move shared anon */
4388 if (!move_anon() || page_mapcount(page) > 2)
4390 } else if (!move_file())
4391 /* we ignore mapcount for file pages */
4393 if (!get_page_unless_zero(page))
4399 static struct page *mc_handle_swap_pte(struct vm_area_struct *vma,
4400 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4403 struct page *page = NULL;
4404 swp_entry_t ent = pte_to_swp_entry(ptent);
4406 if (!move_anon() || non_swap_entry(ent))
4408 usage_count = mem_cgroup_count_swap_user(ent, &page);
4409 if (usage_count > 1) { /* we don't move shared anon */
4414 if (do_swap_account)
4415 entry->val = ent.val;
4420 static struct page *mc_handle_file_pte(struct vm_area_struct *vma,
4421 unsigned long addr, pte_t ptent, swp_entry_t *entry)
4423 struct page *page = NULL;
4424 struct inode *inode;
4425 struct address_space *mapping;
4428 if (!vma->vm_file) /* anonymous vma */
4433 inode = vma->vm_file->f_path.dentry->d_inode;
4434 mapping = vma->vm_file->f_mapping;
4435 if (pte_none(ptent))
4436 pgoff = linear_page_index(vma, addr);
4437 else /* pte_file(ptent) is true */
4438 pgoff = pte_to_pgoff(ptent);
4440 /* page is moved even if it's not RSS of this task(page-faulted). */
4441 if (!mapping_cap_swap_backed(mapping)) { /* normal file */
4442 page = find_get_page(mapping, pgoff);
4443 } else { /* shmem/tmpfs file. we should take account of swap too. */
4445 mem_cgroup_get_shmem_target(inode, pgoff, &page, &ent);
4446 if (do_swap_account)
4447 entry->val = ent.val;
4453 static int is_target_pte_for_mc(struct vm_area_struct *vma,
4454 unsigned long addr, pte_t ptent, union mc_target *target)
4456 struct page *page = NULL;
4457 struct page_cgroup *pc;
4459 swp_entry_t ent = { .val = 0 };
4461 if (pte_present(ptent))
4462 page = mc_handle_present_pte(vma, addr, ptent);
4463 else if (is_swap_pte(ptent))
4464 page = mc_handle_swap_pte(vma, addr, ptent, &ent);
4465 else if (pte_none(ptent) || pte_file(ptent))
4466 page = mc_handle_file_pte(vma, addr, ptent, &ent);
4468 if (!page && !ent.val)
4471 pc = lookup_page_cgroup(page);
4473 * Do only loose check w/o page_cgroup lock.
4474 * mem_cgroup_move_account() checks the pc is valid or not under
4477 if (PageCgroupUsed(pc) && pc->mem_cgroup == mc.from) {
4478 ret = MC_TARGET_PAGE;
4480 target->page = page;
4482 if (!ret || !target)
4485 /* There is a swap entry and a page doesn't exist or isn't charged */
4486 if (ent.val && !ret &&
4487 css_id(&mc.from->css) == lookup_swap_cgroup(ent)) {
4488 ret = MC_TARGET_SWAP;
4495 static int mem_cgroup_count_precharge_pte_range(pmd_t *pmd,
4496 unsigned long addr, unsigned long end,
4497 struct mm_walk *walk)
4499 struct vm_area_struct *vma = walk->private;
4503 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4504 for (; addr != end; pte++, addr += PAGE_SIZE)
4505 if (is_target_pte_for_mc(vma, addr, *pte, NULL))
4506 mc.precharge++; /* increment precharge temporarily */
4507 pte_unmap_unlock(pte - 1, ptl);
4513 static unsigned long mem_cgroup_count_precharge(struct mm_struct *mm)
4515 unsigned long precharge;
4516 struct vm_area_struct *vma;
4518 down_read(&mm->mmap_sem);
4519 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4520 struct mm_walk mem_cgroup_count_precharge_walk = {
4521 .pmd_entry = mem_cgroup_count_precharge_pte_range,
4525 if (is_vm_hugetlb_page(vma))
4527 walk_page_range(vma->vm_start, vma->vm_end,
4528 &mem_cgroup_count_precharge_walk);
4530 up_read(&mm->mmap_sem);
4532 precharge = mc.precharge;
4538 static int mem_cgroup_precharge_mc(struct mm_struct *mm)
4540 return mem_cgroup_do_precharge(mem_cgroup_count_precharge(mm));
4543 static void mem_cgroup_clear_mc(void)
4545 struct mem_cgroup *from = mc.from;
4546 struct mem_cgroup *to = mc.to;
4548 /* we must uncharge all the leftover precharges from mc.to */
4550 __mem_cgroup_cancel_charge(mc.to, mc.precharge);
4554 * we didn't uncharge from mc.from at mem_cgroup_move_account(), so
4555 * we must uncharge here.
4557 if (mc.moved_charge) {
4558 __mem_cgroup_cancel_charge(mc.from, mc.moved_charge);
4559 mc.moved_charge = 0;
4561 /* we must fixup refcnts and charges */
4562 if (mc.moved_swap) {
4563 /* uncharge swap account from the old cgroup */
4564 if (!mem_cgroup_is_root(mc.from))
4565 res_counter_uncharge(&mc.from->memsw,
4566 PAGE_SIZE * mc.moved_swap);
4567 __mem_cgroup_put(mc.from, mc.moved_swap);
4569 if (!mem_cgroup_is_root(mc.to)) {
4571 * we charged both to->res and to->memsw, so we should
4574 res_counter_uncharge(&mc.to->res,
4575 PAGE_SIZE * mc.moved_swap);
4577 /* we've already done mem_cgroup_get(mc.to) */
4581 spin_lock(&mc.lock);
4584 mc.moving_task = NULL;
4585 spin_unlock(&mc.lock);
4586 mem_cgroup_end_move(from);
4587 memcg_oom_recover(from);
4588 memcg_oom_recover(to);
4589 wake_up_all(&mc.waitq);
4592 static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4593 struct cgroup *cgroup,
4594 struct task_struct *p,
4598 struct mem_cgroup *mem = mem_cgroup_from_cont(cgroup);
4600 if (mem->move_charge_at_immigrate) {
4601 struct mm_struct *mm;
4602 struct mem_cgroup *from = mem_cgroup_from_task(p);
4604 VM_BUG_ON(from == mem);
4606 mm = get_task_mm(p);
4609 /* We move charges only when we move a owner of the mm */
4610 if (mm->owner == p) {
4613 VM_BUG_ON(mc.precharge);
4614 VM_BUG_ON(mc.moved_charge);
4615 VM_BUG_ON(mc.moved_swap);
4616 VM_BUG_ON(mc.moving_task);
4617 mem_cgroup_start_move(from);
4618 spin_lock(&mc.lock);
4622 mc.moved_charge = 0;
4624 mc.moving_task = current;
4625 spin_unlock(&mc.lock);
4627 ret = mem_cgroup_precharge_mc(mm);
4629 mem_cgroup_clear_mc();
4636 static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4637 struct cgroup *cgroup,
4638 struct task_struct *p,
4641 mem_cgroup_clear_mc();
4644 static int mem_cgroup_move_charge_pte_range(pmd_t *pmd,
4645 unsigned long addr, unsigned long end,
4646 struct mm_walk *walk)
4649 struct vm_area_struct *vma = walk->private;
4654 pte = pte_offset_map_lock(vma->vm_mm, pmd, addr, &ptl);
4655 for (; addr != end; addr += PAGE_SIZE) {
4656 pte_t ptent = *(pte++);
4657 union mc_target target;
4660 struct page_cgroup *pc;
4666 type = is_target_pte_for_mc(vma, addr, ptent, &target);
4668 case MC_TARGET_PAGE:
4670 if (isolate_lru_page(page))
4672 pc = lookup_page_cgroup(page);
4673 if (!mem_cgroup_move_account(pc,
4674 mc.from, mc.to, false)) {
4676 /* we uncharge from mc.from later. */
4679 putback_lru_page(page);
4680 put: /* is_target_pte_for_mc() gets the page */
4683 case MC_TARGET_SWAP:
4685 if (!mem_cgroup_move_swap_account(ent,
4686 mc.from, mc.to, false)) {
4688 /* we fixup refcnts and charges later. */
4696 pte_unmap_unlock(pte - 1, ptl);
4701 * We have consumed all precharges we got in can_attach().
4702 * We try charge one by one, but don't do any additional
4703 * charges to mc.to if we have failed in charge once in attach()
4706 ret = mem_cgroup_do_precharge(1);
4714 static void mem_cgroup_move_charge(struct mm_struct *mm)
4716 struct vm_area_struct *vma;
4718 lru_add_drain_all();
4719 down_read(&mm->mmap_sem);
4720 for (vma = mm->mmap; vma; vma = vma->vm_next) {
4722 struct mm_walk mem_cgroup_move_charge_walk = {
4723 .pmd_entry = mem_cgroup_move_charge_pte_range,
4727 if (is_vm_hugetlb_page(vma))
4729 ret = walk_page_range(vma->vm_start, vma->vm_end,
4730 &mem_cgroup_move_charge_walk);
4733 * means we have consumed all precharges and failed in
4734 * doing additional charge. Just abandon here.
4738 up_read(&mm->mmap_sem);
4741 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4742 struct cgroup *cont,
4743 struct cgroup *old_cont,
4744 struct task_struct *p,
4747 struct mm_struct *mm;
4750 /* no need to move charge */
4753 mm = get_task_mm(p);
4755 mem_cgroup_move_charge(mm);
4758 mem_cgroup_clear_mc();
4760 #else /* !CONFIG_MMU */
4761 static int mem_cgroup_can_attach(struct cgroup_subsys *ss,
4762 struct cgroup *cgroup,
4763 struct task_struct *p,
4768 static void mem_cgroup_cancel_attach(struct cgroup_subsys *ss,
4769 struct cgroup *cgroup,
4770 struct task_struct *p,
4774 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
4775 struct cgroup *cont,
4776 struct cgroup *old_cont,
4777 struct task_struct *p,
4783 struct cgroup_subsys mem_cgroup_subsys = {
4785 .subsys_id = mem_cgroup_subsys_id,
4786 .create = mem_cgroup_create,
4787 .pre_destroy = mem_cgroup_pre_destroy,
4788 .destroy = mem_cgroup_destroy,
4789 .populate = mem_cgroup_populate,
4790 .can_attach = mem_cgroup_can_attach,
4791 .cancel_attach = mem_cgroup_cancel_attach,
4792 .attach = mem_cgroup_move_task,
4797 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
4799 static int __init disable_swap_account(char *s)
4801 really_do_swap_account = 0;
4804 __setup("noswapaccount", disable_swap_account);