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/slab.h>
31 #include <linux/swap.h>
32 #include <linux/spinlock.h>
34 #include <linux/seq_file.h>
35 #include <linux/vmalloc.h>
36 #include <linux/mm_inline.h>
37 #include <linux/page_cgroup.h>
39 #include <asm/uaccess.h>
41 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
42 #define MEM_CGROUP_RECLAIM_RETRIES 5
45 * Statistics for memory cgroup.
47 enum mem_cgroup_stat_index {
49 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
51 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
52 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
53 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
54 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
56 MEM_CGROUP_STAT_NSTATS,
59 struct mem_cgroup_stat_cpu {
60 s64 count[MEM_CGROUP_STAT_NSTATS];
61 } ____cacheline_aligned_in_smp;
63 struct mem_cgroup_stat {
64 struct mem_cgroup_stat_cpu cpustat[0];
68 * For accounting under irq disable, no need for increment preempt count.
70 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
71 enum mem_cgroup_stat_index idx, int val)
73 stat->count[idx] += val;
76 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
77 enum mem_cgroup_stat_index idx)
81 for_each_possible_cpu(cpu)
82 ret += stat->cpustat[cpu].count[idx];
87 * per-zone information in memory controller.
89 struct mem_cgroup_per_zone {
91 * spin_lock to protect the per cgroup LRU
94 struct list_head lists[NR_LRU_LISTS];
95 unsigned long count[NR_LRU_LISTS];
97 /* Macro for accessing counter */
98 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
100 struct mem_cgroup_per_node {
101 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
104 struct mem_cgroup_lru_info {
105 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
109 * The memory controller data structure. The memory controller controls both
110 * page cache and RSS per cgroup. We would eventually like to provide
111 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
112 * to help the administrator determine what knobs to tune.
114 * TODO: Add a water mark for the memory controller. Reclaim will begin when
115 * we hit the water mark. May be even add a low water mark, such that
116 * no reclaim occurs from a cgroup at it's low water mark, this is
117 * a feature that will be implemented much later in the future.
120 struct cgroup_subsys_state css;
122 * the counter to account for memory usage
124 struct res_counter res;
126 * Per cgroup active and inactive list, similar to the
127 * per zone LRU lists.
129 struct mem_cgroup_lru_info info;
131 int prev_priority; /* for recording reclaim priority */
133 * statistics. This must be placed at the end of memcg.
135 struct mem_cgroup_stat stat;
139 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
140 MEM_CGROUP_CHARGE_TYPE_MAPPED,
141 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
142 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
143 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
147 /* only for here (for easy reading.) */
148 #define PCGF_CACHE (1UL << PCG_CACHE)
149 #define PCGF_USED (1UL << PCG_USED)
150 #define PCGF_ACTIVE (1UL << PCG_ACTIVE)
151 #define PCGF_LOCK (1UL << PCG_LOCK)
152 #define PCGF_FILE (1UL << PCG_FILE)
153 static const unsigned long
154 pcg_default_flags[NR_CHARGE_TYPE] = {
155 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
156 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
157 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
162 * Always modified under lru lock. Then, not necessary to preempt_disable()
164 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
165 struct page_cgroup *pc,
168 int val = (charge)? 1 : -1;
169 struct mem_cgroup_stat *stat = &mem->stat;
170 struct mem_cgroup_stat_cpu *cpustat;
172 VM_BUG_ON(!irqs_disabled());
174 cpustat = &stat->cpustat[smp_processor_id()];
175 if (PageCgroupCache(pc))
176 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
178 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
181 __mem_cgroup_stat_add_safe(cpustat,
182 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
184 __mem_cgroup_stat_add_safe(cpustat,
185 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
188 static struct mem_cgroup_per_zone *
189 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
191 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
194 static struct mem_cgroup_per_zone *
195 page_cgroup_zoneinfo(struct page_cgroup *pc)
197 struct mem_cgroup *mem = pc->mem_cgroup;
198 int nid = page_cgroup_nid(pc);
199 int zid = page_cgroup_zid(pc);
201 return mem_cgroup_zoneinfo(mem, nid, zid);
204 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
208 struct mem_cgroup_per_zone *mz;
211 for_each_online_node(nid)
212 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
213 mz = mem_cgroup_zoneinfo(mem, nid, zid);
214 total += MEM_CGROUP_ZSTAT(mz, idx);
219 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
221 return container_of(cgroup_subsys_state(cont,
222 mem_cgroup_subsys_id), struct mem_cgroup,
226 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
229 * mm_update_next_owner() may clear mm->owner to NULL
230 * if it races with swapoff, page migration, etc.
231 * So this can be called with p == NULL.
236 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
237 struct mem_cgroup, css);
240 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
241 struct page_cgroup *pc)
245 if (PageCgroupUnevictable(pc))
246 lru = LRU_UNEVICTABLE;
248 if (PageCgroupActive(pc))
250 if (PageCgroupFile(pc))
254 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
256 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
260 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
261 struct page_cgroup *pc, bool hot)
265 if (PageCgroupUnevictable(pc))
266 lru = LRU_UNEVICTABLE;
268 if (PageCgroupActive(pc))
270 if (PageCgroupFile(pc))
274 MEM_CGROUP_ZSTAT(mz, lru) += 1;
276 list_add(&pc->lru, &mz->lists[lru]);
278 list_add_tail(&pc->lru, &mz->lists[lru]);
280 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
283 static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
285 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
286 int active = PageCgroupActive(pc);
287 int file = PageCgroupFile(pc);
288 int unevictable = PageCgroupUnevictable(pc);
289 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
290 (LRU_FILE * !!file + !!active);
295 MEM_CGROUP_ZSTAT(mz, from) -= 1;
297 * However this is done under mz->lru_lock, another flags, which
298 * are not related to LRU, will be modified from out-of-lock.
299 * We have to use atomic set/clear flags.
301 if (is_unevictable_lru(lru)) {
302 ClearPageCgroupActive(pc);
303 SetPageCgroupUnevictable(pc);
305 if (is_active_lru(lru))
306 SetPageCgroupActive(pc);
308 ClearPageCgroupActive(pc);
309 ClearPageCgroupUnevictable(pc);
312 MEM_CGROUP_ZSTAT(mz, lru) += 1;
313 list_move(&pc->lru, &mz->lists[lru]);
316 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
321 ret = task->mm && mm_match_cgroup(task->mm, mem);
327 * This routine assumes that the appropriate zone's lru lock is already held
329 void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
331 struct page_cgroup *pc;
332 struct mem_cgroup_per_zone *mz;
335 if (mem_cgroup_subsys.disabled)
339 * We cannot lock_page_cgroup while holding zone's lru_lock,
340 * because other holders of lock_page_cgroup can be interrupted
341 * with an attempt to rotate_reclaimable_page. But we cannot
342 * safely get to page_cgroup without it, so just try_lock it:
343 * mem_cgroup_isolate_pages allows for page left on wrong list.
345 pc = lookup_page_cgroup(page);
346 if (!trylock_page_cgroup(pc))
348 if (pc && PageCgroupUsed(pc)) {
349 mz = page_cgroup_zoneinfo(pc);
350 spin_lock_irqsave(&mz->lru_lock, flags);
351 __mem_cgroup_move_lists(pc, lru);
352 spin_unlock_irqrestore(&mz->lru_lock, flags);
354 unlock_page_cgroup(pc);
358 * Calculate mapped_ratio under memory controller. This will be used in
359 * vmscan.c for deteremining we have to reclaim mapped pages.
361 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
366 * usage is recorded in bytes. But, here, we assume the number of
367 * physical pages can be represented by "long" on any arch.
369 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
370 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
371 return (int)((rss * 100L) / total);
375 * prev_priority control...this will be used in memory reclaim path.
377 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
379 return mem->prev_priority;
382 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
384 if (priority < mem->prev_priority)
385 mem->prev_priority = priority;
388 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
390 mem->prev_priority = priority;
394 * Calculate # of pages to be scanned in this priority/zone.
397 * priority starts from "DEF_PRIORITY" and decremented in each loop.
398 * (see include/linux/mmzone.h)
401 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
402 int priority, enum lru_list lru)
405 int nid = zone->zone_pgdat->node_id;
406 int zid = zone_idx(zone);
407 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
409 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
411 return (nr_pages >> priority);
414 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
415 struct list_head *dst,
416 unsigned long *scanned, int order,
417 int mode, struct zone *z,
418 struct mem_cgroup *mem_cont,
419 int active, int file)
421 unsigned long nr_taken = 0;
425 struct list_head *src;
426 struct page_cgroup *pc, *tmp;
427 int nid = z->zone_pgdat->node_id;
428 int zid = zone_idx(z);
429 struct mem_cgroup_per_zone *mz;
430 int lru = LRU_FILE * !!file + !!active;
433 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
434 src = &mz->lists[lru];
436 spin_lock(&mz->lru_lock);
438 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
439 if (scan >= nr_to_scan)
441 if (unlikely(!PageCgroupUsed(pc)))
445 if (unlikely(!PageLRU(page)))
449 * TODO: play better with lumpy reclaim, grabbing anything.
451 if (PageUnevictable(page) ||
452 (PageActive(page) && !active) ||
453 (!PageActive(page) && active)) {
454 __mem_cgroup_move_lists(pc, page_lru(page));
459 list_move(&pc->lru, &pc_list);
461 if (__isolate_lru_page(page, mode, file) == 0) {
462 list_move(&page->lru, dst);
467 list_splice(&pc_list, src);
468 spin_unlock(&mz->lru_lock);
475 * Unlike exported interface, "oom" parameter is added. if oom==true,
476 * oom-killer can be invoked.
478 static int __mem_cgroup_try_charge(struct mm_struct *mm,
479 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
481 struct mem_cgroup *mem;
482 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
484 * We always charge the cgroup the mm_struct belongs to.
485 * The mm_struct's mem_cgroup changes on task migration if the
486 * thread group leader migrates. It's possible that mm is not
487 * set, if so charge the init_mm (happens for pagecache usage).
489 if (likely(!*memcg)) {
491 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
492 if (unlikely(!mem)) {
497 * For every charge from the cgroup, increment reference count
508 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
509 if (!(gfp_mask & __GFP_WAIT))
512 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
516 * try_to_free_mem_cgroup_pages() might not give us a full
517 * picture of reclaim. Some pages are reclaimed and might be
518 * moved to swap cache or just unmapped from the cgroup.
519 * Check the limit again to see if the reclaim reduced the
520 * current usage of the cgroup before giving up
522 if (res_counter_check_under_limit(&mem->res))
527 mem_cgroup_out_of_memory(mem, gfp_mask);
538 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
539 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
540 * @gfp_mask: gfp_mask for reclaim.
541 * @memcg: a pointer to memory cgroup which is charged against.
543 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
544 * memory cgroup from @mm is got and stored in *memcg.
546 * Returns 0 if success. -ENOMEM at failure.
547 * This call can invoke OOM-Killer.
550 int mem_cgroup_try_charge(struct mm_struct *mm,
551 gfp_t mask, struct mem_cgroup **memcg)
553 return __mem_cgroup_try_charge(mm, mask, memcg, true);
557 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
558 * USED state. If already USED, uncharge and return.
561 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
562 struct page_cgroup *pc,
563 enum charge_type ctype)
565 struct mem_cgroup_per_zone *mz;
568 /* try_charge() can return NULL to *memcg, taking care of it. */
572 lock_page_cgroup(pc);
573 if (unlikely(PageCgroupUsed(pc))) {
574 unlock_page_cgroup(pc);
575 res_counter_uncharge(&mem->res, PAGE_SIZE);
579 pc->mem_cgroup = mem;
581 * If a page is accounted as a page cache, insert to inactive list.
582 * If anon, insert to active list.
584 pc->flags = pcg_default_flags[ctype];
586 mz = page_cgroup_zoneinfo(pc);
588 spin_lock_irqsave(&mz->lru_lock, flags);
589 __mem_cgroup_add_list(mz, pc, true);
590 spin_unlock_irqrestore(&mz->lru_lock, flags);
591 unlock_page_cgroup(pc);
595 * mem_cgroup_move_account - move account of the page
596 * @pc: page_cgroup of the page.
597 * @from: mem_cgroup which the page is moved from.
598 * @to: mem_cgroup which the page is moved to. @from != @to.
600 * The caller must confirm following.
602 * 2. lru_lock of old mem_cgroup(@from) should be held.
604 * returns 0 at success,
605 * returns -EBUSY when lock is busy or "pc" is unstable.
607 * This function does "uncharge" from old cgroup but doesn't do "charge" to
608 * new cgroup. It should be done by a caller.
611 static int mem_cgroup_move_account(struct page_cgroup *pc,
612 struct mem_cgroup *from, struct mem_cgroup *to)
614 struct mem_cgroup_per_zone *from_mz, *to_mz;
618 VM_BUG_ON(!irqs_disabled());
619 VM_BUG_ON(from == to);
621 nid = page_cgroup_nid(pc);
622 zid = page_cgroup_zid(pc);
623 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
624 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
627 if (!trylock_page_cgroup(pc))
630 if (!PageCgroupUsed(pc))
633 if (pc->mem_cgroup != from)
636 if (spin_trylock(&to_mz->lru_lock)) {
637 __mem_cgroup_remove_list(from_mz, pc);
639 res_counter_uncharge(&from->res, PAGE_SIZE);
642 __mem_cgroup_add_list(to_mz, pc, false);
644 spin_unlock(&to_mz->lru_lock);
647 unlock_page_cgroup(pc);
652 * move charges to its parent.
655 static int mem_cgroup_move_parent(struct page_cgroup *pc,
656 struct mem_cgroup *child,
659 struct cgroup *cg = child->css.cgroup;
660 struct cgroup *pcg = cg->parent;
661 struct mem_cgroup *parent;
662 struct mem_cgroup_per_zone *mz;
670 parent = mem_cgroup_from_cont(pcg);
672 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
676 mz = mem_cgroup_zoneinfo(child,
677 page_cgroup_nid(pc), page_cgroup_zid(pc));
679 spin_lock_irqsave(&mz->lru_lock, flags);
680 ret = mem_cgroup_move_account(pc, child, parent);
681 spin_unlock_irqrestore(&mz->lru_lock, flags);
683 /* drop extra refcnt */
684 css_put(&parent->css);
685 /* uncharge if move fails */
687 res_counter_uncharge(&parent->res, PAGE_SIZE);
693 * Charge the memory controller for page usage.
695 * 0 if the charge was successful
696 * < 0 if the cgroup is over its limit
698 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
699 gfp_t gfp_mask, enum charge_type ctype,
700 struct mem_cgroup *memcg)
702 struct mem_cgroup *mem;
703 struct page_cgroup *pc;
706 pc = lookup_page_cgroup(page);
707 /* can happen at boot */
713 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
717 __mem_cgroup_commit_charge(mem, pc, ctype);
721 int mem_cgroup_newpage_charge(struct page *page,
722 struct mm_struct *mm, gfp_t gfp_mask)
724 if (mem_cgroup_subsys.disabled)
726 if (PageCompound(page))
729 * If already mapped, we don't have to account.
730 * If page cache, page->mapping has address_space.
731 * But page->mapping may have out-of-use anon_vma pointer,
732 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
735 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
739 return mem_cgroup_charge_common(page, mm, gfp_mask,
740 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
743 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
746 if (mem_cgroup_subsys.disabled)
748 if (PageCompound(page))
751 * Corner case handling. This is called from add_to_page_cache()
752 * in usual. But some FS (shmem) precharges this page before calling it
753 * and call add_to_page_cache() with GFP_NOWAIT.
755 * For GFP_NOWAIT case, the page may be pre-charged before calling
756 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
757 * charge twice. (It works but has to pay a bit larger cost.)
759 if (!(gfp_mask & __GFP_WAIT)) {
760 struct page_cgroup *pc;
763 pc = lookup_page_cgroup(page);
766 lock_page_cgroup(pc);
767 if (PageCgroupUsed(pc)) {
768 unlock_page_cgroup(pc);
771 unlock_page_cgroup(pc);
777 if (page_is_file_cache(page))
778 return mem_cgroup_charge_common(page, mm, gfp_mask,
779 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
781 return mem_cgroup_charge_common(page, mm, gfp_mask,
782 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
786 int mem_cgroup_cache_charge_swapin(struct page *page,
787 struct mm_struct *mm, gfp_t mask, bool locked)
791 if (mem_cgroup_subsys.disabled)
798 * If not locked, the page can be dropped from SwapCache until
801 if (PageSwapCache(page)) {
802 ret = mem_cgroup_charge_common(page, mm, mask,
803 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
812 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
814 struct page_cgroup *pc;
816 if (mem_cgroup_subsys.disabled)
820 pc = lookup_page_cgroup(page);
821 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
824 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
826 if (mem_cgroup_subsys.disabled)
830 res_counter_uncharge(&mem->res, PAGE_SIZE);
836 * uncharge if !page_mapped(page)
839 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
841 struct page_cgroup *pc;
842 struct mem_cgroup *mem;
843 struct mem_cgroup_per_zone *mz;
846 if (mem_cgroup_subsys.disabled)
849 if (PageSwapCache(page))
853 * Check if our page_cgroup is valid
855 pc = lookup_page_cgroup(page);
856 if (unlikely(!pc || !PageCgroupUsed(pc)))
859 lock_page_cgroup(pc);
861 if (!PageCgroupUsed(pc))
865 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
866 if (page_mapped(page))
869 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
870 if (!PageAnon(page)) { /* Shared memory */
871 if (page->mapping && !page_is_file_cache(page))
873 } else if (page_mapped(page)) /* Anon */
880 ClearPageCgroupUsed(pc);
881 mem = pc->mem_cgroup;
883 mz = page_cgroup_zoneinfo(pc);
884 spin_lock_irqsave(&mz->lru_lock, flags);
885 __mem_cgroup_remove_list(mz, pc);
886 spin_unlock_irqrestore(&mz->lru_lock, flags);
887 unlock_page_cgroup(pc);
889 res_counter_uncharge(&mem->res, PAGE_SIZE);
895 unlock_page_cgroup(pc);
899 void mem_cgroup_uncharge_page(struct page *page)
902 if (page_mapped(page))
904 if (page->mapping && !PageAnon(page))
906 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
909 void mem_cgroup_uncharge_cache_page(struct page *page)
911 VM_BUG_ON(page_mapped(page));
912 VM_BUG_ON(page->mapping);
913 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
916 void mem_cgroup_uncharge_swapcache(struct page *page)
918 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_SWAPOUT);
922 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
925 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
927 struct page_cgroup *pc;
928 struct mem_cgroup *mem = NULL;
931 if (mem_cgroup_subsys.disabled)
934 pc = lookup_page_cgroup(page);
935 lock_page_cgroup(pc);
936 if (PageCgroupUsed(pc)) {
937 mem = pc->mem_cgroup;
940 unlock_page_cgroup(pc);
943 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
950 /* remove redundant charge if migration failed*/
951 void mem_cgroup_end_migration(struct mem_cgroup *mem,
952 struct page *oldpage, struct page *newpage)
954 struct page *target, *unused;
955 struct page_cgroup *pc;
956 enum charge_type ctype;
961 /* at migration success, oldpage->mapping is NULL. */
962 if (oldpage->mapping) {
970 if (PageAnon(target))
971 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
972 else if (page_is_file_cache(target))
973 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
975 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
977 /* unused page is not on radix-tree now. */
979 __mem_cgroup_uncharge_common(unused, ctype);
981 pc = lookup_page_cgroup(target);
983 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
984 * So, double-counting is effectively avoided.
986 __mem_cgroup_commit_charge(mem, pc, ctype);
989 * Both of oldpage and newpage are still under lock_page().
990 * Then, we don't have to care about race in radix-tree.
991 * But we have to be careful that this page is unmapped or not.
993 * There is a case for !page_mapped(). At the start of
994 * migration, oldpage was mapped. But now, it's zapped.
995 * But we know *target* page is not freed/reused under us.
996 * mem_cgroup_uncharge_page() does all necessary checks.
998 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
999 mem_cgroup_uncharge_page(target);
1003 * A call to try to shrink memory usage under specified resource controller.
1004 * This is typically used for page reclaiming for shmem for reducing side
1005 * effect of page allocation from shmem, which is used by some mem_cgroup.
1007 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
1009 struct mem_cgroup *mem;
1011 int retry = MEM_CGROUP_RECLAIM_RETRIES;
1013 if (mem_cgroup_subsys.disabled)
1019 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
1020 if (unlikely(!mem)) {
1028 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
1029 progress += res_counter_check_under_limit(&mem->res);
1030 } while (!progress && --retry);
1038 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1039 unsigned long long val)
1042 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
1046 while (res_counter_set_limit(&memcg->res, val)) {
1047 if (signal_pending(current)) {
1055 progress = try_to_free_mem_cgroup_pages(memcg,
1056 GFP_HIGHUSER_MOVABLE);
1065 * This routine traverse page_cgroup in given list and drop them all.
1066 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1068 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1069 struct mem_cgroup_per_zone *mz,
1072 struct page_cgroup *pc, *busy;
1073 unsigned long flags;
1075 struct list_head *list;
1078 list = &mz->lists[lru];
1080 loop = MEM_CGROUP_ZSTAT(mz, lru);
1081 /* give some margin against EBUSY etc...*/
1086 spin_lock_irqsave(&mz->lru_lock, flags);
1087 if (list_empty(list)) {
1088 spin_unlock_irqrestore(&mz->lru_lock, flags);
1091 pc = list_entry(list->prev, struct page_cgroup, lru);
1093 list_move(&pc->lru, list);
1095 spin_unlock_irqrestore(&mz->lru_lock, flags);
1098 spin_unlock_irqrestore(&mz->lru_lock, flags);
1100 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1104 if (ret == -EBUSY || ret == -EINVAL) {
1105 /* found lock contention or "pc" is obsolete. */
1111 if (!ret && !list_empty(list))
1117 * make mem_cgroup's charge to be 0 if there is no task.
1118 * This enables deleting this mem_cgroup.
1120 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1123 int node, zid, shrink;
1124 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1125 struct cgroup *cgrp = mem->css.cgroup;
1130 /* should free all ? */
1134 while (mem->res.usage > 0) {
1136 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1139 if (signal_pending(current))
1141 /* This is for making all *used* pages to be on LRU. */
1142 lru_add_drain_all();
1144 for_each_node_state(node, N_POSSIBLE) {
1145 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1146 struct mem_cgroup_per_zone *mz;
1148 mz = mem_cgroup_zoneinfo(mem, node, zid);
1150 ret = mem_cgroup_force_empty_list(mem,
1159 /* it seems parent cgroup doesn't have enough mem */
1170 /* returns EBUSY if there is a task or if we come here twice. */
1171 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
1175 /* we call try-to-free pages for make this cgroup empty */
1176 lru_add_drain_all();
1177 /* try to free all pages in this cgroup */
1179 while (nr_retries && mem->res.usage > 0) {
1182 if (signal_pending(current)) {
1186 progress = try_to_free_mem_cgroup_pages(mem,
1187 GFP_HIGHUSER_MOVABLE);
1190 /* maybe some writeback is necessary */
1191 congestion_wait(WRITE, HZ/10);
1195 /* try move_account...there may be some *locked* pages. */
1202 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
1204 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
1208 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
1210 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
1214 * The user of this function is...
1217 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1220 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
1221 unsigned long long val;
1224 switch (cft->private) {
1226 /* This function does all necessary parse...reuse it */
1227 ret = res_counter_memparse_write_strategy(buffer, &val);
1229 ret = mem_cgroup_resize_limit(memcg, val);
1232 ret = -EINVAL; /* should be BUG() ? */
1238 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
1240 struct mem_cgroup *mem;
1242 mem = mem_cgroup_from_cont(cont);
1245 res_counter_reset_max(&mem->res);
1248 res_counter_reset_failcnt(&mem->res);
1254 static const struct mem_cgroup_stat_desc {
1257 } mem_cgroup_stat_desc[] = {
1258 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1259 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
1260 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1261 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
1264 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1265 struct cgroup_map_cb *cb)
1267 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1268 struct mem_cgroup_stat *stat = &mem_cont->stat;
1271 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1274 val = mem_cgroup_read_stat(stat, i);
1275 val *= mem_cgroup_stat_desc[i].unit;
1276 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
1278 /* showing # of active pages */
1280 unsigned long active_anon, inactive_anon;
1281 unsigned long active_file, inactive_file;
1282 unsigned long unevictable;
1284 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1286 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1288 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1290 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1292 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1295 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1296 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1297 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1298 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
1299 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1306 static struct cftype mem_cgroup_files[] = {
1308 .name = "usage_in_bytes",
1309 .private = RES_USAGE,
1310 .read_u64 = mem_cgroup_read,
1313 .name = "max_usage_in_bytes",
1314 .private = RES_MAX_USAGE,
1315 .trigger = mem_cgroup_reset,
1316 .read_u64 = mem_cgroup_read,
1319 .name = "limit_in_bytes",
1320 .private = RES_LIMIT,
1321 .write_string = mem_cgroup_write,
1322 .read_u64 = mem_cgroup_read,
1326 .private = RES_FAILCNT,
1327 .trigger = mem_cgroup_reset,
1328 .read_u64 = mem_cgroup_read,
1332 .read_map = mem_control_stat_show,
1335 .name = "force_empty",
1336 .trigger = mem_cgroup_force_empty_write,
1340 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1342 struct mem_cgroup_per_node *pn;
1343 struct mem_cgroup_per_zone *mz;
1345 int zone, tmp = node;
1347 * This routine is called against possible nodes.
1348 * But it's BUG to call kmalloc() against offline node.
1350 * TODO: this routine can waste much memory for nodes which will
1351 * never be onlined. It's better to use memory hotplug callback
1354 if (!node_state(node, N_NORMAL_MEMORY))
1356 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1360 mem->info.nodeinfo[node] = pn;
1361 memset(pn, 0, sizeof(*pn));
1363 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1364 mz = &pn->zoneinfo[zone];
1365 spin_lock_init(&mz->lru_lock);
1367 INIT_LIST_HEAD(&mz->lists[l]);
1372 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1374 kfree(mem->info.nodeinfo[node]);
1377 static int mem_cgroup_size(void)
1379 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1380 return sizeof(struct mem_cgroup) + cpustat_size;
1383 static struct mem_cgroup *mem_cgroup_alloc(void)
1385 struct mem_cgroup *mem;
1386 int size = mem_cgroup_size();
1388 if (size < PAGE_SIZE)
1389 mem = kmalloc(size, GFP_KERNEL);
1391 mem = vmalloc(size);
1394 memset(mem, 0, size);
1398 static void mem_cgroup_free(struct mem_cgroup *mem)
1400 if (mem_cgroup_size() < PAGE_SIZE)
1407 static struct cgroup_subsys_state *
1408 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1410 struct mem_cgroup *mem;
1413 mem = mem_cgroup_alloc();
1415 return ERR_PTR(-ENOMEM);
1417 res_counter_init(&mem->res);
1419 for_each_node_state(node, N_POSSIBLE)
1420 if (alloc_mem_cgroup_per_zone_info(mem, node))
1425 for_each_node_state(node, N_POSSIBLE)
1426 free_mem_cgroup_per_zone_info(mem, node);
1427 mem_cgroup_free(mem);
1428 return ERR_PTR(-ENOMEM);
1431 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1432 struct cgroup *cont)
1434 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1435 mem_cgroup_force_empty(mem, false);
1438 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1439 struct cgroup *cont)
1442 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1444 for_each_node_state(node, N_POSSIBLE)
1445 free_mem_cgroup_per_zone_info(mem, node);
1447 mem_cgroup_free(mem_cgroup_from_cont(cont));
1450 static int mem_cgroup_populate(struct cgroup_subsys *ss,
1451 struct cgroup *cont)
1453 return cgroup_add_files(cont, ss, mem_cgroup_files,
1454 ARRAY_SIZE(mem_cgroup_files));
1457 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1458 struct cgroup *cont,
1459 struct cgroup *old_cont,
1460 struct task_struct *p)
1462 struct mm_struct *mm;
1463 struct mem_cgroup *mem, *old_mem;
1465 mm = get_task_mm(p);
1469 mem = mem_cgroup_from_cont(cont);
1470 old_mem = mem_cgroup_from_cont(old_cont);
1473 * Only thread group leaders are allowed to migrate, the mm_struct is
1474 * in effect owned by the leader
1476 if (!thread_group_leader(p))
1483 struct cgroup_subsys mem_cgroup_subsys = {
1485 .subsys_id = mem_cgroup_subsys_id,
1486 .create = mem_cgroup_create,
1487 .pre_destroy = mem_cgroup_pre_destroy,
1488 .destroy = mem_cgroup_destroy,
1489 .populate = mem_cgroup_populate,
1490 .attach = mem_cgroup_move_task,