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[0];
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 */
132 * statistics. This must be placed at the end of memcg.
134 struct mem_cgroup_stat stat;
138 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
139 MEM_CGROUP_CHARGE_TYPE_MAPPED,
140 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
141 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
145 /* only for here (for easy reading.) */
146 #define PCGF_CACHE (1UL << PCG_CACHE)
147 #define PCGF_USED (1UL << PCG_USED)
148 #define PCGF_ACTIVE (1UL << PCG_ACTIVE)
149 #define PCGF_LOCK (1UL << PCG_LOCK)
150 #define PCGF_FILE (1UL << PCG_FILE)
151 static const unsigned long
152 pcg_default_flags[NR_CHARGE_TYPE] = {
153 PCGF_CACHE | PCGF_FILE | PCGF_USED | PCGF_LOCK, /* File Cache */
154 PCGF_ACTIVE | PCGF_USED | PCGF_LOCK, /* Anon */
155 PCGF_ACTIVE | PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
160 * Always modified under lru lock. Then, not necessary to preempt_disable()
162 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
163 struct page_cgroup *pc,
166 int val = (charge)? 1 : -1;
167 struct mem_cgroup_stat *stat = &mem->stat;
168 struct mem_cgroup_stat_cpu *cpustat;
170 VM_BUG_ON(!irqs_disabled());
172 cpustat = &stat->cpustat[smp_processor_id()];
173 if (PageCgroupCache(pc))
174 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
176 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
179 __mem_cgroup_stat_add_safe(cpustat,
180 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
182 __mem_cgroup_stat_add_safe(cpustat,
183 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
186 static struct mem_cgroup_per_zone *
187 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
189 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
192 static struct mem_cgroup_per_zone *
193 page_cgroup_zoneinfo(struct page_cgroup *pc)
195 struct mem_cgroup *mem = pc->mem_cgroup;
196 int nid = page_cgroup_nid(pc);
197 int zid = page_cgroup_zid(pc);
199 return mem_cgroup_zoneinfo(mem, nid, zid);
202 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
206 struct mem_cgroup_per_zone *mz;
209 for_each_online_node(nid)
210 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
211 mz = mem_cgroup_zoneinfo(mem, nid, zid);
212 total += MEM_CGROUP_ZSTAT(mz, idx);
217 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
219 return container_of(cgroup_subsys_state(cont,
220 mem_cgroup_subsys_id), struct mem_cgroup,
224 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
227 * mm_update_next_owner() may clear mm->owner to NULL
228 * if it races with swapoff, page migration, etc.
229 * So this can be called with p == NULL.
234 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
235 struct mem_cgroup, css);
238 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
239 struct page_cgroup *pc)
243 if (PageCgroupUnevictable(pc))
244 lru = LRU_UNEVICTABLE;
246 if (PageCgroupActive(pc))
248 if (PageCgroupFile(pc))
252 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
254 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, false);
258 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
259 struct page_cgroup *pc, bool hot)
263 if (PageCgroupUnevictable(pc))
264 lru = LRU_UNEVICTABLE;
266 if (PageCgroupActive(pc))
268 if (PageCgroupFile(pc))
272 MEM_CGROUP_ZSTAT(mz, lru) += 1;
274 list_add(&pc->lru, &mz->lists[lru]);
276 list_add_tail(&pc->lru, &mz->lists[lru]);
278 mem_cgroup_charge_statistics(pc->mem_cgroup, pc, true);
281 static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
283 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
284 int active = PageCgroupActive(pc);
285 int file = PageCgroupFile(pc);
286 int unevictable = PageCgroupUnevictable(pc);
287 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
288 (LRU_FILE * !!file + !!active);
293 MEM_CGROUP_ZSTAT(mz, from) -= 1;
295 * However this is done under mz->lru_lock, another flags, which
296 * are not related to LRU, will be modified from out-of-lock.
297 * We have to use atomic set/clear flags.
299 if (is_unevictable_lru(lru)) {
300 ClearPageCgroupActive(pc);
301 SetPageCgroupUnevictable(pc);
303 if (is_active_lru(lru))
304 SetPageCgroupActive(pc);
306 ClearPageCgroupActive(pc);
307 ClearPageCgroupUnevictable(pc);
310 MEM_CGROUP_ZSTAT(mz, lru) += 1;
311 list_move(&pc->lru, &mz->lists[lru]);
314 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
319 ret = task->mm && mm_match_cgroup(task->mm, mem);
325 * This routine assumes that the appropriate zone's lru lock is already held
327 void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
329 struct page_cgroup *pc;
330 struct mem_cgroup_per_zone *mz;
333 if (mem_cgroup_subsys.disabled)
337 * We cannot lock_page_cgroup while holding zone's lru_lock,
338 * because other holders of lock_page_cgroup can be interrupted
339 * with an attempt to rotate_reclaimable_page. But we cannot
340 * safely get to page_cgroup without it, so just try_lock it:
341 * mem_cgroup_isolate_pages allows for page left on wrong list.
343 pc = lookup_page_cgroup(page);
344 if (!trylock_page_cgroup(pc))
346 if (pc && PageCgroupUsed(pc)) {
347 mz = page_cgroup_zoneinfo(pc);
348 spin_lock_irqsave(&mz->lru_lock, flags);
349 __mem_cgroup_move_lists(pc, lru);
350 spin_unlock_irqrestore(&mz->lru_lock, flags);
352 unlock_page_cgroup(pc);
356 * Calculate mapped_ratio under memory controller. This will be used in
357 * vmscan.c for deteremining we have to reclaim mapped pages.
359 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
364 * usage is recorded in bytes. But, here, we assume the number of
365 * physical pages can be represented by "long" on any arch.
367 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
368 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
369 return (int)((rss * 100L) / total);
373 * prev_priority control...this will be used in memory reclaim path.
375 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
377 return mem->prev_priority;
380 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
382 if (priority < mem->prev_priority)
383 mem->prev_priority = priority;
386 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
388 mem->prev_priority = priority;
392 * Calculate # of pages to be scanned in this priority/zone.
395 * priority starts from "DEF_PRIORITY" and decremented in each loop.
396 * (see include/linux/mmzone.h)
399 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
400 int priority, enum lru_list lru)
403 int nid = zone->zone_pgdat->node_id;
404 int zid = zone_idx(zone);
405 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
407 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
409 return (nr_pages >> priority);
412 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
413 struct list_head *dst,
414 unsigned long *scanned, int order,
415 int mode, struct zone *z,
416 struct mem_cgroup *mem_cont,
417 int active, int file)
419 unsigned long nr_taken = 0;
423 struct list_head *src;
424 struct page_cgroup *pc, *tmp;
425 int nid = z->zone_pgdat->node_id;
426 int zid = zone_idx(z);
427 struct mem_cgroup_per_zone *mz;
428 int lru = LRU_FILE * !!file + !!active;
431 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
432 src = &mz->lists[lru];
434 spin_lock(&mz->lru_lock);
436 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
437 if (scan >= nr_to_scan)
439 if (unlikely(!PageCgroupUsed(pc)))
443 if (unlikely(!PageLRU(page)))
447 * TODO: play better with lumpy reclaim, grabbing anything.
449 if (PageUnevictable(page) ||
450 (PageActive(page) && !active) ||
451 (!PageActive(page) && active)) {
452 __mem_cgroup_move_lists(pc, page_lru(page));
457 list_move(&pc->lru, &pc_list);
459 if (__isolate_lru_page(page, mode, file) == 0) {
460 list_move(&page->lru, dst);
465 list_splice(&pc_list, src);
466 spin_unlock(&mz->lru_lock);
473 * Unlike exported interface, "oom" parameter is added. if oom==true,
474 * oom-killer can be invoked.
476 static int __mem_cgroup_try_charge(struct mm_struct *mm,
477 gfp_t gfp_mask, struct mem_cgroup **memcg, bool oom)
479 struct mem_cgroup *mem;
480 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
482 * We always charge the cgroup the mm_struct belongs to.
483 * The mm_struct's mem_cgroup changes on task migration if the
484 * thread group leader migrates. It's possible that mm is not
485 * set, if so charge the init_mm (happens for pagecache usage).
487 if (likely(!*memcg)) {
489 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
490 if (unlikely(!mem)) {
495 * For every charge from the cgroup, increment reference count
506 while (unlikely(res_counter_charge(&mem->res, PAGE_SIZE))) {
507 if (!(gfp_mask & __GFP_WAIT))
510 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
514 * try_to_free_mem_cgroup_pages() might not give us a full
515 * picture of reclaim. Some pages are reclaimed and might be
516 * moved to swap cache or just unmapped from the cgroup.
517 * Check the limit again to see if the reclaim reduced the
518 * current usage of the cgroup before giving up
520 if (res_counter_check_under_limit(&mem->res))
525 mem_cgroup_out_of_memory(mem, gfp_mask);
536 * mem_cgroup_try_charge - get charge of PAGE_SIZE.
537 * @mm: an mm_struct which is charged against. (when *memcg is NULL)
538 * @gfp_mask: gfp_mask for reclaim.
539 * @memcg: a pointer to memory cgroup which is charged against.
541 * charge against memory cgroup pointed by *memcg. if *memcg == NULL, estimated
542 * memory cgroup from @mm is got and stored in *memcg.
544 * Returns 0 if success. -ENOMEM at failure.
545 * This call can invoke OOM-Killer.
548 int mem_cgroup_try_charge(struct mm_struct *mm,
549 gfp_t mask, struct mem_cgroup **memcg)
551 return __mem_cgroup_try_charge(mm, mask, memcg, true);
555 * commit a charge got by mem_cgroup_try_charge() and makes page_cgroup to be
556 * USED state. If already USED, uncharge and return.
559 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
560 struct page_cgroup *pc,
561 enum charge_type ctype)
563 struct mem_cgroup_per_zone *mz;
566 /* try_charge() can return NULL to *memcg, taking care of it. */
570 lock_page_cgroup(pc);
571 if (unlikely(PageCgroupUsed(pc))) {
572 unlock_page_cgroup(pc);
573 res_counter_uncharge(&mem->res, PAGE_SIZE);
577 pc->mem_cgroup = mem;
579 * If a page is accounted as a page cache, insert to inactive list.
580 * If anon, insert to active list.
582 pc->flags = pcg_default_flags[ctype];
584 mz = page_cgroup_zoneinfo(pc);
586 spin_lock_irqsave(&mz->lru_lock, flags);
587 __mem_cgroup_add_list(mz, pc, true);
588 spin_unlock_irqrestore(&mz->lru_lock, flags);
589 unlock_page_cgroup(pc);
593 * mem_cgroup_move_account - move account of the page
594 * @pc: page_cgroup of the page.
595 * @from: mem_cgroup which the page is moved from.
596 * @to: mem_cgroup which the page is moved to. @from != @to.
598 * The caller must confirm following.
600 * 2. lru_lock of old mem_cgroup(@from) should be held.
602 * returns 0 at success,
603 * returns -EBUSY when lock is busy or "pc" is unstable.
605 * This function does "uncharge" from old cgroup but doesn't do "charge" to
606 * new cgroup. It should be done by a caller.
609 static int mem_cgroup_move_account(struct page_cgroup *pc,
610 struct mem_cgroup *from, struct mem_cgroup *to)
612 struct mem_cgroup_per_zone *from_mz, *to_mz;
616 VM_BUG_ON(!irqs_disabled());
617 VM_BUG_ON(from == to);
619 nid = page_cgroup_nid(pc);
620 zid = page_cgroup_zid(pc);
621 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
622 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
625 if (!trylock_page_cgroup(pc))
628 if (!PageCgroupUsed(pc))
631 if (pc->mem_cgroup != from)
634 if (spin_trylock(&to_mz->lru_lock)) {
635 __mem_cgroup_remove_list(from_mz, pc);
637 res_counter_uncharge(&from->res, PAGE_SIZE);
640 __mem_cgroup_add_list(to_mz, pc, false);
642 spin_unlock(&to_mz->lru_lock);
645 unlock_page_cgroup(pc);
650 * move charges to its parent.
653 static int mem_cgroup_move_parent(struct page_cgroup *pc,
654 struct mem_cgroup *child,
657 struct cgroup *cg = child->css.cgroup;
658 struct cgroup *pcg = cg->parent;
659 struct mem_cgroup *parent;
660 struct mem_cgroup_per_zone *mz;
668 parent = mem_cgroup_from_cont(pcg);
670 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
674 mz = mem_cgroup_zoneinfo(child,
675 page_cgroup_nid(pc), page_cgroup_zid(pc));
677 spin_lock_irqsave(&mz->lru_lock, flags);
678 ret = mem_cgroup_move_account(pc, child, parent);
679 spin_unlock_irqrestore(&mz->lru_lock, flags);
681 /* drop extra refcnt */
682 css_put(&parent->css);
683 /* uncharge if move fails */
685 res_counter_uncharge(&parent->res, PAGE_SIZE);
691 * Charge the memory controller for page usage.
693 * 0 if the charge was successful
694 * < 0 if the cgroup is over its limit
696 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
697 gfp_t gfp_mask, enum charge_type ctype,
698 struct mem_cgroup *memcg)
700 struct mem_cgroup *mem;
701 struct page_cgroup *pc;
704 pc = lookup_page_cgroup(page);
705 /* can happen at boot */
711 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
715 __mem_cgroup_commit_charge(mem, pc, ctype);
719 int mem_cgroup_newpage_charge(struct page *page,
720 struct mm_struct *mm, gfp_t gfp_mask)
722 if (mem_cgroup_subsys.disabled)
724 if (PageCompound(page))
727 * If already mapped, we don't have to account.
728 * If page cache, page->mapping has address_space.
729 * But page->mapping may have out-of-use anon_vma pointer,
730 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
733 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
737 return mem_cgroup_charge_common(page, mm, gfp_mask,
738 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
741 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
744 if (mem_cgroup_subsys.disabled)
746 if (PageCompound(page))
749 * Corner case handling. This is called from add_to_page_cache()
750 * in usual. But some FS (shmem) precharges this page before calling it
751 * and call add_to_page_cache() with GFP_NOWAIT.
753 * For GFP_NOWAIT case, the page may be pre-charged before calling
754 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
755 * charge twice. (It works but has to pay a bit larger cost.)
757 if (!(gfp_mask & __GFP_WAIT)) {
758 struct page_cgroup *pc;
761 pc = lookup_page_cgroup(page);
764 lock_page_cgroup(pc);
765 if (PageCgroupUsed(pc)) {
766 unlock_page_cgroup(pc);
769 unlock_page_cgroup(pc);
775 if (page_is_file_cache(page))
776 return mem_cgroup_charge_common(page, mm, gfp_mask,
777 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
779 return mem_cgroup_charge_common(page, mm, gfp_mask,
780 MEM_CGROUP_CHARGE_TYPE_SHMEM, NULL);
783 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
785 struct page_cgroup *pc;
787 if (mem_cgroup_subsys.disabled)
791 pc = lookup_page_cgroup(page);
792 __mem_cgroup_commit_charge(ptr, pc, MEM_CGROUP_CHARGE_TYPE_MAPPED);
795 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
797 if (mem_cgroup_subsys.disabled)
801 res_counter_uncharge(&mem->res, PAGE_SIZE);
807 * uncharge if !page_mapped(page)
810 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
812 struct page_cgroup *pc;
813 struct mem_cgroup *mem;
814 struct mem_cgroup_per_zone *mz;
817 if (mem_cgroup_subsys.disabled)
821 * Check if our page_cgroup is valid
823 pc = lookup_page_cgroup(page);
824 if (unlikely(!pc || !PageCgroupUsed(pc)))
827 lock_page_cgroup(pc);
828 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED && page_mapped(page))
829 || !PageCgroupUsed(pc)) {
830 /* This happens at race in zap_pte_range() and do_swap_page()*/
831 unlock_page_cgroup(pc);
834 ClearPageCgroupUsed(pc);
835 mem = pc->mem_cgroup;
837 mz = page_cgroup_zoneinfo(pc);
838 spin_lock_irqsave(&mz->lru_lock, flags);
839 __mem_cgroup_remove_list(mz, pc);
840 spin_unlock_irqrestore(&mz->lru_lock, flags);
841 unlock_page_cgroup(pc);
843 res_counter_uncharge(&mem->res, PAGE_SIZE);
849 void mem_cgroup_uncharge_page(struct page *page)
852 if (page_mapped(page))
854 if (page->mapping && !PageAnon(page))
856 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
859 void mem_cgroup_uncharge_cache_page(struct page *page)
861 VM_BUG_ON(page_mapped(page));
862 VM_BUG_ON(page->mapping);
863 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
867 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
870 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
872 struct page_cgroup *pc;
873 struct mem_cgroup *mem = NULL;
876 if (mem_cgroup_subsys.disabled)
879 pc = lookup_page_cgroup(page);
880 lock_page_cgroup(pc);
881 if (PageCgroupUsed(pc)) {
882 mem = pc->mem_cgroup;
885 unlock_page_cgroup(pc);
888 ret = mem_cgroup_try_charge(NULL, GFP_HIGHUSER_MOVABLE, &mem);
895 /* remove redundant charge if migration failed*/
896 void mem_cgroup_end_migration(struct mem_cgroup *mem,
897 struct page *oldpage, struct page *newpage)
899 struct page *target, *unused;
900 struct page_cgroup *pc;
901 enum charge_type ctype;
906 /* at migration success, oldpage->mapping is NULL. */
907 if (oldpage->mapping) {
915 if (PageAnon(target))
916 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
917 else if (page_is_file_cache(target))
918 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
920 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
922 /* unused page is not on radix-tree now. */
923 if (unused && ctype != MEM_CGROUP_CHARGE_TYPE_MAPPED)
924 __mem_cgroup_uncharge_common(unused, ctype);
926 pc = lookup_page_cgroup(target);
928 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
929 * So, double-counting is effectively avoided.
931 __mem_cgroup_commit_charge(mem, pc, ctype);
934 * Both of oldpage and newpage are still under lock_page().
935 * Then, we don't have to care about race in radix-tree.
936 * But we have to be careful that this page is unmapped or not.
938 * There is a case for !page_mapped(). At the start of
939 * migration, oldpage was mapped. But now, it's zapped.
940 * But we know *target* page is not freed/reused under us.
941 * mem_cgroup_uncharge_page() does all necessary checks.
943 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
944 mem_cgroup_uncharge_page(target);
948 * A call to try to shrink memory usage under specified resource controller.
949 * This is typically used for page reclaiming for shmem for reducing side
950 * effect of page allocation from shmem, which is used by some mem_cgroup.
952 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
954 struct mem_cgroup *mem;
956 int retry = MEM_CGROUP_RECLAIM_RETRIES;
958 if (mem_cgroup_subsys.disabled)
964 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
965 if (unlikely(!mem)) {
973 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
974 progress += res_counter_check_under_limit(&mem->res);
975 } while (!progress && --retry);
983 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
984 unsigned long long val)
987 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
991 while (res_counter_set_limit(&memcg->res, val)) {
992 if (signal_pending(current)) {
1000 progress = try_to_free_mem_cgroup_pages(memcg,
1001 GFP_HIGHUSER_MOVABLE);
1010 * This routine traverse page_cgroup in given list and drop them all.
1011 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1013 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1014 struct mem_cgroup_per_zone *mz,
1017 struct page_cgroup *pc, *busy;
1018 unsigned long flags;
1020 struct list_head *list;
1023 list = &mz->lists[lru];
1025 loop = MEM_CGROUP_ZSTAT(mz, lru);
1026 /* give some margin against EBUSY etc...*/
1031 spin_lock_irqsave(&mz->lru_lock, flags);
1032 if (list_empty(list)) {
1033 spin_unlock_irqrestore(&mz->lru_lock, flags);
1036 pc = list_entry(list->prev, struct page_cgroup, lru);
1038 list_move(&pc->lru, list);
1040 spin_unlock_irqrestore(&mz->lru_lock, flags);
1043 spin_unlock_irqrestore(&mz->lru_lock, flags);
1045 ret = mem_cgroup_move_parent(pc, mem, GFP_HIGHUSER_MOVABLE);
1049 if (ret == -EBUSY || ret == -EINVAL) {
1050 /* found lock contention or "pc" is obsolete. */
1056 if (!ret && !list_empty(list))
1062 * make mem_cgroup's charge to be 0 if there is no task.
1063 * This enables deleting this mem_cgroup.
1065 static int mem_cgroup_force_empty(struct mem_cgroup *mem)
1068 int node, zid, shrink;
1069 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1075 while (mem->res.usage > 0) {
1077 if (atomic_read(&mem->css.cgroup->count) > 0)
1080 /* This is for making all *used* pages to be on LRU. */
1081 lru_add_drain_all();
1083 for_each_node_state(node, N_POSSIBLE) {
1084 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1085 struct mem_cgroup_per_zone *mz;
1087 mz = mem_cgroup_zoneinfo(mem, node, zid);
1089 ret = mem_cgroup_force_empty_list(mem,
1098 /* it seems parent cgroup doesn't have enough mem */
1109 /* returns EBUSY if we come here twice. */
1114 /* try to free all pages in this cgroup */
1116 while (nr_retries && mem->res.usage > 0) {
1118 progress = try_to_free_mem_cgroup_pages(mem,
1119 GFP_HIGHUSER_MOVABLE);
1124 /* try move_account...there may be some *locked* pages. */
1131 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
1133 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
1137 * The user of this function is...
1140 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
1143 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
1144 unsigned long long val;
1147 switch (cft->private) {
1149 /* This function does all necessary parse...reuse it */
1150 ret = res_counter_memparse_write_strategy(buffer, &val);
1152 ret = mem_cgroup_resize_limit(memcg, val);
1155 ret = -EINVAL; /* should be BUG() ? */
1161 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
1163 struct mem_cgroup *mem;
1165 mem = mem_cgroup_from_cont(cont);
1168 res_counter_reset_max(&mem->res);
1171 res_counter_reset_failcnt(&mem->res);
1177 static const struct mem_cgroup_stat_desc {
1180 } mem_cgroup_stat_desc[] = {
1181 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
1182 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
1183 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
1184 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
1187 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
1188 struct cgroup_map_cb *cb)
1190 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
1191 struct mem_cgroup_stat *stat = &mem_cont->stat;
1194 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1197 val = mem_cgroup_read_stat(stat, i);
1198 val *= mem_cgroup_stat_desc[i].unit;
1199 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
1201 /* showing # of active pages */
1203 unsigned long active_anon, inactive_anon;
1204 unsigned long active_file, inactive_file;
1205 unsigned long unevictable;
1207 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1209 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1211 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1213 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1215 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1218 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1219 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1220 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1221 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
1222 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1228 static struct cftype mem_cgroup_files[] = {
1230 .name = "usage_in_bytes",
1231 .private = RES_USAGE,
1232 .read_u64 = mem_cgroup_read,
1235 .name = "max_usage_in_bytes",
1236 .private = RES_MAX_USAGE,
1237 .trigger = mem_cgroup_reset,
1238 .read_u64 = mem_cgroup_read,
1241 .name = "limit_in_bytes",
1242 .private = RES_LIMIT,
1243 .write_string = mem_cgroup_write,
1244 .read_u64 = mem_cgroup_read,
1248 .private = RES_FAILCNT,
1249 .trigger = mem_cgroup_reset,
1250 .read_u64 = mem_cgroup_read,
1254 .read_map = mem_control_stat_show,
1258 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1260 struct mem_cgroup_per_node *pn;
1261 struct mem_cgroup_per_zone *mz;
1263 int zone, tmp = node;
1265 * This routine is called against possible nodes.
1266 * But it's BUG to call kmalloc() against offline node.
1268 * TODO: this routine can waste much memory for nodes which will
1269 * never be onlined. It's better to use memory hotplug callback
1272 if (!node_state(node, N_NORMAL_MEMORY))
1274 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1278 mem->info.nodeinfo[node] = pn;
1279 memset(pn, 0, sizeof(*pn));
1281 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1282 mz = &pn->zoneinfo[zone];
1283 spin_lock_init(&mz->lru_lock);
1285 INIT_LIST_HEAD(&mz->lists[l]);
1290 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1292 kfree(mem->info.nodeinfo[node]);
1295 static int mem_cgroup_size(void)
1297 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
1298 return sizeof(struct mem_cgroup) + cpustat_size;
1301 static struct mem_cgroup *mem_cgroup_alloc(void)
1303 struct mem_cgroup *mem;
1304 int size = mem_cgroup_size();
1306 if (size < PAGE_SIZE)
1307 mem = kmalloc(size, GFP_KERNEL);
1309 mem = vmalloc(size);
1312 memset(mem, 0, size);
1316 static void mem_cgroup_free(struct mem_cgroup *mem)
1318 if (mem_cgroup_size() < PAGE_SIZE)
1325 static struct cgroup_subsys_state *
1326 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1328 struct mem_cgroup *mem;
1331 mem = mem_cgroup_alloc();
1333 return ERR_PTR(-ENOMEM);
1335 res_counter_init(&mem->res);
1337 for_each_node_state(node, N_POSSIBLE)
1338 if (alloc_mem_cgroup_per_zone_info(mem, node))
1343 for_each_node_state(node, N_POSSIBLE)
1344 free_mem_cgroup_per_zone_info(mem, node);
1345 mem_cgroup_free(mem);
1346 return ERR_PTR(-ENOMEM);
1349 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1350 struct cgroup *cont)
1352 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1353 mem_cgroup_force_empty(mem);
1356 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1357 struct cgroup *cont)
1360 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1362 for_each_node_state(node, N_POSSIBLE)
1363 free_mem_cgroup_per_zone_info(mem, node);
1365 mem_cgroup_free(mem_cgroup_from_cont(cont));
1368 static int mem_cgroup_populate(struct cgroup_subsys *ss,
1369 struct cgroup *cont)
1371 return cgroup_add_files(cont, ss, mem_cgroup_files,
1372 ARRAY_SIZE(mem_cgroup_files));
1375 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1376 struct cgroup *cont,
1377 struct cgroup *old_cont,
1378 struct task_struct *p)
1380 struct mm_struct *mm;
1381 struct mem_cgroup *mem, *old_mem;
1383 mm = get_task_mm(p);
1387 mem = mem_cgroup_from_cont(cont);
1388 old_mem = mem_cgroup_from_cont(old_cont);
1391 * Only thread group leaders are allowed to migrate, the mm_struct is
1392 * in effect owned by the leader
1394 if (!thread_group_leader(p))
1401 struct cgroup_subsys mem_cgroup_subsys = {
1403 .subsys_id = mem_cgroup_subsys_id,
1404 .create = mem_cgroup_create,
1405 .pre_destroy = mem_cgroup_pre_destroy,
1406 .destroy = mem_cgroup_destroy,
1407 .populate = mem_cgroup_populate,
1408 .attach = mem_cgroup_move_task,