thinkpad-acpi: name event constants
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / mm / memcontrol.c
blob566925e7cdbda7bfc8631554e070f6b798325d59
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/pagemap.h>
25 #include <linux/smp.h>
26 #include <linux/page-flags.h>
27 #include <linux/backing-dev.h>
28 #include <linux/bit_spinlock.h>
29 #include <linux/rcupdate.h>
30 #include <linux/limits.h>
31 #include <linux/mutex.h>
32 #include <linux/slab.h>
33 #include <linux/swap.h>
34 #include <linux/spinlock.h>
35 #include <linux/fs.h>
36 #include <linux/seq_file.h>
37 #include <linux/vmalloc.h>
38 #include <linux/mm_inline.h>
39 #include <linux/page_cgroup.h>
40 #include "internal.h"
42 #include <asm/uaccess.h>
44 struct cgroup_subsys mem_cgroup_subsys __read_mostly;
45 #define MEM_CGROUP_RECLAIM_RETRIES 5
47 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
48 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
49 int do_swap_account __read_mostly;
50 static int really_do_swap_account __initdata = 1; /* for remember boot option*/
51 #else
52 #define do_swap_account (0)
53 #endif
55 static DEFINE_MUTEX(memcg_tasklist); /* can be hold under cgroup_mutex */
58 * Statistics for memory cgroup.
60 enum mem_cgroup_stat_index {
62 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
64 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
65 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
66 MEM_CGROUP_STAT_MAPPED_FILE, /* # of pages charged as file rss */
67 MEM_CGROUP_STAT_PGPGIN_COUNT, /* # of pages paged in */
68 MEM_CGROUP_STAT_PGPGOUT_COUNT, /* # of pages paged out */
70 MEM_CGROUP_STAT_NSTATS,
73 struct mem_cgroup_stat_cpu {
74 s64 count[MEM_CGROUP_STAT_NSTATS];
75 } ____cacheline_aligned_in_smp;
77 struct mem_cgroup_stat {
78 struct mem_cgroup_stat_cpu cpustat[0];
82 * For accounting under irq disable, no need for increment preempt count.
84 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu *stat,
85 enum mem_cgroup_stat_index idx, int val)
87 stat->count[idx] += val;
90 static s64 mem_cgroup_read_stat(struct mem_cgroup_stat *stat,
91 enum mem_cgroup_stat_index idx)
93 int cpu;
94 s64 ret = 0;
95 for_each_possible_cpu(cpu)
96 ret += stat->cpustat[cpu].count[idx];
97 return ret;
100 static s64 mem_cgroup_local_usage(struct mem_cgroup_stat *stat)
102 s64 ret;
104 ret = mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_CACHE);
105 ret += mem_cgroup_read_stat(stat, MEM_CGROUP_STAT_RSS);
106 return ret;
110 * per-zone information in memory controller.
112 struct mem_cgroup_per_zone {
114 * spin_lock to protect the per cgroup LRU
116 struct list_head lists[NR_LRU_LISTS];
117 unsigned long count[NR_LRU_LISTS];
119 struct zone_reclaim_stat reclaim_stat;
121 /* Macro for accessing counter */
122 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
124 struct mem_cgroup_per_node {
125 struct mem_cgroup_per_zone zoneinfo[MAX_NR_ZONES];
128 struct mem_cgroup_lru_info {
129 struct mem_cgroup_per_node *nodeinfo[MAX_NUMNODES];
133 * The memory controller data structure. The memory controller controls both
134 * page cache and RSS per cgroup. We would eventually like to provide
135 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
136 * to help the administrator determine what knobs to tune.
138 * TODO: Add a water mark for the memory controller. Reclaim will begin when
139 * we hit the water mark. May be even add a low water mark, such that
140 * no reclaim occurs from a cgroup at it's low water mark, this is
141 * a feature that will be implemented much later in the future.
143 struct mem_cgroup {
144 struct cgroup_subsys_state css;
146 * the counter to account for memory usage
148 struct res_counter res;
150 * the counter to account for mem+swap usage.
152 struct res_counter memsw;
154 * Per cgroup active and inactive list, similar to the
155 * per zone LRU lists.
157 struct mem_cgroup_lru_info info;
160 protect against reclaim related member.
162 spinlock_t reclaim_param_lock;
164 int prev_priority; /* for recording reclaim priority */
167 * While reclaiming in a hiearchy, we cache the last child we
168 * reclaimed from.
170 int last_scanned_child;
172 * Should the accounting and control be hierarchical, per subtree?
174 bool use_hierarchy;
175 unsigned long last_oom_jiffies;
176 atomic_t refcnt;
178 unsigned int swappiness;
180 /* set when res.limit == memsw.limit */
181 bool memsw_is_minimum;
184 * statistics. This must be placed at the end of memcg.
186 struct mem_cgroup_stat stat;
189 enum charge_type {
190 MEM_CGROUP_CHARGE_TYPE_CACHE = 0,
191 MEM_CGROUP_CHARGE_TYPE_MAPPED,
192 MEM_CGROUP_CHARGE_TYPE_SHMEM, /* used by page migration of shmem */
193 MEM_CGROUP_CHARGE_TYPE_FORCE, /* used by force_empty */
194 MEM_CGROUP_CHARGE_TYPE_SWAPOUT, /* for accounting swapcache */
195 MEM_CGROUP_CHARGE_TYPE_DROP, /* a page was unused swap cache */
196 NR_CHARGE_TYPE,
199 /* only for here (for easy reading.) */
200 #define PCGF_CACHE (1UL << PCG_CACHE)
201 #define PCGF_USED (1UL << PCG_USED)
202 #define PCGF_LOCK (1UL << PCG_LOCK)
203 static const unsigned long
204 pcg_default_flags[NR_CHARGE_TYPE] = {
205 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* File Cache */
206 PCGF_USED | PCGF_LOCK, /* Anon */
207 PCGF_CACHE | PCGF_USED | PCGF_LOCK, /* Shmem */
208 0, /* FORCE */
211 /* for encoding cft->private value on file */
212 #define _MEM (0)
213 #define _MEMSWAP (1)
214 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
215 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
216 #define MEMFILE_ATTR(val) ((val) & 0xffff)
218 static void mem_cgroup_get(struct mem_cgroup *mem);
219 static void mem_cgroup_put(struct mem_cgroup *mem);
220 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem);
222 static void mem_cgroup_charge_statistics(struct mem_cgroup *mem,
223 struct page_cgroup *pc,
224 bool charge)
226 int val = (charge)? 1 : -1;
227 struct mem_cgroup_stat *stat = &mem->stat;
228 struct mem_cgroup_stat_cpu *cpustat;
229 int cpu = get_cpu();
231 cpustat = &stat->cpustat[cpu];
232 if (PageCgroupCache(pc))
233 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_CACHE, val);
234 else
235 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_RSS, val);
237 if (charge)
238 __mem_cgroup_stat_add_safe(cpustat,
239 MEM_CGROUP_STAT_PGPGIN_COUNT, 1);
240 else
241 __mem_cgroup_stat_add_safe(cpustat,
242 MEM_CGROUP_STAT_PGPGOUT_COUNT, 1);
243 put_cpu();
246 static struct mem_cgroup_per_zone *
247 mem_cgroup_zoneinfo(struct mem_cgroup *mem, int nid, int zid)
249 return &mem->info.nodeinfo[nid]->zoneinfo[zid];
252 static struct mem_cgroup_per_zone *
253 page_cgroup_zoneinfo(struct page_cgroup *pc)
255 struct mem_cgroup *mem = pc->mem_cgroup;
256 int nid = page_cgroup_nid(pc);
257 int zid = page_cgroup_zid(pc);
259 if (!mem)
260 return NULL;
262 return mem_cgroup_zoneinfo(mem, nid, zid);
265 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup *mem,
266 enum lru_list idx)
268 int nid, zid;
269 struct mem_cgroup_per_zone *mz;
270 u64 total = 0;
272 for_each_online_node(nid)
273 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
274 mz = mem_cgroup_zoneinfo(mem, nid, zid);
275 total += MEM_CGROUP_ZSTAT(mz, idx);
277 return total;
280 static struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont)
282 return container_of(cgroup_subsys_state(cont,
283 mem_cgroup_subsys_id), struct mem_cgroup,
284 css);
287 struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p)
290 * mm_update_next_owner() may clear mm->owner to NULL
291 * if it races with swapoff, page migration, etc.
292 * So this can be called with p == NULL.
294 if (unlikely(!p))
295 return NULL;
297 return container_of(task_subsys_state(p, mem_cgroup_subsys_id),
298 struct mem_cgroup, css);
301 static struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
303 struct mem_cgroup *mem = NULL;
305 if (!mm)
306 return NULL;
308 * Because we have no locks, mm->owner's may be being moved to other
309 * cgroup. We use css_tryget() here even if this looks
310 * pessimistic (rather than adding locks here).
312 rcu_read_lock();
313 do {
314 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
315 if (unlikely(!mem))
316 break;
317 } while (!css_tryget(&mem->css));
318 rcu_read_unlock();
319 return mem;
323 * Call callback function against all cgroup under hierarchy tree.
325 static int mem_cgroup_walk_tree(struct mem_cgroup *root, void *data,
326 int (*func)(struct mem_cgroup *, void *))
328 int found, ret, nextid;
329 struct cgroup_subsys_state *css;
330 struct mem_cgroup *mem;
332 if (!root->use_hierarchy)
333 return (*func)(root, data);
335 nextid = 1;
336 do {
337 ret = 0;
338 mem = NULL;
340 rcu_read_lock();
341 css = css_get_next(&mem_cgroup_subsys, nextid, &root->css,
342 &found);
343 if (css && css_tryget(css))
344 mem = container_of(css, struct mem_cgroup, css);
345 rcu_read_unlock();
347 if (mem) {
348 ret = (*func)(mem, data);
349 css_put(&mem->css);
351 nextid = found + 1;
352 } while (!ret && css);
354 return ret;
358 * Following LRU functions are allowed to be used without PCG_LOCK.
359 * Operations are called by routine of global LRU independently from memcg.
360 * What we have to take care of here is validness of pc->mem_cgroup.
362 * Changes to pc->mem_cgroup happens when
363 * 1. charge
364 * 2. moving account
365 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
366 * It is added to LRU before charge.
367 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
368 * When moving account, the page is not on LRU. It's isolated.
371 void mem_cgroup_del_lru_list(struct page *page, enum lru_list lru)
373 struct page_cgroup *pc;
374 struct mem_cgroup *mem;
375 struct mem_cgroup_per_zone *mz;
377 if (mem_cgroup_disabled())
378 return;
379 pc = lookup_page_cgroup(page);
380 /* can happen while we handle swapcache. */
381 if (list_empty(&pc->lru) || !pc->mem_cgroup)
382 return;
384 * We don't check PCG_USED bit. It's cleared when the "page" is finally
385 * removed from global LRU.
387 mz = page_cgroup_zoneinfo(pc);
388 mem = pc->mem_cgroup;
389 MEM_CGROUP_ZSTAT(mz, lru) -= 1;
390 list_del_init(&pc->lru);
391 return;
394 void mem_cgroup_del_lru(struct page *page)
396 mem_cgroup_del_lru_list(page, page_lru(page));
399 void mem_cgroup_rotate_lru_list(struct page *page, enum lru_list lru)
401 struct mem_cgroup_per_zone *mz;
402 struct page_cgroup *pc;
404 if (mem_cgroup_disabled())
405 return;
407 pc = lookup_page_cgroup(page);
409 * Used bit is set without atomic ops but after smp_wmb().
410 * For making pc->mem_cgroup visible, insert smp_rmb() here.
412 smp_rmb();
413 /* unused page is not rotated. */
414 if (!PageCgroupUsed(pc))
415 return;
416 mz = page_cgroup_zoneinfo(pc);
417 list_move(&pc->lru, &mz->lists[lru]);
420 void mem_cgroup_add_lru_list(struct page *page, enum lru_list lru)
422 struct page_cgroup *pc;
423 struct mem_cgroup_per_zone *mz;
425 if (mem_cgroup_disabled())
426 return;
427 pc = lookup_page_cgroup(page);
429 * Used bit is set without atomic ops but after smp_wmb().
430 * For making pc->mem_cgroup visible, insert smp_rmb() here.
432 smp_rmb();
433 if (!PageCgroupUsed(pc))
434 return;
436 mz = page_cgroup_zoneinfo(pc);
437 MEM_CGROUP_ZSTAT(mz, lru) += 1;
438 list_add(&pc->lru, &mz->lists[lru]);
442 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
443 * lru because the page may.be reused after it's fully uncharged (because of
444 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
445 * it again. This function is only used to charge SwapCache. It's done under
446 * lock_page and expected that zone->lru_lock is never held.
448 static void mem_cgroup_lru_del_before_commit_swapcache(struct page *page)
450 unsigned long flags;
451 struct zone *zone = page_zone(page);
452 struct page_cgroup *pc = lookup_page_cgroup(page);
454 spin_lock_irqsave(&zone->lru_lock, flags);
456 * Forget old LRU when this page_cgroup is *not* used. This Used bit
457 * is guarded by lock_page() because the page is SwapCache.
459 if (!PageCgroupUsed(pc))
460 mem_cgroup_del_lru_list(page, page_lru(page));
461 spin_unlock_irqrestore(&zone->lru_lock, flags);
464 static void mem_cgroup_lru_add_after_commit_swapcache(struct page *page)
466 unsigned long flags;
467 struct zone *zone = page_zone(page);
468 struct page_cgroup *pc = lookup_page_cgroup(page);
470 spin_lock_irqsave(&zone->lru_lock, flags);
471 /* link when the page is linked to LRU but page_cgroup isn't */
472 if (PageLRU(page) && list_empty(&pc->lru))
473 mem_cgroup_add_lru_list(page, page_lru(page));
474 spin_unlock_irqrestore(&zone->lru_lock, flags);
478 void mem_cgroup_move_lists(struct page *page,
479 enum lru_list from, enum lru_list to)
481 if (mem_cgroup_disabled())
482 return;
483 mem_cgroup_del_lru_list(page, from);
484 mem_cgroup_add_lru_list(page, to);
487 int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *mem)
489 int ret;
490 struct mem_cgroup *curr = NULL;
492 task_lock(task);
493 rcu_read_lock();
494 curr = try_get_mem_cgroup_from_mm(task->mm);
495 rcu_read_unlock();
496 task_unlock(task);
497 if (!curr)
498 return 0;
500 * We should check use_hierarchy of "mem" not "curr". Because checking
501 * use_hierarchy of "curr" here make this function true if hierarchy is
502 * enabled in "curr" and "curr" is a child of "mem" in *cgroup*
503 * hierarchy(even if use_hierarchy is disabled in "mem").
505 if (mem->use_hierarchy)
506 ret = css_is_ancestor(&curr->css, &mem->css);
507 else
508 ret = (curr == mem);
509 css_put(&curr->css);
510 return ret;
514 * prev_priority control...this will be used in memory reclaim path.
516 int mem_cgroup_get_reclaim_priority(struct mem_cgroup *mem)
518 int prev_priority;
520 spin_lock(&mem->reclaim_param_lock);
521 prev_priority = mem->prev_priority;
522 spin_unlock(&mem->reclaim_param_lock);
524 return prev_priority;
527 void mem_cgroup_note_reclaim_priority(struct mem_cgroup *mem, int priority)
529 spin_lock(&mem->reclaim_param_lock);
530 if (priority < mem->prev_priority)
531 mem->prev_priority = priority;
532 spin_unlock(&mem->reclaim_param_lock);
535 void mem_cgroup_record_reclaim_priority(struct mem_cgroup *mem, int priority)
537 spin_lock(&mem->reclaim_param_lock);
538 mem->prev_priority = priority;
539 spin_unlock(&mem->reclaim_param_lock);
542 static int calc_inactive_ratio(struct mem_cgroup *memcg, unsigned long *present_pages)
544 unsigned long active;
545 unsigned long inactive;
546 unsigned long gb;
547 unsigned long inactive_ratio;
549 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_ANON);
550 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_ANON);
552 gb = (inactive + active) >> (30 - PAGE_SHIFT);
553 if (gb)
554 inactive_ratio = int_sqrt(10 * gb);
555 else
556 inactive_ratio = 1;
558 if (present_pages) {
559 present_pages[0] = inactive;
560 present_pages[1] = active;
563 return inactive_ratio;
566 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup *memcg)
568 unsigned long active;
569 unsigned long inactive;
570 unsigned long present_pages[2];
571 unsigned long inactive_ratio;
573 inactive_ratio = calc_inactive_ratio(memcg, present_pages);
575 inactive = present_pages[0];
576 active = present_pages[1];
578 if (inactive * inactive_ratio < active)
579 return 1;
581 return 0;
584 int mem_cgroup_inactive_file_is_low(struct mem_cgroup *memcg)
586 unsigned long active;
587 unsigned long inactive;
589 inactive = mem_cgroup_get_local_zonestat(memcg, LRU_INACTIVE_FILE);
590 active = mem_cgroup_get_local_zonestat(memcg, LRU_ACTIVE_FILE);
592 return (active > inactive);
595 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup *memcg,
596 struct zone *zone,
597 enum lru_list lru)
599 int nid = zone->zone_pgdat->node_id;
600 int zid = zone_idx(zone);
601 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
603 return MEM_CGROUP_ZSTAT(mz, lru);
606 struct zone_reclaim_stat *mem_cgroup_get_reclaim_stat(struct mem_cgroup *memcg,
607 struct zone *zone)
609 int nid = zone->zone_pgdat->node_id;
610 int zid = zone_idx(zone);
611 struct mem_cgroup_per_zone *mz = mem_cgroup_zoneinfo(memcg, nid, zid);
613 return &mz->reclaim_stat;
616 struct zone_reclaim_stat *
617 mem_cgroup_get_reclaim_stat_from_page(struct page *page)
619 struct page_cgroup *pc;
620 struct mem_cgroup_per_zone *mz;
622 if (mem_cgroup_disabled())
623 return NULL;
625 pc = lookup_page_cgroup(page);
627 * Used bit is set without atomic ops but after smp_wmb().
628 * For making pc->mem_cgroup visible, insert smp_rmb() here.
630 smp_rmb();
631 if (!PageCgroupUsed(pc))
632 return NULL;
634 mz = page_cgroup_zoneinfo(pc);
635 if (!mz)
636 return NULL;
638 return &mz->reclaim_stat;
641 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan,
642 struct list_head *dst,
643 unsigned long *scanned, int order,
644 int mode, struct zone *z,
645 struct mem_cgroup *mem_cont,
646 int active, int file)
648 unsigned long nr_taken = 0;
649 struct page *page;
650 unsigned long scan;
651 LIST_HEAD(pc_list);
652 struct list_head *src;
653 struct page_cgroup *pc, *tmp;
654 int nid = z->zone_pgdat->node_id;
655 int zid = zone_idx(z);
656 struct mem_cgroup_per_zone *mz;
657 int lru = LRU_FILE * !!file + !!active;
658 int ret;
660 BUG_ON(!mem_cont);
661 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
662 src = &mz->lists[lru];
664 scan = 0;
665 list_for_each_entry_safe_reverse(pc, tmp, src, lru) {
666 if (scan >= nr_to_scan)
667 break;
669 page = pc->page;
670 if (unlikely(!PageCgroupUsed(pc)))
671 continue;
672 if (unlikely(!PageLRU(page)))
673 continue;
675 scan++;
676 ret = __isolate_lru_page(page, mode, file);
677 switch (ret) {
678 case 0:
679 list_move(&page->lru, dst);
680 mem_cgroup_del_lru(page);
681 nr_taken++;
682 break;
683 case -EBUSY:
684 /* we don't affect global LRU but rotate in our LRU */
685 mem_cgroup_rotate_lru_list(page, page_lru(page));
686 break;
687 default:
688 break;
692 *scanned = scan;
693 return nr_taken;
696 #define mem_cgroup_from_res_counter(counter, member) \
697 container_of(counter, struct mem_cgroup, member)
699 static bool mem_cgroup_check_under_limit(struct mem_cgroup *mem)
701 if (do_swap_account) {
702 if (res_counter_check_under_limit(&mem->res) &&
703 res_counter_check_under_limit(&mem->memsw))
704 return true;
705 } else
706 if (res_counter_check_under_limit(&mem->res))
707 return true;
708 return false;
711 static unsigned int get_swappiness(struct mem_cgroup *memcg)
713 struct cgroup *cgrp = memcg->css.cgroup;
714 unsigned int swappiness;
716 /* root ? */
717 if (cgrp->parent == NULL)
718 return vm_swappiness;
720 spin_lock(&memcg->reclaim_param_lock);
721 swappiness = memcg->swappiness;
722 spin_unlock(&memcg->reclaim_param_lock);
724 return swappiness;
727 static int mem_cgroup_count_children_cb(struct mem_cgroup *mem, void *data)
729 int *val = data;
730 (*val)++;
731 return 0;
735 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
736 * @memcg: The memory cgroup that went over limit
737 * @p: Task that is going to be killed
739 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
740 * enabled
742 void mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
744 struct cgroup *task_cgrp;
745 struct cgroup *mem_cgrp;
747 * Need a buffer in BSS, can't rely on allocations. The code relies
748 * on the assumption that OOM is serialized for memory controller.
749 * If this assumption is broken, revisit this code.
751 static char memcg_name[PATH_MAX];
752 int ret;
754 if (!memcg)
755 return;
758 rcu_read_lock();
760 mem_cgrp = memcg->css.cgroup;
761 task_cgrp = task_cgroup(p, mem_cgroup_subsys_id);
763 ret = cgroup_path(task_cgrp, memcg_name, PATH_MAX);
764 if (ret < 0) {
766 * Unfortunately, we are unable to convert to a useful name
767 * But we'll still print out the usage information
769 rcu_read_unlock();
770 goto done;
772 rcu_read_unlock();
774 printk(KERN_INFO "Task in %s killed", memcg_name);
776 rcu_read_lock();
777 ret = cgroup_path(mem_cgrp, memcg_name, PATH_MAX);
778 if (ret < 0) {
779 rcu_read_unlock();
780 goto done;
782 rcu_read_unlock();
785 * Continues from above, so we don't need an KERN_ level
787 printk(KERN_CONT " as a result of limit of %s\n", memcg_name);
788 done:
790 printk(KERN_INFO "memory: usage %llukB, limit %llukB, failcnt %llu\n",
791 res_counter_read_u64(&memcg->res, RES_USAGE) >> 10,
792 res_counter_read_u64(&memcg->res, RES_LIMIT) >> 10,
793 res_counter_read_u64(&memcg->res, RES_FAILCNT));
794 printk(KERN_INFO "memory+swap: usage %llukB, limit %llukB, "
795 "failcnt %llu\n",
796 res_counter_read_u64(&memcg->memsw, RES_USAGE) >> 10,
797 res_counter_read_u64(&memcg->memsw, RES_LIMIT) >> 10,
798 res_counter_read_u64(&memcg->memsw, RES_FAILCNT));
802 * This function returns the number of memcg under hierarchy tree. Returns
803 * 1(self count) if no children.
805 static int mem_cgroup_count_children(struct mem_cgroup *mem)
807 int num = 0;
808 mem_cgroup_walk_tree(mem, &num, mem_cgroup_count_children_cb);
809 return num;
813 * Visit the first child (need not be the first child as per the ordering
814 * of the cgroup list, since we track last_scanned_child) of @mem and use
815 * that to reclaim free pages from.
817 static struct mem_cgroup *
818 mem_cgroup_select_victim(struct mem_cgroup *root_mem)
820 struct mem_cgroup *ret = NULL;
821 struct cgroup_subsys_state *css;
822 int nextid, found;
824 if (!root_mem->use_hierarchy) {
825 css_get(&root_mem->css);
826 ret = root_mem;
829 while (!ret) {
830 rcu_read_lock();
831 nextid = root_mem->last_scanned_child + 1;
832 css = css_get_next(&mem_cgroup_subsys, nextid, &root_mem->css,
833 &found);
834 if (css && css_tryget(css))
835 ret = container_of(css, struct mem_cgroup, css);
837 rcu_read_unlock();
838 /* Updates scanning parameter */
839 spin_lock(&root_mem->reclaim_param_lock);
840 if (!css) {
841 /* this means start scan from ID:1 */
842 root_mem->last_scanned_child = 0;
843 } else
844 root_mem->last_scanned_child = found;
845 spin_unlock(&root_mem->reclaim_param_lock);
848 return ret;
852 * Scan the hierarchy if needed to reclaim memory. We remember the last child
853 * we reclaimed from, so that we don't end up penalizing one child extensively
854 * based on its position in the children list.
856 * root_mem is the original ancestor that we've been reclaim from.
858 * We give up and return to the caller when we visit root_mem twice.
859 * (other groups can be removed while we're walking....)
861 * If shrink==true, for avoiding to free too much, this returns immedieately.
863 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup *root_mem,
864 gfp_t gfp_mask, bool noswap, bool shrink)
866 struct mem_cgroup *victim;
867 int ret, total = 0;
868 int loop = 0;
870 /* If memsw_is_minimum==1, swap-out is of-no-use. */
871 if (root_mem->memsw_is_minimum)
872 noswap = true;
874 while (loop < 2) {
875 victim = mem_cgroup_select_victim(root_mem);
876 if (victim == root_mem)
877 loop++;
878 if (!mem_cgroup_local_usage(&victim->stat)) {
879 /* this cgroup's local usage == 0 */
880 css_put(&victim->css);
881 continue;
883 /* we use swappiness of local cgroup */
884 ret = try_to_free_mem_cgroup_pages(victim, gfp_mask, noswap,
885 get_swappiness(victim));
886 css_put(&victim->css);
888 * At shrinking usage, we can't check we should stop here or
889 * reclaim more. It's depends on callers. last_scanned_child
890 * will work enough for keeping fairness under tree.
892 if (shrink)
893 return ret;
894 total += ret;
895 if (mem_cgroup_check_under_limit(root_mem))
896 return 1 + total;
898 return total;
901 bool mem_cgroup_oom_called(struct task_struct *task)
903 bool ret = false;
904 struct mem_cgroup *mem;
905 struct mm_struct *mm;
907 rcu_read_lock();
908 mm = task->mm;
909 if (!mm)
910 mm = &init_mm;
911 mem = mem_cgroup_from_task(rcu_dereference(mm->owner));
912 if (mem && time_before(jiffies, mem->last_oom_jiffies + HZ/10))
913 ret = true;
914 rcu_read_unlock();
915 return ret;
918 static int record_last_oom_cb(struct mem_cgroup *mem, void *data)
920 mem->last_oom_jiffies = jiffies;
921 return 0;
924 static void record_last_oom(struct mem_cgroup *mem)
926 mem_cgroup_walk_tree(mem, NULL, record_last_oom_cb);
930 * Currently used to update mapped file statistics, but the routine can be
931 * generalized to update other statistics as well.
933 void mem_cgroup_update_mapped_file_stat(struct page *page, int val)
935 struct mem_cgroup *mem;
936 struct mem_cgroup_stat *stat;
937 struct mem_cgroup_stat_cpu *cpustat;
938 int cpu;
939 struct page_cgroup *pc;
941 if (!page_is_file_cache(page))
942 return;
944 pc = lookup_page_cgroup(page);
945 if (unlikely(!pc))
946 return;
948 lock_page_cgroup(pc);
949 mem = pc->mem_cgroup;
950 if (!mem)
951 goto done;
953 if (!PageCgroupUsed(pc))
954 goto done;
957 * Preemption is already disabled, we don't need get_cpu()
959 cpu = smp_processor_id();
960 stat = &mem->stat;
961 cpustat = &stat->cpustat[cpu];
963 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE, val);
964 done:
965 unlock_page_cgroup(pc);
969 * Unlike exported interface, "oom" parameter is added. if oom==true,
970 * oom-killer can be invoked.
972 static int __mem_cgroup_try_charge(struct mm_struct *mm,
973 gfp_t gfp_mask, struct mem_cgroup **memcg,
974 bool oom)
976 struct mem_cgroup *mem, *mem_over_limit;
977 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
978 struct res_counter *fail_res;
980 if (unlikely(test_thread_flag(TIF_MEMDIE))) {
981 /* Don't account this! */
982 *memcg = NULL;
983 return 0;
987 * We always charge the cgroup the mm_struct belongs to.
988 * The mm_struct's mem_cgroup changes on task migration if the
989 * thread group leader migrates. It's possible that mm is not
990 * set, if so charge the init_mm (happens for pagecache usage).
992 mem = *memcg;
993 if (likely(!mem)) {
994 mem = try_get_mem_cgroup_from_mm(mm);
995 *memcg = mem;
996 } else {
997 css_get(&mem->css);
999 if (unlikely(!mem))
1000 return 0;
1002 VM_BUG_ON(css_is_removed(&mem->css));
1004 while (1) {
1005 int ret;
1006 bool noswap = false;
1008 ret = res_counter_charge(&mem->res, PAGE_SIZE, &fail_res);
1009 if (likely(!ret)) {
1010 if (!do_swap_account)
1011 break;
1012 ret = res_counter_charge(&mem->memsw, PAGE_SIZE,
1013 &fail_res);
1014 if (likely(!ret))
1015 break;
1016 /* mem+swap counter fails */
1017 res_counter_uncharge(&mem->res, PAGE_SIZE);
1018 noswap = true;
1019 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1020 memsw);
1021 } else
1022 /* mem counter fails */
1023 mem_over_limit = mem_cgroup_from_res_counter(fail_res,
1024 res);
1026 if (!(gfp_mask & __GFP_WAIT))
1027 goto nomem;
1029 ret = mem_cgroup_hierarchical_reclaim(mem_over_limit, gfp_mask,
1030 noswap, false);
1031 if (ret)
1032 continue;
1035 * try_to_free_mem_cgroup_pages() might not give us a full
1036 * picture of reclaim. Some pages are reclaimed and might be
1037 * moved to swap cache or just unmapped from the cgroup.
1038 * Check the limit again to see if the reclaim reduced the
1039 * current usage of the cgroup before giving up
1042 if (mem_cgroup_check_under_limit(mem_over_limit))
1043 continue;
1045 if (!nr_retries--) {
1046 if (oom) {
1047 mutex_lock(&memcg_tasklist);
1048 mem_cgroup_out_of_memory(mem_over_limit, gfp_mask);
1049 mutex_unlock(&memcg_tasklist);
1050 record_last_oom(mem_over_limit);
1052 goto nomem;
1055 return 0;
1056 nomem:
1057 css_put(&mem->css);
1058 return -ENOMEM;
1063 * A helper function to get mem_cgroup from ID. must be called under
1064 * rcu_read_lock(). The caller must check css_is_removed() or some if
1065 * it's concern. (dropping refcnt from swap can be called against removed
1066 * memcg.)
1068 static struct mem_cgroup *mem_cgroup_lookup(unsigned short id)
1070 struct cgroup_subsys_state *css;
1072 /* ID 0 is unused ID */
1073 if (!id)
1074 return NULL;
1075 css = css_lookup(&mem_cgroup_subsys, id);
1076 if (!css)
1077 return NULL;
1078 return container_of(css, struct mem_cgroup, css);
1081 static struct mem_cgroup *try_get_mem_cgroup_from_swapcache(struct page *page)
1083 struct mem_cgroup *mem;
1084 struct page_cgroup *pc;
1085 unsigned short id;
1086 swp_entry_t ent;
1088 VM_BUG_ON(!PageLocked(page));
1090 if (!PageSwapCache(page))
1091 return NULL;
1093 pc = lookup_page_cgroup(page);
1094 lock_page_cgroup(pc);
1095 if (PageCgroupUsed(pc)) {
1096 mem = pc->mem_cgroup;
1097 if (mem && !css_tryget(&mem->css))
1098 mem = NULL;
1099 } else {
1100 ent.val = page_private(page);
1101 id = lookup_swap_cgroup(ent);
1102 rcu_read_lock();
1103 mem = mem_cgroup_lookup(id);
1104 if (mem && !css_tryget(&mem->css))
1105 mem = NULL;
1106 rcu_read_unlock();
1108 unlock_page_cgroup(pc);
1109 return mem;
1113 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1114 * USED state. If already USED, uncharge and return.
1117 static void __mem_cgroup_commit_charge(struct mem_cgroup *mem,
1118 struct page_cgroup *pc,
1119 enum charge_type ctype)
1121 /* try_charge() can return NULL to *memcg, taking care of it. */
1122 if (!mem)
1123 return;
1125 lock_page_cgroup(pc);
1126 if (unlikely(PageCgroupUsed(pc))) {
1127 unlock_page_cgroup(pc);
1128 res_counter_uncharge(&mem->res, PAGE_SIZE);
1129 if (do_swap_account)
1130 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1131 css_put(&mem->css);
1132 return;
1134 pc->mem_cgroup = mem;
1135 smp_wmb();
1136 pc->flags = pcg_default_flags[ctype];
1138 mem_cgroup_charge_statistics(mem, pc, true);
1140 unlock_page_cgroup(pc);
1144 * mem_cgroup_move_account - move account of the page
1145 * @pc: page_cgroup of the page.
1146 * @from: mem_cgroup which the page is moved from.
1147 * @to: mem_cgroup which the page is moved to. @from != @to.
1149 * The caller must confirm following.
1150 * - page is not on LRU (isolate_page() is useful.)
1152 * returns 0 at success,
1153 * returns -EBUSY when lock is busy or "pc" is unstable.
1155 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1156 * new cgroup. It should be done by a caller.
1159 static int mem_cgroup_move_account(struct page_cgroup *pc,
1160 struct mem_cgroup *from, struct mem_cgroup *to)
1162 struct mem_cgroup_per_zone *from_mz, *to_mz;
1163 int nid, zid;
1164 int ret = -EBUSY;
1165 struct page *page;
1166 int cpu;
1167 struct mem_cgroup_stat *stat;
1168 struct mem_cgroup_stat_cpu *cpustat;
1170 VM_BUG_ON(from == to);
1171 VM_BUG_ON(PageLRU(pc->page));
1173 nid = page_cgroup_nid(pc);
1174 zid = page_cgroup_zid(pc);
1175 from_mz = mem_cgroup_zoneinfo(from, nid, zid);
1176 to_mz = mem_cgroup_zoneinfo(to, nid, zid);
1178 if (!trylock_page_cgroup(pc))
1179 return ret;
1181 if (!PageCgroupUsed(pc))
1182 goto out;
1184 if (pc->mem_cgroup != from)
1185 goto out;
1187 res_counter_uncharge(&from->res, PAGE_SIZE);
1188 mem_cgroup_charge_statistics(from, pc, false);
1190 page = pc->page;
1191 if (page_is_file_cache(page) && page_mapped(page)) {
1192 cpu = smp_processor_id();
1193 /* Update mapped_file data for mem_cgroup "from" */
1194 stat = &from->stat;
1195 cpustat = &stat->cpustat[cpu];
1196 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1197 -1);
1199 /* Update mapped_file data for mem_cgroup "to" */
1200 stat = &to->stat;
1201 cpustat = &stat->cpustat[cpu];
1202 __mem_cgroup_stat_add_safe(cpustat, MEM_CGROUP_STAT_MAPPED_FILE,
1206 if (do_swap_account)
1207 res_counter_uncharge(&from->memsw, PAGE_SIZE);
1208 css_put(&from->css);
1210 css_get(&to->css);
1211 pc->mem_cgroup = to;
1212 mem_cgroup_charge_statistics(to, pc, true);
1213 ret = 0;
1214 out:
1215 unlock_page_cgroup(pc);
1217 * We charges against "to" which may not have any tasks. Then, "to"
1218 * can be under rmdir(). But in current implementation, caller of
1219 * this function is just force_empty() and it's garanteed that
1220 * "to" is never removed. So, we don't check rmdir status here.
1222 return ret;
1226 * move charges to its parent.
1229 static int mem_cgroup_move_parent(struct page_cgroup *pc,
1230 struct mem_cgroup *child,
1231 gfp_t gfp_mask)
1233 struct page *page = pc->page;
1234 struct cgroup *cg = child->css.cgroup;
1235 struct cgroup *pcg = cg->parent;
1236 struct mem_cgroup *parent;
1237 int ret;
1239 /* Is ROOT ? */
1240 if (!pcg)
1241 return -EINVAL;
1244 parent = mem_cgroup_from_cont(pcg);
1247 ret = __mem_cgroup_try_charge(NULL, gfp_mask, &parent, false);
1248 if (ret || !parent)
1249 return ret;
1251 if (!get_page_unless_zero(page)) {
1252 ret = -EBUSY;
1253 goto uncharge;
1256 ret = isolate_lru_page(page);
1258 if (ret)
1259 goto cancel;
1261 ret = mem_cgroup_move_account(pc, child, parent);
1263 putback_lru_page(page);
1264 if (!ret) {
1265 put_page(page);
1266 /* drop extra refcnt by try_charge() */
1267 css_put(&parent->css);
1268 return 0;
1271 cancel:
1272 put_page(page);
1273 uncharge:
1274 /* drop extra refcnt by try_charge() */
1275 css_put(&parent->css);
1276 /* uncharge if move fails */
1277 res_counter_uncharge(&parent->res, PAGE_SIZE);
1278 if (do_swap_account)
1279 res_counter_uncharge(&parent->memsw, PAGE_SIZE);
1280 return ret;
1284 * Charge the memory controller for page usage.
1285 * Return
1286 * 0 if the charge was successful
1287 * < 0 if the cgroup is over its limit
1289 static int mem_cgroup_charge_common(struct page *page, struct mm_struct *mm,
1290 gfp_t gfp_mask, enum charge_type ctype,
1291 struct mem_cgroup *memcg)
1293 struct mem_cgroup *mem;
1294 struct page_cgroup *pc;
1295 int ret;
1297 pc = lookup_page_cgroup(page);
1298 /* can happen at boot */
1299 if (unlikely(!pc))
1300 return 0;
1301 prefetchw(pc);
1303 mem = memcg;
1304 ret = __mem_cgroup_try_charge(mm, gfp_mask, &mem, true);
1305 if (ret || !mem)
1306 return ret;
1308 __mem_cgroup_commit_charge(mem, pc, ctype);
1309 return 0;
1312 int mem_cgroup_newpage_charge(struct page *page,
1313 struct mm_struct *mm, gfp_t gfp_mask)
1315 if (mem_cgroup_disabled())
1316 return 0;
1317 if (PageCompound(page))
1318 return 0;
1320 * If already mapped, we don't have to account.
1321 * If page cache, page->mapping has address_space.
1322 * But page->mapping may have out-of-use anon_vma pointer,
1323 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1324 * is NULL.
1326 if (page_mapped(page) || (page->mapping && !PageAnon(page)))
1327 return 0;
1328 if (unlikely(!mm))
1329 mm = &init_mm;
1330 return mem_cgroup_charge_common(page, mm, gfp_mask,
1331 MEM_CGROUP_CHARGE_TYPE_MAPPED, NULL);
1334 static void
1335 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1336 enum charge_type ctype);
1338 int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
1339 gfp_t gfp_mask)
1341 struct mem_cgroup *mem = NULL;
1342 int ret;
1344 if (mem_cgroup_disabled())
1345 return 0;
1346 if (PageCompound(page))
1347 return 0;
1349 * Corner case handling. This is called from add_to_page_cache()
1350 * in usual. But some FS (shmem) precharges this page before calling it
1351 * and call add_to_page_cache() with GFP_NOWAIT.
1353 * For GFP_NOWAIT case, the page may be pre-charged before calling
1354 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1355 * charge twice. (It works but has to pay a bit larger cost.)
1356 * And when the page is SwapCache, it should take swap information
1357 * into account. This is under lock_page() now.
1359 if (!(gfp_mask & __GFP_WAIT)) {
1360 struct page_cgroup *pc;
1363 pc = lookup_page_cgroup(page);
1364 if (!pc)
1365 return 0;
1366 lock_page_cgroup(pc);
1367 if (PageCgroupUsed(pc)) {
1368 unlock_page_cgroup(pc);
1369 return 0;
1371 unlock_page_cgroup(pc);
1374 if (unlikely(!mm && !mem))
1375 mm = &init_mm;
1377 if (page_is_file_cache(page))
1378 return mem_cgroup_charge_common(page, mm, gfp_mask,
1379 MEM_CGROUP_CHARGE_TYPE_CACHE, NULL);
1381 /* shmem */
1382 if (PageSwapCache(page)) {
1383 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1384 if (!ret)
1385 __mem_cgroup_commit_charge_swapin(page, mem,
1386 MEM_CGROUP_CHARGE_TYPE_SHMEM);
1387 } else
1388 ret = mem_cgroup_charge_common(page, mm, gfp_mask,
1389 MEM_CGROUP_CHARGE_TYPE_SHMEM, mem);
1391 return ret;
1395 * While swap-in, try_charge -> commit or cancel, the page is locked.
1396 * And when try_charge() successfully returns, one refcnt to memcg without
1397 * struct page_cgroup is aquired. This refcnt will be cumsumed by
1398 * "commit()" or removed by "cancel()"
1400 int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
1401 struct page *page,
1402 gfp_t mask, struct mem_cgroup **ptr)
1404 struct mem_cgroup *mem;
1405 int ret;
1407 if (mem_cgroup_disabled())
1408 return 0;
1410 if (!do_swap_account)
1411 goto charge_cur_mm;
1413 * A racing thread's fault, or swapoff, may have already updated
1414 * the pte, and even removed page from swap cache: return success
1415 * to go on to do_swap_page()'s pte_same() test, which should fail.
1417 if (!PageSwapCache(page))
1418 return 0;
1419 mem = try_get_mem_cgroup_from_swapcache(page);
1420 if (!mem)
1421 goto charge_cur_mm;
1422 *ptr = mem;
1423 ret = __mem_cgroup_try_charge(NULL, mask, ptr, true);
1424 /* drop extra refcnt from tryget */
1425 css_put(&mem->css);
1426 return ret;
1427 charge_cur_mm:
1428 if (unlikely(!mm))
1429 mm = &init_mm;
1430 return __mem_cgroup_try_charge(mm, mask, ptr, true);
1433 static void
1434 __mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr,
1435 enum charge_type ctype)
1437 struct page_cgroup *pc;
1439 if (mem_cgroup_disabled())
1440 return;
1441 if (!ptr)
1442 return;
1443 cgroup_exclude_rmdir(&ptr->css);
1444 pc = lookup_page_cgroup(page);
1445 mem_cgroup_lru_del_before_commit_swapcache(page);
1446 __mem_cgroup_commit_charge(ptr, pc, ctype);
1447 mem_cgroup_lru_add_after_commit_swapcache(page);
1449 * Now swap is on-memory. This means this page may be
1450 * counted both as mem and swap....double count.
1451 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1452 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1453 * may call delete_from_swap_cache() before reach here.
1455 if (do_swap_account && PageSwapCache(page)) {
1456 swp_entry_t ent = {.val = page_private(page)};
1457 unsigned short id;
1458 struct mem_cgroup *memcg;
1460 id = swap_cgroup_record(ent, 0);
1461 rcu_read_lock();
1462 memcg = mem_cgroup_lookup(id);
1463 if (memcg) {
1465 * This recorded memcg can be obsolete one. So, avoid
1466 * calling css_tryget
1468 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1469 mem_cgroup_put(memcg);
1471 rcu_read_unlock();
1474 * At swapin, we may charge account against cgroup which has no tasks.
1475 * So, rmdir()->pre_destroy() can be called while we do this charge.
1476 * In that case, we need to call pre_destroy() again. check it here.
1478 cgroup_release_and_wakeup_rmdir(&ptr->css);
1481 void mem_cgroup_commit_charge_swapin(struct page *page, struct mem_cgroup *ptr)
1483 __mem_cgroup_commit_charge_swapin(page, ptr,
1484 MEM_CGROUP_CHARGE_TYPE_MAPPED);
1487 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *mem)
1489 if (mem_cgroup_disabled())
1490 return;
1491 if (!mem)
1492 return;
1493 res_counter_uncharge(&mem->res, PAGE_SIZE);
1494 if (do_swap_account)
1495 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1496 css_put(&mem->css);
1501 * uncharge if !page_mapped(page)
1503 static struct mem_cgroup *
1504 __mem_cgroup_uncharge_common(struct page *page, enum charge_type ctype)
1506 struct page_cgroup *pc;
1507 struct mem_cgroup *mem = NULL;
1508 struct mem_cgroup_per_zone *mz;
1510 if (mem_cgroup_disabled())
1511 return NULL;
1513 if (PageSwapCache(page))
1514 return NULL;
1517 * Check if our page_cgroup is valid
1519 pc = lookup_page_cgroup(page);
1520 if (unlikely(!pc || !PageCgroupUsed(pc)))
1521 return NULL;
1523 lock_page_cgroup(pc);
1525 mem = pc->mem_cgroup;
1527 if (!PageCgroupUsed(pc))
1528 goto unlock_out;
1530 switch (ctype) {
1531 case MEM_CGROUP_CHARGE_TYPE_MAPPED:
1532 case MEM_CGROUP_CHARGE_TYPE_DROP:
1533 if (page_mapped(page))
1534 goto unlock_out;
1535 break;
1536 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT:
1537 if (!PageAnon(page)) { /* Shared memory */
1538 if (page->mapping && !page_is_file_cache(page))
1539 goto unlock_out;
1540 } else if (page_mapped(page)) /* Anon */
1541 goto unlock_out;
1542 break;
1543 default:
1544 break;
1547 res_counter_uncharge(&mem->res, PAGE_SIZE);
1548 if (do_swap_account && (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT))
1549 res_counter_uncharge(&mem->memsw, PAGE_SIZE);
1550 mem_cgroup_charge_statistics(mem, pc, false);
1552 ClearPageCgroupUsed(pc);
1554 * pc->mem_cgroup is not cleared here. It will be accessed when it's
1555 * freed from LRU. This is safe because uncharged page is expected not
1556 * to be reused (freed soon). Exception is SwapCache, it's handled by
1557 * special functions.
1560 mz = page_cgroup_zoneinfo(pc);
1561 unlock_page_cgroup(pc);
1563 /* at swapout, this memcg will be accessed to record to swap */
1564 if (ctype != MEM_CGROUP_CHARGE_TYPE_SWAPOUT)
1565 css_put(&mem->css);
1567 return mem;
1569 unlock_out:
1570 unlock_page_cgroup(pc);
1571 return NULL;
1574 void mem_cgroup_uncharge_page(struct page *page)
1576 /* early check. */
1577 if (page_mapped(page))
1578 return;
1579 if (page->mapping && !PageAnon(page))
1580 return;
1581 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_MAPPED);
1584 void mem_cgroup_uncharge_cache_page(struct page *page)
1586 VM_BUG_ON(page_mapped(page));
1587 VM_BUG_ON(page->mapping);
1588 __mem_cgroup_uncharge_common(page, MEM_CGROUP_CHARGE_TYPE_CACHE);
1591 #ifdef CONFIG_SWAP
1593 * called after __delete_from_swap_cache() and drop "page" account.
1594 * memcg information is recorded to swap_cgroup of "ent"
1596 void
1597 mem_cgroup_uncharge_swapcache(struct page *page, swp_entry_t ent, bool swapout)
1599 struct mem_cgroup *memcg;
1600 int ctype = MEM_CGROUP_CHARGE_TYPE_SWAPOUT;
1602 if (!swapout) /* this was a swap cache but the swap is unused ! */
1603 ctype = MEM_CGROUP_CHARGE_TYPE_DROP;
1605 memcg = __mem_cgroup_uncharge_common(page, ctype);
1607 /* record memcg information */
1608 if (do_swap_account && swapout && memcg) {
1609 swap_cgroup_record(ent, css_id(&memcg->css));
1610 mem_cgroup_get(memcg);
1612 if (swapout && memcg)
1613 css_put(&memcg->css);
1615 #endif
1617 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
1619 * called from swap_entry_free(). remove record in swap_cgroup and
1620 * uncharge "memsw" account.
1622 void mem_cgroup_uncharge_swap(swp_entry_t ent)
1624 struct mem_cgroup *memcg;
1625 unsigned short id;
1627 if (!do_swap_account)
1628 return;
1630 id = swap_cgroup_record(ent, 0);
1631 rcu_read_lock();
1632 memcg = mem_cgroup_lookup(id);
1633 if (memcg) {
1635 * We uncharge this because swap is freed.
1636 * This memcg can be obsolete one. We avoid calling css_tryget
1638 res_counter_uncharge(&memcg->memsw, PAGE_SIZE);
1639 mem_cgroup_put(memcg);
1641 rcu_read_unlock();
1643 #endif
1646 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1647 * page belongs to.
1649 int mem_cgroup_prepare_migration(struct page *page, struct mem_cgroup **ptr)
1651 struct page_cgroup *pc;
1652 struct mem_cgroup *mem = NULL;
1653 int ret = 0;
1655 if (mem_cgroup_disabled())
1656 return 0;
1658 pc = lookup_page_cgroup(page);
1659 lock_page_cgroup(pc);
1660 if (PageCgroupUsed(pc)) {
1661 mem = pc->mem_cgroup;
1662 css_get(&mem->css);
1664 unlock_page_cgroup(pc);
1666 if (mem) {
1667 ret = __mem_cgroup_try_charge(NULL, GFP_KERNEL, &mem, false);
1668 css_put(&mem->css);
1670 *ptr = mem;
1671 return ret;
1674 /* remove redundant charge if migration failed*/
1675 void mem_cgroup_end_migration(struct mem_cgroup *mem,
1676 struct page *oldpage, struct page *newpage)
1678 struct page *target, *unused;
1679 struct page_cgroup *pc;
1680 enum charge_type ctype;
1682 if (!mem)
1683 return;
1684 cgroup_exclude_rmdir(&mem->css);
1685 /* at migration success, oldpage->mapping is NULL. */
1686 if (oldpage->mapping) {
1687 target = oldpage;
1688 unused = NULL;
1689 } else {
1690 target = newpage;
1691 unused = oldpage;
1694 if (PageAnon(target))
1695 ctype = MEM_CGROUP_CHARGE_TYPE_MAPPED;
1696 else if (page_is_file_cache(target))
1697 ctype = MEM_CGROUP_CHARGE_TYPE_CACHE;
1698 else
1699 ctype = MEM_CGROUP_CHARGE_TYPE_SHMEM;
1701 /* unused page is not on radix-tree now. */
1702 if (unused)
1703 __mem_cgroup_uncharge_common(unused, ctype);
1705 pc = lookup_page_cgroup(target);
1707 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
1708 * So, double-counting is effectively avoided.
1710 __mem_cgroup_commit_charge(mem, pc, ctype);
1713 * Both of oldpage and newpage are still under lock_page().
1714 * Then, we don't have to care about race in radix-tree.
1715 * But we have to be careful that this page is unmapped or not.
1717 * There is a case for !page_mapped(). At the start of
1718 * migration, oldpage was mapped. But now, it's zapped.
1719 * But we know *target* page is not freed/reused under us.
1720 * mem_cgroup_uncharge_page() does all necessary checks.
1722 if (ctype == MEM_CGROUP_CHARGE_TYPE_MAPPED)
1723 mem_cgroup_uncharge_page(target);
1725 * At migration, we may charge account against cgroup which has no tasks
1726 * So, rmdir()->pre_destroy() can be called while we do this charge.
1727 * In that case, we need to call pre_destroy() again. check it here.
1729 cgroup_release_and_wakeup_rmdir(&mem->css);
1733 * A call to try to shrink memory usage on charge failure at shmem's swapin.
1734 * Calling hierarchical_reclaim is not enough because we should update
1735 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
1736 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
1737 * not from the memcg which this page would be charged to.
1738 * try_charge_swapin does all of these works properly.
1740 int mem_cgroup_shmem_charge_fallback(struct page *page,
1741 struct mm_struct *mm,
1742 gfp_t gfp_mask)
1744 struct mem_cgroup *mem = NULL;
1745 int ret;
1747 if (mem_cgroup_disabled())
1748 return 0;
1750 ret = mem_cgroup_try_charge_swapin(mm, page, gfp_mask, &mem);
1751 if (!ret)
1752 mem_cgroup_cancel_charge_swapin(mem); /* it does !mem check */
1754 return ret;
1757 static DEFINE_MUTEX(set_limit_mutex);
1759 static int mem_cgroup_resize_limit(struct mem_cgroup *memcg,
1760 unsigned long long val)
1762 int retry_count;
1763 int progress;
1764 u64 memswlimit;
1765 int ret = 0;
1766 int children = mem_cgroup_count_children(memcg);
1767 u64 curusage, oldusage;
1770 * For keeping hierarchical_reclaim simple, how long we should retry
1771 * is depends on callers. We set our retry-count to be function
1772 * of # of children which we should visit in this loop.
1774 retry_count = MEM_CGROUP_RECLAIM_RETRIES * children;
1776 oldusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1778 while (retry_count) {
1779 if (signal_pending(current)) {
1780 ret = -EINTR;
1781 break;
1784 * Rather than hide all in some function, I do this in
1785 * open coded manner. You see what this really does.
1786 * We have to guarantee mem->res.limit < mem->memsw.limit.
1788 mutex_lock(&set_limit_mutex);
1789 memswlimit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
1790 if (memswlimit < val) {
1791 ret = -EINVAL;
1792 mutex_unlock(&set_limit_mutex);
1793 break;
1795 ret = res_counter_set_limit(&memcg->res, val);
1796 if (!ret) {
1797 if (memswlimit == val)
1798 memcg->memsw_is_minimum = true;
1799 else
1800 memcg->memsw_is_minimum = false;
1802 mutex_unlock(&set_limit_mutex);
1804 if (!ret)
1805 break;
1807 progress = mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL,
1808 false, true);
1809 curusage = res_counter_read_u64(&memcg->res, RES_USAGE);
1810 /* Usage is reduced ? */
1811 if (curusage >= oldusage)
1812 retry_count--;
1813 else
1814 oldusage = curusage;
1817 return ret;
1820 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup *memcg,
1821 unsigned long long val)
1823 int retry_count;
1824 u64 memlimit, oldusage, curusage;
1825 int children = mem_cgroup_count_children(memcg);
1826 int ret = -EBUSY;
1828 /* see mem_cgroup_resize_res_limit */
1829 retry_count = children * MEM_CGROUP_RECLAIM_RETRIES;
1830 oldusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1831 while (retry_count) {
1832 if (signal_pending(current)) {
1833 ret = -EINTR;
1834 break;
1837 * Rather than hide all in some function, I do this in
1838 * open coded manner. You see what this really does.
1839 * We have to guarantee mem->res.limit < mem->memsw.limit.
1841 mutex_lock(&set_limit_mutex);
1842 memlimit = res_counter_read_u64(&memcg->res, RES_LIMIT);
1843 if (memlimit > val) {
1844 ret = -EINVAL;
1845 mutex_unlock(&set_limit_mutex);
1846 break;
1848 ret = res_counter_set_limit(&memcg->memsw, val);
1849 if (!ret) {
1850 if (memlimit == val)
1851 memcg->memsw_is_minimum = true;
1852 else
1853 memcg->memsw_is_minimum = false;
1855 mutex_unlock(&set_limit_mutex);
1857 if (!ret)
1858 break;
1860 mem_cgroup_hierarchical_reclaim(memcg, GFP_KERNEL, true, true);
1861 curusage = res_counter_read_u64(&memcg->memsw, RES_USAGE);
1862 /* Usage is reduced ? */
1863 if (curusage >= oldusage)
1864 retry_count--;
1865 else
1866 oldusage = curusage;
1868 return ret;
1872 * This routine traverse page_cgroup in given list and drop them all.
1873 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
1875 static int mem_cgroup_force_empty_list(struct mem_cgroup *mem,
1876 int node, int zid, enum lru_list lru)
1878 struct zone *zone;
1879 struct mem_cgroup_per_zone *mz;
1880 struct page_cgroup *pc, *busy;
1881 unsigned long flags, loop;
1882 struct list_head *list;
1883 int ret = 0;
1885 zone = &NODE_DATA(node)->node_zones[zid];
1886 mz = mem_cgroup_zoneinfo(mem, node, zid);
1887 list = &mz->lists[lru];
1889 loop = MEM_CGROUP_ZSTAT(mz, lru);
1890 /* give some margin against EBUSY etc...*/
1891 loop += 256;
1892 busy = NULL;
1893 while (loop--) {
1894 ret = 0;
1895 spin_lock_irqsave(&zone->lru_lock, flags);
1896 if (list_empty(list)) {
1897 spin_unlock_irqrestore(&zone->lru_lock, flags);
1898 break;
1900 pc = list_entry(list->prev, struct page_cgroup, lru);
1901 if (busy == pc) {
1902 list_move(&pc->lru, list);
1903 busy = 0;
1904 spin_unlock_irqrestore(&zone->lru_lock, flags);
1905 continue;
1907 spin_unlock_irqrestore(&zone->lru_lock, flags);
1909 ret = mem_cgroup_move_parent(pc, mem, GFP_KERNEL);
1910 if (ret == -ENOMEM)
1911 break;
1913 if (ret == -EBUSY || ret == -EINVAL) {
1914 /* found lock contention or "pc" is obsolete. */
1915 busy = pc;
1916 cond_resched();
1917 } else
1918 busy = NULL;
1921 if (!ret && !list_empty(list))
1922 return -EBUSY;
1923 return ret;
1927 * make mem_cgroup's charge to be 0 if there is no task.
1928 * This enables deleting this mem_cgroup.
1930 static int mem_cgroup_force_empty(struct mem_cgroup *mem, bool free_all)
1932 int ret;
1933 int node, zid, shrink;
1934 int nr_retries = MEM_CGROUP_RECLAIM_RETRIES;
1935 struct cgroup *cgrp = mem->css.cgroup;
1937 css_get(&mem->css);
1939 shrink = 0;
1940 /* should free all ? */
1941 if (free_all)
1942 goto try_to_free;
1943 move_account:
1944 while (mem->res.usage > 0) {
1945 ret = -EBUSY;
1946 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children))
1947 goto out;
1948 ret = -EINTR;
1949 if (signal_pending(current))
1950 goto out;
1951 /* This is for making all *used* pages to be on LRU. */
1952 lru_add_drain_all();
1953 ret = 0;
1954 for_each_node_state(node, N_HIGH_MEMORY) {
1955 for (zid = 0; !ret && zid < MAX_NR_ZONES; zid++) {
1956 enum lru_list l;
1957 for_each_lru(l) {
1958 ret = mem_cgroup_force_empty_list(mem,
1959 node, zid, l);
1960 if (ret)
1961 break;
1964 if (ret)
1965 break;
1967 /* it seems parent cgroup doesn't have enough mem */
1968 if (ret == -ENOMEM)
1969 goto try_to_free;
1970 cond_resched();
1972 ret = 0;
1973 out:
1974 css_put(&mem->css);
1975 return ret;
1977 try_to_free:
1978 /* returns EBUSY if there is a task or if we come here twice. */
1979 if (cgroup_task_count(cgrp) || !list_empty(&cgrp->children) || shrink) {
1980 ret = -EBUSY;
1981 goto out;
1983 /* we call try-to-free pages for make this cgroup empty */
1984 lru_add_drain_all();
1985 /* try to free all pages in this cgroup */
1986 shrink = 1;
1987 while (nr_retries && mem->res.usage > 0) {
1988 int progress;
1990 if (signal_pending(current)) {
1991 ret = -EINTR;
1992 goto out;
1994 progress = try_to_free_mem_cgroup_pages(mem, GFP_KERNEL,
1995 false, get_swappiness(mem));
1996 if (!progress) {
1997 nr_retries--;
1998 /* maybe some writeback is necessary */
1999 congestion_wait(BLK_RW_ASYNC, HZ/10);
2003 lru_add_drain();
2004 /* try move_account...there may be some *locked* pages. */
2005 if (mem->res.usage)
2006 goto move_account;
2007 ret = 0;
2008 goto out;
2011 int mem_cgroup_force_empty_write(struct cgroup *cont, unsigned int event)
2013 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont), true);
2017 static u64 mem_cgroup_hierarchy_read(struct cgroup *cont, struct cftype *cft)
2019 return mem_cgroup_from_cont(cont)->use_hierarchy;
2022 static int mem_cgroup_hierarchy_write(struct cgroup *cont, struct cftype *cft,
2023 u64 val)
2025 int retval = 0;
2026 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2027 struct cgroup *parent = cont->parent;
2028 struct mem_cgroup *parent_mem = NULL;
2030 if (parent)
2031 parent_mem = mem_cgroup_from_cont(parent);
2033 cgroup_lock();
2035 * If parent's use_hiearchy is set, we can't make any modifications
2036 * in the child subtrees. If it is unset, then the change can
2037 * occur, provided the current cgroup has no children.
2039 * For the root cgroup, parent_mem is NULL, we allow value to be
2040 * set if there are no children.
2042 if ((!parent_mem || !parent_mem->use_hierarchy) &&
2043 (val == 1 || val == 0)) {
2044 if (list_empty(&cont->children))
2045 mem->use_hierarchy = val;
2046 else
2047 retval = -EBUSY;
2048 } else
2049 retval = -EINVAL;
2050 cgroup_unlock();
2052 return retval;
2055 static u64 mem_cgroup_read(struct cgroup *cont, struct cftype *cft)
2057 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2058 u64 val = 0;
2059 int type, name;
2061 type = MEMFILE_TYPE(cft->private);
2062 name = MEMFILE_ATTR(cft->private);
2063 switch (type) {
2064 case _MEM:
2065 val = res_counter_read_u64(&mem->res, name);
2066 break;
2067 case _MEMSWAP:
2068 val = res_counter_read_u64(&mem->memsw, name);
2069 break;
2070 default:
2071 BUG();
2072 break;
2074 return val;
2077 * The user of this function is...
2078 * RES_LIMIT.
2080 static int mem_cgroup_write(struct cgroup *cont, struct cftype *cft,
2081 const char *buffer)
2083 struct mem_cgroup *memcg = mem_cgroup_from_cont(cont);
2084 int type, name;
2085 unsigned long long val;
2086 int ret;
2088 type = MEMFILE_TYPE(cft->private);
2089 name = MEMFILE_ATTR(cft->private);
2090 switch (name) {
2091 case RES_LIMIT:
2092 /* This function does all necessary parse...reuse it */
2093 ret = res_counter_memparse_write_strategy(buffer, &val);
2094 if (ret)
2095 break;
2096 if (type == _MEM)
2097 ret = mem_cgroup_resize_limit(memcg, val);
2098 else
2099 ret = mem_cgroup_resize_memsw_limit(memcg, val);
2100 break;
2101 default:
2102 ret = -EINVAL; /* should be BUG() ? */
2103 break;
2105 return ret;
2108 static void memcg_get_hierarchical_limit(struct mem_cgroup *memcg,
2109 unsigned long long *mem_limit, unsigned long long *memsw_limit)
2111 struct cgroup *cgroup;
2112 unsigned long long min_limit, min_memsw_limit, tmp;
2114 min_limit = res_counter_read_u64(&memcg->res, RES_LIMIT);
2115 min_memsw_limit = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2116 cgroup = memcg->css.cgroup;
2117 if (!memcg->use_hierarchy)
2118 goto out;
2120 while (cgroup->parent) {
2121 cgroup = cgroup->parent;
2122 memcg = mem_cgroup_from_cont(cgroup);
2123 if (!memcg->use_hierarchy)
2124 break;
2125 tmp = res_counter_read_u64(&memcg->res, RES_LIMIT);
2126 min_limit = min(min_limit, tmp);
2127 tmp = res_counter_read_u64(&memcg->memsw, RES_LIMIT);
2128 min_memsw_limit = min(min_memsw_limit, tmp);
2130 out:
2131 *mem_limit = min_limit;
2132 *memsw_limit = min_memsw_limit;
2133 return;
2136 static int mem_cgroup_reset(struct cgroup *cont, unsigned int event)
2138 struct mem_cgroup *mem;
2139 int type, name;
2141 mem = mem_cgroup_from_cont(cont);
2142 type = MEMFILE_TYPE(event);
2143 name = MEMFILE_ATTR(event);
2144 switch (name) {
2145 case RES_MAX_USAGE:
2146 if (type == _MEM)
2147 res_counter_reset_max(&mem->res);
2148 else
2149 res_counter_reset_max(&mem->memsw);
2150 break;
2151 case RES_FAILCNT:
2152 if (type == _MEM)
2153 res_counter_reset_failcnt(&mem->res);
2154 else
2155 res_counter_reset_failcnt(&mem->memsw);
2156 break;
2158 return 0;
2162 /* For read statistics */
2163 enum {
2164 MCS_CACHE,
2165 MCS_RSS,
2166 MCS_MAPPED_FILE,
2167 MCS_PGPGIN,
2168 MCS_PGPGOUT,
2169 MCS_INACTIVE_ANON,
2170 MCS_ACTIVE_ANON,
2171 MCS_INACTIVE_FILE,
2172 MCS_ACTIVE_FILE,
2173 MCS_UNEVICTABLE,
2174 NR_MCS_STAT,
2177 struct mcs_total_stat {
2178 s64 stat[NR_MCS_STAT];
2181 struct {
2182 char *local_name;
2183 char *total_name;
2184 } memcg_stat_strings[NR_MCS_STAT] = {
2185 {"cache", "total_cache"},
2186 {"rss", "total_rss"},
2187 {"mapped_file", "total_mapped_file"},
2188 {"pgpgin", "total_pgpgin"},
2189 {"pgpgout", "total_pgpgout"},
2190 {"inactive_anon", "total_inactive_anon"},
2191 {"active_anon", "total_active_anon"},
2192 {"inactive_file", "total_inactive_file"},
2193 {"active_file", "total_active_file"},
2194 {"unevictable", "total_unevictable"}
2198 static int mem_cgroup_get_local_stat(struct mem_cgroup *mem, void *data)
2200 struct mcs_total_stat *s = data;
2201 s64 val;
2203 /* per cpu stat */
2204 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_CACHE);
2205 s->stat[MCS_CACHE] += val * PAGE_SIZE;
2206 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_RSS);
2207 s->stat[MCS_RSS] += val * PAGE_SIZE;
2208 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_MAPPED_FILE);
2209 s->stat[MCS_MAPPED_FILE] += val * PAGE_SIZE;
2210 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGIN_COUNT);
2211 s->stat[MCS_PGPGIN] += val;
2212 val = mem_cgroup_read_stat(&mem->stat, MEM_CGROUP_STAT_PGPGOUT_COUNT);
2213 s->stat[MCS_PGPGOUT] += val;
2215 /* per zone stat */
2216 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_ANON);
2217 s->stat[MCS_INACTIVE_ANON] += val * PAGE_SIZE;
2218 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_ANON);
2219 s->stat[MCS_ACTIVE_ANON] += val * PAGE_SIZE;
2220 val = mem_cgroup_get_local_zonestat(mem, LRU_INACTIVE_FILE);
2221 s->stat[MCS_INACTIVE_FILE] += val * PAGE_SIZE;
2222 val = mem_cgroup_get_local_zonestat(mem, LRU_ACTIVE_FILE);
2223 s->stat[MCS_ACTIVE_FILE] += val * PAGE_SIZE;
2224 val = mem_cgroup_get_local_zonestat(mem, LRU_UNEVICTABLE);
2225 s->stat[MCS_UNEVICTABLE] += val * PAGE_SIZE;
2226 return 0;
2229 static void
2230 mem_cgroup_get_total_stat(struct mem_cgroup *mem, struct mcs_total_stat *s)
2232 mem_cgroup_walk_tree(mem, s, mem_cgroup_get_local_stat);
2235 static int mem_control_stat_show(struct cgroup *cont, struct cftype *cft,
2236 struct cgroup_map_cb *cb)
2238 struct mem_cgroup *mem_cont = mem_cgroup_from_cont(cont);
2239 struct mcs_total_stat mystat;
2240 int i;
2242 memset(&mystat, 0, sizeof(mystat));
2243 mem_cgroup_get_local_stat(mem_cont, &mystat);
2245 for (i = 0; i < NR_MCS_STAT; i++)
2246 cb->fill(cb, memcg_stat_strings[i].local_name, mystat.stat[i]);
2248 /* Hierarchical information */
2250 unsigned long long limit, memsw_limit;
2251 memcg_get_hierarchical_limit(mem_cont, &limit, &memsw_limit);
2252 cb->fill(cb, "hierarchical_memory_limit", limit);
2253 if (do_swap_account)
2254 cb->fill(cb, "hierarchical_memsw_limit", memsw_limit);
2257 memset(&mystat, 0, sizeof(mystat));
2258 mem_cgroup_get_total_stat(mem_cont, &mystat);
2259 for (i = 0; i < NR_MCS_STAT; i++)
2260 cb->fill(cb, memcg_stat_strings[i].total_name, mystat.stat[i]);
2263 #ifdef CONFIG_DEBUG_VM
2264 cb->fill(cb, "inactive_ratio", calc_inactive_ratio(mem_cont, NULL));
2267 int nid, zid;
2268 struct mem_cgroup_per_zone *mz;
2269 unsigned long recent_rotated[2] = {0, 0};
2270 unsigned long recent_scanned[2] = {0, 0};
2272 for_each_online_node(nid)
2273 for (zid = 0; zid < MAX_NR_ZONES; zid++) {
2274 mz = mem_cgroup_zoneinfo(mem_cont, nid, zid);
2276 recent_rotated[0] +=
2277 mz->reclaim_stat.recent_rotated[0];
2278 recent_rotated[1] +=
2279 mz->reclaim_stat.recent_rotated[1];
2280 recent_scanned[0] +=
2281 mz->reclaim_stat.recent_scanned[0];
2282 recent_scanned[1] +=
2283 mz->reclaim_stat.recent_scanned[1];
2285 cb->fill(cb, "recent_rotated_anon", recent_rotated[0]);
2286 cb->fill(cb, "recent_rotated_file", recent_rotated[1]);
2287 cb->fill(cb, "recent_scanned_anon", recent_scanned[0]);
2288 cb->fill(cb, "recent_scanned_file", recent_scanned[1]);
2290 #endif
2292 return 0;
2295 static u64 mem_cgroup_swappiness_read(struct cgroup *cgrp, struct cftype *cft)
2297 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2299 return get_swappiness(memcg);
2302 static int mem_cgroup_swappiness_write(struct cgroup *cgrp, struct cftype *cft,
2303 u64 val)
2305 struct mem_cgroup *memcg = mem_cgroup_from_cont(cgrp);
2306 struct mem_cgroup *parent;
2308 if (val > 100)
2309 return -EINVAL;
2311 if (cgrp->parent == NULL)
2312 return -EINVAL;
2314 parent = mem_cgroup_from_cont(cgrp->parent);
2316 cgroup_lock();
2318 /* If under hierarchy, only empty-root can set this value */
2319 if ((parent->use_hierarchy) ||
2320 (memcg->use_hierarchy && !list_empty(&cgrp->children))) {
2321 cgroup_unlock();
2322 return -EINVAL;
2325 spin_lock(&memcg->reclaim_param_lock);
2326 memcg->swappiness = val;
2327 spin_unlock(&memcg->reclaim_param_lock);
2329 cgroup_unlock();
2331 return 0;
2335 static struct cftype mem_cgroup_files[] = {
2337 .name = "usage_in_bytes",
2338 .private = MEMFILE_PRIVATE(_MEM, RES_USAGE),
2339 .read_u64 = mem_cgroup_read,
2342 .name = "max_usage_in_bytes",
2343 .private = MEMFILE_PRIVATE(_MEM, RES_MAX_USAGE),
2344 .trigger = mem_cgroup_reset,
2345 .read_u64 = mem_cgroup_read,
2348 .name = "limit_in_bytes",
2349 .private = MEMFILE_PRIVATE(_MEM, RES_LIMIT),
2350 .write_string = mem_cgroup_write,
2351 .read_u64 = mem_cgroup_read,
2354 .name = "failcnt",
2355 .private = MEMFILE_PRIVATE(_MEM, RES_FAILCNT),
2356 .trigger = mem_cgroup_reset,
2357 .read_u64 = mem_cgroup_read,
2360 .name = "stat",
2361 .read_map = mem_control_stat_show,
2364 .name = "force_empty",
2365 .trigger = mem_cgroup_force_empty_write,
2368 .name = "use_hierarchy",
2369 .write_u64 = mem_cgroup_hierarchy_write,
2370 .read_u64 = mem_cgroup_hierarchy_read,
2373 .name = "swappiness",
2374 .read_u64 = mem_cgroup_swappiness_read,
2375 .write_u64 = mem_cgroup_swappiness_write,
2379 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2380 static struct cftype memsw_cgroup_files[] = {
2382 .name = "memsw.usage_in_bytes",
2383 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_USAGE),
2384 .read_u64 = mem_cgroup_read,
2387 .name = "memsw.max_usage_in_bytes",
2388 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_MAX_USAGE),
2389 .trigger = mem_cgroup_reset,
2390 .read_u64 = mem_cgroup_read,
2393 .name = "memsw.limit_in_bytes",
2394 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_LIMIT),
2395 .write_string = mem_cgroup_write,
2396 .read_u64 = mem_cgroup_read,
2399 .name = "memsw.failcnt",
2400 .private = MEMFILE_PRIVATE(_MEMSWAP, RES_FAILCNT),
2401 .trigger = mem_cgroup_reset,
2402 .read_u64 = mem_cgroup_read,
2406 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2408 if (!do_swap_account)
2409 return 0;
2410 return cgroup_add_files(cont, ss, memsw_cgroup_files,
2411 ARRAY_SIZE(memsw_cgroup_files));
2413 #else
2414 static int register_memsw_files(struct cgroup *cont, struct cgroup_subsys *ss)
2416 return 0;
2418 #endif
2420 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2422 struct mem_cgroup_per_node *pn;
2423 struct mem_cgroup_per_zone *mz;
2424 enum lru_list l;
2425 int zone, tmp = node;
2427 * This routine is called against possible nodes.
2428 * But it's BUG to call kmalloc() against offline node.
2430 * TODO: this routine can waste much memory for nodes which will
2431 * never be onlined. It's better to use memory hotplug callback
2432 * function.
2434 if (!node_state(node, N_NORMAL_MEMORY))
2435 tmp = -1;
2436 pn = kmalloc_node(sizeof(*pn), GFP_KERNEL, tmp);
2437 if (!pn)
2438 return 1;
2440 mem->info.nodeinfo[node] = pn;
2441 memset(pn, 0, sizeof(*pn));
2443 for (zone = 0; zone < MAX_NR_ZONES; zone++) {
2444 mz = &pn->zoneinfo[zone];
2445 for_each_lru(l)
2446 INIT_LIST_HEAD(&mz->lists[l]);
2448 return 0;
2451 static void free_mem_cgroup_per_zone_info(struct mem_cgroup *mem, int node)
2453 kfree(mem->info.nodeinfo[node]);
2456 static int mem_cgroup_size(void)
2458 int cpustat_size = nr_cpu_ids * sizeof(struct mem_cgroup_stat_cpu);
2459 return sizeof(struct mem_cgroup) + cpustat_size;
2462 static struct mem_cgroup *mem_cgroup_alloc(void)
2464 struct mem_cgroup *mem;
2465 int size = mem_cgroup_size();
2467 if (size < PAGE_SIZE)
2468 mem = kmalloc(size, GFP_KERNEL);
2469 else
2470 mem = vmalloc(size);
2472 if (mem)
2473 memset(mem, 0, size);
2474 return mem;
2478 * At destroying mem_cgroup, references from swap_cgroup can remain.
2479 * (scanning all at force_empty is too costly...)
2481 * Instead of clearing all references at force_empty, we remember
2482 * the number of reference from swap_cgroup and free mem_cgroup when
2483 * it goes down to 0.
2485 * Removal of cgroup itself succeeds regardless of refs from swap.
2488 static void __mem_cgroup_free(struct mem_cgroup *mem)
2490 int node;
2492 free_css_id(&mem_cgroup_subsys, &mem->css);
2494 for_each_node_state(node, N_POSSIBLE)
2495 free_mem_cgroup_per_zone_info(mem, node);
2497 if (mem_cgroup_size() < PAGE_SIZE)
2498 kfree(mem);
2499 else
2500 vfree(mem);
2503 static void mem_cgroup_get(struct mem_cgroup *mem)
2505 atomic_inc(&mem->refcnt);
2508 static void mem_cgroup_put(struct mem_cgroup *mem)
2510 if (atomic_dec_and_test(&mem->refcnt)) {
2511 struct mem_cgroup *parent = parent_mem_cgroup(mem);
2512 __mem_cgroup_free(mem);
2513 if (parent)
2514 mem_cgroup_put(parent);
2519 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
2521 static struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *mem)
2523 if (!mem->res.parent)
2524 return NULL;
2525 return mem_cgroup_from_res_counter(mem->res.parent, res);
2528 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2529 static void __init enable_swap_cgroup(void)
2531 if (!mem_cgroup_disabled() && really_do_swap_account)
2532 do_swap_account = 1;
2534 #else
2535 static void __init enable_swap_cgroup(void)
2538 #endif
2540 static struct cgroup_subsys_state * __ref
2541 mem_cgroup_create(struct cgroup_subsys *ss, struct cgroup *cont)
2543 struct mem_cgroup *mem, *parent;
2544 long error = -ENOMEM;
2545 int node;
2547 mem = mem_cgroup_alloc();
2548 if (!mem)
2549 return ERR_PTR(error);
2551 for_each_node_state(node, N_POSSIBLE)
2552 if (alloc_mem_cgroup_per_zone_info(mem, node))
2553 goto free_out;
2554 /* root ? */
2555 if (cont->parent == NULL) {
2556 enable_swap_cgroup();
2557 parent = NULL;
2558 } else {
2559 parent = mem_cgroup_from_cont(cont->parent);
2560 mem->use_hierarchy = parent->use_hierarchy;
2563 if (parent && parent->use_hierarchy) {
2564 res_counter_init(&mem->res, &parent->res);
2565 res_counter_init(&mem->memsw, &parent->memsw);
2567 * We increment refcnt of the parent to ensure that we can
2568 * safely access it on res_counter_charge/uncharge.
2569 * This refcnt will be decremented when freeing this
2570 * mem_cgroup(see mem_cgroup_put).
2572 mem_cgroup_get(parent);
2573 } else {
2574 res_counter_init(&mem->res, NULL);
2575 res_counter_init(&mem->memsw, NULL);
2577 mem->last_scanned_child = 0;
2578 spin_lock_init(&mem->reclaim_param_lock);
2580 if (parent)
2581 mem->swappiness = get_swappiness(parent);
2582 atomic_set(&mem->refcnt, 1);
2583 return &mem->css;
2584 free_out:
2585 __mem_cgroup_free(mem);
2586 return ERR_PTR(error);
2589 static int mem_cgroup_pre_destroy(struct cgroup_subsys *ss,
2590 struct cgroup *cont)
2592 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2594 return mem_cgroup_force_empty(mem, false);
2597 static void mem_cgroup_destroy(struct cgroup_subsys *ss,
2598 struct cgroup *cont)
2600 struct mem_cgroup *mem = mem_cgroup_from_cont(cont);
2602 mem_cgroup_put(mem);
2605 static int mem_cgroup_populate(struct cgroup_subsys *ss,
2606 struct cgroup *cont)
2608 int ret;
2610 ret = cgroup_add_files(cont, ss, mem_cgroup_files,
2611 ARRAY_SIZE(mem_cgroup_files));
2613 if (!ret)
2614 ret = register_memsw_files(cont, ss);
2615 return ret;
2618 static void mem_cgroup_move_task(struct cgroup_subsys *ss,
2619 struct cgroup *cont,
2620 struct cgroup *old_cont,
2621 struct task_struct *p)
2623 mutex_lock(&memcg_tasklist);
2625 * FIXME: It's better to move charges of this process from old
2626 * memcg to new memcg. But it's just on TODO-List now.
2628 mutex_unlock(&memcg_tasklist);
2631 struct cgroup_subsys mem_cgroup_subsys = {
2632 .name = "memory",
2633 .subsys_id = mem_cgroup_subsys_id,
2634 .create = mem_cgroup_create,
2635 .pre_destroy = mem_cgroup_pre_destroy,
2636 .destroy = mem_cgroup_destroy,
2637 .populate = mem_cgroup_populate,
2638 .attach = mem_cgroup_move_task,
2639 .early_init = 0,
2640 .use_id = 1,
2643 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2645 static int __init disable_swap_account(char *s)
2647 really_do_swap_account = 0;
2648 return 1;
2650 __setup("noswapaccount", disable_swap_account);
2651 #endif