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;
181 * statistics. This must be placed at the end of memcg.
183 struct mem_cgroup_stat stat;
187 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
188 MEM_CGROUP_CHARGE_TYPE_MAPPED,
189 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
190 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
191 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
192 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
196 /* only for here (for easy reading.) */
197 #define PCGF_CACHE (1UL << PCG_CACHE)
198 #define PCGF_USED (1UL << PCG_USED)
199 #define PCGF_LOCK (1UL << PCG_LOCK)
200 static const unsigned long
201 pcg_default_flags[NR_CHARGE_TYPE] = {
202 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
203 PCGF_USED | PCGF_LOCK, /* Anon */
204 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
208 /* for encoding cft->private value on file */
211 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
212 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
213 #define MEMFILE_ATTR(val) ((val) & 0xffff)
215 static void mem_cgroup_get(struct mem_cgroup *mem);
216 static void mem_cgroup_put(struct mem_cgroup *mem);
217 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
219 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
220 struct page_cgroup *pc,
223 int val = (charge)? 1 : -1;
224 struct mem_cgroup_stat *stat = &mem->stat;
225 struct mem_cgroup_stat_cpu *cpustat;
228 cpustat = &stat->cpustat[cpu];
229 if (PageCgroupCache(pc))
230 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
232 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
235 __mem_cgroup_stat_add_safe(cpustat,
236 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
238 __mem_cgroup_stat_add_safe(cpustat,
239 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
243 static struct mem_cgroup_per_zone *
244 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
246 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
249 static struct mem_cgroup_per_zone *
250 page_cgroup_zoneinfo(struct page_cgroup *pc)
252 struct mem_cgroup *mem = pc->mem_cgroup;
253 int nid = page_cgroup_nid(pc);
254 int zid = page_cgroup_zid(pc);
259 return mem_cgroup_zoneinfo(mem, nid, zid);
262 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
266 struct mem_cgroup_per_zone *mz;
269 for_each_online_node(nid)
270 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
271 mz = mem_cgroup_zoneinfo(mem, nid, zid);
272 total += MEM_CGROUP_ZSTAT(mz, idx);
277 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
279 return container_of(cgroup_subsys_state(cont,
280 mem_cgroup_subsys_id), struct mem_cgroup,
284 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
287 * mm_update_next_owner() may clear mm->owner to NULL
288 * if it races with swapoff, page migration, etc.
289 * So this can be called with p == NULL.
294 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
295 struct mem_cgroup, css);
298 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
300 struct mem_cgroup *mem = NULL;
305 * Because we have no locks, mm->owner's may be being moved to other
306 * cgroup. We use css_tryget() here even if this looks
307 * pessimistic (rather than adding locks here).
311 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
314 } while (!css_tryget(&mem->css));
320 * Call callback function against all cgroup under hierarchy tree.
322 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
323 int (*func)(struct mem_cgroup *, void *))
325 int found, ret, nextid;
326 struct cgroup_subsys_state *css;
327 struct mem_cgroup *mem;
329 if (!root->use_hierarchy)
330 return (*func)(root, data);
338 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
340 if (css && css_tryget(css))
341 mem = container_of(css, struct mem_cgroup, css);
345 ret = (*func)(mem, data);
349 } while (!ret && css);
355 * Following LRU functions are allowed to be used without PCG_LOCK.
356 * Operations are called by routine of global LRU independently from memcg.
357 * What we have to take care of here is validness of pc->mem_cgroup.
359 * Changes to pc->mem_cgroup happens when
362 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
363 * It is added to LRU before charge.
364 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
365 * When moving account, the page is not on LRU. It's isolated.
368 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
370 struct page_cgroup *pc;
371 struct mem_cgroup *mem;
372 struct mem_cgroup_per_zone *mz;
374 if (mem_cgroup_disabled())
376 pc = lookup_page_cgroup(page);
377 /* can happen while we handle swapcache. */
378 if (list_empty(&pc->lru) || !pc->mem_cgroup)
381 * We don't check PCG_USED bit. It's cleared when the "page" is finally
382 * removed from global LRU.
384 mz = page_cgroup_zoneinfo(pc);
385 mem = pc->mem_cgroup;
386 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
387 list_del_init(&pc->lru);
391 void mem_cgroup_del_lru(struct page *page)
393 mem_cgroup_del_lru_list(page, page_lru(page));
396 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
398 struct mem_cgroup_per_zone *mz;
399 struct page_cgroup *pc;
401 if (mem_cgroup_disabled())
404 pc = lookup_page_cgroup(page);
406 * Used bit is set without atomic ops but after smp_wmb().
407 * For making pc->mem_cgroup visible, insert smp_rmb() here.
410 /* unused page is not rotated. */
411 if (!PageCgroupUsed(pc))
413 mz = page_cgroup_zoneinfo(pc);
414 list_move(&pc->lru, &mz->lists[lru]);
417 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
419 struct page_cgroup *pc;
420 struct mem_cgroup_per_zone *mz;
422 if (mem_cgroup_disabled())
424 pc = lookup_page_cgroup(page);
426 * Used bit is set without atomic ops but after smp_wmb().
427 * For making pc->mem_cgroup visible, insert smp_rmb() here.
430 if (!PageCgroupUsed(pc))
433 mz = page_cgroup_zoneinfo(pc);
434 MEM_CGROUP_ZSTAT(mz, lru) += 1;
435 list_add(&pc->lru, &mz->lists[lru]);
439 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
440 * lru because the page may.be reused after it's fully uncharged (because of
441 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
442 * it again. This function is only used to charge SwapCache. It's done under
443 * lock_page and expected that zone->lru_lock is never held.
445 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
448 struct zone *zone = page_zone(page);
449 struct page_cgroup *pc = lookup_page_cgroup(page);
451 spin_lock_irqsave(&zone->lru_lock, flags);
453 * Forget old LRU when this page_cgroup is *not* used. This Used bit
454 * is guarded by lock_page() because the page is SwapCache.
456 if (!PageCgroupUsed(pc))
457 mem_cgroup_del_lru_list(page, page_lru(page));
458 spin_unlock_irqrestore(&zone->lru_lock, flags);
461 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
464 struct zone *zone = page_zone(page);
465 struct page_cgroup *pc = lookup_page_cgroup(page);
467 spin_lock_irqsave(&zone->lru_lock, flags);
468 /* link when the page is linked to LRU but page_cgroup isn't */
469 if (PageLRU(page) && list_empty(&pc->lru))
470 mem_cgroup_add_lru_list(page, page_lru(page));
471 spin_unlock_irqrestore(&zone->lru_lock, flags);
475 void mem_cgroup_move_lists(struct page *page,
476 enum lru_list from, enum lru_list to)
478 if (mem_cgroup_disabled())
480 mem_cgroup_del_lru_list(page, from);
481 mem_cgroup_add_lru_list(page, to);
484 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
487 struct mem_cgroup *curr = NULL;
491 curr = try_get_mem_cgroup_from_mm(task->mm);
496 if (curr->use_hierarchy)
497 ret = css_is_ancestor(&curr->css, &mem->css);
505 * prev_priority control...this will be used in memory reclaim path.
507 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
511 spin_lock(&mem->reclaim_param_lock);
512 prev_priority = mem->prev_priority;
513 spin_unlock(&mem->reclaim_param_lock);
515 return prev_priority;
518 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
520 spin_lock(&mem->reclaim_param_lock);
521 if (priority < mem->prev_priority)
522 mem->prev_priority = priority;
523 spin_unlock(&mem->reclaim_param_lock);
526 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
528 spin_lock(&mem->reclaim_param_lock);
529 mem->prev_priority = priority;
530 spin_unlock(&mem->reclaim_param_lock);
533 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
535 unsigned long active;
536 unsigned long inactive;
538 unsigned long inactive_ratio;
540 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
541 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
543 gb = (inactive + active) >> (30 - PAGE_SHIFT);
545 inactive_ratio = int_sqrt(10 * gb);
550 present_pages[0] = inactive;
551 present_pages[1] = active;
554 return inactive_ratio;
557 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
559 unsigned long active;
560 unsigned long inactive;
561 unsigned long present_pages[2];
562 unsigned long inactive_ratio;
564 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
566 inactive = present_pages[0];
567 active = present_pages[1];
569 if (inactive * inactive_ratio < active)
575 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
577 unsigned long active;
578 unsigned long inactive;
580 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
581 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
583 return (active > inactive);
586 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
590 int nid = zone->zone_pgdat->node_id;
591 int zid = zone_idx(zone);
592 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
594 return MEM_CGROUP_ZSTAT(mz, lru);
597 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
600 int nid = zone->zone_pgdat->node_id;
601 int zid = zone_idx(zone);
602 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
604 return &mz->reclaim_stat;
607 struct zone_reclaim_stat *
608 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
610 struct page_cgroup *pc;
611 struct mem_cgroup_per_zone *mz;
613 if (mem_cgroup_disabled())
616 pc = lookup_page_cgroup(page);
618 * Used bit is set without atomic ops but after smp_wmb().
619 * For making pc->mem_cgroup visible, insert smp_rmb() here.
622 if (!PageCgroupUsed(pc))
625 mz = page_cgroup_zoneinfo(pc);
629 return &mz->reclaim_stat;
632 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
633 struct list_head *dst,
634 unsigned long *scanned, int order,
635 int mode, struct zone *z,
636 struct mem_cgroup *mem_cont,
637 int active, int file)
639 unsigned long nr_taken = 0;
643 struct list_head *src;
644 struct page_cgroup *pc, *tmp;
645 int nid = z->zone_pgdat->node_id;
646 int zid = zone_idx(z);
647 struct mem_cgroup_per_zone *mz;
648 int lru = LRU_FILE * !!file + !!active;
651 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
652 src = &mz->lists[lru];
655 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
656 if (scan >= nr_to_scan)
660 if (unlikely(!PageCgroupUsed(pc)))
662 if (unlikely(!PageLRU(page)))
666 if (__isolate_lru_page(page, mode, file) == 0) {
667 list_move(&page->lru, dst);
676 #define mem_cgroup_from_res_counter(counter, member) \
677 container_of(counter, struct mem_cgroup, member)
679 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
681 if (do_swap_account) {
682 if (res_counter_check_under_limit(&mem->res) &&
683 res_counter_check_under_limit(&mem->memsw))
686 if (res_counter_check_under_limit(&mem->res))
691 static unsigned int get_swappiness(struct mem_cgroup *memcg)
693 struct cgroup *cgrp = memcg->css.cgroup;
694 unsigned int swappiness;
697 if (cgrp->parent == NULL)
698 return vm_swappiness;
700 spin_lock(&memcg->reclaim_param_lock);
701 swappiness = memcg->swappiness;
702 spin_unlock(&memcg->reclaim_param_lock);
707 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
715 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
716 * @memcg: The memory cgroup that went over limit
717 * @p: Task that is going to be killed
719 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
722 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
724 struct cgroup *task_cgrp;
725 struct cgroup *mem_cgrp;
727 * Need a buffer in BSS, can't rely on allocations. The code relies
728 * on the assumption that OOM is serialized for memory controller.
729 * If this assumption is broken, revisit this code.
731 static char memcg_name[PATH_MAX];
740 mem_cgrp = memcg->css.cgroup;
741 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
743 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
746 * Unfortunately, we are unable to convert to a useful name
747 * But we'll still print out the usage information
754 printk(KERN_INFO "Task in %s killed", memcg_name);
757 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
765 * Continues from above, so we don't need an KERN_ level
767 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
770 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
771 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
772 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
773 res_counter_read_u64(&memcg->res, RES_FAILCNT));
774 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
776 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
777 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
778 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
782 * This function returns the number of memcg under hierarchy tree. Returns
783 * 1(self count) if no children.
785 static int mem_cgroup_count_children(struct mem_cgroup *mem)
788 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
793 * Visit the first child (need not be the first child as per the ordering
794 * of the cgroup list, since we track last_scanned_child) of @mem and use
795 * that to reclaim free pages from.
797 static struct mem_cgroup *
798 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
800 struct mem_cgroup *ret = NULL;
801 struct cgroup_subsys_state *css;
804 if (!root_mem->use_hierarchy) {
805 css_get(&root_mem->css);
811 nextid = root_mem->last_scanned_child + 1;
812 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
814 if (css && css_tryget(css))
815 ret = container_of(css, struct mem_cgroup, css);
818 /* Updates scanning parameter */
819 spin_lock(&root_mem->reclaim_param_lock);
821 /* this means start scan from ID:1 */
822 root_mem->last_scanned_child = 0;
824 root_mem->last_scanned_child = found;
825 spin_unlock(&root_mem->reclaim_param_lock);
832 * Scan the hierarchy if needed to reclaim memory. We remember the last child
833 * we reclaimed from, so that we don't end up penalizing one child extensively
834 * based on its position in the children list.
836 * root_mem is the original ancestor that we've been reclaim from.
838 * We give up and return to the caller when we visit root_mem twice.
839 * (other groups can be removed while we're walking....)
841 * If shrink==true, for avoiding to free too much, this returns immedieately.
843 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
844 gfp_t gfp_mask, bool noswap, bool shrink)
846 struct mem_cgroup *victim;
851 victim = mem_cgroup_select_victim(root_mem);
852 if (victim == root_mem)
854 if (!mem_cgroup_local_usage(&victim->stat)) {
855 /* this cgroup's local usage == 0 */
856 css_put(&victim->css);
859 /* we use swappiness of local cgroup */
860 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
861 get_swappiness(victim));
862 css_put(&victim->css);
864 * At shrinking usage, we can't check we should stop here or
865 * reclaim more. It's depends on callers. last_scanned_child
866 * will work enough for keeping fairness under tree.
871 if (mem_cgroup_check_under_limit(root_mem))
877 bool mem_cgroup_oom_called(struct task_struct *task)
880 struct mem_cgroup *mem;
881 struct mm_struct *mm;
887 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
888 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
894 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
896 mem->last_oom_jiffies = jiffies;
900 static void record_last_oom(struct mem_cgroup *mem)
902 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
906 * Currently used to update mapped file statistics, but the routine can be
907 * generalized to update other statistics as well.
909 void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
911 struct mem_cgroup *mem;
912 struct mem_cgroup_stat *stat;
913 struct mem_cgroup_stat_cpu *cpustat;
915 struct page_cgroup *pc;
917 if (!page_is_file_cache(page))
920 pc = lookup_page_cgroup(page);
924 lock_page_cgroup(pc);
925 mem = pc->mem_cgroup;
929 if (!PageCgroupUsed(pc))
933 * Preemption is already disabled, we don't need get_cpu()
935 cpu = smp_processor_id();
937 cpustat = &stat->cpustat[cpu];
939 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
941 unlock_page_cgroup(pc);
945 * Unlike exported interface, "oom" parameter is added. if oom==true,
946 * oom-killer can be invoked.
948 static int __mem_cgroup_try_charge(struct mm_struct *mm,
949 gfp_t gfp_mask, struct mem_cgroup **memcg,
952 struct mem_cgroup *mem, *mem_over_limit;
953 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
954 struct res_counter *fail_res;
956 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
957 /* Don't account this! */
963 * We always charge the cgroup the mm_struct belongs to.
964 * The mm_struct's mem_cgroup changes on task migration if the
965 * thread group leader migrates. It's possible that mm is not
966 * set, if so charge the init_mm (happens for pagecache usage).
970 mem = try_get_mem_cgroup_from_mm(mm);
978 VM_BUG_ON(css_is_removed(&mem->css));
984 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
986 if (!do_swap_account)
988 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
992 /* mem+swap counter fails */
993 res_counter_uncharge(&mem->res, PAGE_SIZE);
995 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
998 /* mem counter fails */
999 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1002 if (!(gfp_mask & __GFP_WAIT))
1005 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
1011 * try_to_free_mem_cgroup_pages() might not give us a full
1012 * picture of reclaim. Some pages are reclaimed and might be
1013 * moved to swap cache or just unmapped from the cgroup.
1014 * Check the limit again to see if the reclaim reduced the
1015 * current usage of the cgroup before giving up
1018 if (mem_cgroup_check_under_limit(mem_over_limit))
1021 if (!nr_retries--) {
1023 mutex_lock(&memcg_tasklist);
1024 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1025 mutex_unlock(&memcg_tasklist);
1026 record_last_oom(mem_over_limit);
1039 * A helper function to get mem_cgroup from ID. must be called under
1040 * rcu_read_lock(). The caller must check css_is_removed() or some if
1041 * it's concern. (dropping refcnt from swap can be called against removed
1044 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1046 struct cgroup_subsys_state *css;
1048 /* ID 0 is unused ID */
1051 css = css_lookup(&mem_cgroup_subsys, id);
1054 return container_of(css, struct mem_cgroup, css);
1057 static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1059 struct mem_cgroup *mem;
1060 struct page_cgroup *pc;
1064 VM_BUG_ON(!PageLocked(page));
1066 if (!PageSwapCache(page))
1069 pc = lookup_page_cgroup(page);
1070 lock_page_cgroup(pc);
1071 if (PageCgroupUsed(pc)) {
1072 mem = pc->mem_cgroup;
1073 if (mem && !css_tryget(&mem->css))
1076 ent.val = page_private(page);
1077 id = lookup_swap_cgroup(ent);
1079 mem = mem_cgroup_lookup(id);
1080 if (mem && !css_tryget(&mem->css))
1084 unlock_page_cgroup(pc);
1089 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1090 * USED state. If already USED, uncharge and return.
1093 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1094 struct page_cgroup *pc,
1095 enum charge_type ctype)
1097 /* try_charge() can return NULL to *memcg, taking care of it. */
1101 lock_page_cgroup(pc);
1102 if (unlikely(PageCgroupUsed(pc))) {
1103 unlock_page_cgroup(pc);
1104 res_counter_uncharge(&mem->res, PAGE_SIZE);
1105 if (do_swap_account)
1106 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1110 pc->mem_cgroup = mem;
1112 pc->flags = pcg_default_flags[ctype];
1114 mem_cgroup_charge_statistics(mem, pc, true);
1116 unlock_page_cgroup(pc);
1120 * mem_cgroup_move_account - move account of the page
1121 * @pc: page_cgroup of the page.
1122 * @from: mem_cgroup which the page is moved from.
1123 * @to: mem_cgroup which the page is moved to. @from != @to.
1125 * The caller must confirm following.
1126 * - page is not on LRU (isolate_page() is useful.)
1128 * returns 0 at success,
1129 * returns -EBUSY when lock is busy or "pc" is unstable.
1131 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1132 * new cgroup. It should be done by a caller.
1135 static int mem_cgroup_move_account(struct page_cgroup *pc,
1136 struct mem_cgroup *from, struct mem_cgroup *to)
1138 struct mem_cgroup_per_zone *from_mz, *to_mz;
1143 struct mem_cgroup_stat *stat;
1144 struct mem_cgroup_stat_cpu *cpustat;
1146 VM_BUG_ON(from == to);
1147 VM_BUG_ON(PageLRU(pc->page));
1149 nid = page_cgroup_nid(pc);
1150 zid = page_cgroup_zid(pc);
1151 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1152 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1154 if (!trylock_page_cgroup(pc))
1157 if (!PageCgroupUsed(pc))
1160 if (pc->mem_cgroup != from)
1163 res_counter_uncharge(&from->res, PAGE_SIZE);
1164 mem_cgroup_charge_statistics(from, pc, false);
1167 if (page_is_file_cache(page) && page_mapped(page)) {
1168 cpu = smp_processor_id();
1169 /* Update mapped_file data for mem_cgroup "from" */
1171 cpustat = &stat->cpustat[cpu];
1172 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1175 /* Update mapped_file data for mem_cgroup "to" */
1177 cpustat = &stat->cpustat[cpu];
1178 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1182 if (do_swap_account)
1183 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1184 css_put(&from->css);
1187 pc->mem_cgroup = to;
1188 mem_cgroup_charge_statistics(to, pc, true);
1191 unlock_page_cgroup(pc);
1196 * move charges to its parent.
1199 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1200 struct mem_cgroup *child,
1203 struct page *page = pc->page;
1204 struct cgroup *cg = child->css.cgroup;
1205 struct cgroup *pcg = cg->parent;
1206 struct mem_cgroup *parent;
1214 parent = mem_cgroup_from_cont(pcg);
1217 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
1221 if (!get_page_unless_zero(page)) {
1226 ret = isolate_lru_page(page);
1231 ret = mem_cgroup_move_account(pc, child, parent);
1233 putback_lru_page(page);
1236 /* drop extra refcnt by try_charge() */
1237 css_put(&parent->css);
1244 /* drop extra refcnt by try_charge() */
1245 css_put(&parent->css);
1246 /* uncharge if move fails */
1247 res_counter_uncharge(&parent->res, PAGE_SIZE);
1248 if (do_swap_account)
1249 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
1254 * Charge the memory controller for page usage.
1256 * 0 if the charge was successful
1257 * < 0 if the cgroup is over its limit
1259 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1260 gfp_t gfp_mask, enum charge_type ctype,
1261 struct mem_cgroup *memcg)
1263 struct mem_cgroup *mem;
1264 struct page_cgroup *pc;
1267 pc = lookup_page_cgroup(page);
1268 /* can happen at boot */
1274 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
1278 __mem_cgroup_commit_charge(mem, pc, ctype);
1282 int mem_cgroup_newpage_charge(struct page *page,
1283 struct mm_struct *mm, gfp_t gfp_mask)
1285 if (mem_cgroup_disabled())
1287 if (PageCompound(page))
1290 * If already mapped, we don't have to account.
1291 * If page cache, page->mapping has address_space.
1292 * But page->mapping may have out-of-use anon_vma pointer,
1293 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1296 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1300 return mem_cgroup_charge_common(page, mm, gfp_mask,
1301 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1305 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1306 enum charge_type ctype);
1308 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1311 struct mem_cgroup *mem = NULL;
1314 if (mem_cgroup_disabled())
1316 if (PageCompound(page))
1319 * Corner case handling. This is called from add_to_page_cache()
1320 * in usual. But some FS (shmem) precharges this page before calling it
1321 * and call add_to_page_cache() with GFP_NOWAIT.
1323 * For GFP_NOWAIT case, the page may be pre-charged before calling
1324 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1325 * charge twice. (It works but has to pay a bit larger cost.)
1326 * And when the page is SwapCache, it should take swap information
1327 * into account. This is under lock_page() now.
1329 if (!(gfp_mask & __GFP_WAIT)) {
1330 struct page_cgroup *pc;
1333 pc = lookup_page_cgroup(page);
1336 lock_page_cgroup(pc);
1337 if (PageCgroupUsed(pc)) {
1338 unlock_page_cgroup(pc);
1341 unlock_page_cgroup(pc);
1344 if (unlikely(!mm && !mem))
1347 if (page_is_file_cache(page))
1348 return mem_cgroup_charge_common(page, mm, gfp_mask,
1349 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1352 if (PageSwapCache(page)) {
1353 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1355 __mem_cgroup_commit_charge_swapin(page, mem,
1356 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1358 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1359 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1365 * While swap-in, try_charge -> commit or cancel, the page is locked.
1366 * And when try_charge() successfully returns, one refcnt to memcg without
1367 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1368 * "commit()" or removed by "cancel()"
1370 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1372 gfp_t mask, struct mem_cgroup **ptr)
1374 struct mem_cgroup *mem;
1377 if (mem_cgroup_disabled())
1380 if (!do_swap_account)
1383 * A racing thread's fault, or swapoff, may have already updated
1384 * the pte, and even removed page from swap cache: return success
1385 * to go on to do_swap_page()'s pte_same() test, which should fail.
1387 if (!PageSwapCache(page))
1389 mem = try_get_mem_cgroup_from_swapcache(page);
1393 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1394 /* drop extra refcnt from tryget */
1400 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1404 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1405 enum charge_type ctype)
1407 struct page_cgroup *pc;
1409 if (mem_cgroup_disabled())
1413 pc = lookup_page_cgroup(page);
1414 mem_cgroup_lru_del_before_commit_swapcache(page);
1415 __mem_cgroup_commit_charge(ptr, pc, ctype);
1416 mem_cgroup_lru_add_after_commit_swapcache(page);
1418 * Now swap is on-memory. This means this page may be
1419 * counted both as mem and swap....double count.
1420 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1421 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1422 * may call delete_from_swap_cache() before reach here.
1424 if (do_swap_account && PageSwapCache(page)) {
1425 swp_entry_t ent = {.val = page_private(page)};
1427 struct mem_cgroup *memcg;
1429 id = swap_cgroup_record(ent, 0);
1431 memcg = mem_cgroup_lookup(id);
1434 * This recorded memcg can be obsolete one. So, avoid
1435 * calling css_tryget
1437 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1438 mem_cgroup_put(memcg);
1442 /* add this page(page_cgroup) to the LRU we want. */
1446 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1448 __mem_cgroup_commit_charge_swapin(page, ptr,
1449 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1452 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1454 if (mem_cgroup_disabled())
1458 res_counter_uncharge(&mem->res, PAGE_SIZE);
1459 if (do_swap_account)
1460 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1466 * uncharge if !page_mapped(page)
1468 static struct mem_cgroup *
1469 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1471 struct page_cgroup *pc;
1472 struct mem_cgroup *mem = NULL;
1473 struct mem_cgroup_per_zone *mz;
1475 if (mem_cgroup_disabled())
1478 if (PageSwapCache(page))
1482 * Check if our page_cgroup is valid
1484 pc = lookup_page_cgroup(page);
1485 if (unlikely(!pc || !PageCgroupUsed(pc)))
1488 lock_page_cgroup(pc);
1490 mem = pc->mem_cgroup;
1492 if (!PageCgroupUsed(pc))
1496 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1497 case MEM_CGROUP_CHARGE_TYPE_DROP:
1498 if (page_mapped(page))
1501 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1502 if (!PageAnon(page)) { /* Shared memory */
1503 if (page->mapping && !page_is_file_cache(page))
1505 } else if (page_mapped(page)) /* Anon */
1512 res_counter_uncharge(&mem->res, PAGE_SIZE);
1513 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1514 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1515 mem_cgroup_charge_statistics(mem, pc, false);
1517 ClearPageCgroupUsed(pc);
1519 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1520 * freed from LRU. This is safe because uncharged page is expected not
1521 * to be reused (freed soon). Exception is SwapCache, it's handled by
1522 * special functions.
1525 mz = page_cgroup_zoneinfo(pc);
1526 unlock_page_cgroup(pc);
1528 /* at swapout, this memcg will be accessed to record to swap */
1529 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1535 unlock_page_cgroup(pc);
1539 void mem_cgroup_uncharge_page(struct page *page)
1542 if (page_mapped(page))
1544 if (page->mapping && !PageAnon(page))
1546 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1549 void mem_cgroup_uncharge_cache_page(struct page *page)
1551 VM_BUG_ON(page_mapped(page));
1552 VM_BUG_ON(page->mapping);
1553 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1558 * called after __delete_from_swap_cache() and drop "page" account.
1559 * memcg information is recorded to swap_cgroup of "ent"
1562 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
1564 struct mem_cgroup *memcg;
1565 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1567 if (!swapout) /* this was a swap cache but the swap is unused ! */
1568 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1570 memcg = __mem_cgroup_uncharge_common(page, ctype);
1572 /* record memcg information */
1573 if (do_swap_account && swapout && memcg) {
1574 swap_cgroup_record(ent, css_id(&memcg->css));
1575 mem_cgroup_get(memcg);
1577 if (swapout && memcg)
1578 css_put(&memcg->css);
1582 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1584 * called from swap_entry_free(). remove record in swap_cgroup and
1585 * uncharge "memsw" account.
1587 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1589 struct mem_cgroup *memcg;
1592 if (!do_swap_account)
1595 id = swap_cgroup_record(ent, 0);
1597 memcg = mem_cgroup_lookup(id);
1600 * We uncharge this because swap is freed.
1601 * This memcg can be obsolete one. We avoid calling css_tryget
1603 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1604 mem_cgroup_put(memcg);
1611 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1614 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1616 struct page_cgroup *pc;
1617 struct mem_cgroup *mem = NULL;
1620 if (mem_cgroup_disabled())
1623 pc = lookup_page_cgroup(page);
1624 lock_page_cgroup(pc);
1625 if (PageCgroupUsed(pc)) {
1626 mem = pc->mem_cgroup;
1629 unlock_page_cgroup(pc);
1632 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
1639 /* remove redundant charge if migration failed*/
1640 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1641 struct page *oldpage, struct page *newpage)
1643 struct page *target, *unused;
1644 struct page_cgroup *pc;
1645 enum charge_type ctype;
1650 /* at migration success, oldpage->mapping is NULL. */
1651 if (oldpage->mapping) {
1659 if (PageAnon(target))
1660 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1661 else if (page_is_file_cache(target))
1662 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1664 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1666 /* unused page is not on radix-tree now. */
1668 __mem_cgroup_uncharge_common(unused, ctype);
1670 pc = lookup_page_cgroup(target);
1672 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1673 * So, double-counting is effectively avoided.
1675 __mem_cgroup_commit_charge(mem, pc, ctype);
1678 * Both of oldpage and newpage are still under lock_page().
1679 * Then, we don't have to care about race in radix-tree.
1680 * But we have to be careful that this page is unmapped or not.
1682 * There is a case for !page_mapped(). At the start of
1683 * migration, oldpage was mapped. But now, it's zapped.
1684 * But we know *target* page is not freed/reused under us.
1685 * mem_cgroup_uncharge_page() does all necessary checks.
1687 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1688 mem_cgroup_uncharge_page(target);
1692 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1693 * Calling hierarchical_reclaim is not enough because we should update
1694 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1695 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1696 * not from the memcg which this page would be charged to.
1697 * try_charge_swapin does all of these works properly.
1699 int mem_cgroup_shmem_charge_fallback(struct page *page,
1700 struct mm_struct *mm,
1703 struct mem_cgroup *mem = NULL;
1706 if (mem_cgroup_disabled())
1709 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1711 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
1716 static DEFINE_MUTEX(set_limit_mutex);
1718 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1719 unsigned long long val)
1725 int children = mem_cgroup_count_children(memcg);
1726 u64 curusage, oldusage;
1729 * For keeping hierarchical_reclaim simple, how long we should retry
1730 * is depends on callers. We set our retry-count to be function
1731 * of # of children which we should visit in this loop.
1733 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1735 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1737 while (retry_count) {
1738 if (signal_pending(current)) {
1743 * Rather than hide all in some function, I do this in
1744 * open coded manner. You see what this really does.
1745 * We have to guarantee mem->res.limit < mem->memsw.limit.
1747 mutex_lock(&set_limit_mutex);
1748 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1749 if (memswlimit < val) {
1751 mutex_unlock(&set_limit_mutex);
1754 ret = res_counter_set_limit(&memcg->res, val);
1755 mutex_unlock(&set_limit_mutex);
1760 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
1762 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1763 /* Usage is reduced ? */
1764 if (curusage >= oldusage)
1767 oldusage = curusage;
1773 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1774 unsigned long long val)
1777 u64 memlimit, oldusage, curusage;
1778 int children = mem_cgroup_count_children(memcg);
1781 /* see mem_cgroup_resize_res_limit */
1782 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1783 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1784 while (retry_count) {
1785 if (signal_pending(current)) {
1790 * Rather than hide all in some function, I do this in
1791 * open coded manner. You see what this really does.
1792 * We have to guarantee mem->res.limit < mem->memsw.limit.
1794 mutex_lock(&set_limit_mutex);
1795 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1796 if (memlimit > val) {
1798 mutex_unlock(&set_limit_mutex);
1801 ret = res_counter_set_limit(&memcg->memsw, val);
1802 mutex_unlock(&set_limit_mutex);
1807 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
1808 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1809 /* Usage is reduced ? */
1810 if (curusage >= oldusage)
1813 oldusage = curusage;
1819 * This routine traverse page_cgroup in given list and drop them all.
1820 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1822 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1823 int node, int zid, enum lru_list lru)
1826 struct mem_cgroup_per_zone *mz;
1827 struct page_cgroup *pc, *busy;
1828 unsigned long flags, loop;
1829 struct list_head *list;
1832 zone = &NODE_DATA(node)->node_zones[zid];
1833 mz = mem_cgroup_zoneinfo(mem, node, zid);
1834 list = &mz->lists[lru];
1836 loop = MEM_CGROUP_ZSTAT(mz, lru);
1837 /* give some margin against EBUSY etc...*/
1842 spin_lock_irqsave(&zone->lru_lock, flags);
1843 if (list_empty(list)) {
1844 spin_unlock_irqrestore(&zone->lru_lock, flags);
1847 pc = list_entry(list->prev, struct page_cgroup, lru);
1849 list_move(&pc->lru, list);
1851 spin_unlock_irqrestore(&zone->lru_lock, flags);
1854 spin_unlock_irqrestore(&zone->lru_lock, flags);
1856 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
1860 if (ret == -EBUSY || ret == -EINVAL) {
1861 /* found lock contention or "pc" is obsolete. */
1868 if (!ret && !list_empty(list))
1874 * make mem_cgroup's charge to be 0 if there is no task.
1875 * This enables deleting this mem_cgroup.
1877 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1880 int node, zid, shrink;
1881 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1882 struct cgroup *cgrp = mem->css.cgroup;
1887 /* should free all ? */
1891 while (mem->res.usage > 0) {
1893 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1896 if (signal_pending(current))
1898 /* This is for making all *used* pages to be on LRU. */
1899 lru_add_drain_all();
1901 for_each_node_state(node, N_HIGH_MEMORY) {
1902 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1905 ret = mem_cgroup_force_empty_list(mem,
1914 /* it seems parent cgroup doesn't have enough mem */
1925 /* returns EBUSY if there is a task or if we come here twice. */
1926 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
1930 /* we call try-to-free pages for make this cgroup empty */
1931 lru_add_drain_all();
1932 /* try to free all pages in this cgroup */
1934 while (nr_retries && mem->res.usage > 0) {
1937 if (signal_pending(current)) {
1941 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1942 false, get_swappiness(mem));
1945 /* maybe some writeback is necessary */
1946 congestion_wait(WRITE, HZ/10);
1951 /* try move_account...there may be some *locked* pages. */
1958 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1960 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1964 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1966 return mem_cgroup_from_cont(cont)->use_hierarchy;
1969 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1973 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1974 struct cgroup *parent = cont->parent;
1975 struct mem_cgroup *parent_mem = NULL;
1978 parent_mem = mem_cgroup_from_cont(parent);
1982 * If parent's use_hiearchy is set, we can't make any modifications
1983 * in the child subtrees. If it is unset, then the change can
1984 * occur, provided the current cgroup has no children.
1986 * For the root cgroup, parent_mem is NULL, we allow value to be
1987 * set if there are no children.
1989 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1990 (val == 1 || val == 0)) {
1991 if (list_empty(&cont->children))
1992 mem->use_hierarchy = val;
2002 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2004 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2008 type = MEMFILE_TYPE(cft->private);
2009 name = MEMFILE_ATTR(cft->private);
2012 val = res_counter_read_u64(&mem->res, name);
2015 val = res_counter_read_u64(&mem->memsw, name);
2024 * The user of this function is...
2027 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2030 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2032 unsigned long long val;
2035 type = MEMFILE_TYPE(cft->private);
2036 name = MEMFILE_ATTR(cft->private);
2039 /* This function does all necessary parse...reuse it */
2040 ret = res_counter_memparse_write_strategy(buffer, &val);
2044 ret = mem_cgroup_resize_limit(memcg, val);
2046 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2049 ret = -EINVAL; /* should be BUG() ? */
2055 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2056 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2058 struct cgroup *cgroup;
2059 unsigned long long min_limit, min_memsw_limit, tmp;
2061 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2062 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2063 cgroup = memcg->css.cgroup;
2064 if (!memcg->use_hierarchy)
2067 while (cgroup->parent) {
2068 cgroup = cgroup->parent;
2069 memcg = mem_cgroup_from_cont(cgroup);
2070 if (!memcg->use_hierarchy)
2072 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2073 min_limit = min(min_limit, tmp);
2074 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2075 min_memsw_limit = min(min_memsw_limit, tmp);
2078 *mem_limit = min_limit;
2079 *memsw_limit = min_memsw_limit;
2083 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2085 struct mem_cgroup *mem;
2088 mem = mem_cgroup_from_cont(cont);
2089 type = MEMFILE_TYPE(event);
2090 name = MEMFILE_ATTR(event);
2094 res_counter_reset_max(&mem->res);
2096 res_counter_reset_max(&mem->memsw);
2100 res_counter_reset_failcnt(&mem->res);
2102 res_counter_reset_failcnt(&mem->memsw);
2109 /* For read statistics */
2124 struct mcs_total_stat {
2125 s64 stat[NR_MCS_STAT];
2131 } memcg_stat_strings[NR_MCS_STAT] = {
2132 {"cache", "total_cache"},
2133 {"rss", "total_rss"},
2134 {"mapped_file", "total_mapped_file"},
2135 {"pgpgin", "total_pgpgin"},
2136 {"pgpgout", "total_pgpgout"},
2137 {"inactive_anon", "total_inactive_anon"},
2138 {"active_anon", "total_active_anon"},
2139 {"inactive_file", "total_inactive_file"},
2140 {"active_file", "total_active_file"},
2141 {"unevictable", "total_unevictable"}
2145 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2147 struct mcs_total_stat *s = data;
2151 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2152 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2153 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2154 s->stat[MCS_RSS] += val * PAGE_SIZE;
2155 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2156 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
2157 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2158 s->stat[MCS_PGPGIN] += val;
2159 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2160 s->stat[MCS_PGPGOUT] += val;
2163 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2164 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2165 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2166 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2167 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2168 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2169 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2170 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2171 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2172 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2177 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2179 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2182 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2183 struct cgroup_map_cb *cb)
2185 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2186 struct mcs_total_stat mystat;
2189 memset(&mystat, 0, sizeof(mystat));
2190 mem_cgroup_get_local_stat(mem_cont, &mystat);
2192 for (i = 0; i < NR_MCS_STAT; i++)
2193 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2195 /* Hierarchical information */
2197 unsigned long long limit, memsw_limit;
2198 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2199 cb->fill(cb, "hierarchical_memory_limit", limit);
2200 if (do_swap_account)
2201 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2204 memset(&mystat, 0, sizeof(mystat));
2205 mem_cgroup_get_total_stat(mem_cont, &mystat);
2206 for (i = 0; i < NR_MCS_STAT; i++)
2207 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2210 #ifdef CONFIG_DEBUG_VM
2211 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2215 struct mem_cgroup_per_zone *mz;
2216 unsigned long recent_rotated[2] = {0, 0};
2217 unsigned long recent_scanned[2] = {0, 0};
2219 for_each_online_node(nid)
2220 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2221 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2223 recent_rotated[0] +=
2224 mz->reclaim_stat.recent_rotated[0];
2225 recent_rotated[1] +=
2226 mz->reclaim_stat.recent_rotated[1];
2227 recent_scanned[0] +=
2228 mz->reclaim_stat.recent_scanned[0];
2229 recent_scanned[1] +=
2230 mz->reclaim_stat.recent_scanned[1];
2232 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2233 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2234 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2235 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2242 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2244 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2246 return get_swappiness(memcg);
2249 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2252 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2253 struct mem_cgroup *parent;
2258 if (cgrp->parent == NULL)
2261 parent = mem_cgroup_from_cont(cgrp->parent);
2265 /* If under hierarchy, only empty-root can set this value */
2266 if ((parent->use_hierarchy) ||
2267 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2272 spin_lock(&memcg->reclaim_param_lock);
2273 memcg->swappiness = val;
2274 spin_unlock(&memcg->reclaim_param_lock);
2282 static struct cftype mem_cgroup_files[] = {
2284 .name = "usage_in_bytes",
2285 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2286 .read_u64 = mem_cgroup_read,
2289 .name = "max_usage_in_bytes",
2290 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2291 .trigger = mem_cgroup_reset,
2292 .read_u64 = mem_cgroup_read,
2295 .name = "limit_in_bytes",
2296 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2297 .write_string = mem_cgroup_write,
2298 .read_u64 = mem_cgroup_read,
2302 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2303 .trigger = mem_cgroup_reset,
2304 .read_u64 = mem_cgroup_read,
2308 .read_map = mem_control_stat_show,
2311 .name = "force_empty",
2312 .trigger = mem_cgroup_force_empty_write,
2315 .name = "use_hierarchy",
2316 .write_u64 = mem_cgroup_hierarchy_write,
2317 .read_u64 = mem_cgroup_hierarchy_read,
2320 .name = "swappiness",
2321 .read_u64 = mem_cgroup_swappiness_read,
2322 .write_u64 = mem_cgroup_swappiness_write,
2326 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2327 static struct cftype memsw_cgroup_files[] = {
2329 .name = "memsw.usage_in_bytes",
2330 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2331 .read_u64 = mem_cgroup_read,
2334 .name = "memsw.max_usage_in_bytes",
2335 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2336 .trigger = mem_cgroup_reset,
2337 .read_u64 = mem_cgroup_read,
2340 .name = "memsw.limit_in_bytes",
2341 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2342 .write_string = mem_cgroup_write,
2343 .read_u64 = mem_cgroup_read,
2346 .name = "memsw.failcnt",
2347 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2348 .trigger = mem_cgroup_reset,
2349 .read_u64 = mem_cgroup_read,
2353 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2355 if (!do_swap_account)
2357 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2358 ARRAY_SIZE(memsw_cgroup_files));
2361 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2367 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2369 struct mem_cgroup_per_node *pn;
2370 struct mem_cgroup_per_zone *mz;
2372 int zone, tmp = node;
2374 * This routine is called against possible nodes.
2375 * But it's BUG to call kmalloc() against offline node.
2377 * TODO: this routine can waste much memory for nodes which will
2378 * never be onlined. It's better to use memory hotplug callback
2381 if (!node_state(node, N_NORMAL_MEMORY))
2383 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
2387 mem->info.nodeinfo[node] = pn;
2388 memset(pn, 0, sizeof(*pn));
2390 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2391 mz = &pn->zoneinfo[zone];
2393 INIT_LIST_HEAD(&mz->lists[l]);
2398 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2400 kfree(mem->info.nodeinfo[node]);
2403 static int mem_cgroup_size(void)
2405 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2406 return sizeof(struct mem_cgroup) + cpustat_size;
2409 static struct mem_cgroup *mem_cgroup_alloc(void)
2411 struct mem_cgroup *mem;
2412 int size = mem_cgroup_size();
2414 if (size < PAGE_SIZE)
2415 mem = kmalloc(size, GFP_KERNEL);
2417 mem = vmalloc(size);
2420 memset(mem, 0, size);
2425 * At destroying mem_cgroup, references from swap_cgroup can remain.
2426 * (scanning all at force_empty is too costly...)
2428 * Instead of clearing all references at force_empty, we remember
2429 * the number of reference from swap_cgroup and free mem_cgroup when
2430 * it goes down to 0.
2432 * Removal of cgroup itself succeeds regardless of refs from swap.
2435 static void __mem_cgroup_free(struct mem_cgroup *mem)
2439 free_css_id(&mem_cgroup_subsys, &mem->css);
2441 for_each_node_state(node, N_POSSIBLE)
2442 free_mem_cgroup_per_zone_info(mem, node);
2444 if (mem_cgroup_size() < PAGE_SIZE)
2450 static void mem_cgroup_get(struct mem_cgroup *mem)
2452 atomic_inc(&mem->refcnt);
2455 static void mem_cgroup_put(struct mem_cgroup *mem)
2457 if (atomic_dec_and_test(&mem->refcnt)) {
2458 struct mem_cgroup *parent = parent_mem_cgroup(mem);
2459 __mem_cgroup_free(mem);
2461 mem_cgroup_put(parent);
2466 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2468 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2470 if (!mem->res.parent)
2472 return mem_cgroup_from_res_counter(mem->res.parent, res);
2475 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2476 static void __init enable_swap_cgroup(void)
2478 if (!mem_cgroup_disabled() && really_do_swap_account)
2479 do_swap_account = 1;
2482 static void __init enable_swap_cgroup(void)
2487 static struct cgroup_subsys_state * __ref
2488 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2490 struct mem_cgroup *mem, *parent;
2491 long error = -ENOMEM;
2494 mem = mem_cgroup_alloc();
2496 return ERR_PTR(error);
2498 for_each_node_state(node, N_POSSIBLE)
2499 if (alloc_mem_cgroup_per_zone_info(mem, node))
2502 if (cont->parent == NULL) {
2503 enable_swap_cgroup();
2506 parent = mem_cgroup_from_cont(cont->parent);
2507 mem->use_hierarchy = parent->use_hierarchy;
2510 if (parent && parent->use_hierarchy) {
2511 res_counter_init(&mem->res, &parent->res);
2512 res_counter_init(&mem->memsw, &parent->memsw);
2514 * We increment refcnt of the parent to ensure that we can
2515 * safely access it on res_counter_charge/uncharge.
2516 * This refcnt will be decremented when freeing this
2517 * mem_cgroup(see mem_cgroup_put).
2519 mem_cgroup_get(parent);
2521 res_counter_init(&mem->res, NULL);
2522 res_counter_init(&mem->memsw, NULL);
2524 mem->last_scanned_child = 0;
2525 spin_lock_init(&mem->reclaim_param_lock);
2528 mem->swappiness = get_swappiness(parent);
2529 atomic_set(&mem->refcnt, 1);
2532 __mem_cgroup_free(mem);
2533 return ERR_PTR(error);
2536 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
2537 struct cgroup *cont)
2539 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2541 return mem_cgroup_force_empty(mem, false);
2544 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2545 struct cgroup *cont)
2547 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2549 mem_cgroup_put(mem);
2552 static int mem_cgroup_populate(struct cgroup_subsys *ss,
2553 struct cgroup *cont)
2557 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2558 ARRAY_SIZE(mem_cgroup_files));
2561 ret = register_memsw_files(cont, ss);
2565 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2566 struct cgroup *cont,
2567 struct cgroup *old_cont,
2568 struct task_struct *p)
2570 mutex_lock(&memcg_tasklist);
2572 * FIXME: It's better to move charges of this process from old
2573 * memcg to new memcg. But it's just on TODO-List now.
2575 mutex_unlock(&memcg_tasklist);
2578 struct cgroup_subsys mem_cgroup_subsys = {
2580 .subsys_id = mem_cgroup_subsys_id,
2581 .create = mem_cgroup_create,
2582 .pre_destroy = mem_cgroup_pre_destroy,
2583 .destroy = mem_cgroup_destroy,
2584 .populate = mem_cgroup_populate,
2585 .attach = mem_cgroup_move_task,
2590 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2592 static int __init disable_swap_account(char *s)
2594 really_do_swap_account = 0;
2597 __setup("noswapaccount", disable_swap_account);