memcg: make page->mapping NULL before uncharge
[linux-2.6/linux-2.6-openrd.git] / mm / memcontrol.c
blob6f8b5b3b38b208ad3ecbc46b00f4272d53f03b05
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>
23 #include <linux/mm.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>
32 #include <linux/fs.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)
79 int cpu;
80 s64 ret = 0;
81 for_each_possible_cpu(cpu)
82 ret += stat->cpustat[cpu].count[idx];
83 return ret;
87 * per-zone information in memory controller.
89 struct mem_cgroup_per_zone {
91 * spin_lock to protect the per cgroup LRU
93 spinlock_t lru_lock;
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.
119 struct mem_cgroup {
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.
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)
149 #else
150 #define PAGE_CGROUP_LOCK 0x0
151 #endif
154 * A page_cgroup page is associated with every page descriptor. The
155 * page_cgroup helps us identify information about the cgroup
157 struct page_cgroup {
158 struct list_head lru; /* per cgroup LRU list */
159 struct page *page;
160 struct mem_cgroup *mem_cgroup;
161 int flags;
163 #define PAGE_CGROUP_FLAG_CACHE (0x1) /* charged as cache */
164 #define PAGE_CGROUP_FLAG_ACTIVE (0x2) /* page is active in this cgroup */
165 #define PAGE_CGROUP_FLAG_FILE (0x4) /* page is file system backed */
166 #define PAGE_CGROUP_FLAG_UNEVICTABLE (0x8) /* page is unevictableable */
168 static int page_cgroup_nid(struct page_cgroup *pc)
170 return page_to_nid(pc->page);
173 static enum zone_type page_cgroup_zid(struct page_cgroup *pc)
175 return page_zonenum(pc->page);
178 enum charge_type {
179 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
180 MEM_CGROUP_CHARGE_TYPE_MAPPED,
181 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
182 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
186 * Always modified under lru lock. Then, not necessary to preempt_disable()
188 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem, int flags,
189 bool charge)
191 int val = (charge)? 1 : -1;
192 struct mem_cgroup_stat *stat = &mem->stat;
194 VM_BUG_ON(!irqs_disabled());
195 if (flags & PAGE_CGROUP_FLAG_CACHE)
196 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_CACHE, val);
197 else
198 __mem_cgroup_stat_add_safe(stat, MEM_CGROUP_STAT_RSS, val);
200 if (charge)
201 __mem_cgroup_stat_add_safe(stat,
202 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
203 else
204 __mem_cgroup_stat_add_safe(stat,
205 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
208 static struct mem_cgroup_per_zone *
209 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
211 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
214 static struct mem_cgroup_per_zone *
215 page_cgroup_zoneinfo(struct page_cgroup *pc)
217 struct mem_cgroup *mem = pc->mem_cgroup;
218 int nid = page_cgroup_nid(pc);
219 int zid = page_cgroup_zid(pc);
221 return mem_cgroup_zoneinfo(mem, nid, zid);
224 static unsigned long mem_cgroup_get_all_zonestat(struct mem_cgroup *mem,
225 enum lru_list idx)
227 int nid, zid;
228 struct mem_cgroup_per_zone *mz;
229 u64 total = 0;
231 for_each_online_node(nid)
232 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
233 mz = mem_cgroup_zoneinfo(mem, nid, zid);
234 total += MEM_CGROUP_ZSTAT(mz, idx);
236 return total;
239 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
241 return container_of(cgroup_subsys_state(cont,
242 mem_cgroup_subsys_id), struct mem_cgroup,
243 css);
246 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
249 * mm_update_next_owner() may clear mm->owner to NULL
250 * if it races with swapoff, page migration, etc.
251 * So this can be called with p == NULL.
253 if (unlikely(!p))
254 return NULL;
256 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
257 struct mem_cgroup, css);
260 static inline int page_cgroup_locked(struct page *page)
262 return bit_spin_is_locked(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
265 static void page_assign_page_cgroup(struct page *page, struct page_cgroup *pc)
267 VM_BUG_ON(!page_cgroup_locked(page));
268 page->page_cgroup = ((unsigned long)pc | PAGE_CGROUP_LOCK);
271 struct page_cgroup *page_get_page_cgroup(struct page *page)
273 return (struct page_cgroup *) (page->page_cgroup & ~PAGE_CGROUP_LOCK);
276 static void lock_page_cgroup(struct page *page)
278 bit_spin_lock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
281 static int try_lock_page_cgroup(struct page *page)
283 return bit_spin_trylock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
286 static void unlock_page_cgroup(struct page *page)
288 bit_spin_unlock(PAGE_CGROUP_LOCK_BIT, &page->page_cgroup);
291 static void __mem_cgroup_remove_list(struct mem_cgroup_per_zone *mz,
292 struct page_cgroup *pc)
294 int lru = LRU_BASE;
296 if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
297 lru = LRU_UNEVICTABLE;
298 else {
299 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
300 lru += LRU_ACTIVE;
301 if (pc->flags & PAGE_CGROUP_FLAG_FILE)
302 lru += LRU_FILE;
305 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
307 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, false);
308 list_del(&pc->lru);
311 static void __mem_cgroup_add_list(struct mem_cgroup_per_zone *mz,
312 struct page_cgroup *pc)
314 int lru = LRU_BASE;
316 if (pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE)
317 lru = LRU_UNEVICTABLE;
318 else {
319 if (pc->flags & PAGE_CGROUP_FLAG_ACTIVE)
320 lru += LRU_ACTIVE;
321 if (pc->flags & PAGE_CGROUP_FLAG_FILE)
322 lru += LRU_FILE;
325 MEM_CGROUP_ZSTAT(mz, lru) += 1;
326 list_add(&pc->lru, &mz->lists[lru]);
328 mem_cgroup_charge_statistics(pc->mem_cgroup, pc->flags, true);
331 static void __mem_cgroup_move_lists(struct page_cgroup *pc, enum lru_list lru)
333 struct mem_cgroup_per_zone *mz = page_cgroup_zoneinfo(pc);
334 int active = pc->flags & PAGE_CGROUP_FLAG_ACTIVE;
335 int file = pc->flags & PAGE_CGROUP_FLAG_FILE;
336 int unevictable = pc->flags & PAGE_CGROUP_FLAG_UNEVICTABLE;
337 enum lru_list from = unevictable ? LRU_UNEVICTABLE :
338 (LRU_FILE * !!file + !!active);
340 if (lru == from)
341 return;
343 MEM_CGROUP_ZSTAT(mz, from) -= 1;
345 if (is_unevictable_lru(lru)) {
346 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
347 pc->flags |= PAGE_CGROUP_FLAG_UNEVICTABLE;
348 } else {
349 if (is_active_lru(lru))
350 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
351 else
352 pc->flags &= ~PAGE_CGROUP_FLAG_ACTIVE;
353 pc->flags &= ~PAGE_CGROUP_FLAG_UNEVICTABLE;
356 MEM_CGROUP_ZSTAT(mz, lru) += 1;
357 list_move(&pc->lru, &mz->lists[lru]);
360 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
362 int ret;
364 task_lock(task);
365 ret = task->mm && mm_match_cgroup(task->mm, mem);
366 task_unlock(task);
367 return ret;
371 * This routine assumes that the appropriate zone's lru lock is already held
373 void mem_cgroup_move_lists(struct page *page, enum lru_list lru)
375 struct page_cgroup *pc;
376 struct mem_cgroup_per_zone *mz;
377 unsigned long flags;
379 if (mem_cgroup_subsys.disabled)
380 return;
383 * We cannot lock_page_cgroup while holding zone's lru_lock,
384 * because other holders of lock_page_cgroup can be interrupted
385 * with an attempt to rotate_reclaimable_page. But we cannot
386 * safely get to page_cgroup without it, so just try_lock it:
387 * mem_cgroup_isolate_pages allows for page left on wrong list.
389 if (!try_lock_page_cgroup(page))
390 return;
392 pc = page_get_page_cgroup(page);
393 if (pc) {
394 mz = page_cgroup_zoneinfo(pc);
395 spin_lock_irqsave(&mz->lru_lock, flags);
396 __mem_cgroup_move_lists(pc, lru);
397 spin_unlock_irqrestore(&mz->lru_lock, flags);
399 unlock_page_cgroup(page);
403 * Calculate mapped_ratio under memory controller. This will be used in
404 * vmscan.c for deteremining we have to reclaim mapped pages.
406 int mem_cgroup_calc_mapped_ratio(struct mem_cgroup *mem)
408 long total, rss;
411 * usage is recorded in bytes. But, here, we assume the number of
412 * physical pages can be represented by "long" on any arch.
414 total = (long) (mem->res.usage >> PAGE_SHIFT) + 1L;
415 rss = (long)mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
416 return (int)((rss * 100L) / total);
420 * prev_priority control...this will be used in memory reclaim path.
422 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
424 return mem->prev_priority;
427 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
429 if (priority < mem->prev_priority)
430 mem->prev_priority = priority;
433 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
435 mem->prev_priority = priority;
439 * Calculate # of pages to be scanned in this priority/zone.
440 * See also vmscan.c
442 * priority starts from "DEF_PRIORITY" and decremented in each loop.
443 * (see include/linux/mmzone.h)
446 long mem_cgroup_calc_reclaim(struct mem_cgroup *mem, struct zone *zone,
447 int priority, enum lru_list lru)
449 long nr_pages;
450 int nid = zone->zone_pgdat->node_id;
451 int zid = zone_idx(zone);
452 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(mem, nid, zid);
454 nr_pages = MEM_CGROUP_ZSTAT(mz, lru);
456 return (nr_pages >> priority);
459 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
460 struct list_head *dst,
461 unsigned long *scanned, int order,
462 int mode, struct zone *z,
463 struct mem_cgroup *mem_cont,
464 int active, int file)
466 unsigned long nr_taken = 0;
467 struct page *page;
468 unsigned long scan;
469 LIST_HEAD(pc_list);
470 struct list_head *src;
471 struct page_cgroup *pc, *tmp;
472 int nid = z->zone_pgdat->node_id;
473 int zid = zone_idx(z);
474 struct mem_cgroup_per_zone *mz;
475 int lru = LRU_FILE * !!file + !!active;
477 BUG_ON(!mem_cont);
478 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
479 src = &mz->lists[lru];
481 spin_lock(&mz->lru_lock);
482 scan = 0;
483 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
484 if (scan >= nr_to_scan)
485 break;
486 page = pc->page;
488 if (unlikely(!PageLRU(page)))
489 continue;
492 * TODO: play better with lumpy reclaim, grabbing anything.
494 if (PageUnevictable(page) ||
495 (PageActive(page) && !active) ||
496 (!PageActive(page) && active)) {
497 __mem_cgroup_move_lists(pc, page_lru(page));
498 continue;
501 scan++;
502 list_move(&pc->lru, &pc_list);
504 if (__isolate_lru_page(page, mode, file) == 0) {
505 list_move(&page->lru, dst);
506 nr_taken++;
510 list_splice(&pc_list, src);
511 spin_unlock(&mz->lru_lock);
513 *scanned = scan;
514 return nr_taken;
518 * Charge the memory controller for page usage.
519 * Return
520 * 0 if the charge was successful
521 * < 0 if the cgroup is over its limit
523 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
524 gfp_t gfp_mask, enum charge_type ctype,
525 struct mem_cgroup *memcg)
527 struct mem_cgroup *mem;
528 struct page_cgroup *pc;
529 unsigned long flags;
530 unsigned long nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
531 struct mem_cgroup_per_zone *mz;
533 pc = kmem_cache_alloc(page_cgroup_cache, gfp_mask);
534 if (unlikely(pc == NULL))
535 goto err;
538 * We always charge the cgroup the mm_struct belongs to.
539 * The mm_struct's mem_cgroup changes on task migration if the
540 * thread group leader migrates. It's possible that mm is not
541 * set, if so charge the init_mm (happens for pagecache usage).
543 if (likely(!memcg)) {
544 rcu_read_lock();
545 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
546 if (unlikely(!mem)) {
547 rcu_read_unlock();
548 kmem_cache_free(page_cgroup_cache, pc);
549 return 0;
552 * For every charge from the cgroup, increment reference count
554 css_get(&mem->css);
555 rcu_read_unlock();
556 } else {
557 mem = memcg;
558 css_get(&memcg->css);
561 while (res_counter_charge(&mem->res, PAGE_SIZE)) {
562 if (!(gfp_mask & __GFP_WAIT))
563 goto out;
565 if (try_to_free_mem_cgroup_pages(mem, gfp_mask))
566 continue;
569 * try_to_free_mem_cgroup_pages() might not give us a full
570 * picture of reclaim. Some pages are reclaimed and might be
571 * moved to swap cache or just unmapped from the cgroup.
572 * Check the limit again to see if the reclaim reduced the
573 * current usage of the cgroup before giving up
575 if (res_counter_check_under_limit(&mem->res))
576 continue;
578 if (!nr_retries--) {
579 mem_cgroup_out_of_memory(mem, gfp_mask);
580 goto out;
584 pc->mem_cgroup = mem;
585 pc->page = page;
587 * If a page is accounted as a page cache, insert to inactive list.
588 * If anon, insert to active list.
590 if (ctype == MEM_CGROUP_CHARGE_TYPE_CACHE) {
591 pc->flags = PAGE_CGROUP_FLAG_CACHE;
592 if (page_is_file_cache(page))
593 pc->flags |= PAGE_CGROUP_FLAG_FILE;
594 else
595 pc->flags |= PAGE_CGROUP_FLAG_ACTIVE;
596 } else if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
597 pc->flags = PAGE_CGROUP_FLAG_ACTIVE;
598 else /* MEM_CGROUP_CHARGE_TYPE_SHMEM */
599 pc->flags = PAGE_CGROUP_FLAG_CACHE | PAGE_CGROUP_FLAG_ACTIVE;
601 lock_page_cgroup(page);
602 if (unlikely(page_get_page_cgroup(page))) {
603 unlock_page_cgroup(page);
604 res_counter_uncharge(&mem->res, PAGE_SIZE);
605 css_put(&mem->css);
606 kmem_cache_free(page_cgroup_cache, pc);
607 goto done;
609 page_assign_page_cgroup(page, pc);
611 mz = page_cgroup_zoneinfo(pc);
612 spin_lock_irqsave(&mz->lru_lock, flags);
613 __mem_cgroup_add_list(mz, pc);
614 spin_unlock_irqrestore(&mz->lru_lock, flags);
616 unlock_page_cgroup(page);
617 done:
618 return 0;
619 out:
620 css_put(&mem->css);
621 kmem_cache_free(page_cgroup_cache, pc);
622 err:
623 return -ENOMEM;
626 int mem_cgroup_charge(struct page *page, struct mm_struct *mm, gfp_t gfp_mask)
628 if (mem_cgroup_subsys.disabled)
629 return 0;
632 * If already mapped, we don't have to account.
633 * If page cache, page->mapping has address_space.
634 * But page->mapping may have out-of-use anon_vma pointer,
635 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
636 * is NULL.
638 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
639 return 0;
640 if (unlikely(!mm))
641 mm = &init_mm;
642 return mem_cgroup_charge_common(page, mm, gfp_mask,
643 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
646 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
647 gfp_t gfp_mask)
649 if (mem_cgroup_subsys.disabled)
650 return 0;
653 * Corner case handling. This is called from add_to_page_cache()
654 * in usual. But some FS (shmem) precharges this page before calling it
655 * and call add_to_page_cache() with GFP_NOWAIT.
657 * For GFP_NOWAIT case, the page may be pre-charged before calling
658 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
659 * charge twice. (It works but has to pay a bit larger cost.)
661 if (!(gfp_mask & __GFP_WAIT)) {
662 struct page_cgroup *pc;
664 lock_page_cgroup(page);
665 pc = page_get_page_cgroup(page);
666 if (pc) {
667 VM_BUG_ON(pc->page != page);
668 VM_BUG_ON(!pc->mem_cgroup);
669 unlock_page_cgroup(page);
670 return 0;
672 unlock_page_cgroup(page);
675 if (unlikely(!mm))
676 mm = &init_mm;
678 return mem_cgroup_charge_common(page, mm, gfp_mask,
679 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
683 * uncharge if !page_mapped(page)
685 static void
686 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
688 struct page_cgroup *pc;
689 struct mem_cgroup *mem;
690 struct mem_cgroup_per_zone *mz;
691 unsigned long flags;
693 if (mem_cgroup_subsys.disabled)
694 return;
697 * Check if our page_cgroup is valid
699 lock_page_cgroup(page);
700 pc = page_get_page_cgroup(page);
701 if (unlikely(!pc))
702 goto unlock;
704 VM_BUG_ON(pc->page != page);
706 if ((ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
707 && ((pc->flags & PAGE_CGROUP_FLAG_CACHE)
708 || page_mapped(page)))
709 goto unlock;
711 mz = page_cgroup_zoneinfo(pc);
712 spin_lock_irqsave(&mz->lru_lock, flags);
713 __mem_cgroup_remove_list(mz, pc);
714 spin_unlock_irqrestore(&mz->lru_lock, flags);
716 page_assign_page_cgroup(page, NULL);
717 unlock_page_cgroup(page);
719 mem = pc->mem_cgroup;
720 res_counter_uncharge(&mem->res, PAGE_SIZE);
721 css_put(&mem->css);
723 kmem_cache_free(page_cgroup_cache, pc);
724 return;
725 unlock:
726 unlock_page_cgroup(page);
729 void mem_cgroup_uncharge_page(struct page *page)
731 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
734 void mem_cgroup_uncharge_cache_page(struct page *page)
736 VM_BUG_ON(page_mapped(page));
737 VM_BUG_ON(page->mapping);
738 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
742 * Before starting migration, account against new page.
744 int mem_cgroup_prepare_migration(struct page *page, struct page *newpage)
746 struct page_cgroup *pc;
747 struct mem_cgroup *mem = NULL;
748 enum charge_type ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
749 int ret = 0;
751 if (mem_cgroup_subsys.disabled)
752 return 0;
754 lock_page_cgroup(page);
755 pc = page_get_page_cgroup(page);
756 if (pc) {
757 mem = pc->mem_cgroup;
758 css_get(&mem->css);
759 if (pc->flags & PAGE_CGROUP_FLAG_CACHE) {
760 if (page_is_file_cache(page))
761 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
762 else
763 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
766 unlock_page_cgroup(page);
767 if (mem) {
768 ret = mem_cgroup_charge_common(newpage, NULL, GFP_KERNEL,
769 ctype, mem);
770 css_put(&mem->css);
772 return ret;
775 /* remove redundant charge if migration failed*/
776 void mem_cgroup_end_migration(struct page *newpage)
779 * At success, page->mapping is not NULL.
780 * special rollback care is necessary when
781 * 1. at migration failure. (newpage->mapping is cleared in this case)
782 * 2. the newpage was moved but not remapped again because the task
783 * exits and the newpage is obsolete. In this case, the new page
784 * may be a swapcache. So, we just call mem_cgroup_uncharge_page()
785 * always for avoiding mess. The page_cgroup will be removed if
786 * unnecessary. File cache pages is still on radix-tree. Don't
787 * care it.
789 if (!newpage->mapping)
790 __mem_cgroup_uncharge_common(newpage,
791 MEM_CGROUP_CHARGE_TYPE_FORCE);
792 else if (PageAnon(newpage))
793 mem_cgroup_uncharge_page(newpage);
797 * A call to try to shrink memory usage under specified resource controller.
798 * This is typically used for page reclaiming for shmem for reducing side
799 * effect of page allocation from shmem, which is used by some mem_cgroup.
801 int mem_cgroup_shrink_usage(struct mm_struct *mm, gfp_t gfp_mask)
803 struct mem_cgroup *mem;
804 int progress = 0;
805 int retry = MEM_CGROUP_RECLAIM_RETRIES;
807 if (mem_cgroup_subsys.disabled)
808 return 0;
809 if (!mm)
810 return 0;
812 rcu_read_lock();
813 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
814 if (unlikely(!mem)) {
815 rcu_read_unlock();
816 return 0;
818 css_get(&mem->css);
819 rcu_read_unlock();
821 do {
822 progress = try_to_free_mem_cgroup_pages(mem, gfp_mask);
823 progress += res_counter_check_under_limit(&mem->res);
824 } while (!progress && --retry);
826 css_put(&mem->css);
827 if (!retry)
828 return -ENOMEM;
829 return 0;
832 int mem_cgroup_resize_limit(struct mem_cgroup *memcg, unsigned long long val)
835 int retry_count = MEM_CGROUP_RECLAIM_RETRIES;
836 int progress;
837 int ret = 0;
839 while (res_counter_set_limit(&memcg->res, val)) {
840 if (signal_pending(current)) {
841 ret = -EINTR;
842 break;
844 if (!retry_count) {
845 ret = -EBUSY;
846 break;
848 progress = try_to_free_mem_cgroup_pages(memcg, GFP_KERNEL);
849 if (!progress)
850 retry_count--;
852 return ret;
857 * This routine traverse page_cgroup in given list and drop them all.
858 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
860 #define FORCE_UNCHARGE_BATCH (128)
861 static void mem_cgroup_force_empty_list(struct mem_cgroup *mem,
862 struct mem_cgroup_per_zone *mz,
863 enum lru_list lru)
865 struct page_cgroup *pc;
866 struct page *page;
867 int count = FORCE_UNCHARGE_BATCH;
868 unsigned long flags;
869 struct list_head *list;
871 list = &mz->lists[lru];
873 spin_lock_irqsave(&mz->lru_lock, flags);
874 while (!list_empty(list)) {
875 pc = list_entry(list->prev, struct page_cgroup, lru);
876 page = pc->page;
877 get_page(page);
878 spin_unlock_irqrestore(&mz->lru_lock, flags);
880 * Check if this page is on LRU. !LRU page can be found
881 * if it's under page migration.
883 if (PageLRU(page)) {
884 __mem_cgroup_uncharge_common(page,
885 MEM_CGROUP_CHARGE_TYPE_FORCE);
886 put_page(page);
887 if (--count <= 0) {
888 count = FORCE_UNCHARGE_BATCH;
889 cond_resched();
891 } else
892 cond_resched();
893 spin_lock_irqsave(&mz->lru_lock, flags);
895 spin_unlock_irqrestore(&mz->lru_lock, flags);
899 * make mem_cgroup's charge to be 0 if there is no task.
900 * This enables deleting this mem_cgroup.
902 static int mem_cgroup_force_empty(struct mem_cgroup *mem)
904 int ret = -EBUSY;
905 int node, zid;
907 css_get(&mem->css);
909 * page reclaim code (kswapd etc..) will move pages between
910 * active_list <-> inactive_list while we don't take a lock.
911 * So, we have to do loop here until all lists are empty.
913 while (mem->res.usage > 0) {
914 if (atomic_read(&mem->css.cgroup->count) > 0)
915 goto out;
916 for_each_node_state(node, N_POSSIBLE)
917 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
918 struct mem_cgroup_per_zone *mz;
919 enum lru_list l;
920 mz = mem_cgroup_zoneinfo(mem, node, zid);
921 for_each_lru(l)
922 mem_cgroup_force_empty_list(mem, mz, l);
925 ret = 0;
926 out:
927 css_put(&mem->css);
928 return ret;
931 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
933 return res_counter_read_u64(&mem_cgroup_from_cont(cont)->res,
934 cft->private);
937 * The user of this function is...
938 * RES_LIMIT.
940 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
941 const char *buffer)
943 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
944 unsigned long long val;
945 int ret;
947 switch (cft->private) {
948 case RES_LIMIT:
949 /* This function does all necessary parse...reuse it */
950 ret = res_counter_memparse_write_strategy(buffer, &val);
951 if (!ret)
952 ret = mem_cgroup_resize_limit(memcg, val);
953 break;
954 default:
955 ret = -EINVAL; /* should be BUG() ? */
956 break;
958 return ret;
961 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
963 struct mem_cgroup *mem;
965 mem = mem_cgroup_from_cont(cont);
966 switch (event) {
967 case RES_MAX_USAGE:
968 res_counter_reset_max(&mem->res);
969 break;
970 case RES_FAILCNT:
971 res_counter_reset_failcnt(&mem->res);
972 break;
974 return 0;
977 static int mem_force_empty_write(struct cgroup *cont, unsigned int event)
979 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont));
982 static const struct mem_cgroup_stat_desc {
983 const char *msg;
984 u64 unit;
985 } mem_cgroup_stat_desc[] = {
986 [MEM_CGROUP_STAT_CACHE] = { "cache", PAGE_SIZE, },
987 [MEM_CGROUP_STAT_RSS] = { "rss", PAGE_SIZE, },
988 [MEM_CGROUP_STAT_PGPGIN_COUNT] = {"pgpgin", 1, },
989 [MEM_CGROUP_STAT_PGPGOUT_COUNT] = {"pgpgout", 1, },
992 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
993 struct cgroup_map_cb *cb)
995 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
996 struct mem_cgroup_stat *stat = &mem_cont->stat;
997 int i;
999 for (i = 0; i < ARRAY_SIZE(stat->cpustat[0].count); i++) {
1000 s64 val;
1002 val = mem_cgroup_read_stat(stat, i);
1003 val *= mem_cgroup_stat_desc[i].unit;
1004 cb->fill(cb, mem_cgroup_stat_desc[i].msg, val);
1006 /* showing # of active pages */
1008 unsigned long active_anon, inactive_anon;
1009 unsigned long active_file, inactive_file;
1010 unsigned long unevictable;
1012 inactive_anon = mem_cgroup_get_all_zonestat(mem_cont,
1013 LRU_INACTIVE_ANON);
1014 active_anon = mem_cgroup_get_all_zonestat(mem_cont,
1015 LRU_ACTIVE_ANON);
1016 inactive_file = mem_cgroup_get_all_zonestat(mem_cont,
1017 LRU_INACTIVE_FILE);
1018 active_file = mem_cgroup_get_all_zonestat(mem_cont,
1019 LRU_ACTIVE_FILE);
1020 unevictable = mem_cgroup_get_all_zonestat(mem_cont,
1021 LRU_UNEVICTABLE);
1023 cb->fill(cb, "active_anon", (active_anon) * PAGE_SIZE);
1024 cb->fill(cb, "inactive_anon", (inactive_anon) * PAGE_SIZE);
1025 cb->fill(cb, "active_file", (active_file) * PAGE_SIZE);
1026 cb->fill(cb, "inactive_file", (inactive_file) * PAGE_SIZE);
1027 cb->fill(cb, "unevictable", unevictable * PAGE_SIZE);
1030 return 0;
1033 static struct cftype mem_cgroup_files[] = {
1035 .name = "usage_in_bytes",
1036 .private = RES_USAGE,
1037 .read_u64 = mem_cgroup_read,
1040 .name = "max_usage_in_bytes",
1041 .private = RES_MAX_USAGE,
1042 .trigger = mem_cgroup_reset,
1043 .read_u64 = mem_cgroup_read,
1046 .name = "limit_in_bytes",
1047 .private = RES_LIMIT,
1048 .write_string = mem_cgroup_write,
1049 .read_u64 = mem_cgroup_read,
1052 .name = "failcnt",
1053 .private = RES_FAILCNT,
1054 .trigger = mem_cgroup_reset,
1055 .read_u64 = mem_cgroup_read,
1058 .name = "force_empty",
1059 .trigger = mem_force_empty_write,
1062 .name = "stat",
1063 .read_map = mem_control_stat_show,
1067 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1069 struct mem_cgroup_per_node *pn;
1070 struct mem_cgroup_per_zone *mz;
1071 enum lru_list l;
1072 int zone, tmp = node;
1074 * This routine is called against possible nodes.
1075 * But it's BUG to call kmalloc() against offline node.
1077 * TODO: this routine can waste much memory for nodes which will
1078 * never be onlined. It's better to use memory hotplug callback
1079 * function.
1081 if (!node_state(node, N_NORMAL_MEMORY))
1082 tmp = -1;
1083 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
1084 if (!pn)
1085 return 1;
1087 mem->info.nodeinfo[node] = pn;
1088 memset(pn, 0, sizeof(*pn));
1090 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
1091 mz = &pn->zoneinfo[zone];
1092 spin_lock_init(&mz->lru_lock);
1093 for_each_lru(l)
1094 INIT_LIST_HEAD(&mz->lists[l]);
1096 return 0;
1099 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
1101 kfree(mem->info.nodeinfo[node]);
1104 static struct mem_cgroup *mem_cgroup_alloc(void)
1106 struct mem_cgroup *mem;
1108 if (sizeof(*mem) < PAGE_SIZE)
1109 mem = kmalloc(sizeof(*mem), GFP_KERNEL);
1110 else
1111 mem = vmalloc(sizeof(*mem));
1113 if (mem)
1114 memset(mem, 0, sizeof(*mem));
1115 return mem;
1118 static void mem_cgroup_free(struct mem_cgroup *mem)
1120 if (sizeof(*mem) < PAGE_SIZE)
1121 kfree(mem);
1122 else
1123 vfree(mem);
1127 static struct cgroup_subsys_state *
1128 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
1130 struct mem_cgroup *mem;
1131 int node;
1133 if (unlikely((cont->parent) == NULL)) {
1134 mem = &init_mem_cgroup;
1135 page_cgroup_cache = KMEM_CACHE(page_cgroup, SLAB_PANIC);
1136 } else {
1137 mem = mem_cgroup_alloc();
1138 if (!mem)
1139 return ERR_PTR(-ENOMEM);
1142 res_counter_init(&mem->res);
1144 for_each_node_state(node, N_POSSIBLE)
1145 if (alloc_mem_cgroup_per_zone_info(mem, node))
1146 goto free_out;
1148 return &mem->css;
1149 free_out:
1150 for_each_node_state(node, N_POSSIBLE)
1151 free_mem_cgroup_per_zone_info(mem, node);
1152 if (cont->parent != NULL)
1153 mem_cgroup_free(mem);
1154 return ERR_PTR(-ENOMEM);
1157 static void mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
1158 struct cgroup *cont)
1160 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1161 mem_cgroup_force_empty(mem);
1164 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
1165 struct cgroup *cont)
1167 int node;
1168 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
1170 for_each_node_state(node, N_POSSIBLE)
1171 free_mem_cgroup_per_zone_info(mem, node);
1173 mem_cgroup_free(mem_cgroup_from_cont(cont));
1176 static int mem_cgroup_populate(struct cgroup_subsys *ss,
1177 struct cgroup *cont)
1179 return cgroup_add_files(cont, ss, mem_cgroup_files,
1180 ARRAY_SIZE(mem_cgroup_files));
1183 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
1184 struct cgroup *cont,
1185 struct cgroup *old_cont,
1186 struct task_struct *p)
1188 struct mm_struct *mm;
1189 struct mem_cgroup *mem, *old_mem;
1191 mm = get_task_mm(p);
1192 if (mm == NULL)
1193 return;
1195 mem = mem_cgroup_from_cont(cont);
1196 old_mem = mem_cgroup_from_cont(old_cont);
1199 * Only thread group leaders are allowed to migrate, the mm_struct is
1200 * in effect owned by the leader
1202 if (!thread_group_leader(p))
1203 goto out;
1205 out:
1206 mmput(mm);
1209 struct cgroup_subsys mem_cgroup_subsys = {
1210 .name = "memory",
1211 .subsys_id = mem_cgroup_subsys_id,
1212 .create = mem_cgroup_create,
1213 .pre_destroy = mem_cgroup_pre_destroy,
1214 .destroy = mem_cgroup_destroy,
1215 .populate = mem_cgroup_populate,
1216 .attach = mem_cgroup_move_task,
1217 .early_init = 0,