1 /* memcontrol.c - Memory Controller
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/res_counter.h>
21 #include <linux/memcontrol.h>
22 #include <linux/cgroup.h>
24 #include <linux/pagemap.h>
25 #include <linux/smp.h>
26 #include <linux/page-flags.h>
27 #include <linux/backing-dev.h>
28 #include <linux/bit_spinlock.h>
29 #include <linux/rcupdate.h>
30 #include <linux/limits.h>
31 #include <linux/mutex.h>
32 #include <linux/rbtree.h>
33 #include <linux/slab.h>
34 #include <linux/swap.h>
35 #include <linux/spinlock.h>
37 #include <linux/seq_file.h>
38 #include <linux/vmalloc.h>
39 #include <linux/mm_inline.h>
40 #include <linux/page_cgroup.h>
43 #include <asm/uaccess.h>
45 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
46 #define MEM_CGROUP_RECLAIM_RETRIES 5
47 struct mem_cgroup *root_mem_cgroup __read_mostly;
49 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
50 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
51 int do_swap_account __read_mostly;
52 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
54 #define do_swap_account (0)
57 static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
58 #define SOFTLIMIT_EVENTS_THRESH (1000)
61 * Statistics for memory cgroup.
63 enum mem_cgroup_stat_index {
65 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
67 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
68 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
69 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
70 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
71 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
72 MEM_CGROUP_STAT_EVENTS, /* sum of pagein + pageout for internal use */
74 MEM_CGROUP_STAT_NSTATS,
77 struct mem_cgroup_stat_cpu {
78 s64 count[MEM_CGROUP_STAT_NSTATS];
79 } ____cacheline_aligned_in_smp;
81 struct mem_cgroup_stat {
82 struct mem_cgroup_stat_cpu cpustat[0];
86 __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu *stat,
87 enum mem_cgroup_stat_index idx)
93 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu *stat,
94 enum mem_cgroup_stat_index idx)
96 return stat->count[idx];
100 * For accounting under irq disable, no need for increment preempt count.
102 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
103 enum mem_cgroup_stat_index idx, int val)
105 stat->count[idx] += val;
108 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
109 enum mem_cgroup_stat_index idx)
113 for_each_possible_cpu(cpu)
114 ret += stat->cpustat[cpu].count[idx];
118 static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
122 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
123 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
128 * per-zone information in memory controller.
130 struct mem_cgroup_per_zone {
132 * spin_lock to protect the per cgroup LRU
134 struct list_head lists[NR_LRU_LISTS];
135 unsigned long count[NR_LRU_LISTS];
137 struct zone_reclaim_stat reclaim_stat;
138 struct rb_node tree_node; /* RB tree node */
139 unsigned long long usage_in_excess;/* Set to the value by which */
140 /* the soft limit is exceeded*/
143 /* Macro for accessing counter */
144 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
146 struct mem_cgroup_per_node {
147 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
150 struct mem_cgroup_lru_info {
151 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
155 * Cgroups above their limits are maintained in a RB-Tree, independent of
156 * their hierarchy representation
159 struct mem_cgroup_tree_per_zone {
160 struct rb_root rb_root;
164 struct mem_cgroup_tree_per_node {
165 struct mem_cgroup_tree_per_zone rb_tree_per_zone[MAX_NR_ZONES];
168 struct mem_cgroup_tree {
169 struct mem_cgroup_tree_per_node *rb_tree_per_node[MAX_NUMNODES];
172 static struct mem_cgroup_tree soft_limit_tree __read_mostly;
175 * The memory controller data structure. The memory controller controls both
176 * page cache and RSS per cgroup. We would eventually like to provide
177 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
178 * to help the administrator determine what knobs to tune.
180 * TODO: Add a water mark for the memory controller. Reclaim will begin when
181 * we hit the water mark. May be even add a low water mark, such that
182 * no reclaim occurs from a cgroup at it's low water mark, this is
183 * a feature that will be implemented much later in the future.
186 struct cgroup_subsys_state css;
188 * the counter to account for memory usage
190 struct res_counter res;
192 * the counter to account for mem+swap usage.
194 struct res_counter memsw;
196 * Per cgroup active and inactive list, similar to the
197 * per zone LRU lists.
199 struct mem_cgroup_lru_info info;
202 protect against reclaim related member.
204 spinlock_t reclaim_param_lock;
206 int prev_priority; /* for recording reclaim priority */
209 * While reclaiming in a hiearchy, we cache the last child we
212 int last_scanned_child;
214 * Should the accounting and control be hierarchical, per subtree?
217 unsigned long last_oom_jiffies;
220 unsigned int swappiness;
222 /* set when res.limit == memsw.limit */
223 bool memsw_is_minimum;
226 * statistics. This must be placed at the end of memcg.
228 struct mem_cgroup_stat stat;
232 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
233 MEM_CGROUP_CHARGE_TYPE_MAPPED,
234 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
235 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
236 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
237 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
241 /* only for here (for easy reading.) */
242 #define PCGF_CACHE (1UL << PCG_CACHE)
243 #define PCGF_USED (1UL << PCG_USED)
244 #define PCGF_LOCK (1UL << PCG_LOCK)
245 /* Not used, but added here for completeness */
246 #define PCGF_ACCT (1UL << PCG_ACCT)
248 /* for encoding cft->private value on file */
251 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
252 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
253 #define MEMFILE_ATTR(val) ((val) & 0xffff)
255 static void mem_cgroup_get(struct mem_cgroup *mem);
256 static void mem_cgroup_put(struct mem_cgroup *mem);
257 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
259 static struct mem_cgroup_per_zone *
260 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
262 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
265 static struct mem_cgroup_per_zone *
266 page_cgroup_zoneinfo(struct page_cgroup *pc)
268 struct mem_cgroup *mem = pc->mem_cgroup;
269 int nid = page_cgroup_nid(pc);
270 int zid = page_cgroup_zid(pc);
275 return mem_cgroup_zoneinfo(mem, nid, zid);
278 static struct mem_cgroup_tree_per_zone *
279 soft_limit_tree_node_zone(int nid, int zid)
281 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
284 static struct mem_cgroup_tree_per_zone *
285 soft_limit_tree_from_page(struct page *page)
287 int nid = page_to_nid(page);
288 int zid = page_zonenum(page);
290 return &soft_limit_tree.rb_tree_per_node[nid]->rb_tree_per_zone[zid];
294 mem_cgroup_insert_exceeded(struct mem_cgroup *mem,
295 struct mem_cgroup_per_zone *mz,
296 struct mem_cgroup_tree_per_zone *mctz)
298 struct rb_node **p = &mctz->rb_root.rb_node;
299 struct rb_node *parent = NULL;
300 struct mem_cgroup_per_zone *mz_node;
305 mz->usage_in_excess = res_counter_soft_limit_excess(&mem->res);
306 spin_lock(&mctz->lock);
309 mz_node = rb_entry(parent, struct mem_cgroup_per_zone,
311 if (mz->usage_in_excess < mz_node->usage_in_excess)
314 * We can't avoid mem cgroups that are over their soft
315 * limit by the same amount
317 else if (mz->usage_in_excess >= mz_node->usage_in_excess)
320 rb_link_node(&mz->tree_node, parent, p);
321 rb_insert_color(&mz->tree_node, &mctz->rb_root);
323 spin_unlock(&mctz->lock);
327 mem_cgroup_remove_exceeded(struct mem_cgroup *mem,
328 struct mem_cgroup_per_zone *mz,
329 struct mem_cgroup_tree_per_zone *mctz)
331 spin_lock(&mctz->lock);
332 rb_erase(&mz->tree_node, &mctz->rb_root);
334 spin_unlock(&mctz->lock);
337 static bool mem_cgroup_soft_limit_check(struct mem_cgroup *mem)
342 struct mem_cgroup_stat_cpu *cpustat;
345 cpustat = &mem->stat.cpustat[cpu];
346 val = __mem_cgroup_stat_read_local(cpustat, MEM_CGROUP_STAT_EVENTS);
347 if (unlikely(val > SOFTLIMIT_EVENTS_THRESH)) {
348 __mem_cgroup_stat_reset_safe(cpustat, MEM_CGROUP_STAT_EVENTS);
355 static void mem_cgroup_update_tree(struct mem_cgroup *mem, struct page *page)
357 unsigned long long prev_usage_in_excess, new_usage_in_excess;
358 bool updated_tree = false;
359 struct mem_cgroup_per_zone *mz;
360 struct mem_cgroup_tree_per_zone *mctz;
362 mz = mem_cgroup_zoneinfo(mem, page_to_nid(page), page_zonenum(page));
363 mctz = soft_limit_tree_from_page(page);
366 * We do updates in lazy mode, mem's are removed
367 * lazily from the per-zone, per-node rb tree
369 prev_usage_in_excess = mz->usage_in_excess;
371 new_usage_in_excess = res_counter_soft_limit_excess(&mem->res);
372 if (prev_usage_in_excess) {
373 mem_cgroup_remove_exceeded(mem, mz, mctz);
376 if (!new_usage_in_excess)
378 mem_cgroup_insert_exceeded(mem, mz, mctz);
382 spin_lock(&mctz->lock);
383 mz->usage_in_excess = new_usage_in_excess;
384 spin_unlock(&mctz->lock);
388 static void mem_cgroup_remove_from_trees(struct mem_cgroup *mem)
391 struct mem_cgroup_per_zone *mz;
392 struct mem_cgroup_tree_per_zone *mctz;
394 for_each_node_state(node, N_POSSIBLE) {
395 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
396 mz = mem_cgroup_zoneinfo(mem, node, zone);
397 mctz = soft_limit_tree_node_zone(node, zone);
398 mem_cgroup_remove_exceeded(mem, mz, mctz);
403 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
404 struct page_cgroup *pc,
407 int val = (charge)? 1 : -1;
408 struct mem_cgroup_stat *stat = &mem->stat;
409 struct mem_cgroup_stat_cpu *cpustat;
412 cpustat = &stat->cpustat[cpu];
413 if (PageCgroupCache(pc))
414 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
416 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
419 __mem_cgroup_stat_add_safe(cpustat,
420 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
422 __mem_cgroup_stat_add_safe(cpustat,
423 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
424 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_EVENTS, 1);
428 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
432 struct mem_cgroup_per_zone *mz;
435 for_each_online_node(nid)
436 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
437 mz = mem_cgroup_zoneinfo(mem, nid, zid);
438 total += MEM_CGROUP_ZSTAT(mz, idx);
443 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
445 return container_of(cgroup_subsys_state(cont,
446 mem_cgroup_subsys_id), struct mem_cgroup,
450 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
453 * mm_update_next_owner() may clear mm->owner to NULL
454 * if it races with swapoff, page migration, etc.
455 * So this can be called with p == NULL.
460 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
461 struct mem_cgroup, css);
464 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
466 struct mem_cgroup *mem = NULL;
471 * Because we have no locks, mm->owner's may be being moved to other
472 * cgroup. We use css_tryget() here even if this looks
473 * pessimistic (rather than adding locks here).
477 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
480 } while (!css_tryget(&mem->css));
486 * Call callback function against all cgroup under hierarchy tree.
488 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
489 int (*func)(struct mem_cgroup *, void *))
491 int found, ret, nextid;
492 struct cgroup_subsys_state *css;
493 struct mem_cgroup *mem;
495 if (!root->use_hierarchy)
496 return (*func)(root, data);
504 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
506 if (css && css_tryget(css))
507 mem = container_of(css, struct mem_cgroup, css);
511 ret = (*func)(mem, data);
515 } while (!ret && css);
520 static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
522 return (mem == root_mem_cgroup);
526 * Following LRU functions are allowed to be used without PCG_LOCK.
527 * Operations are called by routine of global LRU independently from memcg.
528 * What we have to take care of here is validness of pc->mem_cgroup.
530 * Changes to pc->mem_cgroup happens when
533 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
534 * It is added to LRU before charge.
535 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
536 * When moving account, the page is not on LRU. It's isolated.
539 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
541 struct page_cgroup *pc;
542 struct mem_cgroup_per_zone *mz;
544 if (mem_cgroup_disabled())
546 pc = lookup_page_cgroup(page);
547 /* can happen while we handle swapcache. */
548 if (!TestClearPageCgroupAcctLRU(pc))
550 VM_BUG_ON(!pc->mem_cgroup);
552 * We don't check PCG_USED bit. It's cleared when the "page" is finally
553 * removed from global LRU.
555 mz = page_cgroup_zoneinfo(pc);
556 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
557 if (mem_cgroup_is_root(pc->mem_cgroup))
559 VM_BUG_ON(list_empty(&pc->lru));
560 list_del_init(&pc->lru);
564 void mem_cgroup_del_lru(struct page *page)
566 mem_cgroup_del_lru_list(page, page_lru(page));
569 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
571 struct mem_cgroup_per_zone *mz;
572 struct page_cgroup *pc;
574 if (mem_cgroup_disabled())
577 pc = lookup_page_cgroup(page);
579 * Used bit is set without atomic ops but after smp_wmb().
580 * For making pc->mem_cgroup visible, insert smp_rmb() here.
583 /* unused or root page is not rotated. */
584 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
586 mz = page_cgroup_zoneinfo(pc);
587 list_move(&pc->lru, &mz->lists[lru]);
590 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
592 struct page_cgroup *pc;
593 struct mem_cgroup_per_zone *mz;
595 if (mem_cgroup_disabled())
597 pc = lookup_page_cgroup(page);
598 VM_BUG_ON(PageCgroupAcctLRU(pc));
600 * Used bit is set without atomic ops but after smp_wmb().
601 * For making pc->mem_cgroup visible, insert smp_rmb() here.
604 if (!PageCgroupUsed(pc))
607 mz = page_cgroup_zoneinfo(pc);
608 MEM_CGROUP_ZSTAT(mz, lru) += 1;
609 SetPageCgroupAcctLRU(pc);
610 if (mem_cgroup_is_root(pc->mem_cgroup))
612 list_add(&pc->lru, &mz->lists[lru]);
616 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
617 * lru because the page may.be reused after it's fully uncharged (because of
618 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
619 * it again. This function is only used to charge SwapCache. It's done under
620 * lock_page and expected that zone->lru_lock is never held.
622 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
625 struct zone *zone = page_zone(page);
626 struct page_cgroup *pc = lookup_page_cgroup(page);
628 spin_lock_irqsave(&zone->lru_lock, flags);
630 * Forget old LRU when this page_cgroup is *not* used. This Used bit
631 * is guarded by lock_page() because the page is SwapCache.
633 if (!PageCgroupUsed(pc))
634 mem_cgroup_del_lru_list(page, page_lru(page));
635 spin_unlock_irqrestore(&zone->lru_lock, flags);
638 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
641 struct zone *zone = page_zone(page);
642 struct page_cgroup *pc = lookup_page_cgroup(page);
644 spin_lock_irqsave(&zone->lru_lock, flags);
645 /* link when the page is linked to LRU but page_cgroup isn't */
646 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
647 mem_cgroup_add_lru_list(page, page_lru(page));
648 spin_unlock_irqrestore(&zone->lru_lock, flags);
652 void mem_cgroup_move_lists(struct page *page,
653 enum lru_list from, enum lru_list to)
655 if (mem_cgroup_disabled())
657 mem_cgroup_del_lru_list(page, from);
658 mem_cgroup_add_lru_list(page, to);
661 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
664 struct mem_cgroup *curr = NULL;
668 curr = try_get_mem_cgroup_from_mm(task->mm);
673 if (curr->use_hierarchy)
674 ret = css_is_ancestor(&curr->css, &mem->css);
682 * prev_priority control...this will be used in memory reclaim path.
684 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
688 spin_lock(&mem->reclaim_param_lock);
689 prev_priority = mem->prev_priority;
690 spin_unlock(&mem->reclaim_param_lock);
692 return prev_priority;
695 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
697 spin_lock(&mem->reclaim_param_lock);
698 if (priority < mem->prev_priority)
699 mem->prev_priority = priority;
700 spin_unlock(&mem->reclaim_param_lock);
703 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
705 spin_lock(&mem->reclaim_param_lock);
706 mem->prev_priority = priority;
707 spin_unlock(&mem->reclaim_param_lock);
710 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
712 unsigned long active;
713 unsigned long inactive;
715 unsigned long inactive_ratio;
717 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
718 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
720 gb = (inactive + active) >> (30 - PAGE_SHIFT);
722 inactive_ratio = int_sqrt(10 * gb);
727 present_pages[0] = inactive;
728 present_pages[1] = active;
731 return inactive_ratio;
734 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
736 unsigned long active;
737 unsigned long inactive;
738 unsigned long present_pages[2];
739 unsigned long inactive_ratio;
741 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
743 inactive = present_pages[0];
744 active = present_pages[1];
746 if (inactive * inactive_ratio < active)
752 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
754 unsigned long active;
755 unsigned long inactive;
757 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
758 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
760 return (active > inactive);
763 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
767 int nid = zone->zone_pgdat->node_id;
768 int zid = zone_idx(zone);
769 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
771 return MEM_CGROUP_ZSTAT(mz, lru);
774 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
777 int nid = zone->zone_pgdat->node_id;
778 int zid = zone_idx(zone);
779 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
781 return &mz->reclaim_stat;
784 struct zone_reclaim_stat *
785 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
787 struct page_cgroup *pc;
788 struct mem_cgroup_per_zone *mz;
790 if (mem_cgroup_disabled())
793 pc = lookup_page_cgroup(page);
795 * Used bit is set without atomic ops but after smp_wmb().
796 * For making pc->mem_cgroup visible, insert smp_rmb() here.
799 if (!PageCgroupUsed(pc))
802 mz = page_cgroup_zoneinfo(pc);
806 return &mz->reclaim_stat;
809 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
810 struct list_head *dst,
811 unsigned long *scanned, int order,
812 int mode, struct zone *z,
813 struct mem_cgroup *mem_cont,
814 int active, int file)
816 unsigned long nr_taken = 0;
820 struct list_head *src;
821 struct page_cgroup *pc, *tmp;
822 int nid = z->zone_pgdat->node_id;
823 int zid = zone_idx(z);
824 struct mem_cgroup_per_zone *mz;
825 int lru = LRU_FILE * file + active;
829 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
830 src = &mz->lists[lru];
833 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
834 if (scan >= nr_to_scan)
838 if (unlikely(!PageCgroupUsed(pc)))
840 if (unlikely(!PageLRU(page)))
844 ret = __isolate_lru_page(page, mode, file);
847 list_move(&page->lru, dst);
848 mem_cgroup_del_lru(page);
852 /* we don't affect global LRU but rotate in our LRU */
853 mem_cgroup_rotate_lru_list(page, page_lru(page));
864 #define mem_cgroup_from_res_counter(counter, member) \
865 container_of(counter, struct mem_cgroup, member)
867 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
869 if (do_swap_account) {
870 if (res_counter_check_under_limit(&mem->res) &&
871 res_counter_check_under_limit(&mem->memsw))
874 if (res_counter_check_under_limit(&mem->res))
879 static unsigned int get_swappiness(struct mem_cgroup *memcg)
881 struct cgroup *cgrp = memcg->css.cgroup;
882 unsigned int swappiness;
885 if (cgrp->parent == NULL)
886 return vm_swappiness;
888 spin_lock(&memcg->reclaim_param_lock);
889 swappiness = memcg->swappiness;
890 spin_unlock(&memcg->reclaim_param_lock);
895 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
903 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
904 * @memcg: The memory cgroup that went over limit
905 * @p: Task that is going to be killed
907 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
910 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
912 struct cgroup *task_cgrp;
913 struct cgroup *mem_cgrp;
915 * Need a buffer in BSS, can't rely on allocations. The code relies
916 * on the assumption that OOM is serialized for memory controller.
917 * If this assumption is broken, revisit this code.
919 static char memcg_name[PATH_MAX];
928 mem_cgrp = memcg->css.cgroup;
929 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
931 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
934 * Unfortunately, we are unable to convert to a useful name
935 * But we'll still print out the usage information
942 printk(KERN_INFO "Task in %s killed", memcg_name);
945 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
953 * Continues from above, so we don't need an KERN_ level
955 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
958 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
959 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
960 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
961 res_counter_read_u64(&memcg->res, RES_FAILCNT));
962 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
964 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
965 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
966 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
970 * This function returns the number of memcg under hierarchy tree. Returns
971 * 1(self count) if no children.
973 static int mem_cgroup_count_children(struct mem_cgroup *mem)
976 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
981 * Visit the first child (need not be the first child as per the ordering
982 * of the cgroup list, since we track last_scanned_child) of @mem and use
983 * that to reclaim free pages from.
985 static struct mem_cgroup *
986 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
988 struct mem_cgroup *ret = NULL;
989 struct cgroup_subsys_state *css;
992 if (!root_mem->use_hierarchy) {
993 css_get(&root_mem->css);
999 nextid = root_mem->last_scanned_child + 1;
1000 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
1002 if (css && css_tryget(css))
1003 ret = container_of(css, struct mem_cgroup, css);
1006 /* Updates scanning parameter */
1007 spin_lock(&root_mem->reclaim_param_lock);
1009 /* this means start scan from ID:1 */
1010 root_mem->last_scanned_child = 0;
1012 root_mem->last_scanned_child = found;
1013 spin_unlock(&root_mem->reclaim_param_lock);
1020 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1021 * we reclaimed from, so that we don't end up penalizing one child extensively
1022 * based on its position in the children list.
1024 * root_mem is the original ancestor that we've been reclaim from.
1026 * We give up and return to the caller when we visit root_mem twice.
1027 * (other groups can be removed while we're walking....)
1029 * If shrink==true, for avoiding to free too much, this returns immedieately.
1031 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
1032 gfp_t gfp_mask, bool noswap, bool shrink)
1034 struct mem_cgroup *victim;
1038 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1039 if (root_mem->memsw_is_minimum)
1043 victim = mem_cgroup_select_victim(root_mem);
1044 if (victim == root_mem)
1046 if (!mem_cgroup_local_usage(&victim->stat)) {
1047 /* this cgroup's local usage == 0 */
1048 css_put(&victim->css);
1051 /* we use swappiness of local cgroup */
1052 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
1053 get_swappiness(victim));
1054 css_put(&victim->css);
1056 * At shrinking usage, we can't check we should stop here or
1057 * reclaim more. It's depends on callers. last_scanned_child
1058 * will work enough for keeping fairness under tree.
1063 if (mem_cgroup_check_under_limit(root_mem))
1069 bool mem_cgroup_oom_called(struct task_struct *task)
1072 struct mem_cgroup *mem;
1073 struct mm_struct *mm;
1079 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1080 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
1086 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
1088 mem->last_oom_jiffies = jiffies;
1092 static void record_last_oom(struct mem_cgroup *mem)
1094 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
1098 * Currently used to update mapped file statistics, but the routine can be
1099 * generalized to update other statistics as well.
1101 void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
1103 struct mem_cgroup *mem;
1104 struct mem_cgroup_stat *stat;
1105 struct mem_cgroup_stat_cpu *cpustat;
1107 struct page_cgroup *pc;
1109 if (!page_is_file_cache(page))
1112 pc = lookup_page_cgroup(page);
1116 lock_page_cgroup(pc);
1117 mem = pc->mem_cgroup;
1121 if (!PageCgroupUsed(pc))
1125 * Preemption is already disabled, we don't need get_cpu()
1127 cpu = smp_processor_id();
1129 cpustat = &stat->cpustat[cpu];
1131 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
1133 unlock_page_cgroup(pc);
1137 * Unlike exported interface, "oom" parameter is added. if oom==true,
1138 * oom-killer can be invoked.
1140 static int __mem_cgroup_try_charge(struct mm_struct *mm,
1141 gfp_t gfp_mask, struct mem_cgroup **memcg,
1142 bool oom, struct page *page)
1144 struct mem_cgroup *mem, *mem_over_limit, *mem_over_soft_limit;
1145 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1146 struct res_counter *fail_res, *soft_fail_res = NULL;
1148 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
1149 /* Don't account this! */
1155 * We always charge the cgroup the mm_struct belongs to.
1156 * The mm_struct's mem_cgroup changes on task migration if the
1157 * thread group leader migrates. It's possible that mm is not
1158 * set, if so charge the init_mm (happens for pagecache usage).
1162 mem = try_get_mem_cgroup_from_mm(mm);
1170 VM_BUG_ON(css_is_removed(&mem->css));
1174 bool noswap = false;
1176 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res,
1179 if (!do_swap_account)
1181 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
1185 /* mem+swap counter fails */
1186 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
1188 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1191 /* mem counter fails */
1192 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1195 if (!(gfp_mask & __GFP_WAIT))
1198 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
1204 * try_to_free_mem_cgroup_pages() might not give us a full
1205 * picture of reclaim. Some pages are reclaimed and might be
1206 * moved to swap cache or just unmapped from the cgroup.
1207 * Check the limit again to see if the reclaim reduced the
1208 * current usage of the cgroup before giving up
1211 if (mem_cgroup_check_under_limit(mem_over_limit))
1214 if (!nr_retries--) {
1216 mutex_lock(&memcg_tasklist);
1217 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1218 mutex_unlock(&memcg_tasklist);
1219 record_last_oom(mem_over_limit);
1225 * Insert just the ancestor, we should trickle down to the correct
1226 * cgroup for reclaim, since the other nodes will be below their
1229 if (soft_fail_res) {
1230 mem_over_soft_limit =
1231 mem_cgroup_from_res_counter(soft_fail_res, res);
1232 if (mem_cgroup_soft_limit_check(mem_over_soft_limit))
1233 mem_cgroup_update_tree(mem_over_soft_limit, page);
1242 * A helper function to get mem_cgroup from ID. must be called under
1243 * rcu_read_lock(). The caller must check css_is_removed() or some if
1244 * it's concern. (dropping refcnt from swap can be called against removed
1247 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1249 struct cgroup_subsys_state *css;
1251 /* ID 0 is unused ID */
1254 css = css_lookup(&mem_cgroup_subsys, id);
1257 return container_of(css, struct mem_cgroup, css);
1260 static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1262 struct mem_cgroup *mem;
1263 struct page_cgroup *pc;
1267 VM_BUG_ON(!PageLocked(page));
1269 if (!PageSwapCache(page))
1272 pc = lookup_page_cgroup(page);
1273 lock_page_cgroup(pc);
1274 if (PageCgroupUsed(pc)) {
1275 mem = pc->mem_cgroup;
1276 if (mem && !css_tryget(&mem->css))
1279 ent.val = page_private(page);
1280 id = lookup_swap_cgroup(ent);
1282 mem = mem_cgroup_lookup(id);
1283 if (mem && !css_tryget(&mem->css))
1287 unlock_page_cgroup(pc);
1292 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1293 * USED state. If already USED, uncharge and return.
1296 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1297 struct page_cgroup *pc,
1298 enum charge_type ctype)
1300 /* try_charge() can return NULL to *memcg, taking care of it. */
1304 lock_page_cgroup(pc);
1305 if (unlikely(PageCgroupUsed(pc))) {
1306 unlock_page_cgroup(pc);
1307 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
1308 if (do_swap_account)
1309 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
1314 pc->mem_cgroup = mem;
1316 * We access a page_cgroup asynchronously without lock_page_cgroup().
1317 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1318 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1319 * before USED bit, we need memory barrier here.
1320 * See mem_cgroup_add_lru_list(), etc.
1324 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1325 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1326 SetPageCgroupCache(pc);
1327 SetPageCgroupUsed(pc);
1329 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1330 ClearPageCgroupCache(pc);
1331 SetPageCgroupUsed(pc);
1337 mem_cgroup_charge_statistics(mem, pc, true);
1339 unlock_page_cgroup(pc);
1343 * mem_cgroup_move_account - move account of the page
1344 * @pc: page_cgroup of the page.
1345 * @from: mem_cgroup which the page is moved from.
1346 * @to: mem_cgroup which the page is moved to. @from != @to.
1348 * The caller must confirm following.
1349 * - page is not on LRU (isolate_page() is useful.)
1351 * returns 0 at success,
1352 * returns -EBUSY when lock is busy or "pc" is unstable.
1354 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1355 * new cgroup. It should be done by a caller.
1358 static int mem_cgroup_move_account(struct page_cgroup *pc,
1359 struct mem_cgroup *from, struct mem_cgroup *to)
1361 struct mem_cgroup_per_zone *from_mz, *to_mz;
1366 struct mem_cgroup_stat *stat;
1367 struct mem_cgroup_stat_cpu *cpustat;
1369 VM_BUG_ON(from == to);
1370 VM_BUG_ON(PageLRU(pc->page));
1372 nid = page_cgroup_nid(pc);
1373 zid = page_cgroup_zid(pc);
1374 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1375 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1377 if (!trylock_page_cgroup(pc))
1380 if (!PageCgroupUsed(pc))
1383 if (pc->mem_cgroup != from)
1386 res_counter_uncharge(&from->res, PAGE_SIZE, NULL);
1387 mem_cgroup_charge_statistics(from, pc, false);
1390 if (page_is_file_cache(page) && page_mapped(page)) {
1391 cpu = smp_processor_id();
1392 /* Update mapped_file data for mem_cgroup "from" */
1394 cpustat = &stat->cpustat[cpu];
1395 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1398 /* Update mapped_file data for mem_cgroup "to" */
1400 cpustat = &stat->cpustat[cpu];
1401 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1405 if (do_swap_account)
1406 res_counter_uncharge(&from->memsw, PAGE_SIZE, NULL);
1407 css_put(&from->css);
1410 pc->mem_cgroup = to;
1411 mem_cgroup_charge_statistics(to, pc, true);
1414 unlock_page_cgroup(pc);
1416 * We charges against "to" which may not have any tasks. Then, "to"
1417 * can be under rmdir(). But in current implementation, caller of
1418 * this function is just force_empty() and it's garanteed that
1419 * "to" is never removed. So, we don't check rmdir status here.
1425 * move charges to its parent.
1428 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1429 struct mem_cgroup *child,
1432 struct page *page = pc->page;
1433 struct cgroup *cg = child->css.cgroup;
1434 struct cgroup *pcg = cg->parent;
1435 struct mem_cgroup *parent;
1443 parent = mem_cgroup_from_cont(pcg);
1446 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false, page);
1450 if (!get_page_unless_zero(page)) {
1455 ret = isolate_lru_page(page);
1460 ret = mem_cgroup_move_account(pc, child, parent);
1462 putback_lru_page(page);
1465 /* drop extra refcnt by try_charge() */
1466 css_put(&parent->css);
1473 /* drop extra refcnt by try_charge() */
1474 css_put(&parent->css);
1475 /* uncharge if move fails */
1476 res_counter_uncharge(&parent->res, PAGE_SIZE, NULL);
1477 if (do_swap_account)
1478 res_counter_uncharge(&parent->memsw, PAGE_SIZE, NULL);
1483 * Charge the memory controller for page usage.
1485 * 0 if the charge was successful
1486 * < 0 if the cgroup is over its limit
1488 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1489 gfp_t gfp_mask, enum charge_type ctype,
1490 struct mem_cgroup *memcg)
1492 struct mem_cgroup *mem;
1493 struct page_cgroup *pc;
1496 pc = lookup_page_cgroup(page);
1497 /* can happen at boot */
1503 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true, page);
1507 __mem_cgroup_commit_charge(mem, pc, ctype);
1511 int mem_cgroup_newpage_charge(struct page *page,
1512 struct mm_struct *mm, gfp_t gfp_mask)
1514 if (mem_cgroup_disabled())
1516 if (PageCompound(page))
1519 * If already mapped, we don't have to account.
1520 * If page cache, page->mapping has address_space.
1521 * But page->mapping may have out-of-use anon_vma pointer,
1522 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1525 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1529 return mem_cgroup_charge_common(page, mm, gfp_mask,
1530 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1534 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1535 enum charge_type ctype);
1537 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1540 struct mem_cgroup *mem = NULL;
1543 if (mem_cgroup_disabled())
1545 if (PageCompound(page))
1548 * Corner case handling. This is called from add_to_page_cache()
1549 * in usual. But some FS (shmem) precharges this page before calling it
1550 * and call add_to_page_cache() with GFP_NOWAIT.
1552 * For GFP_NOWAIT case, the page may be pre-charged before calling
1553 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1554 * charge twice. (It works but has to pay a bit larger cost.)
1555 * And when the page is SwapCache, it should take swap information
1556 * into account. This is under lock_page() now.
1558 if (!(gfp_mask & __GFP_WAIT)) {
1559 struct page_cgroup *pc;
1562 pc = lookup_page_cgroup(page);
1565 lock_page_cgroup(pc);
1566 if (PageCgroupUsed(pc)) {
1567 unlock_page_cgroup(pc);
1570 unlock_page_cgroup(pc);
1573 if (unlikely(!mm && !mem))
1576 if (page_is_file_cache(page))
1577 return mem_cgroup_charge_common(page, mm, gfp_mask,
1578 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1581 if (PageSwapCache(page)) {
1582 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1584 __mem_cgroup_commit_charge_swapin(page, mem,
1585 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1587 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1588 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1594 * While swap-in, try_charge -> commit or cancel, the page is locked.
1595 * And when try_charge() successfully returns, one refcnt to memcg without
1596 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1597 * "commit()" or removed by "cancel()"
1599 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1601 gfp_t mask, struct mem_cgroup **ptr)
1603 struct mem_cgroup *mem;
1606 if (mem_cgroup_disabled())
1609 if (!do_swap_account)
1612 * A racing thread's fault, or swapoff, may have already updated
1613 * the pte, and even removed page from swap cache: return success
1614 * to go on to do_swap_page()'s pte_same() test, which should fail.
1616 if (!PageSwapCache(page))
1618 mem = try_get_mem_cgroup_from_swapcache(page);
1622 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true, page);
1623 /* drop extra refcnt from tryget */
1629 return __mem_cgroup_try_charge(mm, mask, ptr, true, page);
1633 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1634 enum charge_type ctype)
1636 struct page_cgroup *pc;
1638 if (mem_cgroup_disabled())
1642 cgroup_exclude_rmdir(&ptr->css);
1643 pc = lookup_page_cgroup(page);
1644 mem_cgroup_lru_del_before_commit_swapcache(page);
1645 __mem_cgroup_commit_charge(ptr, pc, ctype);
1646 mem_cgroup_lru_add_after_commit_swapcache(page);
1648 * Now swap is on-memory. This means this page may be
1649 * counted both as mem and swap....double count.
1650 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1651 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1652 * may call delete_from_swap_cache() before reach here.
1654 if (do_swap_account && PageSwapCache(page)) {
1655 swp_entry_t ent = {.val = page_private(page)};
1657 struct mem_cgroup *memcg;
1659 id = swap_cgroup_record(ent, 0);
1661 memcg = mem_cgroup_lookup(id);
1664 * This recorded memcg can be obsolete one. So, avoid
1665 * calling css_tryget
1667 res_counter_uncharge(&memcg->memsw, PAGE_SIZE, NULL);
1668 mem_cgroup_put(memcg);
1673 * At swapin, we may charge account against cgroup which has no tasks.
1674 * So, rmdir()->pre_destroy() can be called while we do this charge.
1675 * In that case, we need to call pre_destroy() again. check it here.
1677 cgroup_release_and_wakeup_rmdir(&ptr->css);
1680 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1682 __mem_cgroup_commit_charge_swapin(page, ptr,
1683 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1686 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1688 if (mem_cgroup_disabled())
1692 res_counter_uncharge(&mem->res, PAGE_SIZE, NULL);
1693 if (do_swap_account)
1694 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
1700 * uncharge if !page_mapped(page)
1702 static struct mem_cgroup *
1703 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1705 struct page_cgroup *pc;
1706 struct mem_cgroup *mem = NULL;
1707 struct mem_cgroup_per_zone *mz;
1708 bool soft_limit_excess = false;
1710 if (mem_cgroup_disabled())
1713 if (PageSwapCache(page))
1717 * Check if our page_cgroup is valid
1719 pc = lookup_page_cgroup(page);
1720 if (unlikely(!pc || !PageCgroupUsed(pc)))
1723 lock_page_cgroup(pc);
1725 mem = pc->mem_cgroup;
1727 if (!PageCgroupUsed(pc))
1731 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1732 case MEM_CGROUP_CHARGE_TYPE_DROP:
1733 if (page_mapped(page))
1736 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1737 if (!PageAnon(page)) { /* Shared memory */
1738 if (page->mapping && !page_is_file_cache(page))
1740 } else if (page_mapped(page)) /* Anon */
1747 res_counter_uncharge(&mem->res, PAGE_SIZE, &soft_limit_excess);
1748 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1749 res_counter_uncharge(&mem->memsw, PAGE_SIZE, NULL);
1750 mem_cgroup_charge_statistics(mem, pc, false);
1752 ClearPageCgroupUsed(pc);
1754 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1755 * freed from LRU. This is safe because uncharged page is expected not
1756 * to be reused (freed soon). Exception is SwapCache, it's handled by
1757 * special functions.
1760 mz = page_cgroup_zoneinfo(pc);
1761 unlock_page_cgroup(pc);
1763 if (soft_limit_excess && mem_cgroup_soft_limit_check(mem))
1764 mem_cgroup_update_tree(mem, page);
1765 /* at swapout, this memcg will be accessed to record to swap */
1766 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1772 unlock_page_cgroup(pc);
1776 void mem_cgroup_uncharge_page(struct page *page)
1779 if (page_mapped(page))
1781 if (page->mapping && !PageAnon(page))
1783 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1786 void mem_cgroup_uncharge_cache_page(struct page *page)
1788 VM_BUG_ON(page_mapped(page));
1789 VM_BUG_ON(page->mapping);
1790 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1795 * called after __delete_from_swap_cache() and drop "page" account.
1796 * memcg information is recorded to swap_cgroup of "ent"
1799 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
1801 struct mem_cgroup *memcg;
1802 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1804 if (!swapout) /* this was a swap cache but the swap is unused ! */
1805 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1807 memcg = __mem_cgroup_uncharge_common(page, ctype);
1809 /* record memcg information */
1810 if (do_swap_account && swapout && memcg) {
1811 swap_cgroup_record(ent, css_id(&memcg->css));
1812 mem_cgroup_get(memcg);
1814 if (swapout && memcg)
1815 css_put(&memcg->css);
1819 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1821 * called from swap_entry_free(). remove record in swap_cgroup and
1822 * uncharge "memsw" account.
1824 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1826 struct mem_cgroup *memcg;
1829 if (!do_swap_account)
1832 id = swap_cgroup_record(ent, 0);
1834 memcg = mem_cgroup_lookup(id);
1837 * We uncharge this because swap is freed.
1838 * This memcg can be obsolete one. We avoid calling css_tryget
1840 res_counter_uncharge(&memcg->memsw, PAGE_SIZE, NULL);
1841 mem_cgroup_put(memcg);
1848 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1851 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1853 struct page_cgroup *pc;
1854 struct mem_cgroup *mem = NULL;
1857 if (mem_cgroup_disabled())
1860 pc = lookup_page_cgroup(page);
1861 lock_page_cgroup(pc);
1862 if (PageCgroupUsed(pc)) {
1863 mem = pc->mem_cgroup;
1866 unlock_page_cgroup(pc);
1869 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false,
1877 /* remove redundant charge if migration failed*/
1878 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1879 struct page *oldpage, struct page *newpage)
1881 struct page *target, *unused;
1882 struct page_cgroup *pc;
1883 enum charge_type ctype;
1887 cgroup_exclude_rmdir(&mem->css);
1888 /* at migration success, oldpage->mapping is NULL. */
1889 if (oldpage->mapping) {
1897 if (PageAnon(target))
1898 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1899 else if (page_is_file_cache(target))
1900 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1902 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1904 /* unused page is not on radix-tree now. */
1906 __mem_cgroup_uncharge_common(unused, ctype);
1908 pc = lookup_page_cgroup(target);
1910 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1911 * So, double-counting is effectively avoided.
1913 __mem_cgroup_commit_charge(mem, pc, ctype);
1916 * Both of oldpage and newpage are still under lock_page().
1917 * Then, we don't have to care about race in radix-tree.
1918 * But we have to be careful that this page is unmapped or not.
1920 * There is a case for !page_mapped(). At the start of
1921 * migration, oldpage was mapped. But now, it's zapped.
1922 * But we know *target* page is not freed/reused under us.
1923 * mem_cgroup_uncharge_page() does all necessary checks.
1925 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1926 mem_cgroup_uncharge_page(target);
1928 * At migration, we may charge account against cgroup which has no tasks
1929 * So, rmdir()->pre_destroy() can be called while we do this charge.
1930 * In that case, we need to call pre_destroy() again. check it here.
1932 cgroup_release_and_wakeup_rmdir(&mem->css);
1936 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1937 * Calling hierarchical_reclaim is not enough because we should update
1938 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1939 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1940 * not from the memcg which this page would be charged to.
1941 * try_charge_swapin does all of these works properly.
1943 int mem_cgroup_shmem_charge_fallback(struct page *page,
1944 struct mm_struct *mm,
1947 struct mem_cgroup *mem = NULL;
1950 if (mem_cgroup_disabled())
1953 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1955 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
1960 static DEFINE_MUTEX(set_limit_mutex);
1962 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1963 unsigned long long val)
1969 int children = mem_cgroup_count_children(memcg);
1970 u64 curusage, oldusage;
1973 * For keeping hierarchical_reclaim simple, how long we should retry
1974 * is depends on callers. We set our retry-count to be function
1975 * of # of children which we should visit in this loop.
1977 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1979 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1981 while (retry_count) {
1982 if (signal_pending(current)) {
1987 * Rather than hide all in some function, I do this in
1988 * open coded manner. You see what this really does.
1989 * We have to guarantee mem->res.limit < mem->memsw.limit.
1991 mutex_lock(&set_limit_mutex);
1992 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1993 if (memswlimit < val) {
1995 mutex_unlock(&set_limit_mutex);
1998 ret = res_counter_set_limit(&memcg->res, val);
2000 if (memswlimit == val)
2001 memcg->memsw_is_minimum = true;
2003 memcg->memsw_is_minimum = false;
2005 mutex_unlock(&set_limit_mutex);
2010 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
2012 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
2013 /* Usage is reduced ? */
2014 if (curusage >= oldusage)
2017 oldusage = curusage;
2023 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
2024 unsigned long long val)
2027 u64 memlimit, oldusage, curusage;
2028 int children = mem_cgroup_count_children(memcg);
2031 /* see mem_cgroup_resize_res_limit */
2032 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
2033 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2034 while (retry_count) {
2035 if (signal_pending(current)) {
2040 * Rather than hide all in some function, I do this in
2041 * open coded manner. You see what this really does.
2042 * We have to guarantee mem->res.limit < mem->memsw.limit.
2044 mutex_lock(&set_limit_mutex);
2045 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2046 if (memlimit > val) {
2048 mutex_unlock(&set_limit_mutex);
2051 ret = res_counter_set_limit(&memcg->memsw, val);
2053 if (memlimit == val)
2054 memcg->memsw_is_minimum = true;
2056 memcg->memsw_is_minimum = false;
2058 mutex_unlock(&set_limit_mutex);
2063 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
2064 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
2065 /* Usage is reduced ? */
2066 if (curusage >= oldusage)
2069 oldusage = curusage;
2075 * This routine traverse page_cgroup in given list and drop them all.
2076 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2078 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
2079 int node, int zid, enum lru_list lru)
2082 struct mem_cgroup_per_zone *mz;
2083 struct page_cgroup *pc, *busy;
2084 unsigned long flags, loop;
2085 struct list_head *list;
2088 zone = &NODE_DATA(node)->node_zones[zid];
2089 mz = mem_cgroup_zoneinfo(mem, node, zid);
2090 list = &mz->lists[lru];
2092 loop = MEM_CGROUP_ZSTAT(mz, lru);
2093 /* give some margin against EBUSY etc...*/
2098 spin_lock_irqsave(&zone->lru_lock, flags);
2099 if (list_empty(list)) {
2100 spin_unlock_irqrestore(&zone->lru_lock, flags);
2103 pc = list_entry(list->prev, struct page_cgroup, lru);
2105 list_move(&pc->lru, list);
2107 spin_unlock_irqrestore(&zone->lru_lock, flags);
2110 spin_unlock_irqrestore(&zone->lru_lock, flags);
2112 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
2116 if (ret == -EBUSY || ret == -EINVAL) {
2117 /* found lock contention or "pc" is obsolete. */
2124 if (!ret && !list_empty(list))
2130 * make mem_cgroup's charge to be 0 if there is no task.
2131 * This enables deleting this mem_cgroup.
2133 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
2136 int node, zid, shrink;
2137 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
2138 struct cgroup *cgrp = mem->css.cgroup;
2143 /* should free all ? */
2147 while (mem->res.usage > 0) {
2149 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
2152 if (signal_pending(current))
2154 /* This is for making all *used* pages to be on LRU. */
2155 lru_add_drain_all();
2157 for_each_node_state(node, N_HIGH_MEMORY) {
2158 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
2161 ret = mem_cgroup_force_empty_list(mem,
2170 /* it seems parent cgroup doesn't have enough mem */
2181 /* returns EBUSY if there is a task or if we come here twice. */
2182 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
2186 /* we call try-to-free pages for make this cgroup empty */
2187 lru_add_drain_all();
2188 /* try to free all pages in this cgroup */
2190 while (nr_retries && mem->res.usage > 0) {
2193 if (signal_pending(current)) {
2197 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2198 false, get_swappiness(mem));
2201 /* maybe some writeback is necessary */
2202 congestion_wait(BLK_RW_ASYNC, HZ/10);
2207 /* try move_account...there may be some *locked* pages. */
2214 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2216 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2220 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2222 return mem_cgroup_from_cont(cont)->use_hierarchy;
2225 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2229 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2230 struct cgroup *parent = cont->parent;
2231 struct mem_cgroup *parent_mem = NULL;
2234 parent_mem = mem_cgroup_from_cont(parent);
2238 * If parent's use_hiearchy is set, we can't make any modifications
2239 * in the child subtrees. If it is unset, then the change can
2240 * occur, provided the current cgroup has no children.
2242 * For the root cgroup, parent_mem is NULL, we allow value to be
2243 * set if there are no children.
2245 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2246 (val == 1 || val == 0)) {
2247 if (list_empty(&cont->children))
2248 mem->use_hierarchy = val;
2258 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2260 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2264 type = MEMFILE_TYPE(cft->private);
2265 name = MEMFILE_ATTR(cft->private);
2268 val = res_counter_read_u64(&mem->res, name);
2271 val = res_counter_read_u64(&mem->memsw, name);
2280 * The user of this function is...
2283 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2286 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2288 unsigned long long val;
2291 type = MEMFILE_TYPE(cft->private);
2292 name = MEMFILE_ATTR(cft->private);
2295 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2299 /* This function does all necessary parse...reuse it */
2300 ret = res_counter_memparse_write_strategy(buffer, &val);
2304 ret = mem_cgroup_resize_limit(memcg, val);
2306 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2308 case RES_SOFT_LIMIT:
2309 ret = res_counter_memparse_write_strategy(buffer, &val);
2313 * For memsw, soft limits are hard to implement in terms
2314 * of semantics, for now, we support soft limits for
2315 * control without swap
2318 ret = res_counter_set_soft_limit(&memcg->res, val);
2323 ret = -EINVAL; /* should be BUG() ? */
2329 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2330 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2332 struct cgroup *cgroup;
2333 unsigned long long min_limit, min_memsw_limit, tmp;
2335 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2336 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2337 cgroup = memcg->css.cgroup;
2338 if (!memcg->use_hierarchy)
2341 while (cgroup->parent) {
2342 cgroup = cgroup->parent;
2343 memcg = mem_cgroup_from_cont(cgroup);
2344 if (!memcg->use_hierarchy)
2346 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2347 min_limit = min(min_limit, tmp);
2348 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2349 min_memsw_limit = min(min_memsw_limit, tmp);
2352 *mem_limit = min_limit;
2353 *memsw_limit = min_memsw_limit;
2357 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2359 struct mem_cgroup *mem;
2362 mem = mem_cgroup_from_cont(cont);
2363 type = MEMFILE_TYPE(event);
2364 name = MEMFILE_ATTR(event);
2368 res_counter_reset_max(&mem->res);
2370 res_counter_reset_max(&mem->memsw);
2374 res_counter_reset_failcnt(&mem->res);
2376 res_counter_reset_failcnt(&mem->memsw);
2384 /* For read statistics */
2399 struct mcs_total_stat {
2400 s64 stat[NR_MCS_STAT];
2406 } memcg_stat_strings[NR_MCS_STAT] = {
2407 {"cache", "total_cache"},
2408 {"rss", "total_rss"},
2409 {"mapped_file", "total_mapped_file"},
2410 {"pgpgin", "total_pgpgin"},
2411 {"pgpgout", "total_pgpgout"},
2412 {"inactive_anon", "total_inactive_anon"},
2413 {"active_anon", "total_active_anon"},
2414 {"inactive_file", "total_inactive_file"},
2415 {"active_file", "total_active_file"},
2416 {"unevictable", "total_unevictable"}
2420 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2422 struct mcs_total_stat *s = data;
2426 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2427 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2428 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2429 s->stat[MCS_RSS] += val * PAGE_SIZE;
2430 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2431 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
2432 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2433 s->stat[MCS_PGPGIN] += val;
2434 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2435 s->stat[MCS_PGPGOUT] += val;
2438 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2439 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2440 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2441 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2442 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2443 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2444 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2445 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2446 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2447 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2452 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2454 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2457 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2458 struct cgroup_map_cb *cb)
2460 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2461 struct mcs_total_stat mystat;
2464 memset(&mystat, 0, sizeof(mystat));
2465 mem_cgroup_get_local_stat(mem_cont, &mystat);
2467 for (i = 0; i < NR_MCS_STAT; i++)
2468 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2470 /* Hierarchical information */
2472 unsigned long long limit, memsw_limit;
2473 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2474 cb->fill(cb, "hierarchical_memory_limit", limit);
2475 if (do_swap_account)
2476 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2479 memset(&mystat, 0, sizeof(mystat));
2480 mem_cgroup_get_total_stat(mem_cont, &mystat);
2481 for (i = 0; i < NR_MCS_STAT; i++)
2482 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2485 #ifdef CONFIG_DEBUG_VM
2486 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2490 struct mem_cgroup_per_zone *mz;
2491 unsigned long recent_rotated[2] = {0, 0};
2492 unsigned long recent_scanned[2] = {0, 0};
2494 for_each_online_node(nid)
2495 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2496 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2498 recent_rotated[0] +=
2499 mz->reclaim_stat.recent_rotated[0];
2500 recent_rotated[1] +=
2501 mz->reclaim_stat.recent_rotated[1];
2502 recent_scanned[0] +=
2503 mz->reclaim_stat.recent_scanned[0];
2504 recent_scanned[1] +=
2505 mz->reclaim_stat.recent_scanned[1];
2507 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2508 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2509 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2510 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2517 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2519 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2521 return get_swappiness(memcg);
2524 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2527 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2528 struct mem_cgroup *parent;
2533 if (cgrp->parent == NULL)
2536 parent = mem_cgroup_from_cont(cgrp->parent);
2540 /* If under hierarchy, only empty-root can set this value */
2541 if ((parent->use_hierarchy) ||
2542 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2547 spin_lock(&memcg->reclaim_param_lock);
2548 memcg->swappiness = val;
2549 spin_unlock(&memcg->reclaim_param_lock);
2557 static struct cftype mem_cgroup_files[] = {
2559 .name = "usage_in_bytes",
2560 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2561 .read_u64 = mem_cgroup_read,
2564 .name = "max_usage_in_bytes",
2565 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2566 .trigger = mem_cgroup_reset,
2567 .read_u64 = mem_cgroup_read,
2570 .name = "limit_in_bytes",
2571 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2572 .write_string = mem_cgroup_write,
2573 .read_u64 = mem_cgroup_read,
2576 .name = "soft_limit_in_bytes",
2577 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2578 .write_string = mem_cgroup_write,
2579 .read_u64 = mem_cgroup_read,
2583 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2584 .trigger = mem_cgroup_reset,
2585 .read_u64 = mem_cgroup_read,
2589 .read_map = mem_control_stat_show,
2592 .name = "force_empty",
2593 .trigger = mem_cgroup_force_empty_write,
2596 .name = "use_hierarchy",
2597 .write_u64 = mem_cgroup_hierarchy_write,
2598 .read_u64 = mem_cgroup_hierarchy_read,
2601 .name = "swappiness",
2602 .read_u64 = mem_cgroup_swappiness_read,
2603 .write_u64 = mem_cgroup_swappiness_write,
2607 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2608 static struct cftype memsw_cgroup_files[] = {
2610 .name = "memsw.usage_in_bytes",
2611 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2612 .read_u64 = mem_cgroup_read,
2615 .name = "memsw.max_usage_in_bytes",
2616 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2617 .trigger = mem_cgroup_reset,
2618 .read_u64 = mem_cgroup_read,
2621 .name = "memsw.limit_in_bytes",
2622 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2623 .write_string = mem_cgroup_write,
2624 .read_u64 = mem_cgroup_read,
2627 .name = "memsw.failcnt",
2628 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2629 .trigger = mem_cgroup_reset,
2630 .read_u64 = mem_cgroup_read,
2634 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2636 if (!do_swap_account)
2638 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2639 ARRAY_SIZE(memsw_cgroup_files));
2642 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2648 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2650 struct mem_cgroup_per_node *pn;
2651 struct mem_cgroup_per_zone *mz;
2653 int zone, tmp = node;
2655 * This routine is called against possible nodes.
2656 * But it's BUG to call kmalloc() against offline node.
2658 * TODO: this routine can waste much memory for nodes which will
2659 * never be onlined. It's better to use memory hotplug callback
2662 if (!node_state(node, N_NORMAL_MEMORY))
2664 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
2668 mem->info.nodeinfo[node] = pn;
2669 memset(pn, 0, sizeof(*pn));
2671 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2672 mz = &pn->zoneinfo[zone];
2674 INIT_LIST_HEAD(&mz->lists[l]);
2675 mz->usage_in_excess = 0;
2680 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2682 kfree(mem->info.nodeinfo[node]);
2685 static int mem_cgroup_size(void)
2687 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2688 return sizeof(struct mem_cgroup) + cpustat_size;
2691 static struct mem_cgroup *mem_cgroup_alloc(void)
2693 struct mem_cgroup *mem;
2694 int size = mem_cgroup_size();
2696 if (size < PAGE_SIZE)
2697 mem = kmalloc(size, GFP_KERNEL);
2699 mem = vmalloc(size);
2702 memset(mem, 0, size);
2707 * At destroying mem_cgroup, references from swap_cgroup can remain.
2708 * (scanning all at force_empty is too costly...)
2710 * Instead of clearing all references at force_empty, we remember
2711 * the number of reference from swap_cgroup and free mem_cgroup when
2712 * it goes down to 0.
2714 * Removal of cgroup itself succeeds regardless of refs from swap.
2717 static void __mem_cgroup_free(struct mem_cgroup *mem)
2721 mem_cgroup_remove_from_trees(mem);
2722 free_css_id(&mem_cgroup_subsys, &mem->css);
2724 for_each_node_state(node, N_POSSIBLE)
2725 free_mem_cgroup_per_zone_info(mem, node);
2727 if (mem_cgroup_size() < PAGE_SIZE)
2733 static void mem_cgroup_get(struct mem_cgroup *mem)
2735 atomic_inc(&mem->refcnt);
2738 static void mem_cgroup_put(struct mem_cgroup *mem)
2740 if (atomic_dec_and_test(&mem->refcnt)) {
2741 struct mem_cgroup *parent = parent_mem_cgroup(mem);
2742 __mem_cgroup_free(mem);
2744 mem_cgroup_put(parent);
2749 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2751 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2753 if (!mem->res.parent)
2755 return mem_cgroup_from_res_counter(mem->res.parent, res);
2758 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2759 static void __init enable_swap_cgroup(void)
2761 if (!mem_cgroup_disabled() && really_do_swap_account)
2762 do_swap_account = 1;
2765 static void __init enable_swap_cgroup(void)
2770 static int mem_cgroup_soft_limit_tree_init(void)
2772 struct mem_cgroup_tree_per_node *rtpn;
2773 struct mem_cgroup_tree_per_zone *rtpz;
2774 int tmp, node, zone;
2776 for_each_node_state(node, N_POSSIBLE) {
2778 if (!node_state(node, N_NORMAL_MEMORY))
2780 rtpn = kzalloc_node(sizeof(*rtpn), GFP_KERNEL, tmp);
2784 soft_limit_tree.rb_tree_per_node[node] = rtpn;
2786 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2787 rtpz = &rtpn->rb_tree_per_zone[zone];
2788 rtpz->rb_root = RB_ROOT;
2789 spin_lock_init(&rtpz->lock);
2795 static struct cgroup_subsys_state * __ref
2796 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2798 struct mem_cgroup *mem, *parent;
2799 long error = -ENOMEM;
2802 mem = mem_cgroup_alloc();
2804 return ERR_PTR(error);
2806 for_each_node_state(node, N_POSSIBLE)
2807 if (alloc_mem_cgroup_per_zone_info(mem, node))
2811 if (cont->parent == NULL) {
2812 enable_swap_cgroup();
2814 root_mem_cgroup = mem;
2815 if (mem_cgroup_soft_limit_tree_init())
2819 parent = mem_cgroup_from_cont(cont->parent);
2820 mem->use_hierarchy = parent->use_hierarchy;
2823 if (parent && parent->use_hierarchy) {
2824 res_counter_init(&mem->res, &parent->res);
2825 res_counter_init(&mem->memsw, &parent->memsw);
2827 * We increment refcnt of the parent to ensure that we can
2828 * safely access it on res_counter_charge/uncharge.
2829 * This refcnt will be decremented when freeing this
2830 * mem_cgroup(see mem_cgroup_put).
2832 mem_cgroup_get(parent);
2834 res_counter_init(&mem->res, NULL);
2835 res_counter_init(&mem->memsw, NULL);
2837 mem->last_scanned_child = 0;
2838 spin_lock_init(&mem->reclaim_param_lock);
2841 mem->swappiness = get_swappiness(parent);
2842 atomic_set(&mem->refcnt, 1);
2845 __mem_cgroup_free(mem);
2846 root_mem_cgroup = NULL;
2847 return ERR_PTR(error);
2850 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
2851 struct cgroup *cont)
2853 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2855 return mem_cgroup_force_empty(mem, false);
2858 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2859 struct cgroup *cont)
2861 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2863 mem_cgroup_put(mem);
2866 static int mem_cgroup_populate(struct cgroup_subsys *ss,
2867 struct cgroup *cont)
2871 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2872 ARRAY_SIZE(mem_cgroup_files));
2875 ret = register_memsw_files(cont, ss);
2879 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2880 struct cgroup *cont,
2881 struct cgroup *old_cont,
2882 struct task_struct *p,
2885 mutex_lock(&memcg_tasklist);
2887 * FIXME: It's better to move charges of this process from old
2888 * memcg to new memcg. But it's just on TODO-List now.
2890 mutex_unlock(&memcg_tasklist);
2893 struct cgroup_subsys mem_cgroup_subsys = {
2895 .subsys_id = mem_cgroup_subsys_id,
2896 .create = mem_cgroup_create,
2897 .pre_destroy = mem_cgroup_pre_destroy,
2898 .destroy = mem_cgroup_destroy,
2899 .populate = mem_cgroup_populate,
2900 .attach = mem_cgroup_move_task,
2905 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2907 static int __init disable_swap_account(char *s)
2909 really_do_swap_account = 0;
2912 __setup("noswapaccount", disable_swap_account);