1 /* memcontrol.c - Memory Controller
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/res_counter.h>
21 #include <linux/memcontrol.h>
22 #include <linux/cgroup.h>
24 #include <linux/pagemap.h>
25 #include <linux/smp.h>
26 #include <linux/page-flags.h>
27 #include <linux/backing-dev.h>
28 #include <linux/bit_spinlock.h>
29 #include <linux/rcupdate.h>
30 #include <linux/limits.h>
31 #include <linux/mutex.h>
32 #include <linux/slab.h>
33 #include <linux/swap.h>
34 #include <linux/spinlock.h>
36 #include <linux/seq_file.h>
37 #include <linux/vmalloc.h>
38 #include <linux/mm_inline.h>
39 #include <linux/page_cgroup.h>
42 #include <asm/uaccess.h>
44 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
45 #define MEM_CGROUP_RECLAIM_RETRIES 5
47 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
48 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
49 int do_swap_account __read_mostly;
50 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
52 #define do_swap_account (0)
55 static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
58 * Statistics for memory cgroup.
60 enum mem_cgroup_stat_index {
62 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
64 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
65 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
66 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
67 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
68 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
70 MEM_CGROUP_STAT_NSTATS,
73 struct mem_cgroup_stat_cpu {
74 s64 count[MEM_CGROUP_STAT_NSTATS];
75 } ____cacheline_aligned_in_smp;
77 struct mem_cgroup_stat {
78 struct mem_cgroup_stat_cpu cpustat[0];
82 * For accounting under irq disable, no need for increment preempt count.
84 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
85 enum mem_cgroup_stat_index idx, int val)
87 stat->count[idx] += val;
90 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
91 enum mem_cgroup_stat_index idx)
95 for_each_possible_cpu(cpu)
96 ret += stat->cpustat[cpu].count[idx];
100 static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
104 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
105 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
110 * per-zone information in memory controller.
112 struct mem_cgroup_per_zone {
114 * spin_lock to protect the per cgroup LRU
116 struct list_head lists[NR_LRU_LISTS];
117 unsigned long count[NR_LRU_LISTS];
119 struct zone_reclaim_stat reclaim_stat;
121 /* Macro for accessing counter */
122 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
124 struct mem_cgroup_per_node {
125 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
128 struct mem_cgroup_lru_info {
129 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
133 * The memory controller data structure. The memory controller controls both
134 * page cache and RSS per cgroup. We would eventually like to provide
135 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
136 * to help the administrator determine what knobs to tune.
138 * TODO: Add a water mark for the memory controller. Reclaim will begin when
139 * we hit the water mark. May be even add a low water mark, such that
140 * no reclaim occurs from a cgroup at it's low water mark, this is
141 * a feature that will be implemented much later in the future.
144 struct cgroup_subsys_state css;
146 * the counter to account for memory usage
148 struct res_counter res;
150 * the counter to account for mem+swap usage.
152 struct res_counter memsw;
154 * Per cgroup active and inactive list, similar to the
155 * per zone LRU lists.
157 struct mem_cgroup_lru_info info;
160 protect against reclaim related member.
162 spinlock_t reclaim_param_lock;
164 int prev_priority; /* for recording reclaim priority */
167 * While reclaiming in a hiearchy, we cache the last child we
170 int last_scanned_child;
172 * Should the accounting and control be hierarchical, per subtree?
175 unsigned long last_oom_jiffies;
178 unsigned int swappiness;
180 /* set when res.limit == memsw.limit */
181 bool memsw_is_minimum;
184 * statistics. This must be placed at the end of memcg.
186 struct mem_cgroup_stat stat;
190 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
191 MEM_CGROUP_CHARGE_TYPE_MAPPED,
192 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
193 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
194 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
195 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
199 /* only for here (for easy reading.) */
200 #define PCGF_CACHE (1UL << PCG_CACHE)
201 #define PCGF_USED (1UL << PCG_USED)
202 #define PCGF_LOCK (1UL << PCG_LOCK)
203 static const unsigned long
204 pcg_default_flags[NR_CHARGE_TYPE] = {
205 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
206 PCGF_USED | PCGF_LOCK, /* Anon */
207 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
211 /* for encoding cft->private value on file */
214 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
215 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
216 #define MEMFILE_ATTR(val) ((val) & 0xffff)
218 static void mem_cgroup_get(struct mem_cgroup *mem);
219 static void mem_cgroup_put(struct mem_cgroup *mem);
220 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
222 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
223 struct page_cgroup *pc,
226 int val = (charge)? 1 : -1;
227 struct mem_cgroup_stat *stat = &mem->stat;
228 struct mem_cgroup_stat_cpu *cpustat;
231 cpustat = &stat->cpustat[cpu];
232 if (PageCgroupCache(pc))
233 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
235 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
238 __mem_cgroup_stat_add_safe(cpustat,
239 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
241 __mem_cgroup_stat_add_safe(cpustat,
242 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
246 static struct mem_cgroup_per_zone *
247 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
249 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
252 static struct mem_cgroup_per_zone *
253 page_cgroup_zoneinfo(struct page_cgroup *pc)
255 struct mem_cgroup *mem = pc->mem_cgroup;
256 int nid = page_cgroup_nid(pc);
257 int zid = page_cgroup_zid(pc);
262 return mem_cgroup_zoneinfo(mem, nid, zid);
265 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
269 struct mem_cgroup_per_zone *mz;
272 for_each_online_node(nid)
273 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
274 mz = mem_cgroup_zoneinfo(mem, nid, zid);
275 total += MEM_CGROUP_ZSTAT(mz, idx);
280 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
282 return container_of(cgroup_subsys_state(cont,
283 mem_cgroup_subsys_id), struct mem_cgroup,
287 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
290 * mm_update_next_owner() may clear mm->owner to NULL
291 * if it races with swapoff, page migration, etc.
292 * So this can be called with p == NULL.
297 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
298 struct mem_cgroup, css);
301 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
303 struct mem_cgroup *mem = NULL;
308 * Because we have no locks, mm->owner's may be being moved to other
309 * cgroup. We use css_tryget() here even if this looks
310 * pessimistic (rather than adding locks here).
314 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
317 } while (!css_tryget(&mem->css));
323 * Call callback function against all cgroup under hierarchy tree.
325 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
326 int (*func)(struct mem_cgroup *, void *))
328 int found, ret, nextid;
329 struct cgroup_subsys_state *css;
330 struct mem_cgroup *mem;
332 if (!root->use_hierarchy)
333 return (*func)(root, data);
341 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
343 if (css && css_tryget(css))
344 mem = container_of(css, struct mem_cgroup, css);
348 ret = (*func)(mem, data);
352 } while (!ret && css);
358 * Following LRU functions are allowed to be used without PCG_LOCK.
359 * Operations are called by routine of global LRU independently from memcg.
360 * What we have to take care of here is validness of pc->mem_cgroup.
362 * Changes to pc->mem_cgroup happens when
365 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
366 * It is added to LRU before charge.
367 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
368 * When moving account, the page is not on LRU. It's isolated.
371 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
373 struct page_cgroup *pc;
374 struct mem_cgroup *mem;
375 struct mem_cgroup_per_zone *mz;
377 if (mem_cgroup_disabled())
379 pc = lookup_page_cgroup(page);
380 /* can happen while we handle swapcache. */
381 if (list_empty(&pc->lru) || !pc->mem_cgroup)
384 * We don't check PCG_USED bit. It's cleared when the "page" is finally
385 * removed from global LRU.
387 mz = page_cgroup_zoneinfo(pc);
388 mem = pc->mem_cgroup;
389 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
390 list_del_init(&pc->lru);
394 void mem_cgroup_del_lru(struct page *page)
396 mem_cgroup_del_lru_list(page, page_lru(page));
399 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
401 struct mem_cgroup_per_zone *mz;
402 struct page_cgroup *pc;
404 if (mem_cgroup_disabled())
407 pc = lookup_page_cgroup(page);
409 * Used bit is set without atomic ops but after smp_wmb().
410 * For making pc->mem_cgroup visible, insert smp_rmb() here.
413 /* unused page is not rotated. */
414 if (!PageCgroupUsed(pc))
416 mz = page_cgroup_zoneinfo(pc);
417 list_move(&pc->lru, &mz->lists[lru]);
420 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
422 struct page_cgroup *pc;
423 struct mem_cgroup_per_zone *mz;
425 if (mem_cgroup_disabled())
427 pc = lookup_page_cgroup(page);
429 * Used bit is set without atomic ops but after smp_wmb().
430 * For making pc->mem_cgroup visible, insert smp_rmb() here.
433 if (!PageCgroupUsed(pc))
436 mz = page_cgroup_zoneinfo(pc);
437 MEM_CGROUP_ZSTAT(mz, lru) += 1;
438 list_add(&pc->lru, &mz->lists[lru]);
442 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
443 * lru because the page may.be reused after it's fully uncharged (because of
444 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
445 * it again. This function is only used to charge SwapCache. It's done under
446 * lock_page and expected that zone->lru_lock is never held.
448 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
451 struct zone *zone = page_zone(page);
452 struct page_cgroup *pc = lookup_page_cgroup(page);
454 spin_lock_irqsave(&zone->lru_lock, flags);
456 * Forget old LRU when this page_cgroup is *not* used. This Used bit
457 * is guarded by lock_page() because the page is SwapCache.
459 if (!PageCgroupUsed(pc))
460 mem_cgroup_del_lru_list(page, page_lru(page));
461 spin_unlock_irqrestore(&zone->lru_lock, flags);
464 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
467 struct zone *zone = page_zone(page);
468 struct page_cgroup *pc = lookup_page_cgroup(page);
470 spin_lock_irqsave(&zone->lru_lock, flags);
471 /* link when the page is linked to LRU but page_cgroup isn't */
472 if (PageLRU(page) && list_empty(&pc->lru))
473 mem_cgroup_add_lru_list(page, page_lru(page));
474 spin_unlock_irqrestore(&zone->lru_lock, flags);
478 void mem_cgroup_move_lists(struct page *page,
479 enum lru_list from, enum lru_list to)
481 if (mem_cgroup_disabled())
483 mem_cgroup_del_lru_list(page, from);
484 mem_cgroup_add_lru_list(page, to);
487 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
490 struct mem_cgroup *curr = NULL;
494 curr = try_get_mem_cgroup_from_mm(task->mm);
499 if (curr->use_hierarchy)
500 ret = css_is_ancestor(&curr->css, &mem->css);
508 * prev_priority control...this will be used in memory reclaim path.
510 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
514 spin_lock(&mem->reclaim_param_lock);
515 prev_priority = mem->prev_priority;
516 spin_unlock(&mem->reclaim_param_lock);
518 return prev_priority;
521 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
523 spin_lock(&mem->reclaim_param_lock);
524 if (priority < mem->prev_priority)
525 mem->prev_priority = priority;
526 spin_unlock(&mem->reclaim_param_lock);
529 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
531 spin_lock(&mem->reclaim_param_lock);
532 mem->prev_priority = priority;
533 spin_unlock(&mem->reclaim_param_lock);
536 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
538 unsigned long active;
539 unsigned long inactive;
541 unsigned long inactive_ratio;
543 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
544 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
546 gb = (inactive + active) >> (30 - PAGE_SHIFT);
548 inactive_ratio = int_sqrt(10 * gb);
553 present_pages[0] = inactive;
554 present_pages[1] = active;
557 return inactive_ratio;
560 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
562 unsigned long active;
563 unsigned long inactive;
564 unsigned long present_pages[2];
565 unsigned long inactive_ratio;
567 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
569 inactive = present_pages[0];
570 active = present_pages[1];
572 if (inactive * inactive_ratio < active)
578 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
580 unsigned long active;
581 unsigned long inactive;
583 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
584 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
586 return (active > inactive);
589 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
593 int nid = zone->zone_pgdat->node_id;
594 int zid = zone_idx(zone);
595 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
597 return MEM_CGROUP_ZSTAT(mz, lru);
600 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
603 int nid = zone->zone_pgdat->node_id;
604 int zid = zone_idx(zone);
605 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
607 return &mz->reclaim_stat;
610 struct zone_reclaim_stat *
611 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
613 struct page_cgroup *pc;
614 struct mem_cgroup_per_zone *mz;
616 if (mem_cgroup_disabled())
619 pc = lookup_page_cgroup(page);
621 * Used bit is set without atomic ops but after smp_wmb().
622 * For making pc->mem_cgroup visible, insert smp_rmb() here.
625 if (!PageCgroupUsed(pc))
628 mz = page_cgroup_zoneinfo(pc);
632 return &mz->reclaim_stat;
635 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
636 struct list_head *dst,
637 unsigned long *scanned, int order,
638 int mode, struct zone *z,
639 struct mem_cgroup *mem_cont,
640 int active, int file)
642 unsigned long nr_taken = 0;
646 struct list_head *src;
647 struct page_cgroup *pc, *tmp;
648 int nid = z->zone_pgdat->node_id;
649 int zid = zone_idx(z);
650 struct mem_cgroup_per_zone *mz;
651 int lru = LRU_FILE * !!file + !!active;
654 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
655 src = &mz->lists[lru];
658 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
659 if (scan >= nr_to_scan)
663 if (unlikely(!PageCgroupUsed(pc)))
665 if (unlikely(!PageLRU(page)))
669 if (__isolate_lru_page(page, mode, file) == 0) {
670 list_move(&page->lru, dst);
679 #define mem_cgroup_from_res_counter(counter, member) \
680 container_of(counter, struct mem_cgroup, member)
682 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
684 if (do_swap_account) {
685 if (res_counter_check_under_limit(&mem->res) &&
686 res_counter_check_under_limit(&mem->memsw))
689 if (res_counter_check_under_limit(&mem->res))
694 static unsigned int get_swappiness(struct mem_cgroup *memcg)
696 struct cgroup *cgrp = memcg->css.cgroup;
697 unsigned int swappiness;
700 if (cgrp->parent == NULL)
701 return vm_swappiness;
703 spin_lock(&memcg->reclaim_param_lock);
704 swappiness = memcg->swappiness;
705 spin_unlock(&memcg->reclaim_param_lock);
710 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
718 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
719 * @memcg: The memory cgroup that went over limit
720 * @p: Task that is going to be killed
722 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
725 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
727 struct cgroup *task_cgrp;
728 struct cgroup *mem_cgrp;
730 * Need a buffer in BSS, can't rely on allocations. The code relies
731 * on the assumption that OOM is serialized for memory controller.
732 * If this assumption is broken, revisit this code.
734 static char memcg_name[PATH_MAX];
743 mem_cgrp = memcg->css.cgroup;
744 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
746 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
749 * Unfortunately, we are unable to convert to a useful name
750 * But we'll still print out the usage information
757 printk(KERN_INFO "Task in %s killed", memcg_name);
760 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
768 * Continues from above, so we don't need an KERN_ level
770 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
773 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
774 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
775 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
776 res_counter_read_u64(&memcg->res, RES_FAILCNT));
777 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
779 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
780 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
781 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
785 * This function returns the number of memcg under hierarchy tree. Returns
786 * 1(self count) if no children.
788 static int mem_cgroup_count_children(struct mem_cgroup *mem)
791 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
796 * Visit the first child (need not be the first child as per the ordering
797 * of the cgroup list, since we track last_scanned_child) of @mem and use
798 * that to reclaim free pages from.
800 static struct mem_cgroup *
801 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
803 struct mem_cgroup *ret = NULL;
804 struct cgroup_subsys_state *css;
807 if (!root_mem->use_hierarchy) {
808 css_get(&root_mem->css);
814 nextid = root_mem->last_scanned_child + 1;
815 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
817 if (css && css_tryget(css))
818 ret = container_of(css, struct mem_cgroup, css);
821 /* Updates scanning parameter */
822 spin_lock(&root_mem->reclaim_param_lock);
824 /* this means start scan from ID:1 */
825 root_mem->last_scanned_child = 0;
827 root_mem->last_scanned_child = found;
828 spin_unlock(&root_mem->reclaim_param_lock);
835 * Scan the hierarchy if needed to reclaim memory. We remember the last child
836 * we reclaimed from, so that we don't end up penalizing one child extensively
837 * based on its position in the children list.
839 * root_mem is the original ancestor that we've been reclaim from.
841 * We give up and return to the caller when we visit root_mem twice.
842 * (other groups can be removed while we're walking....)
844 * If shrink==true, for avoiding to free too much, this returns immedieately.
846 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
847 gfp_t gfp_mask, bool noswap, bool shrink)
849 struct mem_cgroup *victim;
853 /* If memsw_is_minimum==1, swap-out is of-no-use. */
854 if (root_mem->memsw_is_minimum)
858 victim = mem_cgroup_select_victim(root_mem);
859 if (victim == root_mem)
861 if (!mem_cgroup_local_usage(&victim->stat)) {
862 /* this cgroup's local usage == 0 */
863 css_put(&victim->css);
866 /* we use swappiness of local cgroup */
867 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
868 get_swappiness(victim));
869 css_put(&victim->css);
871 * At shrinking usage, we can't check we should stop here or
872 * reclaim more. It's depends on callers. last_scanned_child
873 * will work enough for keeping fairness under tree.
878 if (mem_cgroup_check_under_limit(root_mem))
884 bool mem_cgroup_oom_called(struct task_struct *task)
887 struct mem_cgroup *mem;
888 struct mm_struct *mm;
894 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
895 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
901 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
903 mem->last_oom_jiffies = jiffies;
907 static void record_last_oom(struct mem_cgroup *mem)
909 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
913 * Currently used to update mapped file statistics, but the routine can be
914 * generalized to update other statistics as well.
916 void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
918 struct mem_cgroup *mem;
919 struct mem_cgroup_stat *stat;
920 struct mem_cgroup_stat_cpu *cpustat;
922 struct page_cgroup *pc;
924 if (!page_is_file_cache(page))
927 pc = lookup_page_cgroup(page);
931 lock_page_cgroup(pc);
932 mem = pc->mem_cgroup;
936 if (!PageCgroupUsed(pc))
940 * Preemption is already disabled, we don't need get_cpu()
942 cpu = smp_processor_id();
944 cpustat = &stat->cpustat[cpu];
946 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
948 unlock_page_cgroup(pc);
952 * Unlike exported interface, "oom" parameter is added. if oom==true,
953 * oom-killer can be invoked.
955 static int __mem_cgroup_try_charge(struct mm_struct *mm,
956 gfp_t gfp_mask, struct mem_cgroup **memcg,
959 struct mem_cgroup *mem, *mem_over_limit;
960 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
961 struct res_counter *fail_res;
963 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
964 /* Don't account this! */
970 * We always charge the cgroup the mm_struct belongs to.
971 * The mm_struct's mem_cgroup changes on task migration if the
972 * thread group leader migrates. It's possible that mm is not
973 * set, if so charge the init_mm (happens for pagecache usage).
977 mem = try_get_mem_cgroup_from_mm(mm);
985 VM_BUG_ON(css_is_removed(&mem->css));
991 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
993 if (!do_swap_account)
995 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
999 /* mem+swap counter fails */
1000 res_counter_uncharge(&mem->res, PAGE_SIZE);
1002 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1005 /* mem counter fails */
1006 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1009 if (!(gfp_mask & __GFP_WAIT))
1012 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
1018 * try_to_free_mem_cgroup_pages() might not give us a full
1019 * picture of reclaim. Some pages are reclaimed and might be
1020 * moved to swap cache or just unmapped from the cgroup.
1021 * Check the limit again to see if the reclaim reduced the
1022 * current usage of the cgroup before giving up
1025 if (mem_cgroup_check_under_limit(mem_over_limit))
1028 if (!nr_retries--) {
1030 mutex_lock(&memcg_tasklist);
1031 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1032 mutex_unlock(&memcg_tasklist);
1033 record_last_oom(mem_over_limit);
1046 * A helper function to get mem_cgroup from ID. must be called under
1047 * rcu_read_lock(). The caller must check css_is_removed() or some if
1048 * it's concern. (dropping refcnt from swap can be called against removed
1051 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1053 struct cgroup_subsys_state *css;
1055 /* ID 0 is unused ID */
1058 css = css_lookup(&mem_cgroup_subsys, id);
1061 return container_of(css, struct mem_cgroup, css);
1064 static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1066 struct mem_cgroup *mem;
1067 struct page_cgroup *pc;
1071 VM_BUG_ON(!PageLocked(page));
1073 if (!PageSwapCache(page))
1076 pc = lookup_page_cgroup(page);
1077 lock_page_cgroup(pc);
1078 if (PageCgroupUsed(pc)) {
1079 mem = pc->mem_cgroup;
1080 if (mem && !css_tryget(&mem->css))
1083 ent.val = page_private(page);
1084 id = lookup_swap_cgroup(ent);
1086 mem = mem_cgroup_lookup(id);
1087 if (mem && !css_tryget(&mem->css))
1091 unlock_page_cgroup(pc);
1096 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1097 * USED state. If already USED, uncharge and return.
1100 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1101 struct page_cgroup *pc,
1102 enum charge_type ctype)
1104 /* try_charge() can return NULL to *memcg, taking care of it. */
1108 lock_page_cgroup(pc);
1109 if (unlikely(PageCgroupUsed(pc))) {
1110 unlock_page_cgroup(pc);
1111 res_counter_uncharge(&mem->res, PAGE_SIZE);
1112 if (do_swap_account)
1113 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1117 pc->mem_cgroup = mem;
1119 pc->flags = pcg_default_flags[ctype];
1121 mem_cgroup_charge_statistics(mem, pc, true);
1123 unlock_page_cgroup(pc);
1127 * mem_cgroup_move_account - move account of the page
1128 * @pc: page_cgroup of the page.
1129 * @from: mem_cgroup which the page is moved from.
1130 * @to: mem_cgroup which the page is moved to. @from != @to.
1132 * The caller must confirm following.
1133 * - page is not on LRU (isolate_page() is useful.)
1135 * returns 0 at success,
1136 * returns -EBUSY when lock is busy or "pc" is unstable.
1138 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1139 * new cgroup. It should be done by a caller.
1142 static int mem_cgroup_move_account(struct page_cgroup *pc,
1143 struct mem_cgroup *from, struct mem_cgroup *to)
1145 struct mem_cgroup_per_zone *from_mz, *to_mz;
1150 struct mem_cgroup_stat *stat;
1151 struct mem_cgroup_stat_cpu *cpustat;
1153 VM_BUG_ON(from == to);
1154 VM_BUG_ON(PageLRU(pc->page));
1156 nid = page_cgroup_nid(pc);
1157 zid = page_cgroup_zid(pc);
1158 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1159 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1161 if (!trylock_page_cgroup(pc))
1164 if (!PageCgroupUsed(pc))
1167 if (pc->mem_cgroup != from)
1170 res_counter_uncharge(&from->res, PAGE_SIZE);
1171 mem_cgroup_charge_statistics(from, pc, false);
1174 if (page_is_file_cache(page) && page_mapped(page)) {
1175 cpu = smp_processor_id();
1176 /* Update mapped_file data for mem_cgroup "from" */
1178 cpustat = &stat->cpustat[cpu];
1179 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1182 /* Update mapped_file data for mem_cgroup "to" */
1184 cpustat = &stat->cpustat[cpu];
1185 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1189 if (do_swap_account)
1190 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1191 css_put(&from->css);
1194 pc->mem_cgroup = to;
1195 mem_cgroup_charge_statistics(to, pc, true);
1198 unlock_page_cgroup(pc);
1203 * move charges to its parent.
1206 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1207 struct mem_cgroup *child,
1210 struct page *page = pc->page;
1211 struct cgroup *cg = child->css.cgroup;
1212 struct cgroup *pcg = cg->parent;
1213 struct mem_cgroup *parent;
1221 parent = mem_cgroup_from_cont(pcg);
1224 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
1228 if (!get_page_unless_zero(page)) {
1233 ret = isolate_lru_page(page);
1238 ret = mem_cgroup_move_account(pc, child, parent);
1240 putback_lru_page(page);
1243 /* drop extra refcnt by try_charge() */
1244 css_put(&parent->css);
1251 /* drop extra refcnt by try_charge() */
1252 css_put(&parent->css);
1253 /* uncharge if move fails */
1254 res_counter_uncharge(&parent->res, PAGE_SIZE);
1255 if (do_swap_account)
1256 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
1261 * Charge the memory controller for page usage.
1263 * 0 if the charge was successful
1264 * < 0 if the cgroup is over its limit
1266 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1267 gfp_t gfp_mask, enum charge_type ctype,
1268 struct mem_cgroup *memcg)
1270 struct mem_cgroup *mem;
1271 struct page_cgroup *pc;
1274 pc = lookup_page_cgroup(page);
1275 /* can happen at boot */
1281 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
1285 __mem_cgroup_commit_charge(mem, pc, ctype);
1289 int mem_cgroup_newpage_charge(struct page *page,
1290 struct mm_struct *mm, gfp_t gfp_mask)
1292 if (mem_cgroup_disabled())
1294 if (PageCompound(page))
1297 * If already mapped, we don't have to account.
1298 * If page cache, page->mapping has address_space.
1299 * But page->mapping may have out-of-use anon_vma pointer,
1300 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1303 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1307 return mem_cgroup_charge_common(page, mm, gfp_mask,
1308 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1312 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1313 enum charge_type ctype);
1315 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1318 struct mem_cgroup *mem = NULL;
1321 if (mem_cgroup_disabled())
1323 if (PageCompound(page))
1326 * Corner case handling. This is called from add_to_page_cache()
1327 * in usual. But some FS (shmem) precharges this page before calling it
1328 * and call add_to_page_cache() with GFP_NOWAIT.
1330 * For GFP_NOWAIT case, the page may be pre-charged before calling
1331 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1332 * charge twice. (It works but has to pay a bit larger cost.)
1333 * And when the page is SwapCache, it should take swap information
1334 * into account. This is under lock_page() now.
1336 if (!(gfp_mask & __GFP_WAIT)) {
1337 struct page_cgroup *pc;
1340 pc = lookup_page_cgroup(page);
1343 lock_page_cgroup(pc);
1344 if (PageCgroupUsed(pc)) {
1345 unlock_page_cgroup(pc);
1348 unlock_page_cgroup(pc);
1351 if (unlikely(!mm && !mem))
1354 if (page_is_file_cache(page))
1355 return mem_cgroup_charge_common(page, mm, gfp_mask,
1356 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1359 if (PageSwapCache(page)) {
1360 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1362 __mem_cgroup_commit_charge_swapin(page, mem,
1363 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1365 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1366 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1372 * While swap-in, try_charge -> commit or cancel, the page is locked.
1373 * And when try_charge() successfully returns, one refcnt to memcg without
1374 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1375 * "commit()" or removed by "cancel()"
1377 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1379 gfp_t mask, struct mem_cgroup **ptr)
1381 struct mem_cgroup *mem;
1384 if (mem_cgroup_disabled())
1387 if (!do_swap_account)
1390 * A racing thread's fault, or swapoff, may have already updated
1391 * the pte, and even removed page from swap cache: return success
1392 * to go on to do_swap_page()'s pte_same() test, which should fail.
1394 if (!PageSwapCache(page))
1396 mem = try_get_mem_cgroup_from_swapcache(page);
1400 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1401 /* drop extra refcnt from tryget */
1407 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1411 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1412 enum charge_type ctype)
1414 struct page_cgroup *pc;
1416 if (mem_cgroup_disabled())
1420 pc = lookup_page_cgroup(page);
1421 mem_cgroup_lru_del_before_commit_swapcache(page);
1422 __mem_cgroup_commit_charge(ptr, pc, ctype);
1423 mem_cgroup_lru_add_after_commit_swapcache(page);
1425 * Now swap is on-memory. This means this page may be
1426 * counted both as mem and swap....double count.
1427 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1428 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1429 * may call delete_from_swap_cache() before reach here.
1431 if (do_swap_account && PageSwapCache(page)) {
1432 swp_entry_t ent = {.val = page_private(page)};
1434 struct mem_cgroup *memcg;
1436 id = swap_cgroup_record(ent, 0);
1438 memcg = mem_cgroup_lookup(id);
1441 * This recorded memcg can be obsolete one. So, avoid
1442 * calling css_tryget
1444 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1445 mem_cgroup_put(memcg);
1449 /* add this page(page_cgroup) to the LRU we want. */
1453 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1455 __mem_cgroup_commit_charge_swapin(page, ptr,
1456 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1459 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1461 if (mem_cgroup_disabled())
1465 res_counter_uncharge(&mem->res, PAGE_SIZE);
1466 if (do_swap_account)
1467 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1473 * uncharge if !page_mapped(page)
1475 static struct mem_cgroup *
1476 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1478 struct page_cgroup *pc;
1479 struct mem_cgroup *mem = NULL;
1480 struct mem_cgroup_per_zone *mz;
1482 if (mem_cgroup_disabled())
1485 if (PageSwapCache(page))
1489 * Check if our page_cgroup is valid
1491 pc = lookup_page_cgroup(page);
1492 if (unlikely(!pc || !PageCgroupUsed(pc)))
1495 lock_page_cgroup(pc);
1497 mem = pc->mem_cgroup;
1499 if (!PageCgroupUsed(pc))
1503 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1504 case MEM_CGROUP_CHARGE_TYPE_DROP:
1505 if (page_mapped(page))
1508 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1509 if (!PageAnon(page)) { /* Shared memory */
1510 if (page->mapping && !page_is_file_cache(page))
1512 } else if (page_mapped(page)) /* Anon */
1519 res_counter_uncharge(&mem->res, PAGE_SIZE);
1520 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1521 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1522 mem_cgroup_charge_statistics(mem, pc, false);
1524 ClearPageCgroupUsed(pc);
1526 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1527 * freed from LRU. This is safe because uncharged page is expected not
1528 * to be reused (freed soon). Exception is SwapCache, it's handled by
1529 * special functions.
1532 mz = page_cgroup_zoneinfo(pc);
1533 unlock_page_cgroup(pc);
1535 /* at swapout, this memcg will be accessed to record to swap */
1536 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1542 unlock_page_cgroup(pc);
1546 void mem_cgroup_uncharge_page(struct page *page)
1549 if (page_mapped(page))
1551 if (page->mapping && !PageAnon(page))
1553 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1556 void mem_cgroup_uncharge_cache_page(struct page *page)
1558 VM_BUG_ON(page_mapped(page));
1559 VM_BUG_ON(page->mapping);
1560 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1565 * called after __delete_from_swap_cache() and drop "page" account.
1566 * memcg information is recorded to swap_cgroup of "ent"
1569 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
1571 struct mem_cgroup *memcg;
1572 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1574 if (!swapout) /* this was a swap cache but the swap is unused ! */
1575 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1577 memcg = __mem_cgroup_uncharge_common(page, ctype);
1579 /* record memcg information */
1580 if (do_swap_account && swapout && memcg) {
1581 swap_cgroup_record(ent, css_id(&memcg->css));
1582 mem_cgroup_get(memcg);
1584 if (swapout && memcg)
1585 css_put(&memcg->css);
1589 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1591 * called from swap_entry_free(). remove record in swap_cgroup and
1592 * uncharge "memsw" account.
1594 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1596 struct mem_cgroup *memcg;
1599 if (!do_swap_account)
1602 id = swap_cgroup_record(ent, 0);
1604 memcg = mem_cgroup_lookup(id);
1607 * We uncharge this because swap is freed.
1608 * This memcg can be obsolete one. We avoid calling css_tryget
1610 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1611 mem_cgroup_put(memcg);
1618 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1621 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1623 struct page_cgroup *pc;
1624 struct mem_cgroup *mem = NULL;
1627 if (mem_cgroup_disabled())
1630 pc = lookup_page_cgroup(page);
1631 lock_page_cgroup(pc);
1632 if (PageCgroupUsed(pc)) {
1633 mem = pc->mem_cgroup;
1636 unlock_page_cgroup(pc);
1639 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
1646 /* remove redundant charge if migration failed*/
1647 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1648 struct page *oldpage, struct page *newpage)
1650 struct page *target, *unused;
1651 struct page_cgroup *pc;
1652 enum charge_type ctype;
1657 /* at migration success, oldpage->mapping is NULL. */
1658 if (oldpage->mapping) {
1666 if (PageAnon(target))
1667 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1668 else if (page_is_file_cache(target))
1669 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1671 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1673 /* unused page is not on radix-tree now. */
1675 __mem_cgroup_uncharge_common(unused, ctype);
1677 pc = lookup_page_cgroup(target);
1679 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1680 * So, double-counting is effectively avoided.
1682 __mem_cgroup_commit_charge(mem, pc, ctype);
1685 * Both of oldpage and newpage are still under lock_page().
1686 * Then, we don't have to care about race in radix-tree.
1687 * But we have to be careful that this page is unmapped or not.
1689 * There is a case for !page_mapped(). At the start of
1690 * migration, oldpage was mapped. But now, it's zapped.
1691 * But we know *target* page is not freed/reused under us.
1692 * mem_cgroup_uncharge_page() does all necessary checks.
1694 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1695 mem_cgroup_uncharge_page(target);
1699 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1700 * Calling hierarchical_reclaim is not enough because we should update
1701 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1702 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1703 * not from the memcg which this page would be charged to.
1704 * try_charge_swapin does all of these works properly.
1706 int mem_cgroup_shmem_charge_fallback(struct page *page,
1707 struct mm_struct *mm,
1710 struct mem_cgroup *mem = NULL;
1713 if (mem_cgroup_disabled())
1716 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1718 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
1723 static DEFINE_MUTEX(set_limit_mutex);
1725 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1726 unsigned long long val)
1732 int children = mem_cgroup_count_children(memcg);
1733 u64 curusage, oldusage;
1736 * For keeping hierarchical_reclaim simple, how long we should retry
1737 * is depends on callers. We set our retry-count to be function
1738 * of # of children which we should visit in this loop.
1740 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1742 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1744 while (retry_count) {
1745 if (signal_pending(current)) {
1750 * Rather than hide all in some function, I do this in
1751 * open coded manner. You see what this really does.
1752 * We have to guarantee mem->res.limit < mem->memsw.limit.
1754 mutex_lock(&set_limit_mutex);
1755 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1756 if (memswlimit < val) {
1758 mutex_unlock(&set_limit_mutex);
1761 ret = res_counter_set_limit(&memcg->res, val);
1763 if (memswlimit == val)
1764 memcg->memsw_is_minimum = true;
1766 memcg->memsw_is_minimum = false;
1768 mutex_unlock(&set_limit_mutex);
1773 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
1775 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1776 /* Usage is reduced ? */
1777 if (curusage >= oldusage)
1780 oldusage = curusage;
1786 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1787 unsigned long long val)
1790 u64 memlimit, oldusage, curusage;
1791 int children = mem_cgroup_count_children(memcg);
1794 /* see mem_cgroup_resize_res_limit */
1795 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1796 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1797 while (retry_count) {
1798 if (signal_pending(current)) {
1803 * Rather than hide all in some function, I do this in
1804 * open coded manner. You see what this really does.
1805 * We have to guarantee mem->res.limit < mem->memsw.limit.
1807 mutex_lock(&set_limit_mutex);
1808 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1809 if (memlimit > val) {
1811 mutex_unlock(&set_limit_mutex);
1814 ret = res_counter_set_limit(&memcg->memsw, val);
1816 if (memlimit == val)
1817 memcg->memsw_is_minimum = true;
1819 memcg->memsw_is_minimum = false;
1821 mutex_unlock(&set_limit_mutex);
1826 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
1827 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1828 /* Usage is reduced ? */
1829 if (curusage >= oldusage)
1832 oldusage = curusage;
1838 * This routine traverse page_cgroup in given list and drop them all.
1839 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1841 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1842 int node, int zid, enum lru_list lru)
1845 struct mem_cgroup_per_zone *mz;
1846 struct page_cgroup *pc, *busy;
1847 unsigned long flags, loop;
1848 struct list_head *list;
1851 zone = &NODE_DATA(node)->node_zones[zid];
1852 mz = mem_cgroup_zoneinfo(mem, node, zid);
1853 list = &mz->lists[lru];
1855 loop = MEM_CGROUP_ZSTAT(mz, lru);
1856 /* give some margin against EBUSY etc...*/
1861 spin_lock_irqsave(&zone->lru_lock, flags);
1862 if (list_empty(list)) {
1863 spin_unlock_irqrestore(&zone->lru_lock, flags);
1866 pc = list_entry(list->prev, struct page_cgroup, lru);
1868 list_move(&pc->lru, list);
1870 spin_unlock_irqrestore(&zone->lru_lock, flags);
1873 spin_unlock_irqrestore(&zone->lru_lock, flags);
1875 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
1879 if (ret == -EBUSY || ret == -EINVAL) {
1880 /* found lock contention or "pc" is obsolete. */
1887 if (!ret && !list_empty(list))
1893 * make mem_cgroup's charge to be 0 if there is no task.
1894 * This enables deleting this mem_cgroup.
1896 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1899 int node, zid, shrink;
1900 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1901 struct cgroup *cgrp = mem->css.cgroup;
1906 /* should free all ? */
1910 while (mem->res.usage > 0) {
1912 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1915 if (signal_pending(current))
1917 /* This is for making all *used* pages to be on LRU. */
1918 lru_add_drain_all();
1920 for_each_node_state(node, N_HIGH_MEMORY) {
1921 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1924 ret = mem_cgroup_force_empty_list(mem,
1933 /* it seems parent cgroup doesn't have enough mem */
1944 /* returns EBUSY if there is a task or if we come here twice. */
1945 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
1949 /* we call try-to-free pages for make this cgroup empty */
1950 lru_add_drain_all();
1951 /* try to free all pages in this cgroup */
1953 while (nr_retries && mem->res.usage > 0) {
1956 if (signal_pending(current)) {
1960 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1961 false, get_swappiness(mem));
1964 /* maybe some writeback is necessary */
1965 congestion_wait(WRITE, HZ/10);
1970 /* try move_account...there may be some *locked* pages. */
1977 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1979 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1983 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1985 return mem_cgroup_from_cont(cont)->use_hierarchy;
1988 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1992 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1993 struct cgroup *parent = cont->parent;
1994 struct mem_cgroup *parent_mem = NULL;
1997 parent_mem = mem_cgroup_from_cont(parent);
2001 * If parent's use_hiearchy is set, we can't make any modifications
2002 * in the child subtrees. If it is unset, then the change can
2003 * occur, provided the current cgroup has no children.
2005 * For the root cgroup, parent_mem is NULL, we allow value to be
2006 * set if there are no children.
2008 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2009 (val == 1 || val == 0)) {
2010 if (list_empty(&cont->children))
2011 mem->use_hierarchy = val;
2021 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2023 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2027 type = MEMFILE_TYPE(cft->private);
2028 name = MEMFILE_ATTR(cft->private);
2031 val = res_counter_read_u64(&mem->res, name);
2034 val = res_counter_read_u64(&mem->memsw, name);
2043 * The user of this function is...
2046 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2049 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2051 unsigned long long val;
2054 type = MEMFILE_TYPE(cft->private);
2055 name = MEMFILE_ATTR(cft->private);
2058 /* This function does all necessary parse...reuse it */
2059 ret = res_counter_memparse_write_strategy(buffer, &val);
2063 ret = mem_cgroup_resize_limit(memcg, val);
2065 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2068 ret = -EINVAL; /* should be BUG() ? */
2074 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2075 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2077 struct cgroup *cgroup;
2078 unsigned long long min_limit, min_memsw_limit, tmp;
2080 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2081 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2082 cgroup = memcg->css.cgroup;
2083 if (!memcg->use_hierarchy)
2086 while (cgroup->parent) {
2087 cgroup = cgroup->parent;
2088 memcg = mem_cgroup_from_cont(cgroup);
2089 if (!memcg->use_hierarchy)
2091 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2092 min_limit = min(min_limit, tmp);
2093 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2094 min_memsw_limit = min(min_memsw_limit, tmp);
2097 *mem_limit = min_limit;
2098 *memsw_limit = min_memsw_limit;
2102 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2104 struct mem_cgroup *mem;
2107 mem = mem_cgroup_from_cont(cont);
2108 type = MEMFILE_TYPE(event);
2109 name = MEMFILE_ATTR(event);
2113 res_counter_reset_max(&mem->res);
2115 res_counter_reset_max(&mem->memsw);
2119 res_counter_reset_failcnt(&mem->res);
2121 res_counter_reset_failcnt(&mem->memsw);
2128 /* For read statistics */
2143 struct mcs_total_stat {
2144 s64 stat[NR_MCS_STAT];
2150 } memcg_stat_strings[NR_MCS_STAT] = {
2151 {"cache", "total_cache"},
2152 {"rss", "total_rss"},
2153 {"mapped_file", "total_mapped_file"},
2154 {"pgpgin", "total_pgpgin"},
2155 {"pgpgout", "total_pgpgout"},
2156 {"inactive_anon", "total_inactive_anon"},
2157 {"active_anon", "total_active_anon"},
2158 {"inactive_file", "total_inactive_file"},
2159 {"active_file", "total_active_file"},
2160 {"unevictable", "total_unevictable"}
2164 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2166 struct mcs_total_stat *s = data;
2170 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2171 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2172 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2173 s->stat[MCS_RSS] += val * PAGE_SIZE;
2174 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2175 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
2176 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2177 s->stat[MCS_PGPGIN] += val;
2178 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2179 s->stat[MCS_PGPGOUT] += val;
2182 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2183 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2184 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2185 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2186 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2187 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2188 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2189 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2190 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2191 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2196 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2198 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2201 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2202 struct cgroup_map_cb *cb)
2204 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2205 struct mcs_total_stat mystat;
2208 memset(&mystat, 0, sizeof(mystat));
2209 mem_cgroup_get_local_stat(mem_cont, &mystat);
2211 for (i = 0; i < NR_MCS_STAT; i++)
2212 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2214 /* Hierarchical information */
2216 unsigned long long limit, memsw_limit;
2217 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2218 cb->fill(cb, "hierarchical_memory_limit", limit);
2219 if (do_swap_account)
2220 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2223 memset(&mystat, 0, sizeof(mystat));
2224 mem_cgroup_get_total_stat(mem_cont, &mystat);
2225 for (i = 0; i < NR_MCS_STAT; i++)
2226 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2229 #ifdef CONFIG_DEBUG_VM
2230 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2234 struct mem_cgroup_per_zone *mz;
2235 unsigned long recent_rotated[2] = {0, 0};
2236 unsigned long recent_scanned[2] = {0, 0};
2238 for_each_online_node(nid)
2239 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2240 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2242 recent_rotated[0] +=
2243 mz->reclaim_stat.recent_rotated[0];
2244 recent_rotated[1] +=
2245 mz->reclaim_stat.recent_rotated[1];
2246 recent_scanned[0] +=
2247 mz->reclaim_stat.recent_scanned[0];
2248 recent_scanned[1] +=
2249 mz->reclaim_stat.recent_scanned[1];
2251 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2252 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2253 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2254 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2261 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2263 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2265 return get_swappiness(memcg);
2268 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2271 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2272 struct mem_cgroup *parent;
2277 if (cgrp->parent == NULL)
2280 parent = mem_cgroup_from_cont(cgrp->parent);
2284 /* If under hierarchy, only empty-root can set this value */
2285 if ((parent->use_hierarchy) ||
2286 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2291 spin_lock(&memcg->reclaim_param_lock);
2292 memcg->swappiness = val;
2293 spin_unlock(&memcg->reclaim_param_lock);
2301 static struct cftype mem_cgroup_files[] = {
2303 .name = "usage_in_bytes",
2304 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2305 .read_u64 = mem_cgroup_read,
2308 .name = "max_usage_in_bytes",
2309 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2310 .trigger = mem_cgroup_reset,
2311 .read_u64 = mem_cgroup_read,
2314 .name = "limit_in_bytes",
2315 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2316 .write_string = mem_cgroup_write,
2317 .read_u64 = mem_cgroup_read,
2321 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2322 .trigger = mem_cgroup_reset,
2323 .read_u64 = mem_cgroup_read,
2327 .read_map = mem_control_stat_show,
2330 .name = "force_empty",
2331 .trigger = mem_cgroup_force_empty_write,
2334 .name = "use_hierarchy",
2335 .write_u64 = mem_cgroup_hierarchy_write,
2336 .read_u64 = mem_cgroup_hierarchy_read,
2339 .name = "swappiness",
2340 .read_u64 = mem_cgroup_swappiness_read,
2341 .write_u64 = mem_cgroup_swappiness_write,
2345 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2346 static struct cftype memsw_cgroup_files[] = {
2348 .name = "memsw.usage_in_bytes",
2349 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2350 .read_u64 = mem_cgroup_read,
2353 .name = "memsw.max_usage_in_bytes",
2354 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2355 .trigger = mem_cgroup_reset,
2356 .read_u64 = mem_cgroup_read,
2359 .name = "memsw.limit_in_bytes",
2360 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2361 .write_string = mem_cgroup_write,
2362 .read_u64 = mem_cgroup_read,
2365 .name = "memsw.failcnt",
2366 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2367 .trigger = mem_cgroup_reset,
2368 .read_u64 = mem_cgroup_read,
2372 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2374 if (!do_swap_account)
2376 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2377 ARRAY_SIZE(memsw_cgroup_files));
2380 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2386 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2388 struct mem_cgroup_per_node *pn;
2389 struct mem_cgroup_per_zone *mz;
2391 int zone, tmp = node;
2393 * This routine is called against possible nodes.
2394 * But it's BUG to call kmalloc() against offline node.
2396 * TODO: this routine can waste much memory for nodes which will
2397 * never be onlined. It's better to use memory hotplug callback
2400 if (!node_state(node, N_NORMAL_MEMORY))
2402 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
2406 mem->info.nodeinfo[node] = pn;
2407 memset(pn, 0, sizeof(*pn));
2409 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2410 mz = &pn->zoneinfo[zone];
2412 INIT_LIST_HEAD(&mz->lists[l]);
2417 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2419 kfree(mem->info.nodeinfo[node]);
2422 static int mem_cgroup_size(void)
2424 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2425 return sizeof(struct mem_cgroup) + cpustat_size;
2428 static struct mem_cgroup *mem_cgroup_alloc(void)
2430 struct mem_cgroup *mem;
2431 int size = mem_cgroup_size();
2433 if (size < PAGE_SIZE)
2434 mem = kmalloc(size, GFP_KERNEL);
2436 mem = vmalloc(size);
2439 memset(mem, 0, size);
2444 * At destroying mem_cgroup, references from swap_cgroup can remain.
2445 * (scanning all at force_empty is too costly...)
2447 * Instead of clearing all references at force_empty, we remember
2448 * the number of reference from swap_cgroup and free mem_cgroup when
2449 * it goes down to 0.
2451 * Removal of cgroup itself succeeds regardless of refs from swap.
2454 static void __mem_cgroup_free(struct mem_cgroup *mem)
2458 free_css_id(&mem_cgroup_subsys, &mem->css);
2460 for_each_node_state(node, N_POSSIBLE)
2461 free_mem_cgroup_per_zone_info(mem, node);
2463 if (mem_cgroup_size() < PAGE_SIZE)
2469 static void mem_cgroup_get(struct mem_cgroup *mem)
2471 atomic_inc(&mem->refcnt);
2474 static void mem_cgroup_put(struct mem_cgroup *mem)
2476 if (atomic_dec_and_test(&mem->refcnt)) {
2477 struct mem_cgroup *parent = parent_mem_cgroup(mem);
2478 __mem_cgroup_free(mem);
2480 mem_cgroup_put(parent);
2485 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2487 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2489 if (!mem->res.parent)
2491 return mem_cgroup_from_res_counter(mem->res.parent, res);
2494 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2495 static void __init enable_swap_cgroup(void)
2497 if (!mem_cgroup_disabled() && really_do_swap_account)
2498 do_swap_account = 1;
2501 static void __init enable_swap_cgroup(void)
2506 static struct cgroup_subsys_state * __ref
2507 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2509 struct mem_cgroup *mem, *parent;
2510 long error = -ENOMEM;
2513 mem = mem_cgroup_alloc();
2515 return ERR_PTR(error);
2517 for_each_node_state(node, N_POSSIBLE)
2518 if (alloc_mem_cgroup_per_zone_info(mem, node))
2521 if (cont->parent == NULL) {
2522 enable_swap_cgroup();
2525 parent = mem_cgroup_from_cont(cont->parent);
2526 mem->use_hierarchy = parent->use_hierarchy;
2529 if (parent && parent->use_hierarchy) {
2530 res_counter_init(&mem->res, &parent->res);
2531 res_counter_init(&mem->memsw, &parent->memsw);
2533 * We increment refcnt of the parent to ensure that we can
2534 * safely access it on res_counter_charge/uncharge.
2535 * This refcnt will be decremented when freeing this
2536 * mem_cgroup(see mem_cgroup_put).
2538 mem_cgroup_get(parent);
2540 res_counter_init(&mem->res, NULL);
2541 res_counter_init(&mem->memsw, NULL);
2543 mem->last_scanned_child = 0;
2544 spin_lock_init(&mem->reclaim_param_lock);
2547 mem->swappiness = get_swappiness(parent);
2548 atomic_set(&mem->refcnt, 1);
2551 __mem_cgroup_free(mem);
2552 return ERR_PTR(error);
2555 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
2556 struct cgroup *cont)
2558 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2560 return mem_cgroup_force_empty(mem, false);
2563 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2564 struct cgroup *cont)
2566 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2568 mem_cgroup_put(mem);
2571 static int mem_cgroup_populate(struct cgroup_subsys *ss,
2572 struct cgroup *cont)
2576 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2577 ARRAY_SIZE(mem_cgroup_files));
2580 ret = register_memsw_files(cont, ss);
2584 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2585 struct cgroup *cont,
2586 struct cgroup *old_cont,
2587 struct task_struct *p)
2589 mutex_lock(&memcg_tasklist);
2591 * FIXME: It's better to move charges of this process from old
2592 * memcg to new memcg. But it's just on TODO-List now.
2594 mutex_unlock(&memcg_tasklist);
2597 struct cgroup_subsys mem_cgroup_subsys = {
2599 .subsys_id = mem_cgroup_subsys_id,
2600 .create = mem_cgroup_create,
2601 .pre_destroy = mem_cgroup_pre_destroy,
2602 .destroy = mem_cgroup_destroy,
2603 .populate = mem_cgroup_populate,
2604 .attach = mem_cgroup_move_task,
2609 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2611 static int __init disable_swap_account(char *s)
2613 really_do_swap_account = 0;
2616 __setup("noswapaccount", disable_swap_account);