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/smp.h>
25 #include <linux/page-flags.h>
26 #include <linux/backing-dev.h>
27 #include <linux/bit_spinlock.h>
28 #include <linux/rcupdate.h>
29 #include <linux/slab.h>
30 #include <linux/swap.h>
31 #include <linux/spinlock.h>
33 #include <linux/seq_file.h>
34 #include <linux/vmalloc.h>
35 #include <linux/mm_inline.h>
36 #include <linux/page_cgroup.h>
38 #include <asm/uaccess.h>
40 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
41 #define MEM_CGROUP_RECLAIM_RETRIES 5
44 * Statistics for memory cgroup.
46 enum mem_cgroup_stat_index {
48 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
50 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
51 MEM_CGROUP_STAT_RSS, /* # of pages charged as rss */
52 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
53 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
55 MEM_CGROUP_STAT_NSTATS,
58 struct mem_cgroup_stat_cpu {
59 s64 count[MEM_CGROUP_STAT_NSTATS];
60 } ____cacheline_aligned_in_smp;
62 struct mem_cgroup_stat {
63 struct mem_cgroup_stat_cpu cpustat[NR_CPUS];
67 * For accounting under irq disable, no need for increment preempt count.
69 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
70 enum mem_cgroup_stat_index idx, int val)
72 stat->count[idx] += val;
75 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
76 enum mem_cgroup_stat_index idx)
80 for_each_possible_cpu(cpu)
81 ret += stat->cpustat[cpu].count[idx];
86 * per-zone information in memory controller.
88 struct mem_cgroup_per_zone {
90 * spin_lock to protect the per cgroup LRU
93 struct list_head lists[NR_LRU_LISTS];
94 unsigned long count[NR_LRU_LISTS];
96 /* Macro for accessing counter */
97 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
99 struct mem_cgroup_per_node {
100 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
103 struct mem_cgroup_lru_info {
104 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
108 * The memory controller data structure. The memory controller controls both
109 * page cache and RSS per cgroup. We would eventually like to provide
110 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
111 * to help the administrator determine what knobs to tune.
113 * TODO: Add a water mark for the memory controller. Reclaim will begin when
114 * we hit the water mark. May be even add a low water mark, such that
115 * no reclaim occurs from a cgroup at it's low water mark, this is
116 * a feature that will be implemented much later in the future.
119 struct cgroup_subsys_state css;
121 * the counter to account for memory usage
123 struct res_counter res;
125 * Per cgroup active and inactive list, similar to the
126 * per zone LRU lists.
128 struct mem_cgroup_lru_info info;
130 int prev_priority; /* for recording reclaim priority */
134 struct mem_cgroup_stat stat;
136 static struct mem_cgroup init_mem_cgroup;
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 */
146 /* only for here (for easy reading.) */
147 #define PCGF_CACHE (1UL << PCG_CACHE)
148 #define PCGF_USED (1UL << PCG_USED)
149 #define PCGF_ACTIVE (1UL << PCG_ACTIVE)
150 #define PCGF_LOCK (1UL << PCG_LOCK)
151 #define PCGF_FILE (1UL << PCG_FILE)
152 static const unsigned long
153 pcg_default_flags[NR_CHARGE_TYPE] = {
154 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
155 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
156 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
161 * Always modified under lru lock. Then, not necessary to preempt_disable()
163 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
164 struct page_cgroup *pc,
167 int val = (charge)? 1 : -1;
168 struct mem_cgroup_stat *stat = &mem->stat;
169 struct mem_cgroup_stat_cpu *cpustat;
171 VM_BUG_ON(!irqs_disabled());
173 cpustat = &stat->cpustat[smp_processor_id()];
174 if (PageCgroupCache(pc))
175 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
177 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
180 __mem_cgroup_stat_add_safe(cpustat,
181 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
183 __mem_cgroup_stat_add_safe(cpustat,
184 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
187 static struct mem_cgroup_per_zone *
188 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
190 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
193 static struct mem_cgroup_per_zone *
194 page_cgroup_zoneinfo(struct page_cgroup *pc)
196 struct mem_cgroup *mem = pc->mem_cgroup;
197 int nid = page_cgroup_nid(pc);
198 int zid = page_cgroup_zid(pc);
200 return mem_cgroup_zoneinfo(mem, nid, zid);
203 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
207 struct mem_cgroup_per_zone *mz;
210 for_each_online_node(nid)
211 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
212 mz = mem_cgroup_zoneinfo(mem, nid, zid);
213 total += MEM_CGROUP_ZSTAT(mz, idx);
218 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
220 return container_of(cgroup_subsys_state(cont,
221 mem_cgroup_subsys_id), struct mem_cgroup,
225 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
228 * mm_update_next_owner() may clear mm->owner to NULL
229 * if it races with swapoff, page migration, etc.
230 * So this can be called with p == NULL.
235 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
236 struct mem_cgroup, css);
239 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
240 struct page_cgroup *pc)
244 if (PageCgroupUnevictable(pc))
245 lru = LRU_UNEVICTABLE;
247 if (PageCgroupActive(pc))
249 if (PageCgroupFile(pc))
253 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
255 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
259 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
260 struct page_cgroup *pc)
264 if (PageCgroupUnevictable(pc))
265 lru = LRU_UNEVICTABLE;
267 if (PageCgroupActive(pc))
269 if (PageCgroupFile(pc))
273 MEM_CGROUP_ZSTAT(mz, lru) += 1;
274 list_add(&pc->lru, &mz->lists[lru]);
276 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
279 static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
281 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
282 int active = PageCgroupActive(pc);
283 int file = PageCgroupFile(pc);
284 int unevictable = PageCgroupUnevictable(pc);
285 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
286 (LRU_FILE * !!file + !!active);
291 MEM_CGROUP_ZSTAT(mz, from) -= 1;
293 * However this is done under mz->lru_lock, another flags, which
294 * are not related to LRU, will be modified from out-of-lock.
295 * We have to use atomic set/clear flags.
297 if (is_unevictable_lru(lru)) {
298 ClearPageCgroupActive(pc);
299 SetPageCgroupUnevictable(pc);
301 if (is_active_lru(lru))
302 SetPageCgroupActive(pc);
304 ClearPageCgroupActive(pc);
305 ClearPageCgroupUnevictable(pc);
308 MEM_CGROUP_ZSTAT(mz, lru) += 1;
309 list_move(&pc->lru, &mz->lists[lru]);
312 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
317 ret = task->mm && mm_match_cgroup(task->mm, mem);
323 * This routine assumes that the appropriate zone's lru lock is already held
325 void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
327 struct page_cgroup *pc;
328 struct mem_cgroup_per_zone *mz;
331 if (mem_cgroup_subsys.disabled)
335 * We cannot lock_page_cgroup while holding zone's lru_lock,
336 * because other holders of lock_page_cgroup can be interrupted
337 * with an attempt to rotate_reclaimable_page. But we cannot
338 * safely get to page_cgroup without it, so just try_lock it:
339 * mem_cgroup_isolate_pages allows for page left on wrong list.
341 pc = lookup_page_cgroup(page);
342 if (!trylock_page_cgroup(pc))
344 if (pc && PageCgroupUsed(pc)) {
345 mz = page_cgroup_zoneinfo(pc);
346 spin_lock_irqsave(&mz->lru_lock, flags);
347 __mem_cgroup_move_lists(pc, lru);
348 spin_unlock_irqrestore(&mz->lru_lock, flags);
350 unlock_page_cgroup(pc);
354 * Calculate mapped_ratio under memory controller. This will be used in
355 * vmscan.c for deteremining we have to reclaim mapped pages.
357 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
362 * usage is recorded in bytes. But, here, we assume the number of
363 * physical pages can be represented by "long" on any arch.
365 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
366 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
367 return (int)((rss * 100L) / total);
371 * prev_priority control...this will be used in memory reclaim path.
373 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
375 return mem->prev_priority;
378 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
380 if (priority < mem->prev_priority)
381 mem->prev_priority = priority;
384 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
386 mem->prev_priority = priority;
390 * Calculate # of pages to be scanned in this priority/zone.
393 * priority starts from "DEF_PRIORITY" and decremented in each loop.
394 * (see include/linux/mmzone.h)
397 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
398 int priority, enum lru_list lru)
401 int nid = zone->zone_pgdat->node_id;
402 int zid = zone_idx(zone);
403 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
405 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
407 return (nr_pages >> priority);
410 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
411 struct list_head *dst,
412 unsigned long *scanned, int order,
413 int mode, struct zone *z,
414 struct mem_cgroup *mem_cont,
415 int active, int file)
417 unsigned long nr_taken = 0;
421 struct list_head *src;
422 struct page_cgroup *pc, *tmp;
423 int nid = z->zone_pgdat->node_id;
424 int zid = zone_idx(z);
425 struct mem_cgroup_per_zone *mz;
426 int lru = LRU_FILE * !!file + !!active;
429 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
430 src = &mz->lists[lru];
432 spin_lock(&mz->lru_lock);
434 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
435 if (scan >= nr_to_scan)
437 if (unlikely(!PageCgroupUsed(pc)))
441 if (unlikely(!PageLRU(page)))
445 * TODO: play better with lumpy reclaim, grabbing anything.
447 if (PageUnevictable(page) ||
448 (PageActive(page) && !active) ||
449 (!PageActive(page) && active)) {
450 __mem_cgroup_move_lists(pc, page_lru(page));
455 list_move(&pc->lru, &pc_list);
457 if (__isolate_lru_page(page, mode, file) == 0) {
458 list_move(&page->lru, dst);
463 list_splice(&pc_list, src);
464 spin_unlock(&mz->lru_lock);
472 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
473 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
474 * @gfp_mask: gfp_mask for reclaim.
475 * @memcg: a pointer to memory cgroup which is charged against.
477 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
478 * memory cgroup from @mm is got and stored in *memcg.
480 * Returns 0 if success. -ENOMEM at failure.
483 int mem_cgroup_try_charge(struct mm_struct *mm,
484 gfp_t gfp_mask, struct mem_cgroup **memcg)
486 struct mem_cgroup *mem;
487 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
489 * We always charge the cgroup the mm_struct belongs to.
490 * The mm_struct's mem_cgroup changes on task migration if the
491 * thread group leader migrates. It's possible that mm is not
492 * set, if so charge the init_mm (happens for pagecache usage).
494 if (likely(!*memcg)) {
496 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
497 if (unlikely(!mem)) {
502 * For every charge from the cgroup, increment reference count
513 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
514 if (!(gfp_mask & __GFP_WAIT))
517 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
521 * try_to_free_mem_cgroup_pages() might not give us a full
522 * picture of reclaim. Some pages are reclaimed and might be
523 * moved to swap cache or just unmapped from the cgroup.
524 * Check the limit again to see if the reclaim reduced the
525 * current usage of the cgroup before giving up
527 if (res_counter_check_under_limit(&mem->res))
531 mem_cgroup_out_of_memory(mem, gfp_mask);
542 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
543 * USED state. If already USED, uncharge and return.
546 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
547 struct page_cgroup *pc,
548 enum charge_type ctype)
550 struct mem_cgroup_per_zone *mz;
553 /* try_charge() can return NULL to *memcg, taking care of it. */
557 lock_page_cgroup(pc);
558 if (unlikely(PageCgroupUsed(pc))) {
559 unlock_page_cgroup(pc);
560 res_counter_uncharge(&mem->res, PAGE_SIZE);
564 pc->mem_cgroup = mem;
566 * If a page is accounted as a page cache, insert to inactive list.
567 * If anon, insert to active list.
569 pc->flags = pcg_default_flags[ctype];
571 mz = page_cgroup_zoneinfo(pc);
573 spin_lock_irqsave(&mz->lru_lock, flags);
574 __mem_cgroup_add_list(mz, pc);
575 spin_unlock_irqrestore(&mz->lru_lock, flags);
576 unlock_page_cgroup(pc);
580 * Charge the memory controller for page usage.
582 * 0 if the charge was successful
583 * < 0 if the cgroup is over its limit
585 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
586 gfp_t gfp_mask, enum charge_type ctype,
587 struct mem_cgroup *memcg)
589 struct mem_cgroup *mem;
590 struct page_cgroup *pc;
593 pc = lookup_page_cgroup(page);
594 /* can happen at boot */
600 ret = mem_cgroup_try_charge(mm, gfp_mask, &mem);
604 __mem_cgroup_commit_charge(mem, pc, ctype);
608 int mem_cgroup_newpage_charge(struct page *page,
609 struct mm_struct *mm, gfp_t gfp_mask)
611 if (mem_cgroup_subsys.disabled)
613 if (PageCompound(page))
616 * If already mapped, we don't have to account.
617 * If page cache, page->mapping has address_space.
618 * But page->mapping may have out-of-use anon_vma pointer,
619 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
622 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
626 return mem_cgroup_charge_common(page, mm, gfp_mask,
627 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
630 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
633 if (mem_cgroup_subsys.disabled)
635 if (PageCompound(page))
638 * Corner case handling. This is called from add_to_page_cache()
639 * in usual. But some FS (shmem) precharges this page before calling it
640 * and call add_to_page_cache() with GFP_NOWAIT.
642 * For GFP_NOWAIT case, the page may be pre-charged before calling
643 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
644 * charge twice. (It works but has to pay a bit larger cost.)
646 if (!(gfp_mask & __GFP_WAIT)) {
647 struct page_cgroup *pc;
650 pc = lookup_page_cgroup(page);
653 lock_page_cgroup(pc);
654 if (PageCgroupUsed(pc)) {
655 unlock_page_cgroup(pc);
658 unlock_page_cgroup(pc);
664 if (page_is_file_cache(page))
665 return mem_cgroup_charge_common(page, mm, gfp_mask,
666 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
668 return mem_cgroup_charge_common(page, mm, gfp_mask,
669 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
672 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
674 struct page_cgroup *pc;
676 if (mem_cgroup_subsys.disabled)
680 pc = lookup_page_cgroup(page);
681 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
684 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
686 if (mem_cgroup_subsys.disabled)
690 res_counter_uncharge(&mem->res, PAGE_SIZE);
696 * uncharge if !page_mapped(page)
699 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
701 struct page_cgroup *pc;
702 struct mem_cgroup *mem;
703 struct mem_cgroup_per_zone *mz;
706 if (mem_cgroup_subsys.disabled)
710 * Check if our page_cgroup is valid
712 pc = lookup_page_cgroup(page);
713 if (unlikely(!pc || !PageCgroupUsed(pc)))
716 lock_page_cgroup(pc);
717 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED && page_mapped(page))
718 || !PageCgroupUsed(pc)) {
719 /* This happens at race in zap_pte_range() and do_swap_page()*/
720 unlock_page_cgroup(pc);
723 ClearPageCgroupUsed(pc);
724 mem = pc->mem_cgroup;
726 mz = page_cgroup_zoneinfo(pc);
727 spin_lock_irqsave(&mz->lru_lock, flags);
728 __mem_cgroup_remove_list(mz, pc);
729 spin_unlock_irqrestore(&mz->lru_lock, flags);
730 unlock_page_cgroup(pc);
732 res_counter_uncharge(&mem->res, PAGE_SIZE);
738 void mem_cgroup_uncharge_page(struct page *page)
741 if (page_mapped(page))
743 if (page->mapping && !PageAnon(page))
745 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
748 void mem_cgroup_uncharge_cache_page(struct page *page)
750 VM_BUG_ON(page_mapped(page));
751 VM_BUG_ON(page->mapping);
752 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
756 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
759 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
761 struct page_cgroup *pc;
762 struct mem_cgroup *mem = NULL;
765 if (mem_cgroup_subsys.disabled)
768 pc = lookup_page_cgroup(page);
769 lock_page_cgroup(pc);
770 if (PageCgroupUsed(pc)) {
771 mem = pc->mem_cgroup;
774 unlock_page_cgroup(pc);
777 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
784 /* remove redundant charge if migration failed*/
785 void mem_cgroup_end_migration(struct mem_cgroup *mem,
786 struct page *oldpage, struct page *newpage)
788 struct page *target, *unused;
789 struct page_cgroup *pc;
790 enum charge_type ctype;
795 /* at migration success, oldpage->mapping is NULL. */
796 if (oldpage->mapping) {
804 if (PageAnon(target))
805 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
806 else if (page_is_file_cache(target))
807 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
809 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
811 /* unused page is not on radix-tree now. */
812 if (unused && ctype != MEM_CGROUP_CHARGE_TYPE_MAPPED)
813 __mem_cgroup_uncharge_common(unused, ctype);
815 pc = lookup_page_cgroup(target);
817 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
818 * So, double-counting is effectively avoided.
820 __mem_cgroup_commit_charge(mem, pc, ctype);
823 * Both of oldpage and newpage are still under lock_page().
824 * Then, we don't have to care about race in radix-tree.
825 * But we have to be careful that this page is unmapped or not.
827 * There is a case for !page_mapped(). At the start of
828 * migration, oldpage was mapped. But now, it's zapped.
829 * But we know *target* page is not freed/reused under us.
830 * mem_cgroup_uncharge_page() does all necessary checks.
832 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
833 mem_cgroup_uncharge_page(target);
837 * A call to try to shrink memory usage under specified resource controller.
838 * This is typically used for page reclaiming for shmem for reducing side
839 * effect of page allocation from shmem, which is used by some mem_cgroup.
841 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
843 struct mem_cgroup *mem;
845 int retry = MEM_CGROUP_RECLAIM_RETRIES;
847 if (mem_cgroup_subsys.disabled)
853 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
854 if (unlikely(!mem)) {
862 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
863 progress += res_counter_check_under_limit(&mem->res);
864 } while (!progress && --retry);
872 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
873 unsigned long long val)
876 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
880 while (res_counter_set_limit(&memcg->res, val)) {
881 if (signal_pending(current)) {
889 progress = try_to_free_mem_cgroup_pages(memcg,
890 GFP_HIGHUSER_MOVABLE);
899 * This routine traverse page_cgroup in given list and drop them all.
900 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
902 #define FORCE_UNCHARGE_BATCH (128)
903 static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
904 struct mem_cgroup_per_zone *mz,
907 struct page_cgroup *pc;
909 int count = FORCE_UNCHARGE_BATCH;
911 struct list_head *list;
913 list = &mz->lists[lru];
915 spin_lock_irqsave(&mz->lru_lock, flags);
916 while (!list_empty(list)) {
917 pc = list_entry(list->prev, struct page_cgroup, lru);
919 if (!PageCgroupUsed(pc))
922 spin_unlock_irqrestore(&mz->lru_lock, flags);
924 * Check if this page is on LRU. !LRU page can be found
925 * if it's under page migration.
928 __mem_cgroup_uncharge_common(page,
929 MEM_CGROUP_CHARGE_TYPE_FORCE);
932 count = FORCE_UNCHARGE_BATCH;
936 spin_lock_irqsave(&mz->lru_lock, flags);
939 spin_lock_irqsave(&mz->lru_lock, flags);
941 spin_unlock_irqrestore(&mz->lru_lock, flags);
945 * make mem_cgroup's charge to be 0 if there is no task.
946 * This enables deleting this mem_cgroup.
948 static int mem_cgroup_force_empty(struct mem_cgroup *mem)
955 * page reclaim code (kswapd etc..) will move pages between
956 * active_list <-> inactive_list while we don't take a lock.
957 * So, we have to do loop here until all lists are empty.
959 while (mem->res.usage > 0) {
960 if (atomic_read(&mem->css.cgroup->count) > 0)
962 /* This is for making all *used* pages to be on LRU. */
964 for_each_node_state(node, N_POSSIBLE)
965 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
966 struct mem_cgroup_per_zone *mz;
968 mz = mem_cgroup_zoneinfo(mem, node, zid);
970 mem_cgroup_force_empty_list(mem, mz, l);
980 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
982 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
986 * The user of this function is...
989 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
992 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
993 unsigned long long val;
996 switch (cft->private) {
998 /* This function does all necessary parse...reuse it */
999 ret = res_counter_memparse_write_strategy(buffer, &val);
1001 ret = mem_cgroup_resize_limit(memcg, val);
1004 ret = -EINVAL; /* should be BUG() ? */
1010 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
1012 struct mem_cgroup *mem;
1014 mem = mem_cgroup_from_cont(cont);
1017 res_counter_reset_max(&mem->res);
1020 res_counter_reset_failcnt(&mem->res);
1026 static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
1028 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
1031 static const struct mem_cgroup_stat_desc {
1034 } mem_cgroup_stat_desc[] = {
1035 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1036 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
1037 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1038 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
1041 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1042 struct cgroup_map_cb *cb)
1044 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1045 struct mem_cgroup_stat *stat = &mem_cont->stat;
1048 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1051 val = mem_cgroup_read_stat(stat, i);
1052 val *= mem_cgroup_stat_desc[i].unit;
1053 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
1055 /* showing # of active pages */
1057 unsigned long active_anon, inactive_anon;
1058 unsigned long active_file, inactive_file;
1059 unsigned long unevictable;
1061 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1063 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1065 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1067 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1069 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1072 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1073 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1074 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1075 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
1076 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1082 static struct cftype mem_cgroup_files[] = {
1084 .name = "usage_in_bytes",
1085 .private = RES_USAGE,
1086 .read_u64 = mem_cgroup_read,
1089 .name = "max_usage_in_bytes",
1090 .private = RES_MAX_USAGE,
1091 .trigger = mem_cgroup_reset,
1092 .read_u64 = mem_cgroup_read,
1095 .name = "limit_in_bytes",
1096 .private = RES_LIMIT,
1097 .write_string = mem_cgroup_write,
1098 .read_u64 = mem_cgroup_read,
1102 .private = RES_FAILCNT,
1103 .trigger = mem_cgroup_reset,
1104 .read_u64 = mem_cgroup_read,
1107 .name = "force_empty",
1108 .trigger = mem_force_empty_write,
1112 .read_map = mem_control_stat_show,
1116 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1118 struct mem_cgroup_per_node *pn;
1119 struct mem_cgroup_per_zone *mz;
1121 int zone, tmp = node;
1123 * This routine is called against possible nodes.
1124 * But it's BUG to call kmalloc() against offline node.
1126 * TODO: this routine can waste much memory for nodes which will
1127 * never be onlined. It's better to use memory hotplug callback
1130 if (!node_state(node, N_NORMAL_MEMORY))
1132 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1136 mem->info.nodeinfo[node] = pn;
1137 memset(pn, 0, sizeof(*pn));
1139 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1140 mz = &pn->zoneinfo[zone];
1141 spin_lock_init(&mz->lru_lock);
1143 INIT_LIST_HEAD(&mz->lists[l]);
1148 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1150 kfree(mem->info.nodeinfo[node]);
1153 static struct mem_cgroup *mem_cgroup_alloc(void)
1155 struct mem_cgroup *mem;
1157 if (sizeof(*mem) < PAGE_SIZE)
1158 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1160 mem = vmalloc(sizeof(*mem));
1163 memset(mem, 0, sizeof(*mem));
1167 static void mem_cgroup_free(struct mem_cgroup *mem)
1169 if (sizeof(*mem) < PAGE_SIZE)
1176 static struct cgroup_subsys_state *
1177 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1179 struct mem_cgroup *mem;
1182 if (unlikely((cont->parent) == NULL)) {
1183 mem = &init_mem_cgroup;
1185 mem = mem_cgroup_alloc();
1187 return ERR_PTR(-ENOMEM);
1190 res_counter_init(&mem->res);
1192 for_each_node_state(node, N_POSSIBLE)
1193 if (alloc_mem_cgroup_per_zone_info(mem, node))
1198 for_each_node_state(node, N_POSSIBLE)
1199 free_mem_cgroup_per_zone_info(mem, node);
1200 if (cont->parent != NULL)
1201 mem_cgroup_free(mem);
1202 return ERR_PTR(-ENOMEM);
1205 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1206 struct cgroup *cont)
1208 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1209 mem_cgroup_force_empty(mem);
1212 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1213 struct cgroup *cont)
1216 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1218 for_each_node_state(node, N_POSSIBLE)
1219 free_mem_cgroup_per_zone_info(mem, node);
1221 mem_cgroup_free(mem_cgroup_from_cont(cont));
1224 static int mem_cgroup_populate(struct cgroup_subsys *ss,
1225 struct cgroup *cont)
1227 return cgroup_add_files(cont, ss, mem_cgroup_files,
1228 ARRAY_SIZE(mem_cgroup_files));
1231 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1232 struct cgroup *cont,
1233 struct cgroup *old_cont,
1234 struct task_struct *p)
1236 struct mm_struct *mm;
1237 struct mem_cgroup *mem, *old_mem;
1239 mm = get_task_mm(p);
1243 mem = mem_cgroup_from_cont(cont);
1244 old_mem = mem_cgroup_from_cont(old_cont);
1247 * Only thread group leaders are allowed to migrate, the mm_struct is
1248 * in effect owned by the leader
1250 if (!thread_group_leader(p))
1257 struct cgroup_subsys mem_cgroup_subsys = {
1259 .subsys_id = mem_cgroup_subsys_id,
1260 .create = mem_cgroup_create,
1261 .pre_destroy = mem_cgroup_pre_destroy,
1262 .destroy = mem_cgroup_destroy,
1263 .populate = mem_cgroup_populate,
1264 .attach = mem_cgroup_move_task,