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/mutex.h>
31 #include <linux/slab.h>
32 #include <linux/swap.h>
33 #include <linux/spinlock.h>
35 #include <linux/seq_file.h>
36 #include <linux/vmalloc.h>
37 #include <linux/mm_inline.h>
38 #include <linux/page_cgroup.h>
41 #include <asm/uaccess.h>
43 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
44 #define MEM_CGROUP_RECLAIM_RETRIES 5
46 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
47 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 0 */
48 int do_swap_account __read_mostly;
49 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
51 #define do_swap_account (0)
56 * Statistics for memory cgroup.
58 enum mem_cgroup_stat_index {
60 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
62 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
63 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
64 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
65 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
67 MEM_CGROUP_STAT_NSTATS,
70 struct mem_cgroup_stat_cpu {
71 s64 count[MEM_CGROUP_STAT_NSTATS];
72 } ____cacheline_aligned_in_smp;
74 struct mem_cgroup_stat {
75 struct mem_cgroup_stat_cpu cpustat[0];
79 * For accounting under irq disable, no need for increment preempt count.
81 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
82 enum mem_cgroup_stat_index idx, int val)
84 stat->count[idx] += val;
87 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
88 enum mem_cgroup_stat_index idx)
92 for_each_possible_cpu(cpu)
93 ret += stat->cpustat[cpu].count[idx];
98 * per-zone information in memory controller.
100 struct mem_cgroup_per_zone {
102 * spin_lock to protect the per cgroup LRU
104 struct list_head lists[NR_LRU_LISTS];
105 unsigned long count[NR_LRU_LISTS];
107 /* Macro for accessing counter */
108 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
110 struct mem_cgroup_per_node {
111 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
114 struct mem_cgroup_lru_info {
115 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
119 * The memory controller data structure. The memory controller controls both
120 * page cache and RSS per cgroup. We would eventually like to provide
121 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
122 * to help the administrator determine what knobs to tune.
124 * TODO: Add a water mark for the memory controller. Reclaim will begin when
125 * we hit the water mark. May be even add a low water mark, such that
126 * no reclaim occurs from a cgroup at it's low water mark, this is
127 * a feature that will be implemented much later in the future.
130 struct cgroup_subsys_state css;
132 * the counter to account for memory usage
134 struct res_counter res;
136 * the counter to account for mem+swap usage.
138 struct res_counter memsw;
140 * Per cgroup active and inactive list, similar to the
141 * per zone LRU lists.
143 struct mem_cgroup_lru_info info;
145 int prev_priority; /* for recording reclaim priority */
148 * While reclaiming in a hiearchy, we cache the last child we
149 * reclaimed from. Protected by cgroup_lock()
151 struct mem_cgroup *last_scanned_child;
153 * Should the accounting and control be hierarchical, per subtree?
156 unsigned long last_oom_jiffies;
160 unsigned int inactive_ratio;
163 * statistics. This must be placed at the end of memcg.
165 struct mem_cgroup_stat stat;
169 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
170 MEM_CGROUP_CHARGE_TYPE_MAPPED,
171 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
172 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
173 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
177 /* only for here (for easy reading.) */
178 #define PCGF_CACHE (1UL << PCG_CACHE)
179 #define PCGF_USED (1UL << PCG_USED)
180 #define PCGF_LOCK (1UL << PCG_LOCK)
181 static const unsigned long
182 pcg_default_flags[NR_CHARGE_TYPE] = {
183 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
184 PCGF_USED | PCGF_LOCK, /* Anon */
185 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
189 /* for encoding cft->private value on file */
192 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
193 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
194 #define MEMFILE_ATTR(val) ((val) & 0xffff)
196 static void mem_cgroup_get(struct mem_cgroup *mem);
197 static void mem_cgroup_put(struct mem_cgroup *mem);
199 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
200 struct page_cgroup *pc,
203 int val = (charge)? 1 : -1;
204 struct mem_cgroup_stat *stat = &mem->stat;
205 struct mem_cgroup_stat_cpu *cpustat;
208 cpustat = &stat->cpustat[cpu];
209 if (PageCgroupCache(pc))
210 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
212 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
215 __mem_cgroup_stat_add_safe(cpustat,
216 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
218 __mem_cgroup_stat_add_safe(cpustat,
219 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
223 static struct mem_cgroup_per_zone *
224 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
226 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
229 static struct mem_cgroup_per_zone *
230 page_cgroup_zoneinfo(struct page_cgroup *pc)
232 struct mem_cgroup *mem = pc->mem_cgroup;
233 int nid = page_cgroup_nid(pc);
234 int zid = page_cgroup_zid(pc);
239 return mem_cgroup_zoneinfo(mem, nid, zid);
242 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
246 struct mem_cgroup_per_zone *mz;
249 for_each_online_node(nid)
250 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
251 mz = mem_cgroup_zoneinfo(mem, nid, zid);
252 total += MEM_CGROUP_ZSTAT(mz, idx);
257 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
259 return container_of(cgroup_subsys_state(cont,
260 mem_cgroup_subsys_id), struct mem_cgroup,
264 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
267 * mm_update_next_owner() may clear mm->owner to NULL
268 * if it races with swapoff, page migration, etc.
269 * So this can be called with p == NULL.
274 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
275 struct mem_cgroup, css);
279 * Following LRU functions are allowed to be used without PCG_LOCK.
280 * Operations are called by routine of global LRU independently from memcg.
281 * What we have to take care of here is validness of pc->mem_cgroup.
283 * Changes to pc->mem_cgroup happens when
286 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
287 * It is added to LRU before charge.
288 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
289 * When moving account, the page is not on LRU. It's isolated.
292 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
294 struct page_cgroup *pc;
295 struct mem_cgroup *mem;
296 struct mem_cgroup_per_zone *mz;
298 if (mem_cgroup_disabled())
300 pc = lookup_page_cgroup(page);
301 /* can happen while we handle swapcache. */
302 if (list_empty(&pc->lru))
304 mz = page_cgroup_zoneinfo(pc);
305 mem = pc->mem_cgroup;
306 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
307 list_del_init(&pc->lru);
311 void mem_cgroup_del_lru(struct page *page)
313 mem_cgroup_del_lru_list(page, page_lru(page));
316 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
318 struct mem_cgroup_per_zone *mz;
319 struct page_cgroup *pc;
321 if (mem_cgroup_disabled())
324 pc = lookup_page_cgroup(page);
326 /* unused page is not rotated. */
327 if (!PageCgroupUsed(pc))
329 mz = page_cgroup_zoneinfo(pc);
330 list_move(&pc->lru, &mz->lists[lru]);
333 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
335 struct page_cgroup *pc;
336 struct mem_cgroup_per_zone *mz;
338 if (mem_cgroup_disabled())
340 pc = lookup_page_cgroup(page);
341 /* barrier to sync with "charge" */
343 if (!PageCgroupUsed(pc))
346 mz = page_cgroup_zoneinfo(pc);
347 MEM_CGROUP_ZSTAT(mz, lru) += 1;
348 list_add(&pc->lru, &mz->lists[lru]);
351 * To add swapcache into LRU. Be careful to all this function.
352 * zone->lru_lock shouldn't be held and irq must not be disabled.
354 static void mem_cgroup_lru_fixup(struct page *page)
356 if (!isolate_lru_page(page))
357 putback_lru_page(page);
360 void mem_cgroup_move_lists(struct page *page,
361 enum lru_list from, enum lru_list to)
363 if (mem_cgroup_disabled())
365 mem_cgroup_del_lru_list(page, from);
366 mem_cgroup_add_lru_list(page, to);
369 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
374 ret = task->mm && mm_match_cgroup(task->mm, mem);
380 * Calculate mapped_ratio under memory controller. This will be used in
381 * vmscan.c for deteremining we have to reclaim mapped pages.
383 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
388 * usage is recorded in bytes. But, here, we assume the number of
389 * physical pages can be represented by "long" on any arch.
391 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
392 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
393 return (int)((rss * 100L) / total);
397 * prev_priority control...this will be used in memory reclaim path.
399 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
401 return mem->prev_priority;
404 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
406 if (priority < mem->prev_priority)
407 mem->prev_priority = priority;
410 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
412 mem->prev_priority = priority;
416 * Calculate # of pages to be scanned in this priority/zone.
419 * priority starts from "DEF_PRIORITY" and decremented in each loop.
420 * (see include/linux/mmzone.h)
423 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
424 int priority, enum lru_list lru)
427 int nid = zone->zone_pgdat->node_id;
428 int zid = zone_idx(zone);
429 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
431 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
433 return (nr_pages >> priority);
436 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg, struct zone *zone)
438 unsigned long active;
439 unsigned long inactive;
441 inactive = mem_cgroup_get_all_zonestat(memcg, LRU_INACTIVE_ANON);
442 active = mem_cgroup_get_all_zonestat(memcg, LRU_ACTIVE_ANON);
444 if (inactive * memcg->inactive_ratio < active)
450 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
454 int nid = zone->zone_pgdat->node_id;
455 int zid = zone_idx(zone);
456 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
458 return MEM_CGROUP_ZSTAT(mz, lru);
461 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
462 struct list_head *dst,
463 unsigned long *scanned, int order,
464 int mode, struct zone *z,
465 struct mem_cgroup *mem_cont,
466 int active, int file)
468 unsigned long nr_taken = 0;
472 struct list_head *src;
473 struct page_cgroup *pc, *tmp;
474 int nid = z->zone_pgdat->node_id;
475 int zid = zone_idx(z);
476 struct mem_cgroup_per_zone *mz;
477 int lru = LRU_FILE * !!file + !!active;
480 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
481 src = &mz->lists[lru];
484 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
485 if (scan >= nr_to_scan)
489 if (unlikely(!PageCgroupUsed(pc)))
491 if (unlikely(!PageLRU(page)))
495 if (__isolate_lru_page(page, mode, file) == 0) {
496 list_move(&page->lru, dst);
505 #define mem_cgroup_from_res_counter(counter, member) \
506 container_of(counter, struct mem_cgroup, member)
509 * This routine finds the DFS walk successor. This routine should be
510 * called with cgroup_mutex held
512 static struct mem_cgroup *
513 mem_cgroup_get_next_node(struct mem_cgroup *curr, struct mem_cgroup *root_mem)
515 struct cgroup *cgroup, *curr_cgroup, *root_cgroup;
517 curr_cgroup = curr->css.cgroup;
518 root_cgroup = root_mem->css.cgroup;
520 if (!list_empty(&curr_cgroup->children)) {
522 * Walk down to children
524 mem_cgroup_put(curr);
525 cgroup = list_entry(curr_cgroup->children.next,
526 struct cgroup, sibling);
527 curr = mem_cgroup_from_cont(cgroup);
528 mem_cgroup_get(curr);
533 if (curr_cgroup == root_cgroup) {
534 mem_cgroup_put(curr);
536 mem_cgroup_get(curr);
543 if (curr_cgroup->sibling.next != &curr_cgroup->parent->children) {
544 mem_cgroup_put(curr);
545 cgroup = list_entry(curr_cgroup->sibling.next, struct cgroup,
547 curr = mem_cgroup_from_cont(cgroup);
548 mem_cgroup_get(curr);
553 * Go up to next parent and next parent's sibling if need be
555 curr_cgroup = curr_cgroup->parent;
559 root_mem->last_scanned_child = curr;
564 * Visit the first child (need not be the first child as per the ordering
565 * of the cgroup list, since we track last_scanned_child) of @mem and use
566 * that to reclaim free pages from.
568 static struct mem_cgroup *
569 mem_cgroup_get_first_node(struct mem_cgroup *root_mem)
571 struct cgroup *cgroup;
572 struct mem_cgroup *ret;
573 bool obsolete = (root_mem->last_scanned_child &&
574 root_mem->last_scanned_child->obsolete);
577 * Scan all children under the mem_cgroup mem
580 if (list_empty(&root_mem->css.cgroup->children)) {
585 if (!root_mem->last_scanned_child || obsolete) {
588 mem_cgroup_put(root_mem->last_scanned_child);
590 cgroup = list_first_entry(&root_mem->css.cgroup->children,
591 struct cgroup, sibling);
592 ret = mem_cgroup_from_cont(cgroup);
595 ret = mem_cgroup_get_next_node(root_mem->last_scanned_child,
599 root_mem->last_scanned_child = ret;
604 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
606 if (do_swap_account) {
607 if (res_counter_check_under_limit(&mem->res) &&
608 res_counter_check_under_limit(&mem->memsw))
611 if (res_counter_check_under_limit(&mem->res))
617 * Dance down the hierarchy if needed to reclaim memory. We remember the
618 * last child we reclaimed from, so that we don't end up penalizing
619 * one child extensively based on its position in the children list.
621 * root_mem is the original ancestor that we've been reclaim from.
623 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
624 gfp_t gfp_mask, bool noswap)
626 struct mem_cgroup *next_mem;
630 * Reclaim unconditionally and don't check for return value.
631 * We need to reclaim in the current group and down the tree.
632 * One might think about checking for children before reclaiming,
633 * but there might be left over accounting, even after children
636 ret = try_to_free_mem_cgroup_pages(root_mem, gfp_mask, noswap);
637 if (mem_cgroup_check_under_limit(root_mem))
639 if (!root_mem->use_hierarchy)
642 next_mem = mem_cgroup_get_first_node(root_mem);
644 while (next_mem != root_mem) {
645 if (next_mem->obsolete) {
646 mem_cgroup_put(next_mem);
648 next_mem = mem_cgroup_get_first_node(root_mem);
652 ret = try_to_free_mem_cgroup_pages(next_mem, gfp_mask, noswap);
653 if (mem_cgroup_check_under_limit(root_mem))
656 next_mem = mem_cgroup_get_next_node(next_mem, root_mem);
662 bool mem_cgroup_oom_called(struct task_struct *task)
665 struct mem_cgroup *mem;
666 struct mm_struct *mm;
672 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
673 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
679 * Unlike exported interface, "oom" parameter is added. if oom==true,
680 * oom-killer can be invoked.
682 static int __mem_cgroup_try_charge(struct mm_struct *mm,
683 gfp_t gfp_mask, struct mem_cgroup **memcg,
686 struct mem_cgroup *mem, *mem_over_limit;
687 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
688 struct res_counter *fail_res;
690 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
691 /* Don't account this! */
697 * We always charge the cgroup the mm_struct belongs to.
698 * The mm_struct's mem_cgroup changes on task migration if the
699 * thread group leader migrates. It's possible that mm is not
700 * set, if so charge the init_mm (happens for pagecache usage).
702 if (likely(!*memcg)) {
704 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
705 if (unlikely(!mem)) {
710 * For every charge from the cgroup, increment reference count
724 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
726 if (!do_swap_account)
728 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
732 /* mem+swap counter fails */
733 res_counter_uncharge(&mem->res, PAGE_SIZE);
735 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
738 /* mem counter fails */
739 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
742 if (!(gfp_mask & __GFP_WAIT))
745 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
749 * try_to_free_mem_cgroup_pages() might not give us a full
750 * picture of reclaim. Some pages are reclaimed and might be
751 * moved to swap cache or just unmapped from the cgroup.
752 * Check the limit again to see if the reclaim reduced the
753 * current usage of the cgroup before giving up
756 if (mem_cgroup_check_under_limit(mem_over_limit))
761 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
762 mem_over_limit->last_oom_jiffies = jiffies;
774 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
775 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
776 * @gfp_mask: gfp_mask for reclaim.
777 * @memcg: a pointer to memory cgroup which is charged against.
779 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
780 * memory cgroup from @mm is got and stored in *memcg.
782 * Returns 0 if success. -ENOMEM at failure.
783 * This call can invoke OOM-Killer.
786 int mem_cgroup_try_charge(struct mm_struct *mm,
787 gfp_t mask, struct mem_cgroup **memcg)
789 return __mem_cgroup_try_charge(mm, mask, memcg, true);
793 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
794 * USED state. If already USED, uncharge and return.
797 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
798 struct page_cgroup *pc,
799 enum charge_type ctype)
801 /* try_charge() can return NULL to *memcg, taking care of it. */
805 lock_page_cgroup(pc);
806 if (unlikely(PageCgroupUsed(pc))) {
807 unlock_page_cgroup(pc);
808 res_counter_uncharge(&mem->res, PAGE_SIZE);
810 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
814 pc->mem_cgroup = mem;
816 pc->flags = pcg_default_flags[ctype];
818 mem_cgroup_charge_statistics(mem, pc, true);
820 unlock_page_cgroup(pc);
824 * mem_cgroup_move_account - move account of the page
825 * @pc: page_cgroup of the page.
826 * @from: mem_cgroup which the page is moved from.
827 * @to: mem_cgroup which the page is moved to. @from != @to.
829 * The caller must confirm following.
830 * - page is not on LRU (isolate_page() is useful.)
832 * returns 0 at success,
833 * returns -EBUSY when lock is busy or "pc" is unstable.
835 * This function does "uncharge" from old cgroup but doesn't do "charge" to
836 * new cgroup. It should be done by a caller.
839 static int mem_cgroup_move_account(struct page_cgroup *pc,
840 struct mem_cgroup *from, struct mem_cgroup *to)
842 struct mem_cgroup_per_zone *from_mz, *to_mz;
846 VM_BUG_ON(from == to);
847 VM_BUG_ON(PageLRU(pc->page));
849 nid = page_cgroup_nid(pc);
850 zid = page_cgroup_zid(pc);
851 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
852 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
854 if (!trylock_page_cgroup(pc))
857 if (!PageCgroupUsed(pc))
860 if (pc->mem_cgroup != from)
864 res_counter_uncharge(&from->res, PAGE_SIZE);
865 mem_cgroup_charge_statistics(from, pc, false);
867 res_counter_uncharge(&from->memsw, PAGE_SIZE);
869 mem_cgroup_charge_statistics(to, pc, true);
873 unlock_page_cgroup(pc);
878 * move charges to its parent.
881 static int mem_cgroup_move_parent(struct page_cgroup *pc,
882 struct mem_cgroup *child,
885 struct page *page = pc->page;
886 struct cgroup *cg = child->css.cgroup;
887 struct cgroup *pcg = cg->parent;
888 struct mem_cgroup *parent;
896 parent = mem_cgroup_from_cont(pcg);
899 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
903 if (!get_page_unless_zero(page))
906 ret = isolate_lru_page(page);
911 ret = mem_cgroup_move_account(pc, child, parent);
913 /* drop extra refcnt by try_charge() (move_account increment one) */
914 css_put(&parent->css);
915 putback_lru_page(page);
920 /* uncharge if move fails */
922 res_counter_uncharge(&parent->res, PAGE_SIZE);
924 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
930 * Charge the memory controller for page usage.
932 * 0 if the charge was successful
933 * < 0 if the cgroup is over its limit
935 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
936 gfp_t gfp_mask, enum charge_type ctype,
937 struct mem_cgroup *memcg)
939 struct mem_cgroup *mem;
940 struct page_cgroup *pc;
943 pc = lookup_page_cgroup(page);
944 /* can happen at boot */
950 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
954 __mem_cgroup_commit_charge(mem, pc, ctype);
958 int mem_cgroup_newpage_charge(struct page *page,
959 struct mm_struct *mm, gfp_t gfp_mask)
961 if (mem_cgroup_disabled())
963 if (PageCompound(page))
966 * If already mapped, we don't have to account.
967 * If page cache, page->mapping has address_space.
968 * But page->mapping may have out-of-use anon_vma pointer,
969 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
972 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
976 return mem_cgroup_charge_common(page, mm, gfp_mask,
977 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
980 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
983 if (mem_cgroup_disabled())
985 if (PageCompound(page))
988 * Corner case handling. This is called from add_to_page_cache()
989 * in usual. But some FS (shmem) precharges this page before calling it
990 * and call add_to_page_cache() with GFP_NOWAIT.
992 * For GFP_NOWAIT case, the page may be pre-charged before calling
993 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
994 * charge twice. (It works but has to pay a bit larger cost.)
996 if (!(gfp_mask & __GFP_WAIT)) {
997 struct page_cgroup *pc;
1000 pc = lookup_page_cgroup(page);
1003 lock_page_cgroup(pc);
1004 if (PageCgroupUsed(pc)) {
1005 unlock_page_cgroup(pc);
1008 unlock_page_cgroup(pc);
1014 if (page_is_file_cache(page))
1015 return mem_cgroup_charge_common(page, mm, gfp_mask,
1016 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1018 return mem_cgroup_charge_common(page, mm, gfp_mask,
1019 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
1022 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1024 gfp_t mask, struct mem_cgroup **ptr)
1026 struct mem_cgroup *mem;
1029 if (mem_cgroup_disabled())
1032 if (!do_swap_account)
1036 * A racing thread's fault, or swapoff, may have already updated
1037 * the pte, and even removed page from swap cache: return success
1038 * to go on to do_swap_page()'s pte_same() test, which should fail.
1040 if (!PageSwapCache(page))
1043 ent.val = page_private(page);
1045 mem = lookup_swap_cgroup(ent);
1046 if (!mem || mem->obsolete)
1049 return __mem_cgroup_try_charge(NULL, mask, ptr, true);
1053 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1058 int mem_cgroup_cache_charge_swapin(struct page *page,
1059 struct mm_struct *mm, gfp_t mask, bool locked)
1063 if (mem_cgroup_disabled())
1070 * If not locked, the page can be dropped from SwapCache until
1073 if (PageSwapCache(page)) {
1074 struct mem_cgroup *mem = NULL;
1077 ent.val = page_private(page);
1078 if (do_swap_account) {
1079 mem = lookup_swap_cgroup(ent);
1080 if (mem && mem->obsolete)
1085 ret = mem_cgroup_charge_common(page, mm, mask,
1086 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1088 if (!ret && do_swap_account) {
1089 /* avoid double counting */
1090 mem = swap_cgroup_record(ent, NULL);
1092 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1093 mem_cgroup_put(mem);
1099 /* add this page(page_cgroup) to the LRU we want. */
1100 mem_cgroup_lru_fixup(page);
1106 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1108 struct page_cgroup *pc;
1110 if (mem_cgroup_disabled())
1114 pc = lookup_page_cgroup(page);
1115 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1117 * Now swap is on-memory. This means this page may be
1118 * counted both as mem and swap....double count.
1119 * Fix it by uncharging from memsw. This SwapCache is stable
1120 * because we're still under lock_page().
1122 if (do_swap_account) {
1123 swp_entry_t ent = {.val = page_private(page)};
1124 struct mem_cgroup *memcg;
1125 memcg = swap_cgroup_record(ent, NULL);
1127 /* If memcg is obsolete, memcg can be != ptr */
1128 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1129 mem_cgroup_put(memcg);
1133 /* add this page(page_cgroup) to the LRU we want. */
1134 mem_cgroup_lru_fixup(page);
1137 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1139 if (mem_cgroup_disabled())
1143 res_counter_uncharge(&mem->res, PAGE_SIZE);
1144 if (do_swap_account)
1145 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1151 * uncharge if !page_mapped(page)
1153 static struct mem_cgroup *
1154 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1156 struct page_cgroup *pc;
1157 struct mem_cgroup *mem = NULL;
1158 struct mem_cgroup_per_zone *mz;
1160 if (mem_cgroup_disabled())
1163 if (PageSwapCache(page))
1167 * Check if our page_cgroup is valid
1169 pc = lookup_page_cgroup(page);
1170 if (unlikely(!pc || !PageCgroupUsed(pc)))
1173 lock_page_cgroup(pc);
1175 mem = pc->mem_cgroup;
1177 if (!PageCgroupUsed(pc))
1181 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1182 if (page_mapped(page))
1185 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1186 if (!PageAnon(page)) { /* Shared memory */
1187 if (page->mapping && !page_is_file_cache(page))
1189 } else if (page_mapped(page)) /* Anon */
1196 res_counter_uncharge(&mem->res, PAGE_SIZE);
1197 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1198 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1200 mem_cgroup_charge_statistics(mem, pc, false);
1201 ClearPageCgroupUsed(pc);
1203 mz = page_cgroup_zoneinfo(pc);
1204 unlock_page_cgroup(pc);
1206 /* at swapout, this memcg will be accessed to record to swap */
1207 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1213 unlock_page_cgroup(pc);
1217 void mem_cgroup_uncharge_page(struct page *page)
1220 if (page_mapped(page))
1222 if (page->mapping && !PageAnon(page))
1224 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1227 void mem_cgroup_uncharge_cache_page(struct page *page)
1229 VM_BUG_ON(page_mapped(page));
1230 VM_BUG_ON(page->mapping);
1231 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1235 * called from __delete_from_swap_cache() and drop "page" account.
1236 * memcg information is recorded to swap_cgroup of "ent"
1238 void mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent)
1240 struct mem_cgroup *memcg;
1242 memcg = __mem_cgroup_uncharge_common(page,
1243 MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
1244 /* record memcg information */
1245 if (do_swap_account && memcg) {
1246 swap_cgroup_record(ent, memcg);
1247 mem_cgroup_get(memcg);
1250 css_put(&memcg->css);
1253 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1255 * called from swap_entry_free(). remove record in swap_cgroup and
1256 * uncharge "memsw" account.
1258 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1260 struct mem_cgroup *memcg;
1262 if (!do_swap_account)
1265 memcg = swap_cgroup_record(ent, NULL);
1267 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1268 mem_cgroup_put(memcg);
1274 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1277 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1279 struct page_cgroup *pc;
1280 struct mem_cgroup *mem = NULL;
1283 if (mem_cgroup_disabled())
1286 pc = lookup_page_cgroup(page);
1287 lock_page_cgroup(pc);
1288 if (PageCgroupUsed(pc)) {
1289 mem = pc->mem_cgroup;
1292 unlock_page_cgroup(pc);
1295 ret = mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem);
1302 /* remove redundant charge if migration failed*/
1303 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1304 struct page *oldpage, struct page *newpage)
1306 struct page *target, *unused;
1307 struct page_cgroup *pc;
1308 enum charge_type ctype;
1313 /* at migration success, oldpage->mapping is NULL. */
1314 if (oldpage->mapping) {
1322 if (PageAnon(target))
1323 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1324 else if (page_is_file_cache(target))
1325 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1327 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1329 /* unused page is not on radix-tree now. */
1331 __mem_cgroup_uncharge_common(unused, ctype);
1333 pc = lookup_page_cgroup(target);
1335 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1336 * So, double-counting is effectively avoided.
1338 __mem_cgroup_commit_charge(mem, pc, ctype);
1341 * Both of oldpage and newpage are still under lock_page().
1342 * Then, we don't have to care about race in radix-tree.
1343 * But we have to be careful that this page is unmapped or not.
1345 * There is a case for !page_mapped(). At the start of
1346 * migration, oldpage was mapped. But now, it's zapped.
1347 * But we know *target* page is not freed/reused under us.
1348 * mem_cgroup_uncharge_page() does all necessary checks.
1350 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1351 mem_cgroup_uncharge_page(target);
1355 * A call to try to shrink memory usage under specified resource controller.
1356 * This is typically used for page reclaiming for shmem for reducing side
1357 * effect of page allocation from shmem, which is used by some mem_cgroup.
1359 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1361 struct mem_cgroup *mem;
1363 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1365 if (mem_cgroup_disabled())
1371 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1372 if (unlikely(!mem)) {
1380 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask, true);
1381 progress += mem_cgroup_check_under_limit(mem);
1382 } while (!progress && --retry);
1391 * The inactive anon list should be small enough that the VM never has to
1392 * do too much work, but large enough that each inactive page has a chance
1393 * to be referenced again before it is swapped out.
1395 * this calculation is straightforward porting from
1396 * page_alloc.c::setup_per_zone_inactive_ratio().
1397 * it describe more detail.
1399 static void mem_cgroup_set_inactive_ratio(struct mem_cgroup *memcg)
1401 unsigned int gb, ratio;
1403 gb = res_counter_read_u64(&memcg->res, RES_LIMIT) >> 30;
1405 ratio = int_sqrt(10 * gb);
1409 memcg->inactive_ratio = ratio;
1413 static DEFINE_MUTEX(set_limit_mutex);
1415 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1416 unsigned long long val)
1419 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1424 while (retry_count) {
1425 if (signal_pending(current)) {
1430 * Rather than hide all in some function, I do this in
1431 * open coded manner. You see what this really does.
1432 * We have to guarantee mem->res.limit < mem->memsw.limit.
1434 mutex_lock(&set_limit_mutex);
1435 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1436 if (memswlimit < val) {
1438 mutex_unlock(&set_limit_mutex);
1441 ret = res_counter_set_limit(&memcg->res, val);
1442 mutex_unlock(&set_limit_mutex);
1447 progress = try_to_free_mem_cgroup_pages(memcg,
1449 if (!progress) retry_count--;
1453 mem_cgroup_set_inactive_ratio(memcg);
1458 int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1459 unsigned long long val)
1461 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1462 u64 memlimit, oldusage, curusage;
1465 if (!do_swap_account)
1468 while (retry_count) {
1469 if (signal_pending(current)) {
1474 * Rather than hide all in some function, I do this in
1475 * open coded manner. You see what this really does.
1476 * We have to guarantee mem->res.limit < mem->memsw.limit.
1478 mutex_lock(&set_limit_mutex);
1479 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1480 if (memlimit > val) {
1482 mutex_unlock(&set_limit_mutex);
1485 ret = res_counter_set_limit(&memcg->memsw, val);
1486 mutex_unlock(&set_limit_mutex);
1491 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1492 try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL, true);
1493 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1494 if (curusage >= oldusage)
1501 * This routine traverse page_cgroup in given list and drop them all.
1502 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1504 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1505 int node, int zid, enum lru_list lru)
1508 struct mem_cgroup_per_zone *mz;
1509 struct page_cgroup *pc, *busy;
1510 unsigned long flags, loop;
1511 struct list_head *list;
1514 zone = &NODE_DATA(node)->node_zones[zid];
1515 mz = mem_cgroup_zoneinfo(mem, node, zid);
1516 list = &mz->lists[lru];
1518 loop = MEM_CGROUP_ZSTAT(mz, lru);
1519 /* give some margin against EBUSY etc...*/
1524 spin_lock_irqsave(&zone->lru_lock, flags);
1525 if (list_empty(list)) {
1526 spin_unlock_irqrestore(&zone->lru_lock, flags);
1529 pc = list_entry(list->prev, struct page_cgroup, lru);
1531 list_move(&pc->lru, list);
1533 spin_unlock_irqrestore(&zone->lru_lock, flags);
1536 spin_unlock_irqrestore(&zone->lru_lock, flags);
1538 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
1542 if (ret == -EBUSY || ret == -EINVAL) {
1543 /* found lock contention or "pc" is obsolete. */
1550 if (!ret && !list_empty(list))
1556 * make mem_cgroup's charge to be 0 if there is no task.
1557 * This enables deleting this mem_cgroup.
1559 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1562 int node, zid, shrink;
1563 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1564 struct cgroup *cgrp = mem->css.cgroup;
1569 /* should free all ? */
1573 while (mem->res.usage > 0) {
1575 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1578 if (signal_pending(current))
1580 /* This is for making all *used* pages to be on LRU. */
1581 lru_add_drain_all();
1583 for_each_node_state(node, N_POSSIBLE) {
1584 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1587 ret = mem_cgroup_force_empty_list(mem,
1596 /* it seems parent cgroup doesn't have enough mem */
1607 /* returns EBUSY if there is a task or if we come here twice. */
1608 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
1612 /* we call try-to-free pages for make this cgroup empty */
1613 lru_add_drain_all();
1614 /* try to free all pages in this cgroup */
1616 while (nr_retries && mem->res.usage > 0) {
1619 if (signal_pending(current)) {
1623 progress = try_to_free_mem_cgroup_pages(mem,
1627 /* maybe some writeback is necessary */
1628 congestion_wait(WRITE, HZ/10);
1633 /* try move_account...there may be some *locked* pages. */
1640 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1642 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1646 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
1648 return mem_cgroup_from_cont(cont)->use_hierarchy;
1651 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
1655 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1656 struct cgroup *parent = cont->parent;
1657 struct mem_cgroup *parent_mem = NULL;
1660 parent_mem = mem_cgroup_from_cont(parent);
1664 * If parent's use_hiearchy is set, we can't make any modifications
1665 * in the child subtrees. If it is unset, then the change can
1666 * occur, provided the current cgroup has no children.
1668 * For the root cgroup, parent_mem is NULL, we allow value to be
1669 * set if there are no children.
1671 if ((!parent_mem || !parent_mem->use_hierarchy) &&
1672 (val == 1 || val == 0)) {
1673 if (list_empty(&cont->children))
1674 mem->use_hierarchy = val;
1684 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
1686 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1690 type = MEMFILE_TYPE(cft->private);
1691 name = MEMFILE_ATTR(cft->private);
1694 val = res_counter_read_u64(&mem->res, name);
1697 if (do_swap_account)
1698 val = res_counter_read_u64(&mem->memsw, name);
1707 * The user of this function is...
1710 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1713 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
1715 unsigned long long val;
1718 type = MEMFILE_TYPE(cft->private);
1719 name = MEMFILE_ATTR(cft->private);
1722 /* This function does all necessary parse...reuse it */
1723 ret = res_counter_memparse_write_strategy(buffer, &val);
1727 ret = mem_cgroup_resize_limit(memcg, val);
1729 ret = mem_cgroup_resize_memsw_limit(memcg, val);
1732 ret = -EINVAL; /* should be BUG() ? */
1738 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
1740 struct mem_cgroup *mem;
1743 mem = mem_cgroup_from_cont(cont);
1744 type = MEMFILE_TYPE(event);
1745 name = MEMFILE_ATTR(event);
1749 res_counter_reset_max(&mem->res);
1751 res_counter_reset_max(&mem->memsw);
1755 res_counter_reset_failcnt(&mem->res);
1757 res_counter_reset_failcnt(&mem->memsw);
1763 static const struct mem_cgroup_stat_desc {
1766 } mem_cgroup_stat_desc[] = {
1767 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1768 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
1769 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1770 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
1773 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1774 struct cgroup_map_cb *cb)
1776 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1777 struct mem_cgroup_stat *stat = &mem_cont->stat;
1780 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1783 val = mem_cgroup_read_stat(stat, i);
1784 val *= mem_cgroup_stat_desc[i].unit;
1785 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
1787 /* showing # of active pages */
1789 unsigned long active_anon, inactive_anon;
1790 unsigned long active_file, inactive_file;
1791 unsigned long unevictable;
1793 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1795 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1797 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1799 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1801 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1804 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1805 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1806 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1807 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
1808 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1815 static struct cftype mem_cgroup_files[] = {
1817 .name = "usage_in_bytes",
1818 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
1819 .read_u64 = mem_cgroup_read,
1822 .name = "max_usage_in_bytes",
1823 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
1824 .trigger = mem_cgroup_reset,
1825 .read_u64 = mem_cgroup_read,
1828 .name = "limit_in_bytes",
1829 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
1830 .write_string = mem_cgroup_write,
1831 .read_u64 = mem_cgroup_read,
1835 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
1836 .trigger = mem_cgroup_reset,
1837 .read_u64 = mem_cgroup_read,
1841 .read_map = mem_control_stat_show,
1844 .name = "force_empty",
1845 .trigger = mem_cgroup_force_empty_write,
1848 .name = "use_hierarchy",
1849 .write_u64 = mem_cgroup_hierarchy_write,
1850 .read_u64 = mem_cgroup_hierarchy_read,
1854 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1855 static struct cftype memsw_cgroup_files[] = {
1857 .name = "memsw.usage_in_bytes",
1858 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
1859 .read_u64 = mem_cgroup_read,
1862 .name = "memsw.max_usage_in_bytes",
1863 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
1864 .trigger = mem_cgroup_reset,
1865 .read_u64 = mem_cgroup_read,
1868 .name = "memsw.limit_in_bytes",
1869 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
1870 .write_string = mem_cgroup_write,
1871 .read_u64 = mem_cgroup_read,
1874 .name = "memsw.failcnt",
1875 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
1876 .trigger = mem_cgroup_reset,
1877 .read_u64 = mem_cgroup_read,
1881 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1883 if (!do_swap_account)
1885 return cgroup_add_files(cont, ss, memsw_cgroup_files,
1886 ARRAY_SIZE(memsw_cgroup_files));
1889 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
1895 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1897 struct mem_cgroup_per_node *pn;
1898 struct mem_cgroup_per_zone *mz;
1900 int zone, tmp = node;
1902 * This routine is called against possible nodes.
1903 * But it's BUG to call kmalloc() against offline node.
1905 * TODO: this routine can waste much memory for nodes which will
1906 * never be onlined. It's better to use memory hotplug callback
1909 if (!node_state(node, N_NORMAL_MEMORY))
1911 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1915 mem->info.nodeinfo[node] = pn;
1916 memset(pn, 0, sizeof(*pn));
1918 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1919 mz = &pn->zoneinfo[zone];
1921 INIT_LIST_HEAD(&mz->lists[l]);
1926 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1928 kfree(mem->info.nodeinfo[node]);
1931 static int mem_cgroup_size(void)
1933 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1934 return sizeof(struct mem_cgroup) + cpustat_size;
1937 static struct mem_cgroup *mem_cgroup_alloc(void)
1939 struct mem_cgroup *mem;
1940 int size = mem_cgroup_size();
1942 if (size < PAGE_SIZE)
1943 mem = kmalloc(size, GFP_KERNEL);
1945 mem = vmalloc(size);
1948 memset(mem, 0, size);
1953 * At destroying mem_cgroup, references from swap_cgroup can remain.
1954 * (scanning all at force_empty is too costly...)
1956 * Instead of clearing all references at force_empty, we remember
1957 * the number of reference from swap_cgroup and free mem_cgroup when
1958 * it goes down to 0.
1960 * When mem_cgroup is destroyed, mem->obsolete will be set to 0 and
1961 * entry which points to this memcg will be ignore at swapin.
1963 * Removal of cgroup itself succeeds regardless of refs from swap.
1966 static void mem_cgroup_free(struct mem_cgroup *mem)
1970 if (atomic_read(&mem->refcnt) > 0)
1974 for_each_node_state(node, N_POSSIBLE)
1975 free_mem_cgroup_per_zone_info(mem, node);
1977 if (mem_cgroup_size() < PAGE_SIZE)
1983 static void mem_cgroup_get(struct mem_cgroup *mem)
1985 atomic_inc(&mem->refcnt);
1988 static void mem_cgroup_put(struct mem_cgroup *mem)
1990 if (atomic_dec_and_test(&mem->refcnt)) {
1993 mem_cgroup_free(mem);
1998 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1999 static void __init enable_swap_cgroup(void)
2001 if (!mem_cgroup_disabled() && really_do_swap_account)
2002 do_swap_account = 1;
2005 static void __init enable_swap_cgroup(void)
2010 static struct cgroup_subsys_state *
2011 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2013 struct mem_cgroup *mem, *parent;
2016 mem = mem_cgroup_alloc();
2018 return ERR_PTR(-ENOMEM);
2020 for_each_node_state(node, N_POSSIBLE)
2021 if (alloc_mem_cgroup_per_zone_info(mem, node))
2024 if (cont->parent == NULL) {
2025 enable_swap_cgroup();
2028 parent = mem_cgroup_from_cont(cont->parent);
2029 mem->use_hierarchy = parent->use_hierarchy;
2032 if (parent && parent->use_hierarchy) {
2033 res_counter_init(&mem->res, &parent->res);
2034 res_counter_init(&mem->memsw, &parent->memsw);
2036 res_counter_init(&mem->res, NULL);
2037 res_counter_init(&mem->memsw, NULL);
2039 mem_cgroup_set_inactive_ratio(mem);
2040 mem->last_scanned_child = NULL;
2044 for_each_node_state(node, N_POSSIBLE)
2045 free_mem_cgroup_per_zone_info(mem, node);
2046 mem_cgroup_free(mem);
2047 return ERR_PTR(-ENOMEM);
2050 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
2051 struct cgroup *cont)
2053 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2055 mem_cgroup_force_empty(mem, false);
2058 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2059 struct cgroup *cont)
2061 mem_cgroup_free(mem_cgroup_from_cont(cont));
2064 static int mem_cgroup_populate(struct cgroup_subsys *ss,
2065 struct cgroup *cont)
2069 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2070 ARRAY_SIZE(mem_cgroup_files));
2073 ret = register_memsw_files(cont, ss);
2077 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2078 struct cgroup *cont,
2079 struct cgroup *old_cont,
2080 struct task_struct *p)
2083 * FIXME: It's better to move charges of this process from old
2084 * memcg to new memcg. But it's just on TODO-List now.
2088 struct cgroup_subsys mem_cgroup_subsys = {
2090 .subsys_id = mem_cgroup_subsys_id,
2091 .create = mem_cgroup_create,
2092 .pre_destroy = mem_cgroup_pre_destroy,
2093 .destroy = mem_cgroup_destroy,
2094 .populate = mem_cgroup_populate,
2095 .attach = mem_cgroup_move_task,
2099 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2101 static int __init disable_swap_account(char *s)
2103 really_do_swap_account = 0;
2106 __setup("noswapaccount", disable_swap_account);