1 /* memcontrol.c - Memory Controller
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
20 #include <linux/res_counter.h>
21 #include <linux/memcontrol.h>
22 #include <linux/cgroup.h>
24 #include <linux/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>
36 #include <linux/seq_file.h>
37 #include <linux/vmalloc.h>
38 #include <linux/mm_inline.h>
39 #include <linux/page_cgroup.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*/
52 #define do_swap_account (0)
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
)
95 for_each_possible_cpu(cpu
)
96 ret
+= stat
->cpustat
[cpu
].count
[idx
];
100 static s64
mem_cgroup_local_usage(struct mem_cgroup_stat
*stat
)
104 ret
= mem_cgroup_read_stat(stat
, MEM_CGROUP_STAT_CACHE
);
105 ret
+= mem_cgroup_read_stat(stat
, MEM_CGROUP_STAT_RSS
);
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.
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
170 int last_scanned_child
;
172 * Should the accounting and control be hierarchical, per subtree?
175 unsigned long last_oom_jiffies
;
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
;
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 */
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 */
211 /* for encoding cft->private value on file */
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
,
226 int val
= (charge
)? 1 : -1;
227 struct mem_cgroup_stat
*stat
= &mem
->stat
;
228 struct mem_cgroup_stat_cpu
*cpustat
;
231 cpustat
= &stat
->cpustat
[cpu
];
232 if (PageCgroupCache(pc
))
233 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_CACHE
, val
);
235 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_RSS
, val
);
238 __mem_cgroup_stat_add_safe(cpustat
,
239 MEM_CGROUP_STAT_PGPGIN_COUNT
, 1);
241 __mem_cgroup_stat_add_safe(cpustat
,
242 MEM_CGROUP_STAT_PGPGOUT_COUNT
, 1);
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
);
262 return mem_cgroup_zoneinfo(mem
, nid
, zid
);
265 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup
*mem
,
269 struct mem_cgroup_per_zone
*mz
;
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
);
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
,
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.
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
;
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).
314 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
317 } while (!css_tryget(&mem
->css
));
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
);
341 css
= css_get_next(&mem_cgroup_subsys
, nextid
, &root
->css
,
343 if (css
&& css_tryget(css
))
344 mem
= container_of(css
, struct mem_cgroup
, css
);
348 ret
= (*func
)(mem
, data
);
352 } while (!ret
&& css
);
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
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())
379 pc
= lookup_page_cgroup(page
);
380 /* can happen while we handle swapcache. */
381 if (list_empty(&pc
->lru
) || !pc
->mem_cgroup
)
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
);
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())
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.
413 /* unused page is not rotated. */
414 if (!PageCgroupUsed(pc
))
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())
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.
433 if (!PageCgroupUsed(pc
))
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
)
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
)
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())
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
)
490 struct mem_cgroup
*curr
= NULL
;
494 curr
= try_get_mem_cgroup_from_mm(task
->mm
);
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
);
514 * prev_priority control...this will be used in memory reclaim path.
516 int mem_cgroup_get_reclaim_priority(struct mem_cgroup
*mem
)
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
;
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
);
554 inactive_ratio
= int_sqrt(10 * gb
);
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
)
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
,
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
,
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())
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.
631 if (!PageCgroupUsed(pc
))
634 mz
= page_cgroup_zoneinfo(pc
);
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;
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
;
661 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
662 src
= &mz
->lists
[lru
];
665 list_for_each_entry_safe_reverse(pc
, tmp
, src
, lru
) {
666 if (scan
>= nr_to_scan
)
670 if (unlikely(!PageCgroupUsed(pc
)))
672 if (unlikely(!PageLRU(page
)))
676 ret
= __isolate_lru_page(page
, mode
, file
);
679 list_move(&page
->lru
, dst
);
680 mem_cgroup_del_lru(page
);
684 /* we don't affect global LRU but rotate in our LRU */
685 mem_cgroup_rotate_lru_list(page
, page_lru(page
));
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
))
706 if (res_counter_check_under_limit(&mem
->res
))
711 static unsigned int get_swappiness(struct mem_cgroup
*memcg
)
713 struct cgroup
*cgrp
= memcg
->css
.cgroup
;
714 unsigned int swappiness
;
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
);
727 static int mem_cgroup_count_children_cb(struct mem_cgroup
*mem
, void *data
)
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
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
];
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
);
766 * Unfortunately, we are unable to convert to a useful name
767 * But we'll still print out the usage information
774 printk(KERN_INFO
"Task in %s killed", memcg_name
);
777 ret
= cgroup_path(mem_cgrp
, memcg_name
, PATH_MAX
);
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
);
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, "
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
)
808 mem_cgroup_walk_tree(mem
, &num
, mem_cgroup_count_children_cb
);
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
;
824 if (!root_mem
->use_hierarchy
) {
825 css_get(&root_mem
->css
);
831 nextid
= root_mem
->last_scanned_child
+ 1;
832 css
= css_get_next(&mem_cgroup_subsys
, nextid
, &root_mem
->css
,
834 if (css
&& css_tryget(css
))
835 ret
= container_of(css
, struct mem_cgroup
, css
);
838 /* Updates scanning parameter */
839 spin_lock(&root_mem
->reclaim_param_lock
);
841 /* this means start scan from ID:1 */
842 root_mem
->last_scanned_child
= 0;
844 root_mem
->last_scanned_child
= found
;
845 spin_unlock(&root_mem
->reclaim_param_lock
);
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
;
870 /* If memsw_is_minimum==1, swap-out is of-no-use. */
871 if (root_mem
->memsw_is_minimum
)
875 victim
= mem_cgroup_select_victim(root_mem
);
876 if (victim
== root_mem
)
878 if (!mem_cgroup_local_usage(&victim
->stat
)) {
879 /* this cgroup's local usage == 0 */
880 css_put(&victim
->css
);
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.
895 if (mem_cgroup_check_under_limit(root_mem
))
901 bool mem_cgroup_oom_called(struct task_struct
*task
)
904 struct mem_cgroup
*mem
;
905 struct mm_struct
*mm
;
911 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
912 if (mem
&& time_before(jiffies
, mem
->last_oom_jiffies
+ HZ
/10))
918 static int record_last_oom_cb(struct mem_cgroup
*mem
, void *data
)
920 mem
->last_oom_jiffies
= jiffies
;
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
;
939 struct page_cgroup
*pc
;
941 if (!page_is_file_cache(page
))
944 pc
= lookup_page_cgroup(page
);
948 lock_page_cgroup(pc
);
949 mem
= pc
->mem_cgroup
;
953 if (!PageCgroupUsed(pc
))
957 * Preemption is already disabled, we don't need get_cpu()
959 cpu
= smp_processor_id();
961 cpustat
= &stat
->cpustat
[cpu
];
963 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_MAPPED_FILE
, val
);
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
,
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! */
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).
994 mem
= try_get_mem_cgroup_from_mm(mm
);
1002 VM_BUG_ON(css_is_removed(&mem
->css
));
1006 bool noswap
= false;
1008 ret
= res_counter_charge(&mem
->res
, PAGE_SIZE
, &fail_res
);
1010 if (!do_swap_account
)
1012 ret
= res_counter_charge(&mem
->memsw
, PAGE_SIZE
,
1016 /* mem+swap counter fails */
1017 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
1019 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
,
1022 /* mem counter fails */
1023 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
,
1026 if (!(gfp_mask
& __GFP_WAIT
))
1029 ret
= mem_cgroup_hierarchical_reclaim(mem_over_limit
, gfp_mask
,
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
))
1045 if (!nr_retries
--) {
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
);
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
1068 static struct mem_cgroup
*mem_cgroup_lookup(unsigned short id
)
1070 struct cgroup_subsys_state
*css
;
1072 /* ID 0 is unused ID */
1075 css
= css_lookup(&mem_cgroup_subsys
, id
);
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
;
1088 VM_BUG_ON(!PageLocked(page
));
1090 if (!PageSwapCache(page
))
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
))
1100 ent
.val
= page_private(page
);
1101 id
= lookup_swap_cgroup(ent
);
1103 mem
= mem_cgroup_lookup(id
);
1104 if (mem
&& !css_tryget(&mem
->css
))
1108 unlock_page_cgroup(pc
);
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. */
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
);
1134 pc
->mem_cgroup
= mem
;
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
;
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
))
1181 if (!PageCgroupUsed(pc
))
1184 if (pc
->mem_cgroup
!= from
)
1187 res_counter_uncharge(&from
->res
, PAGE_SIZE
);
1188 mem_cgroup_charge_statistics(from
, pc
, false);
1191 if (page_is_file_cache(page
) && page_mapped(page
)) {
1192 cpu
= smp_processor_id();
1193 /* Update mapped_file data for mem_cgroup "from" */
1195 cpustat
= &stat
->cpustat
[cpu
];
1196 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_MAPPED_FILE
,
1199 /* Update mapped_file data for mem_cgroup "to" */
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
);
1211 pc
->mem_cgroup
= to
;
1212 mem_cgroup_charge_statistics(to
, pc
, true);
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.
1226 * move charges to its parent.
1229 static int mem_cgroup_move_parent(struct page_cgroup
*pc
,
1230 struct mem_cgroup
*child
,
1233 struct page
*page
= pc
->page
;
1234 struct cgroup
*cg
= child
->css
.cgroup
;
1235 struct cgroup
*pcg
= cg
->parent
;
1236 struct mem_cgroup
*parent
;
1244 parent
= mem_cgroup_from_cont(pcg
);
1247 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, &parent
, false);
1251 if (!get_page_unless_zero(page
)) {
1256 ret
= isolate_lru_page(page
);
1261 ret
= mem_cgroup_move_account(pc
, child
, parent
);
1263 putback_lru_page(page
);
1266 /* drop extra refcnt by try_charge() */
1267 css_put(&parent
->css
);
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
);
1284 * Charge the memory controller for page usage.
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
;
1297 pc
= lookup_page_cgroup(page
);
1298 /* can happen at boot */
1304 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, &mem
, true);
1308 __mem_cgroup_commit_charge(mem
, pc
, ctype
);
1312 int mem_cgroup_newpage_charge(struct page
*page
,
1313 struct mm_struct
*mm
, gfp_t gfp_mask
)
1315 if (mem_cgroup_disabled())
1317 if (PageCompound(page
))
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
1326 if (page_mapped(page
) || (page
->mapping
&& !PageAnon(page
)))
1330 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
1331 MEM_CGROUP_CHARGE_TYPE_MAPPED
, NULL
);
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
,
1341 struct mem_cgroup
*mem
= NULL
;
1344 if (mem_cgroup_disabled())
1346 if (PageCompound(page
))
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
);
1366 lock_page_cgroup(pc
);
1367 if (PageCgroupUsed(pc
)) {
1368 unlock_page_cgroup(pc
);
1371 unlock_page_cgroup(pc
);
1374 if (unlikely(!mm
&& !mem
))
1377 if (page_is_file_cache(page
))
1378 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
1379 MEM_CGROUP_CHARGE_TYPE_CACHE
, NULL
);
1382 if (PageSwapCache(page
)) {
1383 ret
= mem_cgroup_try_charge_swapin(mm
, page
, gfp_mask
, &mem
);
1385 __mem_cgroup_commit_charge_swapin(page
, mem
,
1386 MEM_CGROUP_CHARGE_TYPE_SHMEM
);
1388 ret
= mem_cgroup_charge_common(page
, mm
, gfp_mask
,
1389 MEM_CGROUP_CHARGE_TYPE_SHMEM
, mem
);
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
,
1402 gfp_t mask
, struct mem_cgroup
**ptr
)
1404 struct mem_cgroup
*mem
;
1407 if (mem_cgroup_disabled())
1410 if (!do_swap_account
)
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
))
1419 mem
= try_get_mem_cgroup_from_swapcache(page
);
1423 ret
= __mem_cgroup_try_charge(NULL
, mask
, ptr
, true);
1424 /* drop extra refcnt from tryget */
1430 return __mem_cgroup_try_charge(mm
, mask
, ptr
, true);
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())
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
)};
1458 struct mem_cgroup
*memcg
;
1460 id
= swap_cgroup_record(ent
, 0);
1462 memcg
= mem_cgroup_lookup(id
);
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
);
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())
1493 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
1494 if (do_swap_account
)
1495 res_counter_uncharge(&mem
->memsw
, PAGE_SIZE
);
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())
1513 if (PageSwapCache(page
))
1517 * Check if our page_cgroup is valid
1519 pc
= lookup_page_cgroup(page
);
1520 if (unlikely(!pc
|| !PageCgroupUsed(pc
)))
1523 lock_page_cgroup(pc
);
1525 mem
= pc
->mem_cgroup
;
1527 if (!PageCgroupUsed(pc
))
1531 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
1532 case MEM_CGROUP_CHARGE_TYPE_DROP
:
1533 if (page_mapped(page
))
1536 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT
:
1537 if (!PageAnon(page
)) { /* Shared memory */
1538 if (page
->mapping
&& !page_is_file_cache(page
))
1540 } else if (page_mapped(page
)) /* Anon */
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
)
1570 unlock_page_cgroup(pc
);
1574 void mem_cgroup_uncharge_page(struct page
*page
)
1577 if (page_mapped(page
))
1579 if (page
->mapping
&& !PageAnon(page
))
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
);
1593 * called after __delete_from_swap_cache() and drop "page" account.
1594 * memcg information is recorded to swap_cgroup of "ent"
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
);
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
;
1627 if (!do_swap_account
)
1630 id
= swap_cgroup_record(ent
, 0);
1632 memcg
= mem_cgroup_lookup(id
);
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
);
1646 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
1649 int mem_cgroup_prepare_migration(struct page
*page
, struct mem_cgroup
**ptr
)
1651 struct page_cgroup
*pc
;
1652 struct mem_cgroup
*mem
= NULL
;
1655 if (mem_cgroup_disabled())
1658 pc
= lookup_page_cgroup(page
);
1659 lock_page_cgroup(pc
);
1660 if (PageCgroupUsed(pc
)) {
1661 mem
= pc
->mem_cgroup
;
1664 unlock_page_cgroup(pc
);
1667 ret
= __mem_cgroup_try_charge(NULL
, GFP_KERNEL
, &mem
, false);
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
;
1684 cgroup_exclude_rmdir(&mem
->css
);
1685 /* at migration success, oldpage->mapping is NULL. */
1686 if (oldpage
->mapping
) {
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
;
1699 ctype
= MEM_CGROUP_CHARGE_TYPE_SHMEM
;
1701 /* unused page is not on radix-tree now. */
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
,
1744 struct mem_cgroup
*mem
= NULL
;
1747 if (mem_cgroup_disabled())
1750 ret
= mem_cgroup_try_charge_swapin(mm
, page
, gfp_mask
, &mem
);
1752 mem_cgroup_cancel_charge_swapin(mem
); /* it does !mem check */
1757 static DEFINE_MUTEX(set_limit_mutex
);
1759 static int mem_cgroup_resize_limit(struct mem_cgroup
*memcg
,
1760 unsigned long long val
)
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
)) {
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
) {
1792 mutex_unlock(&set_limit_mutex
);
1795 ret
= res_counter_set_limit(&memcg
->res
, val
);
1797 if (memswlimit
== val
)
1798 memcg
->memsw_is_minimum
= true;
1800 memcg
->memsw_is_minimum
= false;
1802 mutex_unlock(&set_limit_mutex
);
1807 progress
= mem_cgroup_hierarchical_reclaim(memcg
, GFP_KERNEL
,
1809 curusage
= res_counter_read_u64(&memcg
->res
, RES_USAGE
);
1810 /* Usage is reduced ? */
1811 if (curusage
>= oldusage
)
1814 oldusage
= curusage
;
1820 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup
*memcg
,
1821 unsigned long long val
)
1824 u64 memlimit
, oldusage
, curusage
;
1825 int children
= mem_cgroup_count_children(memcg
);
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
)) {
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
) {
1845 mutex_unlock(&set_limit_mutex
);
1848 ret
= res_counter_set_limit(&memcg
->memsw
, val
);
1850 if (memlimit
== val
)
1851 memcg
->memsw_is_minimum
= true;
1853 memcg
->memsw_is_minimum
= false;
1855 mutex_unlock(&set_limit_mutex
);
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
)
1866 oldusage
= curusage
;
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
)
1879 struct mem_cgroup_per_zone
*mz
;
1880 struct page_cgroup
*pc
, *busy
;
1881 unsigned long flags
, loop
;
1882 struct list_head
*list
;
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...*/
1895 spin_lock_irqsave(&zone
->lru_lock
, flags
);
1896 if (list_empty(list
)) {
1897 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
1900 pc
= list_entry(list
->prev
, struct page_cgroup
, lru
);
1902 list_move(&pc
->lru
, list
);
1904 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
1907 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
1909 ret
= mem_cgroup_move_parent(pc
, mem
, GFP_KERNEL
);
1913 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
1914 /* found lock contention or "pc" is obsolete. */
1921 if (!ret
&& !list_empty(list
))
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
)
1933 int node
, zid
, shrink
;
1934 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
1935 struct cgroup
*cgrp
= mem
->css
.cgroup
;
1940 /* should free all ? */
1944 while (mem
->res
.usage
> 0) {
1946 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
))
1949 if (signal_pending(current
))
1951 /* This is for making all *used* pages to be on LRU. */
1952 lru_add_drain_all();
1954 for_each_node_state(node
, N_HIGH_MEMORY
) {
1955 for (zid
= 0; !ret
&& zid
< MAX_NR_ZONES
; zid
++) {
1958 ret
= mem_cgroup_force_empty_list(mem
,
1967 /* it seems parent cgroup doesn't have enough mem */
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
) {
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 */
1987 while (nr_retries
&& mem
->res
.usage
> 0) {
1990 if (signal_pending(current
)) {
1994 progress
= try_to_free_mem_cgroup_pages(mem
, GFP_KERNEL
,
1995 false, get_swappiness(mem
));
1998 /* maybe some writeback is necessary */
1999 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
2004 /* try move_account...there may be some *locked* pages. */
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
,
2026 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
2027 struct cgroup
*parent
= cont
->parent
;
2028 struct mem_cgroup
*parent_mem
= NULL
;
2031 parent_mem
= mem_cgroup_from_cont(parent
);
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
;
2055 static u64
mem_cgroup_read(struct cgroup
*cont
, struct cftype
*cft
)
2057 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
2061 type
= MEMFILE_TYPE(cft
->private);
2062 name
= MEMFILE_ATTR(cft
->private);
2065 val
= res_counter_read_u64(&mem
->res
, name
);
2068 val
= res_counter_read_u64(&mem
->memsw
, name
);
2077 * The user of this function is...
2080 static int mem_cgroup_write(struct cgroup
*cont
, struct cftype
*cft
,
2083 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cont
);
2085 unsigned long long val
;
2088 type
= MEMFILE_TYPE(cft
->private);
2089 name
= MEMFILE_ATTR(cft
->private);
2092 /* This function does all necessary parse...reuse it */
2093 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
2097 ret
= mem_cgroup_resize_limit(memcg
, val
);
2099 ret
= mem_cgroup_resize_memsw_limit(memcg
, val
);
2102 ret
= -EINVAL
; /* should be BUG() ? */
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
)
2120 while (cgroup
->parent
) {
2121 cgroup
= cgroup
->parent
;
2122 memcg
= mem_cgroup_from_cont(cgroup
);
2123 if (!memcg
->use_hierarchy
)
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
);
2131 *mem_limit
= min_limit
;
2132 *memsw_limit
= min_memsw_limit
;
2136 static int mem_cgroup_reset(struct cgroup
*cont
, unsigned int event
)
2138 struct mem_cgroup
*mem
;
2141 mem
= mem_cgroup_from_cont(cont
);
2142 type
= MEMFILE_TYPE(event
);
2143 name
= MEMFILE_ATTR(event
);
2147 res_counter_reset_max(&mem
->res
);
2149 res_counter_reset_max(&mem
->memsw
);
2153 res_counter_reset_failcnt(&mem
->res
);
2155 res_counter_reset_failcnt(&mem
->memsw
);
2162 /* For read statistics */
2177 struct mcs_total_stat
{
2178 s64 stat
[NR_MCS_STAT
];
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
;
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
;
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
;
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
;
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
));
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]);
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
,
2305 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
2306 struct mem_cgroup
*parent
;
2311 if (cgrp
->parent
== NULL
)
2314 parent
= mem_cgroup_from_cont(cgrp
->parent
);
2318 /* If under hierarchy, only empty-root can set this value */
2319 if ((parent
->use_hierarchy
) ||
2320 (memcg
->use_hierarchy
&& !list_empty(&cgrp
->children
))) {
2325 spin_lock(&memcg
->reclaim_param_lock
);
2326 memcg
->swappiness
= val
;
2327 spin_unlock(&memcg
->reclaim_param_lock
);
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
,
2355 .private = MEMFILE_PRIVATE(_MEM
, RES_FAILCNT
),
2356 .trigger
= mem_cgroup_reset
,
2357 .read_u64
= mem_cgroup_read
,
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
)
2410 return cgroup_add_files(cont
, ss
, memsw_cgroup_files
,
2411 ARRAY_SIZE(memsw_cgroup_files
));
2414 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
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
;
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
2434 if (!node_state(node
, N_NORMAL_MEMORY
))
2436 pn
= kmalloc_node(sizeof(*pn
), GFP_KERNEL
, tmp
);
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
];
2446 INIT_LIST_HEAD(&mz
->lists
[l
]);
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
);
2470 mem
= vmalloc(size
);
2473 memset(mem
, 0, size
);
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
)
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
)
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
);
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
)
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;
2535 static void __init
enable_swap_cgroup(void)
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
;
2547 mem
= mem_cgroup_alloc();
2549 return ERR_PTR(error
);
2551 for_each_node_state(node
, N_POSSIBLE
)
2552 if (alloc_mem_cgroup_per_zone_info(mem
, node
))
2555 if (cont
->parent
== NULL
) {
2556 enable_swap_cgroup();
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
);
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
);
2581 mem
->swappiness
= get_swappiness(parent
);
2582 atomic_set(&mem
->refcnt
, 1);
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
)
2610 ret
= cgroup_add_files(cont
, ss
, mem_cgroup_files
,
2611 ARRAY_SIZE(mem_cgroup_files
));
2614 ret
= register_memsw_files(cont
, ss
);
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
= {
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
,
2643 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2645 static int __init
disable_swap_account(char *s
)
2647 really_do_swap_account
= 0;
2650 __setup("noswapaccount", disable_swap_account
);