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
46 struct mem_cgroup *root_mem_cgroup __read_mostly;
48 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
49 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
50 int do_swap_account __read_mostly;
51 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
53 #define do_swap_account (0)
56 static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
59 * Statistics for memory cgroup.
61 enum mem_cgroup_stat_index {
63 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
65 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
66 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
67 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
68 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
69 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
71 MEM_CGROUP_STAT_NSTATS,
74 struct mem_cgroup_stat_cpu {
75 s64 count[MEM_CGROUP_STAT_NSTATS];
76 } ____cacheline_aligned_in_smp;
78 struct mem_cgroup_stat {
79 struct mem_cgroup_stat_cpu cpustat[0];
83 * For accounting under irq disable, no need for increment preempt count.
85 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
86 enum mem_cgroup_stat_index idx, int val)
88 stat->count[idx] += val;
91 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
92 enum mem_cgroup_stat_index idx)
96 for_each_possible_cpu(cpu)
97 ret += stat->cpustat[cpu].count[idx];
101 static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
105 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
106 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
111 * per-zone information in memory controller.
113 struct mem_cgroup_per_zone {
115 * spin_lock to protect the per cgroup LRU
117 struct list_head lists[NR_LRU_LISTS];
118 unsigned long count[NR_LRU_LISTS];
120 struct zone_reclaim_stat reclaim_stat;
122 /* Macro for accessing counter */
123 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
125 struct mem_cgroup_per_node {
126 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
129 struct mem_cgroup_lru_info {
130 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
134 * The memory controller data structure. The memory controller controls both
135 * page cache and RSS per cgroup. We would eventually like to provide
136 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
137 * to help the administrator determine what knobs to tune.
139 * TODO: Add a water mark for the memory controller. Reclaim will begin when
140 * we hit the water mark. May be even add a low water mark, such that
141 * no reclaim occurs from a cgroup at it's low water mark, this is
142 * a feature that will be implemented much later in the future.
145 struct cgroup_subsys_state css;
147 * the counter to account for memory usage
149 struct res_counter res;
151 * the counter to account for mem+swap usage.
153 struct res_counter memsw;
155 * Per cgroup active and inactive list, similar to the
156 * per zone LRU lists.
158 struct mem_cgroup_lru_info info;
161 protect against reclaim related member.
163 spinlock_t reclaim_param_lock;
165 int prev_priority; /* for recording reclaim priority */
168 * While reclaiming in a hiearchy, we cache the last child we
171 int last_scanned_child;
173 * Should the accounting and control be hierarchical, per subtree?
176 unsigned long last_oom_jiffies;
179 unsigned int swappiness;
181 /* set when res.limit == memsw.limit */
182 bool memsw_is_minimum;
185 * statistics. This must be placed at the end of memcg.
187 struct mem_cgroup_stat stat;
191 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
192 MEM_CGROUP_CHARGE_TYPE_MAPPED,
193 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
194 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
195 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
196 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
200 /* only for here (for easy reading.) */
201 #define PCGF_CACHE (1UL << PCG_CACHE)
202 #define PCGF_USED (1UL << PCG_USED)
203 #define PCGF_LOCK (1UL << PCG_LOCK)
204 /* Not used, but added here for completeness */
205 #define PCGF_ACCT (1UL << PCG_ACCT)
207 /* for encoding cft->private value on file */
210 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
211 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
212 #define MEMFILE_ATTR(val) ((val) & 0xffff)
214 static void mem_cgroup_get(struct mem_cgroup *mem);
215 static void mem_cgroup_put(struct mem_cgroup *mem);
216 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
218 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
219 struct page_cgroup *pc,
222 int val = (charge)? 1 : -1;
223 struct mem_cgroup_stat *stat = &mem->stat;
224 struct mem_cgroup_stat_cpu *cpustat;
227 cpustat = &stat->cpustat[cpu];
228 if (PageCgroupCache(pc))
229 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
231 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
234 __mem_cgroup_stat_add_safe(cpustat,
235 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
237 __mem_cgroup_stat_add_safe(cpustat,
238 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
242 static struct mem_cgroup_per_zone *
243 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
245 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
248 static struct mem_cgroup_per_zone *
249 page_cgroup_zoneinfo(struct page_cgroup *pc)
251 struct mem_cgroup *mem = pc->mem_cgroup;
252 int nid = page_cgroup_nid(pc);
253 int zid = page_cgroup_zid(pc);
258 return mem_cgroup_zoneinfo(mem, nid, zid);
261 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
265 struct mem_cgroup_per_zone *mz;
268 for_each_online_node(nid)
269 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
270 mz = mem_cgroup_zoneinfo(mem, nid, zid);
271 total += MEM_CGROUP_ZSTAT(mz, idx);
276 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
278 return container_of(cgroup_subsys_state(cont,
279 mem_cgroup_subsys_id), struct mem_cgroup,
283 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
286 * mm_update_next_owner() may clear mm->owner to NULL
287 * if it races with swapoff, page migration, etc.
288 * So this can be called with p == NULL.
293 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
294 struct mem_cgroup, css);
297 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
299 struct mem_cgroup *mem = NULL;
304 * Because we have no locks, mm->owner's may be being moved to other
305 * cgroup. We use css_tryget() here even if this looks
306 * pessimistic (rather than adding locks here).
310 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
313 } while (!css_tryget(&mem->css));
319 * Call callback function against all cgroup under hierarchy tree.
321 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
322 int (*func)(struct mem_cgroup *, void *))
324 int found, ret, nextid;
325 struct cgroup_subsys_state *css;
326 struct mem_cgroup *mem;
328 if (!root->use_hierarchy)
329 return (*func)(root, data);
337 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
339 if (css && css_tryget(css))
340 mem = container_of(css, struct mem_cgroup, css);
344 ret = (*func)(mem, data);
348 } while (!ret && css);
353 static inline bool mem_cgroup_is_root(struct mem_cgroup *mem)
355 return (mem == root_mem_cgroup);
359 * Following LRU functions are allowed to be used without PCG_LOCK.
360 * Operations are called by routine of global LRU independently from memcg.
361 * What we have to take care of here is validness of pc->mem_cgroup.
363 * Changes to pc->mem_cgroup happens when
366 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
367 * It is added to LRU before charge.
368 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
369 * When moving account, the page is not on LRU. It's isolated.
372 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
374 struct page_cgroup *pc;
375 struct mem_cgroup_per_zone *mz;
377 if (mem_cgroup_disabled())
379 pc = lookup_page_cgroup(page);
380 /* can happen while we handle swapcache. */
381 if (!TestClearPageCgroupAcctLRU(pc))
383 VM_BUG_ON(!pc->mem_cgroup);
385 * We don't check PCG_USED bit. It's cleared when the "page" is finally
386 * removed from global LRU.
388 mz = page_cgroup_zoneinfo(pc);
389 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
390 if (mem_cgroup_is_root(pc->mem_cgroup))
392 VM_BUG_ON(list_empty(&pc->lru));
393 list_del_init(&pc->lru);
397 void mem_cgroup_del_lru(struct page *page)
399 mem_cgroup_del_lru_list(page, page_lru(page));
402 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
404 struct mem_cgroup_per_zone *mz;
405 struct page_cgroup *pc;
407 if (mem_cgroup_disabled())
410 pc = lookup_page_cgroup(page);
412 * Used bit is set without atomic ops but after smp_wmb().
413 * For making pc->mem_cgroup visible, insert smp_rmb() here.
416 /* unused or root page is not rotated. */
417 if (!PageCgroupUsed(pc) || mem_cgroup_is_root(pc->mem_cgroup))
419 mz = page_cgroup_zoneinfo(pc);
420 list_move(&pc->lru, &mz->lists[lru]);
423 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
425 struct page_cgroup *pc;
426 struct mem_cgroup_per_zone *mz;
428 if (mem_cgroup_disabled())
430 pc = lookup_page_cgroup(page);
431 VM_BUG_ON(PageCgroupAcctLRU(pc));
433 * Used bit is set without atomic ops but after smp_wmb().
434 * For making pc->mem_cgroup visible, insert smp_rmb() here.
437 if (!PageCgroupUsed(pc))
440 mz = page_cgroup_zoneinfo(pc);
441 MEM_CGROUP_ZSTAT(mz, lru) += 1;
442 SetPageCgroupAcctLRU(pc);
443 if (mem_cgroup_is_root(pc->mem_cgroup))
445 list_add(&pc->lru, &mz->lists[lru]);
449 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
450 * lru because the page may.be reused after it's fully uncharged (because of
451 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
452 * it again. This function is only used to charge SwapCache. It's done under
453 * lock_page and expected that zone->lru_lock is never held.
455 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
458 struct zone *zone = page_zone(page);
459 struct page_cgroup *pc = lookup_page_cgroup(page);
461 spin_lock_irqsave(&zone->lru_lock, flags);
463 * Forget old LRU when this page_cgroup is *not* used. This Used bit
464 * is guarded by lock_page() because the page is SwapCache.
466 if (!PageCgroupUsed(pc))
467 mem_cgroup_del_lru_list(page, page_lru(page));
468 spin_unlock_irqrestore(&zone->lru_lock, flags);
471 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
474 struct zone *zone = page_zone(page);
475 struct page_cgroup *pc = lookup_page_cgroup(page);
477 spin_lock_irqsave(&zone->lru_lock, flags);
478 /* link when the page is linked to LRU but page_cgroup isn't */
479 if (PageLRU(page) && !PageCgroupAcctLRU(pc))
480 mem_cgroup_add_lru_list(page, page_lru(page));
481 spin_unlock_irqrestore(&zone->lru_lock, flags);
485 void mem_cgroup_move_lists(struct page *page,
486 enum lru_list from, enum lru_list to)
488 if (mem_cgroup_disabled())
490 mem_cgroup_del_lru_list(page, from);
491 mem_cgroup_add_lru_list(page, to);
494 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
497 struct mem_cgroup *curr = NULL;
501 curr = try_get_mem_cgroup_from_mm(task->mm);
506 if (curr->use_hierarchy)
507 ret = css_is_ancestor(&curr->css, &mem->css);
515 * prev_priority control...this will be used in memory reclaim path.
517 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
521 spin_lock(&mem->reclaim_param_lock);
522 prev_priority = mem->prev_priority;
523 spin_unlock(&mem->reclaim_param_lock);
525 return prev_priority;
528 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
530 spin_lock(&mem->reclaim_param_lock);
531 if (priority < mem->prev_priority)
532 mem->prev_priority = priority;
533 spin_unlock(&mem->reclaim_param_lock);
536 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
538 spin_lock(&mem->reclaim_param_lock);
539 mem->prev_priority = priority;
540 spin_unlock(&mem->reclaim_param_lock);
543 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
545 unsigned long active;
546 unsigned long inactive;
548 unsigned long inactive_ratio;
550 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
551 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
553 gb = (inactive + active) >> (30 - PAGE_SHIFT);
555 inactive_ratio = int_sqrt(10 * gb);
560 present_pages[0] = inactive;
561 present_pages[1] = active;
564 return inactive_ratio;
567 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
569 unsigned long active;
570 unsigned long inactive;
571 unsigned long present_pages[2];
572 unsigned long inactive_ratio;
574 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
576 inactive = present_pages[0];
577 active = present_pages[1];
579 if (inactive * inactive_ratio < active)
585 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
587 unsigned long active;
588 unsigned long inactive;
590 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
591 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
593 return (active > inactive);
596 unsigned long mem_cgroup_zone_nr_pages(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 MEM_CGROUP_ZSTAT(mz, lru);
607 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
610 int nid = zone->zone_pgdat->node_id;
611 int zid = zone_idx(zone);
612 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
614 return &mz->reclaim_stat;
617 struct zone_reclaim_stat *
618 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
620 struct page_cgroup *pc;
621 struct mem_cgroup_per_zone *mz;
623 if (mem_cgroup_disabled())
626 pc = lookup_page_cgroup(page);
628 * Used bit is set without atomic ops but after smp_wmb().
629 * For making pc->mem_cgroup visible, insert smp_rmb() here.
632 if (!PageCgroupUsed(pc))
635 mz = page_cgroup_zoneinfo(pc);
639 return &mz->reclaim_stat;
642 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
643 struct list_head *dst,
644 unsigned long *scanned, int order,
645 int mode, struct zone *z,
646 struct mem_cgroup *mem_cont,
647 int active, int file)
649 unsigned long nr_taken = 0;
653 struct list_head *src;
654 struct page_cgroup *pc, *tmp;
655 int nid = z->zone_pgdat->node_id;
656 int zid = zone_idx(z);
657 struct mem_cgroup_per_zone *mz;
658 int lru = LRU_FILE * file + active;
662 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
663 src = &mz->lists[lru];
666 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
667 if (scan >= nr_to_scan)
671 if (unlikely(!PageCgroupUsed(pc)))
673 if (unlikely(!PageLRU(page)))
677 ret = __isolate_lru_page(page, mode, file);
680 list_move(&page->lru, dst);
681 mem_cgroup_del_lru(page);
685 /* we don't affect global LRU but rotate in our LRU */
686 mem_cgroup_rotate_lru_list(page, page_lru(page));
697 #define mem_cgroup_from_res_counter(counter, member) \
698 container_of(counter, struct mem_cgroup, member)
700 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
702 if (do_swap_account) {
703 if (res_counter_check_under_limit(&mem->res) &&
704 res_counter_check_under_limit(&mem->memsw))
707 if (res_counter_check_under_limit(&mem->res))
712 static unsigned int get_swappiness(struct mem_cgroup *memcg)
714 struct cgroup *cgrp = memcg->css.cgroup;
715 unsigned int swappiness;
718 if (cgrp->parent == NULL)
719 return vm_swappiness;
721 spin_lock(&memcg->reclaim_param_lock);
722 swappiness = memcg->swappiness;
723 spin_unlock(&memcg->reclaim_param_lock);
728 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
736 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
737 * @memcg: The memory cgroup that went over limit
738 * @p: Task that is going to be killed
740 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
743 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
745 struct cgroup *task_cgrp;
746 struct cgroup *mem_cgrp;
748 * Need a buffer in BSS, can't rely on allocations. The code relies
749 * on the assumption that OOM is serialized for memory controller.
750 * If this assumption is broken, revisit this code.
752 static char memcg_name[PATH_MAX];
761 mem_cgrp = memcg->css.cgroup;
762 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
764 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
767 * Unfortunately, we are unable to convert to a useful name
768 * But we'll still print out the usage information
775 printk(KERN_INFO "Task in %s killed", memcg_name);
778 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
786 * Continues from above, so we don't need an KERN_ level
788 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
791 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
792 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
793 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
794 res_counter_read_u64(&memcg->res, RES_FAILCNT));
795 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
797 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
798 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
799 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
803 * This function returns the number of memcg under hierarchy tree. Returns
804 * 1(self count) if no children.
806 static int mem_cgroup_count_children(struct mem_cgroup *mem)
809 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
814 * Visit the first child (need not be the first child as per the ordering
815 * of the cgroup list, since we track last_scanned_child) of @mem and use
816 * that to reclaim free pages from.
818 static struct mem_cgroup *
819 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
821 struct mem_cgroup *ret = NULL;
822 struct cgroup_subsys_state *css;
825 if (!root_mem->use_hierarchy) {
826 css_get(&root_mem->css);
832 nextid = root_mem->last_scanned_child + 1;
833 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
835 if (css && css_tryget(css))
836 ret = container_of(css, struct mem_cgroup, css);
839 /* Updates scanning parameter */
840 spin_lock(&root_mem->reclaim_param_lock);
842 /* this means start scan from ID:1 */
843 root_mem->last_scanned_child = 0;
845 root_mem->last_scanned_child = found;
846 spin_unlock(&root_mem->reclaim_param_lock);
853 * Scan the hierarchy if needed to reclaim memory. We remember the last child
854 * we reclaimed from, so that we don't end up penalizing one child extensively
855 * based on its position in the children list.
857 * root_mem is the original ancestor that we've been reclaim from.
859 * We give up and return to the caller when we visit root_mem twice.
860 * (other groups can be removed while we're walking....)
862 * If shrink==true, for avoiding to free too much, this returns immedieately.
864 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
865 gfp_t gfp_mask, bool noswap, bool shrink)
867 struct mem_cgroup *victim;
871 /* If memsw_is_minimum==1, swap-out is of-no-use. */
872 if (root_mem->memsw_is_minimum)
876 victim = mem_cgroup_select_victim(root_mem);
877 if (victim == root_mem)
879 if (!mem_cgroup_local_usage(&victim->stat)) {
880 /* this cgroup's local usage == 0 */
881 css_put(&victim->css);
884 /* we use swappiness of local cgroup */
885 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
886 get_swappiness(victim));
887 css_put(&victim->css);
889 * At shrinking usage, we can't check we should stop here or
890 * reclaim more. It's depends on callers. last_scanned_child
891 * will work enough for keeping fairness under tree.
896 if (mem_cgroup_check_under_limit(root_mem))
902 bool mem_cgroup_oom_called(struct task_struct *task)
905 struct mem_cgroup *mem;
906 struct mm_struct *mm;
912 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
913 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
919 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
921 mem->last_oom_jiffies = jiffies;
925 static void record_last_oom(struct mem_cgroup *mem)
927 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
931 * Currently used to update mapped file statistics, but the routine can be
932 * generalized to update other statistics as well.
934 void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
936 struct mem_cgroup *mem;
937 struct mem_cgroup_stat *stat;
938 struct mem_cgroup_stat_cpu *cpustat;
940 struct page_cgroup *pc;
942 if (!page_is_file_cache(page))
945 pc = lookup_page_cgroup(page);
949 lock_page_cgroup(pc);
950 mem = pc->mem_cgroup;
954 if (!PageCgroupUsed(pc))
958 * Preemption is already disabled, we don't need get_cpu()
960 cpu = smp_processor_id();
962 cpustat = &stat->cpustat[cpu];
964 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
966 unlock_page_cgroup(pc);
970 * Unlike exported interface, "oom" parameter is added. if oom==true,
971 * oom-killer can be invoked.
973 static int __mem_cgroup_try_charge(struct mm_struct *mm,
974 gfp_t gfp_mask, struct mem_cgroup **memcg,
977 struct mem_cgroup *mem, *mem_over_limit;
978 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
979 struct res_counter *fail_res;
981 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
982 /* Don't account this! */
988 * We always charge the cgroup the mm_struct belongs to.
989 * The mm_struct's mem_cgroup changes on task migration if the
990 * thread group leader migrates. It's possible that mm is not
991 * set, if so charge the init_mm (happens for pagecache usage).
995 mem = try_get_mem_cgroup_from_mm(mm);
1003 VM_BUG_ON(css_is_removed(&mem->css));
1007 bool noswap = false;
1009 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
1011 if (!do_swap_account)
1013 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
1017 /* mem+swap counter fails */
1018 res_counter_uncharge(&mem->res, PAGE_SIZE);
1020 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1023 /* mem counter fails */
1024 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1027 if (!(gfp_mask & __GFP_WAIT))
1030 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
1036 * try_to_free_mem_cgroup_pages() might not give us a full
1037 * picture of reclaim. Some pages are reclaimed and might be
1038 * moved to swap cache or just unmapped from the cgroup.
1039 * Check the limit again to see if the reclaim reduced the
1040 * current usage of the cgroup before giving up
1043 if (mem_cgroup_check_under_limit(mem_over_limit))
1046 if (!nr_retries--) {
1048 mutex_lock(&memcg_tasklist);
1049 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1050 mutex_unlock(&memcg_tasklist);
1051 record_last_oom(mem_over_limit);
1064 * A helper function to get mem_cgroup from ID. must be called under
1065 * rcu_read_lock(). The caller must check css_is_removed() or some if
1066 * it's concern. (dropping refcnt from swap can be called against removed
1069 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1071 struct cgroup_subsys_state *css;
1073 /* ID 0 is unused ID */
1076 css = css_lookup(&mem_cgroup_subsys, id);
1079 return container_of(css, struct mem_cgroup, css);
1082 static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1084 struct mem_cgroup *mem;
1085 struct page_cgroup *pc;
1089 VM_BUG_ON(!PageLocked(page));
1091 if (!PageSwapCache(page))
1094 pc = lookup_page_cgroup(page);
1095 lock_page_cgroup(pc);
1096 if (PageCgroupUsed(pc)) {
1097 mem = pc->mem_cgroup;
1098 if (mem && !css_tryget(&mem->css))
1101 ent.val = page_private(page);
1102 id = lookup_swap_cgroup(ent);
1104 mem = mem_cgroup_lookup(id);
1105 if (mem && !css_tryget(&mem->css))
1109 unlock_page_cgroup(pc);
1114 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1115 * USED state. If already USED, uncharge and return.
1118 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1119 struct page_cgroup *pc,
1120 enum charge_type ctype)
1122 /* try_charge() can return NULL to *memcg, taking care of it. */
1126 lock_page_cgroup(pc);
1127 if (unlikely(PageCgroupUsed(pc))) {
1128 unlock_page_cgroup(pc);
1129 res_counter_uncharge(&mem->res, PAGE_SIZE);
1130 if (do_swap_account)
1131 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1136 pc->mem_cgroup = mem;
1138 * We access a page_cgroup asynchronously without lock_page_cgroup().
1139 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1140 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1141 * before USED bit, we need memory barrier here.
1142 * See mem_cgroup_add_lru_list(), etc.
1146 case MEM_CGROUP_CHARGE_TYPE_CACHE:
1147 case MEM_CGROUP_CHARGE_TYPE_SHMEM:
1148 SetPageCgroupCache(pc);
1149 SetPageCgroupUsed(pc);
1151 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1152 ClearPageCgroupCache(pc);
1153 SetPageCgroupUsed(pc);
1159 mem_cgroup_charge_statistics(mem, pc, true);
1161 unlock_page_cgroup(pc);
1165 * mem_cgroup_move_account - move account of the page
1166 * @pc: page_cgroup of the page.
1167 * @from: mem_cgroup which the page is moved from.
1168 * @to: mem_cgroup which the page is moved to. @from != @to.
1170 * The caller must confirm following.
1171 * - page is not on LRU (isolate_page() is useful.)
1173 * returns 0 at success,
1174 * returns -EBUSY when lock is busy or "pc" is unstable.
1176 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1177 * new cgroup. It should be done by a caller.
1180 static int mem_cgroup_move_account(struct page_cgroup *pc,
1181 struct mem_cgroup *from, struct mem_cgroup *to)
1183 struct mem_cgroup_per_zone *from_mz, *to_mz;
1188 struct mem_cgroup_stat *stat;
1189 struct mem_cgroup_stat_cpu *cpustat;
1191 VM_BUG_ON(from == to);
1192 VM_BUG_ON(PageLRU(pc->page));
1194 nid = page_cgroup_nid(pc);
1195 zid = page_cgroup_zid(pc);
1196 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1197 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1199 if (!trylock_page_cgroup(pc))
1202 if (!PageCgroupUsed(pc))
1205 if (pc->mem_cgroup != from)
1208 res_counter_uncharge(&from->res, PAGE_SIZE);
1209 mem_cgroup_charge_statistics(from, pc, false);
1212 if (page_is_file_cache(page) && page_mapped(page)) {
1213 cpu = smp_processor_id();
1214 /* Update mapped_file data for mem_cgroup "from" */
1216 cpustat = &stat->cpustat[cpu];
1217 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1220 /* Update mapped_file data for mem_cgroup "to" */
1222 cpustat = &stat->cpustat[cpu];
1223 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1227 if (do_swap_account)
1228 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1229 css_put(&from->css);
1232 pc->mem_cgroup = to;
1233 mem_cgroup_charge_statistics(to, pc, true);
1236 unlock_page_cgroup(pc);
1238 * We charges against "to" which may not have any tasks. Then, "to"
1239 * can be under rmdir(). But in current implementation, caller of
1240 * this function is just force_empty() and it's garanteed that
1241 * "to" is never removed. So, we don't check rmdir status here.
1247 * move charges to its parent.
1250 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1251 struct mem_cgroup *child,
1254 struct page *page = pc->page;
1255 struct cgroup *cg = child->css.cgroup;
1256 struct cgroup *pcg = cg->parent;
1257 struct mem_cgroup *parent;
1265 parent = mem_cgroup_from_cont(pcg);
1268 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
1272 if (!get_page_unless_zero(page)) {
1277 ret = isolate_lru_page(page);
1282 ret = mem_cgroup_move_account(pc, child, parent);
1284 putback_lru_page(page);
1287 /* drop extra refcnt by try_charge() */
1288 css_put(&parent->css);
1295 /* drop extra refcnt by try_charge() */
1296 css_put(&parent->css);
1297 /* uncharge if move fails */
1298 res_counter_uncharge(&parent->res, PAGE_SIZE);
1299 if (do_swap_account)
1300 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
1305 * Charge the memory controller for page usage.
1307 * 0 if the charge was successful
1308 * < 0 if the cgroup is over its limit
1310 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1311 gfp_t gfp_mask, enum charge_type ctype,
1312 struct mem_cgroup *memcg)
1314 struct mem_cgroup *mem;
1315 struct page_cgroup *pc;
1318 pc = lookup_page_cgroup(page);
1319 /* can happen at boot */
1325 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
1329 __mem_cgroup_commit_charge(mem, pc, ctype);
1333 int mem_cgroup_newpage_charge(struct page *page,
1334 struct mm_struct *mm, gfp_t gfp_mask)
1336 if (mem_cgroup_disabled())
1338 if (PageCompound(page))
1341 * If already mapped, we don't have to account.
1342 * If page cache, page->mapping has address_space.
1343 * But page->mapping may have out-of-use anon_vma pointer,
1344 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1347 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1351 return mem_cgroup_charge_common(page, mm, gfp_mask,
1352 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1356 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1357 enum charge_type ctype);
1359 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1362 struct mem_cgroup *mem = NULL;
1365 if (mem_cgroup_disabled())
1367 if (PageCompound(page))
1370 * Corner case handling. This is called from add_to_page_cache()
1371 * in usual. But some FS (shmem) precharges this page before calling it
1372 * and call add_to_page_cache() with GFP_NOWAIT.
1374 * For GFP_NOWAIT case, the page may be pre-charged before calling
1375 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1376 * charge twice. (It works but has to pay a bit larger cost.)
1377 * And when the page is SwapCache, it should take swap information
1378 * into account. This is under lock_page() now.
1380 if (!(gfp_mask & __GFP_WAIT)) {
1381 struct page_cgroup *pc;
1384 pc = lookup_page_cgroup(page);
1387 lock_page_cgroup(pc);
1388 if (PageCgroupUsed(pc)) {
1389 unlock_page_cgroup(pc);
1392 unlock_page_cgroup(pc);
1395 if (unlikely(!mm && !mem))
1398 if (page_is_file_cache(page))
1399 return mem_cgroup_charge_common(page, mm, gfp_mask,
1400 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1403 if (PageSwapCache(page)) {
1404 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1406 __mem_cgroup_commit_charge_swapin(page, mem,
1407 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1409 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1410 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1416 * While swap-in, try_charge -> commit or cancel, the page is locked.
1417 * And when try_charge() successfully returns, one refcnt to memcg without
1418 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1419 * "commit()" or removed by "cancel()"
1421 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1423 gfp_t mask, struct mem_cgroup **ptr)
1425 struct mem_cgroup *mem;
1428 if (mem_cgroup_disabled())
1431 if (!do_swap_account)
1434 * A racing thread's fault, or swapoff, may have already updated
1435 * the pte, and even removed page from swap cache: return success
1436 * to go on to do_swap_page()'s pte_same() test, which should fail.
1438 if (!PageSwapCache(page))
1440 mem = try_get_mem_cgroup_from_swapcache(page);
1444 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1445 /* drop extra refcnt from tryget */
1451 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1455 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1456 enum charge_type ctype)
1458 struct page_cgroup *pc;
1460 if (mem_cgroup_disabled())
1464 cgroup_exclude_rmdir(&ptr->css);
1465 pc = lookup_page_cgroup(page);
1466 mem_cgroup_lru_del_before_commit_swapcache(page);
1467 __mem_cgroup_commit_charge(ptr, pc, ctype);
1468 mem_cgroup_lru_add_after_commit_swapcache(page);
1470 * Now swap is on-memory. This means this page may be
1471 * counted both as mem and swap....double count.
1472 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1473 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1474 * may call delete_from_swap_cache() before reach here.
1476 if (do_swap_account && PageSwapCache(page)) {
1477 swp_entry_t ent = {.val = page_private(page)};
1479 struct mem_cgroup *memcg;
1481 id = swap_cgroup_record(ent, 0);
1483 memcg = mem_cgroup_lookup(id);
1486 * This recorded memcg can be obsolete one. So, avoid
1487 * calling css_tryget
1489 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1490 mem_cgroup_put(memcg);
1495 * At swapin, we may charge account against cgroup which has no tasks.
1496 * So, rmdir()->pre_destroy() can be called while we do this charge.
1497 * In that case, we need to call pre_destroy() again. check it here.
1499 cgroup_release_and_wakeup_rmdir(&ptr->css);
1502 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1504 __mem_cgroup_commit_charge_swapin(page, ptr,
1505 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1508 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1510 if (mem_cgroup_disabled())
1514 res_counter_uncharge(&mem->res, PAGE_SIZE);
1515 if (do_swap_account)
1516 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1522 * uncharge if !page_mapped(page)
1524 static struct mem_cgroup *
1525 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1527 struct page_cgroup *pc;
1528 struct mem_cgroup *mem = NULL;
1529 struct mem_cgroup_per_zone *mz;
1531 if (mem_cgroup_disabled())
1534 if (PageSwapCache(page))
1538 * Check if our page_cgroup is valid
1540 pc = lookup_page_cgroup(page);
1541 if (unlikely(!pc || !PageCgroupUsed(pc)))
1544 lock_page_cgroup(pc);
1546 mem = pc->mem_cgroup;
1548 if (!PageCgroupUsed(pc))
1552 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1553 case MEM_CGROUP_CHARGE_TYPE_DROP:
1554 if (page_mapped(page))
1557 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1558 if (!PageAnon(page)) { /* Shared memory */
1559 if (page->mapping && !page_is_file_cache(page))
1561 } else if (page_mapped(page)) /* Anon */
1568 res_counter_uncharge(&mem->res, PAGE_SIZE);
1569 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1570 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1571 mem_cgroup_charge_statistics(mem, pc, false);
1573 ClearPageCgroupUsed(pc);
1575 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1576 * freed from LRU. This is safe because uncharged page is expected not
1577 * to be reused (freed soon). Exception is SwapCache, it's handled by
1578 * special functions.
1581 mz = page_cgroup_zoneinfo(pc);
1582 unlock_page_cgroup(pc);
1584 /* at swapout, this memcg will be accessed to record to swap */
1585 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1591 unlock_page_cgroup(pc);
1595 void mem_cgroup_uncharge_page(struct page *page)
1598 if (page_mapped(page))
1600 if (page->mapping && !PageAnon(page))
1602 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1605 void mem_cgroup_uncharge_cache_page(struct page *page)
1607 VM_BUG_ON(page_mapped(page));
1608 VM_BUG_ON(page->mapping);
1609 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1614 * called after __delete_from_swap_cache() and drop "page" account.
1615 * memcg information is recorded to swap_cgroup of "ent"
1618 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
1620 struct mem_cgroup *memcg;
1621 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1623 if (!swapout) /* this was a swap cache but the swap is unused ! */
1624 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1626 memcg = __mem_cgroup_uncharge_common(page, ctype);
1628 /* record memcg information */
1629 if (do_swap_account && swapout && memcg) {
1630 swap_cgroup_record(ent, css_id(&memcg->css));
1631 mem_cgroup_get(memcg);
1633 if (swapout && memcg)
1634 css_put(&memcg->css);
1638 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1640 * called from swap_entry_free(). remove record in swap_cgroup and
1641 * uncharge "memsw" account.
1643 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1645 struct mem_cgroup *memcg;
1648 if (!do_swap_account)
1651 id = swap_cgroup_record(ent, 0);
1653 memcg = mem_cgroup_lookup(id);
1656 * We uncharge this because swap is freed.
1657 * This memcg can be obsolete one. We avoid calling css_tryget
1659 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1660 mem_cgroup_put(memcg);
1667 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1670 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1672 struct page_cgroup *pc;
1673 struct mem_cgroup *mem = NULL;
1676 if (mem_cgroup_disabled())
1679 pc = lookup_page_cgroup(page);
1680 lock_page_cgroup(pc);
1681 if (PageCgroupUsed(pc)) {
1682 mem = pc->mem_cgroup;
1685 unlock_page_cgroup(pc);
1688 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
1695 /* remove redundant charge if migration failed*/
1696 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1697 struct page *oldpage, struct page *newpage)
1699 struct page *target, *unused;
1700 struct page_cgroup *pc;
1701 enum charge_type ctype;
1705 cgroup_exclude_rmdir(&mem->css);
1706 /* at migration success, oldpage->mapping is NULL. */
1707 if (oldpage->mapping) {
1715 if (PageAnon(target))
1716 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1717 else if (page_is_file_cache(target))
1718 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1720 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1722 /* unused page is not on radix-tree now. */
1724 __mem_cgroup_uncharge_common(unused, ctype);
1726 pc = lookup_page_cgroup(target);
1728 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1729 * So, double-counting is effectively avoided.
1731 __mem_cgroup_commit_charge(mem, pc, ctype);
1734 * Both of oldpage and newpage are still under lock_page().
1735 * Then, we don't have to care about race in radix-tree.
1736 * But we have to be careful that this page is unmapped or not.
1738 * There is a case for !page_mapped(). At the start of
1739 * migration, oldpage was mapped. But now, it's zapped.
1740 * But we know *target* page is not freed/reused under us.
1741 * mem_cgroup_uncharge_page() does all necessary checks.
1743 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1744 mem_cgroup_uncharge_page(target);
1746 * At migration, we may charge account against cgroup which has no tasks
1747 * So, rmdir()->pre_destroy() can be called while we do this charge.
1748 * In that case, we need to call pre_destroy() again. check it here.
1750 cgroup_release_and_wakeup_rmdir(&mem->css);
1754 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1755 * Calling hierarchical_reclaim is not enough because we should update
1756 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1757 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1758 * not from the memcg which this page would be charged to.
1759 * try_charge_swapin does all of these works properly.
1761 int mem_cgroup_shmem_charge_fallback(struct page *page,
1762 struct mm_struct *mm,
1765 struct mem_cgroup *mem = NULL;
1768 if (mem_cgroup_disabled())
1771 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1773 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
1778 static DEFINE_MUTEX(set_limit_mutex);
1780 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1781 unsigned long long val)
1787 int children = mem_cgroup_count_children(memcg);
1788 u64 curusage, oldusage;
1791 * For keeping hierarchical_reclaim simple, how long we should retry
1792 * is depends on callers. We set our retry-count to be function
1793 * of # of children which we should visit in this loop.
1795 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1797 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1799 while (retry_count) {
1800 if (signal_pending(current)) {
1805 * Rather than hide all in some function, I do this in
1806 * open coded manner. You see what this really does.
1807 * We have to guarantee mem->res.limit < mem->memsw.limit.
1809 mutex_lock(&set_limit_mutex);
1810 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1811 if (memswlimit < val) {
1813 mutex_unlock(&set_limit_mutex);
1816 ret = res_counter_set_limit(&memcg->res, val);
1818 if (memswlimit == val)
1819 memcg->memsw_is_minimum = true;
1821 memcg->memsw_is_minimum = false;
1823 mutex_unlock(&set_limit_mutex);
1828 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
1830 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1831 /* Usage is reduced ? */
1832 if (curusage >= oldusage)
1835 oldusage = curusage;
1841 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1842 unsigned long long val)
1845 u64 memlimit, oldusage, curusage;
1846 int children = mem_cgroup_count_children(memcg);
1849 /* see mem_cgroup_resize_res_limit */
1850 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1851 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1852 while (retry_count) {
1853 if (signal_pending(current)) {
1858 * Rather than hide all in some function, I do this in
1859 * open coded manner. You see what this really does.
1860 * We have to guarantee mem->res.limit < mem->memsw.limit.
1862 mutex_lock(&set_limit_mutex);
1863 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1864 if (memlimit > val) {
1866 mutex_unlock(&set_limit_mutex);
1869 ret = res_counter_set_limit(&memcg->memsw, val);
1871 if (memlimit == val)
1872 memcg->memsw_is_minimum = true;
1874 memcg->memsw_is_minimum = false;
1876 mutex_unlock(&set_limit_mutex);
1881 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
1882 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1883 /* Usage is reduced ? */
1884 if (curusage >= oldusage)
1887 oldusage = curusage;
1893 * This routine traverse page_cgroup in given list and drop them all.
1894 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1896 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1897 int node, int zid, enum lru_list lru)
1900 struct mem_cgroup_per_zone *mz;
1901 struct page_cgroup *pc, *busy;
1902 unsigned long flags, loop;
1903 struct list_head *list;
1906 zone = &NODE_DATA(node)->node_zones[zid];
1907 mz = mem_cgroup_zoneinfo(mem, node, zid);
1908 list = &mz->lists[lru];
1910 loop = MEM_CGROUP_ZSTAT(mz, lru);
1911 /* give some margin against EBUSY etc...*/
1916 spin_lock_irqsave(&zone->lru_lock, flags);
1917 if (list_empty(list)) {
1918 spin_unlock_irqrestore(&zone->lru_lock, flags);
1921 pc = list_entry(list->prev, struct page_cgroup, lru);
1923 list_move(&pc->lru, list);
1925 spin_unlock_irqrestore(&zone->lru_lock, flags);
1928 spin_unlock_irqrestore(&zone->lru_lock, flags);
1930 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
1934 if (ret == -EBUSY || ret == -EINVAL) {
1935 /* found lock contention or "pc" is obsolete. */
1942 if (!ret && !list_empty(list))
1948 * make mem_cgroup's charge to be 0 if there is no task.
1949 * This enables deleting this mem_cgroup.
1951 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1954 int node, zid, shrink;
1955 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1956 struct cgroup *cgrp = mem->css.cgroup;
1961 /* should free all ? */
1965 while (mem->res.usage > 0) {
1967 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1970 if (signal_pending(current))
1972 /* This is for making all *used* pages to be on LRU. */
1973 lru_add_drain_all();
1975 for_each_node_state(node, N_HIGH_MEMORY) {
1976 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1979 ret = mem_cgroup_force_empty_list(mem,
1988 /* it seems parent cgroup doesn't have enough mem */
1999 /* returns EBUSY if there is a task or if we come here twice. */
2000 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
2004 /* we call try-to-free pages for make this cgroup empty */
2005 lru_add_drain_all();
2006 /* try to free all pages in this cgroup */
2008 while (nr_retries && mem->res.usage > 0) {
2011 if (signal_pending(current)) {
2015 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
2016 false, get_swappiness(mem));
2019 /* maybe some writeback is necessary */
2020 congestion_wait(BLK_RW_ASYNC, HZ/10);
2025 /* try move_account...there may be some *locked* pages. */
2032 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2034 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2038 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2040 return mem_cgroup_from_cont(cont)->use_hierarchy;
2043 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2047 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2048 struct cgroup *parent = cont->parent;
2049 struct mem_cgroup *parent_mem = NULL;
2052 parent_mem = mem_cgroup_from_cont(parent);
2056 * If parent's use_hiearchy is set, we can't make any modifications
2057 * in the child subtrees. If it is unset, then the change can
2058 * occur, provided the current cgroup has no children.
2060 * For the root cgroup, parent_mem is NULL, we allow value to be
2061 * set if there are no children.
2063 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2064 (val == 1 || val == 0)) {
2065 if (list_empty(&cont->children))
2066 mem->use_hierarchy = val;
2076 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2078 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2082 type = MEMFILE_TYPE(cft->private);
2083 name = MEMFILE_ATTR(cft->private);
2086 val = res_counter_read_u64(&mem->res, name);
2089 val = res_counter_read_u64(&mem->memsw, name);
2098 * The user of this function is...
2101 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2104 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2106 unsigned long long val;
2109 type = MEMFILE_TYPE(cft->private);
2110 name = MEMFILE_ATTR(cft->private);
2113 if (mem_cgroup_is_root(memcg)) { /* Can't set limit on root */
2117 /* This function does all necessary parse...reuse it */
2118 ret = res_counter_memparse_write_strategy(buffer, &val);
2122 ret = mem_cgroup_resize_limit(memcg, val);
2124 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2126 case RES_SOFT_LIMIT:
2127 ret = res_counter_memparse_write_strategy(buffer, &val);
2131 * For memsw, soft limits are hard to implement in terms
2132 * of semantics, for now, we support soft limits for
2133 * control without swap
2136 ret = res_counter_set_soft_limit(&memcg->res, val);
2141 ret = -EINVAL; /* should be BUG() ? */
2147 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2148 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2150 struct cgroup *cgroup;
2151 unsigned long long min_limit, min_memsw_limit, tmp;
2153 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2154 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2155 cgroup = memcg->css.cgroup;
2156 if (!memcg->use_hierarchy)
2159 while (cgroup->parent) {
2160 cgroup = cgroup->parent;
2161 memcg = mem_cgroup_from_cont(cgroup);
2162 if (!memcg->use_hierarchy)
2164 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2165 min_limit = min(min_limit, tmp);
2166 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2167 min_memsw_limit = min(min_memsw_limit, tmp);
2170 *mem_limit = min_limit;
2171 *memsw_limit = min_memsw_limit;
2175 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2177 struct mem_cgroup *mem;
2180 mem = mem_cgroup_from_cont(cont);
2181 type = MEMFILE_TYPE(event);
2182 name = MEMFILE_ATTR(event);
2186 res_counter_reset_max(&mem->res);
2188 res_counter_reset_max(&mem->memsw);
2192 res_counter_reset_failcnt(&mem->res);
2194 res_counter_reset_failcnt(&mem->memsw);
2201 /* For read statistics */
2216 struct mcs_total_stat {
2217 s64 stat[NR_MCS_STAT];
2223 } memcg_stat_strings[NR_MCS_STAT] = {
2224 {"cache", "total_cache"},
2225 {"rss", "total_rss"},
2226 {"mapped_file", "total_mapped_file"},
2227 {"pgpgin", "total_pgpgin"},
2228 {"pgpgout", "total_pgpgout"},
2229 {"inactive_anon", "total_inactive_anon"},
2230 {"active_anon", "total_active_anon"},
2231 {"inactive_file", "total_inactive_file"},
2232 {"active_file", "total_active_file"},
2233 {"unevictable", "total_unevictable"}
2237 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2239 struct mcs_total_stat *s = data;
2243 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2244 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2245 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2246 s->stat[MCS_RSS] += val * PAGE_SIZE;
2247 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2248 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
2249 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2250 s->stat[MCS_PGPGIN] += val;
2251 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2252 s->stat[MCS_PGPGOUT] += val;
2255 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2256 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2257 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2258 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2259 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2260 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2261 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2262 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2263 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2264 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2269 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2271 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2274 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2275 struct cgroup_map_cb *cb)
2277 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2278 struct mcs_total_stat mystat;
2281 memset(&mystat, 0, sizeof(mystat));
2282 mem_cgroup_get_local_stat(mem_cont, &mystat);
2284 for (i = 0; i < NR_MCS_STAT; i++)
2285 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2287 /* Hierarchical information */
2289 unsigned long long limit, memsw_limit;
2290 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2291 cb->fill(cb, "hierarchical_memory_limit", limit);
2292 if (do_swap_account)
2293 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2296 memset(&mystat, 0, sizeof(mystat));
2297 mem_cgroup_get_total_stat(mem_cont, &mystat);
2298 for (i = 0; i < NR_MCS_STAT; i++)
2299 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2302 #ifdef CONFIG_DEBUG_VM
2303 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2307 struct mem_cgroup_per_zone *mz;
2308 unsigned long recent_rotated[2] = {0, 0};
2309 unsigned long recent_scanned[2] = {0, 0};
2311 for_each_online_node(nid)
2312 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2313 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2315 recent_rotated[0] +=
2316 mz->reclaim_stat.recent_rotated[0];
2317 recent_rotated[1] +=
2318 mz->reclaim_stat.recent_rotated[1];
2319 recent_scanned[0] +=
2320 mz->reclaim_stat.recent_scanned[0];
2321 recent_scanned[1] +=
2322 mz->reclaim_stat.recent_scanned[1];
2324 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2325 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2326 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2327 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2334 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2336 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2338 return get_swappiness(memcg);
2341 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2344 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2345 struct mem_cgroup *parent;
2350 if (cgrp->parent == NULL)
2353 parent = mem_cgroup_from_cont(cgrp->parent);
2357 /* If under hierarchy, only empty-root can set this value */
2358 if ((parent->use_hierarchy) ||
2359 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2364 spin_lock(&memcg->reclaim_param_lock);
2365 memcg->swappiness = val;
2366 spin_unlock(&memcg->reclaim_param_lock);
2374 static struct cftype mem_cgroup_files[] = {
2376 .name = "usage_in_bytes",
2377 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2378 .read_u64 = mem_cgroup_read,
2381 .name = "max_usage_in_bytes",
2382 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2383 .trigger = mem_cgroup_reset,
2384 .read_u64 = mem_cgroup_read,
2387 .name = "limit_in_bytes",
2388 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2389 .write_string = mem_cgroup_write,
2390 .read_u64 = mem_cgroup_read,
2393 .name = "soft_limit_in_bytes",
2394 .private = MEMFILE_PRIVATE(_MEM, RES_SOFT_LIMIT),
2395 .write_string = mem_cgroup_write,
2396 .read_u64 = mem_cgroup_read,
2400 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2401 .trigger = mem_cgroup_reset,
2402 .read_u64 = mem_cgroup_read,
2406 .read_map = mem_control_stat_show,
2409 .name = "force_empty",
2410 .trigger = mem_cgroup_force_empty_write,
2413 .name = "use_hierarchy",
2414 .write_u64 = mem_cgroup_hierarchy_write,
2415 .read_u64 = mem_cgroup_hierarchy_read,
2418 .name = "swappiness",
2419 .read_u64 = mem_cgroup_swappiness_read,
2420 .write_u64 = mem_cgroup_swappiness_write,
2424 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2425 static struct cftype memsw_cgroup_files[] = {
2427 .name = "memsw.usage_in_bytes",
2428 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2429 .read_u64 = mem_cgroup_read,
2432 .name = "memsw.max_usage_in_bytes",
2433 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2434 .trigger = mem_cgroup_reset,
2435 .read_u64 = mem_cgroup_read,
2438 .name = "memsw.limit_in_bytes",
2439 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2440 .write_string = mem_cgroup_write,
2441 .read_u64 = mem_cgroup_read,
2444 .name = "memsw.failcnt",
2445 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2446 .trigger = mem_cgroup_reset,
2447 .read_u64 = mem_cgroup_read,
2451 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2453 if (!do_swap_account)
2455 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2456 ARRAY_SIZE(memsw_cgroup_files));
2459 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2465 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2467 struct mem_cgroup_per_node *pn;
2468 struct mem_cgroup_per_zone *mz;
2470 int zone, tmp = node;
2472 * This routine is called against possible nodes.
2473 * But it's BUG to call kmalloc() against offline node.
2475 * TODO: this routine can waste much memory for nodes which will
2476 * never be onlined. It's better to use memory hotplug callback
2479 if (!node_state(node, N_NORMAL_MEMORY))
2481 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
2485 mem->info.nodeinfo[node] = pn;
2486 memset(pn, 0, sizeof(*pn));
2488 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2489 mz = &pn->zoneinfo[zone];
2491 INIT_LIST_HEAD(&mz->lists[l]);
2496 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2498 kfree(mem->info.nodeinfo[node]);
2501 static int mem_cgroup_size(void)
2503 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2504 return sizeof(struct mem_cgroup) + cpustat_size;
2507 static struct mem_cgroup *mem_cgroup_alloc(void)
2509 struct mem_cgroup *mem;
2510 int size = mem_cgroup_size();
2512 if (size < PAGE_SIZE)
2513 mem = kmalloc(size, GFP_KERNEL);
2515 mem = vmalloc(size);
2518 memset(mem, 0, size);
2523 * At destroying mem_cgroup, references from swap_cgroup can remain.
2524 * (scanning all at force_empty is too costly...)
2526 * Instead of clearing all references at force_empty, we remember
2527 * the number of reference from swap_cgroup and free mem_cgroup when
2528 * it goes down to 0.
2530 * Removal of cgroup itself succeeds regardless of refs from swap.
2533 static void __mem_cgroup_free(struct mem_cgroup *mem)
2537 free_css_id(&mem_cgroup_subsys, &mem->css);
2539 for_each_node_state(node, N_POSSIBLE)
2540 free_mem_cgroup_per_zone_info(mem, node);
2542 if (mem_cgroup_size() < PAGE_SIZE)
2548 static void mem_cgroup_get(struct mem_cgroup *mem)
2550 atomic_inc(&mem->refcnt);
2553 static void mem_cgroup_put(struct mem_cgroup *mem)
2555 if (atomic_dec_and_test(&mem->refcnt)) {
2556 struct mem_cgroup *parent = parent_mem_cgroup(mem);
2557 __mem_cgroup_free(mem);
2559 mem_cgroup_put(parent);
2564 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2566 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2568 if (!mem->res.parent)
2570 return mem_cgroup_from_res_counter(mem->res.parent, res);
2573 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2574 static void __init enable_swap_cgroup(void)
2576 if (!mem_cgroup_disabled() && really_do_swap_account)
2577 do_swap_account = 1;
2580 static void __init enable_swap_cgroup(void)
2585 static struct cgroup_subsys_state * __ref
2586 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2588 struct mem_cgroup *mem, *parent;
2589 long error = -ENOMEM;
2592 mem = mem_cgroup_alloc();
2594 return ERR_PTR(error);
2596 for_each_node_state(node, N_POSSIBLE)
2597 if (alloc_mem_cgroup_per_zone_info(mem, node))
2600 if (cont->parent == NULL) {
2601 enable_swap_cgroup();
2603 root_mem_cgroup = mem;
2605 parent = mem_cgroup_from_cont(cont->parent);
2606 mem->use_hierarchy = parent->use_hierarchy;
2609 if (parent && parent->use_hierarchy) {
2610 res_counter_init(&mem->res, &parent->res);
2611 res_counter_init(&mem->memsw, &parent->memsw);
2613 * We increment refcnt of the parent to ensure that we can
2614 * safely access it on res_counter_charge/uncharge.
2615 * This refcnt will be decremented when freeing this
2616 * mem_cgroup(see mem_cgroup_put).
2618 mem_cgroup_get(parent);
2620 res_counter_init(&mem->res, NULL);
2621 res_counter_init(&mem->memsw, NULL);
2623 mem->last_scanned_child = 0;
2624 spin_lock_init(&mem->reclaim_param_lock);
2627 mem->swappiness = get_swappiness(parent);
2628 atomic_set(&mem->refcnt, 1);
2631 __mem_cgroup_free(mem);
2632 root_mem_cgroup = NULL;
2633 return ERR_PTR(error);
2636 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
2637 struct cgroup *cont)
2639 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2641 return mem_cgroup_force_empty(mem, false);
2644 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2645 struct cgroup *cont)
2647 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2649 mem_cgroup_put(mem);
2652 static int mem_cgroup_populate(struct cgroup_subsys *ss,
2653 struct cgroup *cont)
2657 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2658 ARRAY_SIZE(mem_cgroup_files));
2661 ret = register_memsw_files(cont, ss);
2665 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2666 struct cgroup *cont,
2667 struct cgroup *old_cont,
2668 struct task_struct *p,
2671 mutex_lock(&memcg_tasklist);
2673 * FIXME: It's better to move charges of this process from old
2674 * memcg to new memcg. But it's just on TODO-List now.
2676 mutex_unlock(&memcg_tasklist);
2679 struct cgroup_subsys mem_cgroup_subsys = {
2681 .subsys_id = mem_cgroup_subsys_id,
2682 .create = mem_cgroup_create,
2683 .pre_destroy = mem_cgroup_pre_destroy,
2684 .destroy = mem_cgroup_destroy,
2685 .populate = mem_cgroup_populate,
2686 .attach = mem_cgroup_move_task,
2691 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2693 static int __init disable_swap_account(char *s)
2695 really_do_swap_account = 0;
2698 __setup("noswapaccount", disable_swap_account);