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>
37 #include <asm/uaccess.h>
39 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
40 static struct kmem_cache *page_cgroup_cache __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 void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat *stat,
70 enum mem_cgroup_stat_index idx, int val)
72 int cpu = smp_processor_id();
73 stat->cpustat[cpu].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 */
135 struct mem_cgroup_stat stat;
137 static struct mem_cgroup init_mem_cgroup;
140 * We use the lower bit of the page->page_cgroup pointer as a bit spin
141 * lock. We need to ensure that page->page_cgroup is at least two
142 * byte aligned (based on comments from Nick Piggin). But since
143 * bit_spin_lock doesn't actually set that lock bit in a non-debug
144 * uniprocessor kernel, we should avoid setting it here too.
146 #define PAGE_CGROUP_LOCK_BIT 0x0
147 #if defined(CONFIG_SMP) || defined(CONFIG_DEBUG_SPINLOCK)
148 #define PAGE_CGROUP_LOCK (1 << PAGE_CGROUP_LOCK_BIT)
150 #define PAGE_CGROUP_LOCK 0x0
154 * A page_cgroup page is associated with every page descriptor. The
155 * page_cgroup helps us identify information about the cgroup
158 struct list_head lru; /* per cgroup LRU list */
160 struct mem_cgroup *mem_cgroup;
163 #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
164 #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
166 static int page_cgroup_nid(struct page_cgroup *pc)
168 return page_to_nid(pc->page);
171 static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
173 return page_zonenum(pc->page);
177 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
178 MEM_CGROUP_CHARGE_TYPE_MAPPED,
179 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
183 * Always modified under lru lock. Then, not necessary to preempt_disable()
185 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
188 int val = (charge)? 1 : -1;
189 struct mem_cgroup_stat *stat = &mem->stat;
191 VM_BUG_ON(!irqs_disabled());
192 if (flags & PAGE_CGROUP_FLAG_CACHE)
193 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
195 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
198 __mem_cgroup_stat_add_safe(stat,
199 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
201 __mem_cgroup_stat_add_safe(stat,
202 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
205 static struct mem_cgroup_per_zone *
206 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
208 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
211 static struct mem_cgroup_per_zone *
212 page_cgroup_zoneinfo(struct page_cgroup *pc)
214 struct mem_cgroup *mem = pc->mem_cgroup;
215 int nid = page_cgroup_nid(pc);
216 int zid = page_cgroup_zid(pc);
218 return mem_cgroup_zoneinfo(mem, nid, zid);
221 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
225 struct mem_cgroup_per_zone *mz;
228 for_each_online_node(nid)
229 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
230 mz = mem_cgroup_zoneinfo(mem, nid, zid);
231 total += MEM_CGROUP_ZSTAT(mz, idx);
236 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
238 return container_of(cgroup_subsys_state(cont,
239 mem_cgroup_subsys_id), struct mem_cgroup,
243 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
246 * mm_update_next_owner() may clear mm->owner to NULL
247 * if it races with swapoff, page migration, etc.
248 * So this can be called with p == NULL.
253 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
254 struct mem_cgroup, css);
257 static inline int page_cgroup_locked(struct page *page)
259 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
262 static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
264 VM_BUG_ON(!page_cgroup_locked(page));
265 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
268 struct page_cgroup *page_get_page_cgroup(struct page *page)
270 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
273 static void lock_page_cgroup(struct page *page)
275 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
278 static int try_lock_page_cgroup(struct page *page)
280 return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
283 static void unlock_page_cgroup(struct page *page)
285 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
288 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
289 struct page_cgroup *pc)
291 int from = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
294 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
296 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
300 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
301 struct page_cgroup *pc)
303 int lru = LRU_INACTIVE;
305 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
308 MEM_CGROUP_ZSTAT(mz, lru) += 1;
309 list_add(&pc->lru, &mz->lists[lru]);
311 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
314 static void __mem_cgroup_move_lists(struct page_cgroup *pc, bool active)
316 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
317 int lru = LRU_INACTIVE;
319 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
322 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
325 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
327 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
330 MEM_CGROUP_ZSTAT(mz, lru) += 1;
331 list_move(&pc->lru, &mz->lists[lru]);
334 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
339 ret = task->mm && mm_match_cgroup(task->mm, mem);
345 * This routine assumes that the appropriate zone's lru lock is already held
347 void mem_cgroup_move_lists(struct page *page, bool active)
349 struct page_cgroup *pc;
350 struct mem_cgroup_per_zone *mz;
353 if (mem_cgroup_subsys.disabled)
357 * We cannot lock_page_cgroup while holding zone's lru_lock,
358 * because other holders of lock_page_cgroup can be interrupted
359 * with an attempt to rotate_reclaimable_page. But we cannot
360 * safely get to page_cgroup without it, so just try_lock it:
361 * mem_cgroup_isolate_pages allows for page left on wrong list.
363 if (!try_lock_page_cgroup(page))
366 pc = page_get_page_cgroup(page);
368 mz = page_cgroup_zoneinfo(pc);
369 spin_lock_irqsave(&mz->lru_lock, flags);
370 __mem_cgroup_move_lists(pc, active);
371 spin_unlock_irqrestore(&mz->lru_lock, flags);
373 unlock_page_cgroup(page);
377 * Calculate mapped_ratio under memory controller. This will be used in
378 * vmscan.c for deteremining we have to reclaim mapped pages.
380 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
385 * usage is recorded in bytes. But, here, we assume the number of
386 * physical pages can be represented by "long" on any arch.
388 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
389 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
390 return (int)((rss * 100L) / total);
394 * This function is called from vmscan.c. In page reclaiming loop. balance
395 * between active and inactive list is calculated. For memory controller
396 * page reclaiming, we should use using mem_cgroup's imbalance rather than
397 * zone's global lru imbalance.
399 long mem_cgroup_reclaim_imbalance(struct mem_cgroup *mem)
401 unsigned long active, inactive;
402 /* active and inactive are the number of pages. 'long' is ok.*/
403 active = mem_cgroup_get_all_zonestat(mem, LRU_ACTIVE);
404 inactive = mem_cgroup_get_all_zonestat(mem, LRU_INACTIVE);
405 return (long) (active / (inactive + 1));
409 * prev_priority control...this will be used in memory reclaim path.
411 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
413 return mem->prev_priority;
416 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
418 if (priority < mem->prev_priority)
419 mem->prev_priority = priority;
422 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
424 mem->prev_priority = priority;
428 * Calculate # of pages to be scanned in this priority/zone.
431 * priority starts from "DEF_PRIORITY" and decremented in each loop.
432 * (see include/linux/mmzone.h)
435 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
436 int priority, enum lru_list lru)
439 int nid = zone->zone_pgdat->node_id;
440 int zid = zone_idx(zone);
441 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
443 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
445 return (nr_pages >> priority);
448 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
449 struct list_head *dst,
450 unsigned long *scanned, int order,
451 int mode, struct zone *z,
452 struct mem_cgroup *mem_cont,
455 unsigned long nr_taken = 0;
459 struct list_head *src;
460 struct page_cgroup *pc, *tmp;
461 int nid = z->zone_pgdat->node_id;
462 int zid = zone_idx(z);
463 struct mem_cgroup_per_zone *mz;
467 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
468 src = &mz->lists[lru];
470 spin_lock(&mz->lru_lock);
472 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
473 if (scan >= nr_to_scan)
477 if (unlikely(!PageLRU(page)))
480 if (PageActive(page) && !active) {
481 __mem_cgroup_move_lists(pc, true);
484 if (!PageActive(page) && active) {
485 __mem_cgroup_move_lists(pc, false);
490 list_move(&pc->lru, &pc_list);
492 if (__isolate_lru_page(page, mode) == 0) {
493 list_move(&page->lru, dst);
498 list_splice(&pc_list, src);
499 spin_unlock(&mz->lru_lock);
506 * Charge the memory controller for page usage.
508 * 0 if the charge was successful
509 * < 0 if the cgroup is over its limit
511 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
512 gfp_t gfp_mask, enum charge_type ctype,
513 struct mem_cgroup *memcg)
515 struct mem_cgroup *mem;
516 struct page_cgroup *pc;
518 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
519 struct mem_cgroup_per_zone *mz;
521 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
522 if (unlikely(pc == NULL))
526 * We always charge the cgroup the mm_struct belongs to.
527 * The mm_struct's mem_cgroup changes on task migration if the
528 * thread group leader migrates. It's possible that mm is not
529 * set, if so charge the init_mm (happens for pagecache usage).
531 if (likely(!memcg)) {
533 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
534 if (unlikely(!mem)) {
536 kmem_cache_free(page_cgroup_cache, pc);
540 * For every charge from the cgroup, increment reference count
546 css_get(&memcg->css);
549 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
550 if (!(gfp_mask & __GFP_WAIT))
553 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
557 * try_to_free_mem_cgroup_pages() might not give us a full
558 * picture of reclaim. Some pages are reclaimed and might be
559 * moved to swap cache or just unmapped from the cgroup.
560 * Check the limit again to see if the reclaim reduced the
561 * current usage of the cgroup before giving up
563 if (res_counter_check_under_limit(&mem->res))
567 mem_cgroup_out_of_memory(mem, gfp_mask);
572 pc->mem_cgroup = mem;
575 * If a page is accounted as a page cache, insert to inactive list.
576 * If anon, insert to active list.
578 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE)
579 pc->flags = PAGE_CGROUP_FLAG_CACHE;
581 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
583 lock_page_cgroup(page);
584 if (unlikely(page_get_page_cgroup(page))) {
585 unlock_page_cgroup(page);
586 res_counter_uncharge(&mem->res, PAGE_SIZE);
588 kmem_cache_free(page_cgroup_cache, pc);
591 page_assign_page_cgroup(page, pc);
593 mz = page_cgroup_zoneinfo(pc);
594 spin_lock_irqsave(&mz->lru_lock, flags);
595 __mem_cgroup_add_list(mz, pc);
596 spin_unlock_irqrestore(&mz->lru_lock, flags);
598 unlock_page_cgroup(page);
603 kmem_cache_free(page_cgroup_cache, pc);
608 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
610 if (mem_cgroup_subsys.disabled)
614 * If already mapped, we don't have to account.
615 * If page cache, page->mapping has address_space.
616 * But page->mapping may have out-of-use anon_vma pointer,
617 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
620 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
624 return mem_cgroup_charge_common(page, mm, gfp_mask,
625 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
628 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
631 if (mem_cgroup_subsys.disabled)
635 * Corner case handling. This is called from add_to_page_cache()
636 * in usual. But some FS (shmem) precharges this page before calling it
637 * and call add_to_page_cache() with GFP_NOWAIT.
639 * For GFP_NOWAIT case, the page may be pre-charged before calling
640 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
641 * charge twice. (It works but has to pay a bit larger cost.)
643 if (!(gfp_mask & __GFP_WAIT)) {
644 struct page_cgroup *pc;
646 lock_page_cgroup(page);
647 pc = page_get_page_cgroup(page);
649 VM_BUG_ON(pc->page != page);
650 VM_BUG_ON(!pc->mem_cgroup);
651 unlock_page_cgroup(page);
654 unlock_page_cgroup(page);
660 return mem_cgroup_charge_common(page, mm, gfp_mask,
661 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
665 * uncharge if !page_mapped(page)
668 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
670 struct page_cgroup *pc;
671 struct mem_cgroup *mem;
672 struct mem_cgroup_per_zone *mz;
675 if (mem_cgroup_subsys.disabled)
679 * Check if our page_cgroup is valid
681 lock_page_cgroup(page);
682 pc = page_get_page_cgroup(page);
686 VM_BUG_ON(pc->page != page);
688 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
689 && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
690 || page_mapped(page)))
693 mz = page_cgroup_zoneinfo(pc);
694 spin_lock_irqsave(&mz->lru_lock, flags);
695 __mem_cgroup_remove_list(mz, pc);
696 spin_unlock_irqrestore(&mz->lru_lock, flags);
698 page_assign_page_cgroup(page, NULL);
699 unlock_page_cgroup(page);
701 mem = pc->mem_cgroup;
702 res_counter_uncharge(&mem->res, PAGE_SIZE);
705 kmem_cache_free(page_cgroup_cache, pc);
708 unlock_page_cgroup(page);
711 void mem_cgroup_uncharge_page(struct page *page)
713 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
716 void mem_cgroup_uncharge_cache_page(struct page *page)
718 VM_BUG_ON(page_mapped(page));
719 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
723 * Before starting migration, account against new page.
725 int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
727 struct page_cgroup *pc;
728 struct mem_cgroup *mem = NULL;
729 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
732 if (mem_cgroup_subsys.disabled)
735 lock_page_cgroup(page);
736 pc = page_get_page_cgroup(page);
738 mem = pc->mem_cgroup;
740 if (pc->flags & PAGE_CGROUP_FLAG_CACHE)
741 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
743 unlock_page_cgroup(page);
745 ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
752 /* remove redundant charge if migration failed*/
753 void mem_cgroup_end_migration(struct page *newpage)
756 * At success, page->mapping is not NULL.
757 * special rollback care is necessary when
758 * 1. at migration failure. (newpage->mapping is cleared in this case)
759 * 2. the newpage was moved but not remapped again because the task
760 * exits and the newpage is obsolete. In this case, the new page
761 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
762 * always for avoiding mess. The page_cgroup will be removed if
763 * unnecessary. File cache pages is still on radix-tree. Don't
766 if (!newpage->mapping)
767 __mem_cgroup_uncharge_common(newpage,
768 MEM_CGROUP_CHARGE_TYPE_FORCE);
769 else if (PageAnon(newpage))
770 mem_cgroup_uncharge_page(newpage);
774 * A call to try to shrink memory usage under specified resource controller.
775 * This is typically used for page reclaiming for shmem for reducing side
776 * effect of page allocation from shmem, which is used by some mem_cgroup.
778 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
780 struct mem_cgroup *mem;
782 int retry = MEM_CGROUP_RECLAIM_RETRIES;
784 if (mem_cgroup_subsys.disabled)
790 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
791 if (unlikely(!mem)) {
799 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
800 progress += res_counter_check_under_limit(&mem->res);
801 } while (!progress && --retry);
809 int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
812 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
816 while (res_counter_set_limit(&memcg->res, val)) {
817 if (signal_pending(current)) {
825 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
834 * This routine traverse page_cgroup in given list and drop them all.
835 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
837 #define FORCE_UNCHARGE_BATCH (128)
838 static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
839 struct mem_cgroup_per_zone *mz,
842 struct page_cgroup *pc;
844 int count = FORCE_UNCHARGE_BATCH;
846 struct list_head *list;
848 list = &mz->lists[lru];
850 spin_lock_irqsave(&mz->lru_lock, flags);
851 while (!list_empty(list)) {
852 pc = list_entry(list->prev, struct page_cgroup, lru);
855 spin_unlock_irqrestore(&mz->lru_lock, flags);
857 * Check if this page is on LRU. !LRU page can be found
858 * if it's under page migration.
861 __mem_cgroup_uncharge_common(page,
862 MEM_CGROUP_CHARGE_TYPE_FORCE);
865 count = FORCE_UNCHARGE_BATCH;
870 spin_lock_irqsave(&mz->lru_lock, flags);
872 spin_unlock_irqrestore(&mz->lru_lock, flags);
876 * make mem_cgroup's charge to be 0 if there is no task.
877 * This enables deleting this mem_cgroup.
879 static int mem_cgroup_force_empty(struct mem_cgroup *mem)
886 * page reclaim code (kswapd etc..) will move pages between
887 * active_list <-> inactive_list while we don't take a lock.
888 * So, we have to do loop here until all lists are empty.
890 while (mem->res.usage > 0) {
891 if (atomic_read(&mem->css.cgroup->count) > 0)
893 for_each_node_state(node, N_POSSIBLE)
894 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
895 struct mem_cgroup_per_zone *mz;
897 mz = mem_cgroup_zoneinfo(mem, node, zid);
899 mem_cgroup_force_empty_list(mem, mz, l);
908 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
910 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
914 * The user of this function is...
917 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
920 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
921 unsigned long long val;
924 switch (cft->private) {
926 /* This function does all necessary parse...reuse it */
927 ret = res_counter_memparse_write_strategy(buffer, &val);
929 ret = mem_cgroup_resize_limit(memcg, val);
932 ret = -EINVAL; /* should be BUG() ? */
938 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
940 struct mem_cgroup *mem;
942 mem = mem_cgroup_from_cont(cont);
945 res_counter_reset_max(&mem->res);
948 res_counter_reset_failcnt(&mem->res);
954 static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
956 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
959 static const struct mem_cgroup_stat_desc {
962 } mem_cgroup_stat_desc[] = {
963 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
964 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
965 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
966 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
969 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
970 struct cgroup_map_cb *cb)
972 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
973 struct mem_cgroup_stat *stat = &mem_cont->stat;
976 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
979 val = mem_cgroup_read_stat(stat, i);
980 val *= mem_cgroup_stat_desc[i].unit;
981 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
983 /* showing # of active pages */
985 unsigned long active, inactive;
987 inactive = mem_cgroup_get_all_zonestat(mem_cont,
989 active = mem_cgroup_get_all_zonestat(mem_cont,
991 cb->fill(cb, "active", (active) * PAGE_SIZE);
992 cb->fill(cb, "inactive", (inactive) * PAGE_SIZE);
997 static struct cftype mem_cgroup_files[] = {
999 .name = "usage_in_bytes",
1000 .private = RES_USAGE,
1001 .read_u64 = mem_cgroup_read,
1004 .name = "max_usage_in_bytes",
1005 .private = RES_MAX_USAGE,
1006 .trigger = mem_cgroup_reset,
1007 .read_u64 = mem_cgroup_read,
1010 .name = "limit_in_bytes",
1011 .private = RES_LIMIT,
1012 .write_string = mem_cgroup_write,
1013 .read_u64 = mem_cgroup_read,
1017 .private = RES_FAILCNT,
1018 .trigger = mem_cgroup_reset,
1019 .read_u64 = mem_cgroup_read,
1022 .name = "force_empty",
1023 .trigger = mem_force_empty_write,
1027 .read_map = mem_control_stat_show,
1031 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1033 struct mem_cgroup_per_node *pn;
1034 struct mem_cgroup_per_zone *mz;
1036 int zone, tmp = node;
1038 * This routine is called against possible nodes.
1039 * But it's BUG to call kmalloc() against offline node.
1041 * TODO: this routine can waste much memory for nodes which will
1042 * never be onlined. It's better to use memory hotplug callback
1045 if (!node_state(node, N_NORMAL_MEMORY))
1047 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1051 mem->info.nodeinfo[node] = pn;
1052 memset(pn, 0, sizeof(*pn));
1054 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1055 mz = &pn->zoneinfo[zone];
1056 spin_lock_init(&mz->lru_lock);
1058 INIT_LIST_HEAD(&mz->lists[l]);
1063 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1065 kfree(mem->info.nodeinfo[node]);
1068 static struct mem_cgroup *mem_cgroup_alloc(void)
1070 struct mem_cgroup *mem;
1072 if (sizeof(*mem) < PAGE_SIZE)
1073 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1075 mem = vmalloc(sizeof(*mem));
1078 memset(mem, 0, sizeof(*mem));
1082 static void mem_cgroup_free(struct mem_cgroup *mem)
1084 if (sizeof(*mem) < PAGE_SIZE)
1091 static struct cgroup_subsys_state *
1092 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1094 struct mem_cgroup *mem;
1097 if (unlikely((cont->parent) == NULL)) {
1098 mem = &init_mem_cgroup;
1099 page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
1101 mem = mem_cgroup_alloc();
1103 return ERR_PTR(-ENOMEM);
1106 res_counter_init(&mem->res);
1108 for_each_node_state(node, N_POSSIBLE)
1109 if (alloc_mem_cgroup_per_zone_info(mem, node))
1114 for_each_node_state(node, N_POSSIBLE)
1115 free_mem_cgroup_per_zone_info(mem, node);
1116 if (cont->parent != NULL)
1117 mem_cgroup_free(mem);
1118 return ERR_PTR(-ENOMEM);
1121 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1122 struct cgroup *cont)
1124 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1125 mem_cgroup_force_empty(mem);
1128 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1129 struct cgroup *cont)
1132 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1134 for_each_node_state(node, N_POSSIBLE)
1135 free_mem_cgroup_per_zone_info(mem, node);
1137 mem_cgroup_free(mem_cgroup_from_cont(cont));
1140 static int mem_cgroup_populate(struct cgroup_subsys *ss,
1141 struct cgroup *cont)
1143 return cgroup_add_files(cont, ss, mem_cgroup_files,
1144 ARRAY_SIZE(mem_cgroup_files));
1147 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1148 struct cgroup *cont,
1149 struct cgroup *old_cont,
1150 struct task_struct *p)
1152 struct mm_struct *mm;
1153 struct mem_cgroup *mem, *old_mem;
1155 mm = get_task_mm(p);
1159 mem = mem_cgroup_from_cont(cont);
1160 old_mem = mem_cgroup_from_cont(old_cont);
1163 * Only thread group leaders are allowed to migrate, the mm_struct is
1164 * in effect owned by the leader
1166 if (!thread_group_leader(p))
1173 struct cgroup_subsys mem_cgroup_subsys = {
1175 .subsys_id = mem_cgroup_subsys_id,
1176 .create = mem_cgroup_create,
1177 .pre_destroy = mem_cgroup_pre_destroy,
1178 .destroy = mem_cgroup_destroy,
1179 .populate = mem_cgroup_populate,
1180 .attach = mem_cgroup_move_task,