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/rbtree.h>
33 #include <linux/slab.h>
34 #include <linux/swap.h>
35 #include <linux/spinlock.h>
37 #include <linux/seq_file.h>
38 #include <linux/vmalloc.h>
39 #include <linux/mm_inline.h>
40 #include <linux/page_cgroup.h>
41 #include <linux/cpu.h>
44 #include <asm/uaccess.h>
46 struct cgroup_subsys mem_cgroup_subsys __read_mostly
;
47 #define MEM_CGROUP_RECLAIM_RETRIES 5
48 struct mem_cgroup
*root_mem_cgroup __read_mostly
;
50 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
51 /* Turned on only when memory cgroup is enabled && really_do_swap_account = 1 */
52 int do_swap_account __read_mostly
;
53 static int really_do_swap_account __initdata
= 1; /* for remember boot option*/
55 #define do_swap_account (0)
58 static DEFINE_MUTEX(memcg_tasklist
); /* can be hold under cgroup_mutex */
59 #define SOFTLIMIT_EVENTS_THRESH (1000)
62 * Statistics for memory cgroup.
64 enum mem_cgroup_stat_index
{
66 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
68 MEM_CGROUP_STAT_CACHE
, /* # of pages charged as cache */
69 MEM_CGROUP_STAT_RSS
, /* # of pages charged as anon rss */
70 MEM_CGROUP_STAT_FILE_MAPPED
, /* # of pages charged as file rss */
71 MEM_CGROUP_STAT_PGPGIN_COUNT
, /* # of pages paged in */
72 MEM_CGROUP_STAT_PGPGOUT_COUNT
, /* # of pages paged out */
73 MEM_CGROUP_STAT_EVENTS
, /* sum of pagein + pageout for internal use */
74 MEM_CGROUP_STAT_SWAPOUT
, /* # of pages, swapped out */
76 MEM_CGROUP_STAT_NSTATS
,
79 struct mem_cgroup_stat_cpu
{
80 s64 count
[MEM_CGROUP_STAT_NSTATS
];
81 } ____cacheline_aligned_in_smp
;
83 struct mem_cgroup_stat
{
84 struct mem_cgroup_stat_cpu cpustat
[0];
88 __mem_cgroup_stat_reset_safe(struct mem_cgroup_stat_cpu
*stat
,
89 enum mem_cgroup_stat_index idx
)
95 __mem_cgroup_stat_read_local(struct mem_cgroup_stat_cpu
*stat
,
96 enum mem_cgroup_stat_index idx
)
98 return stat
->count
[idx
];
102 * For accounting under irq disable, no need for increment preempt count.
104 static inline void __mem_cgroup_stat_add_safe(struct mem_cgroup_stat_cpu
*stat
,
105 enum mem_cgroup_stat_index idx
, int val
)
107 stat
->count
[idx
] += val
;
110 static s64
mem_cgroup_read_stat(struct mem_cgroup_stat
*stat
,
111 enum mem_cgroup_stat_index idx
)
115 for_each_possible_cpu(cpu
)
116 ret
+= stat
->cpustat
[cpu
].count
[idx
];
120 static s64
mem_cgroup_local_usage(struct mem_cgroup_stat
*stat
)
124 ret
= mem_cgroup_read_stat(stat
, MEM_CGROUP_STAT_CACHE
);
125 ret
+= mem_cgroup_read_stat(stat
, MEM_CGROUP_STAT_RSS
);
130 * per-zone information in memory controller.
132 struct mem_cgroup_per_zone
{
134 * spin_lock to protect the per cgroup LRU
136 struct list_head lists
[NR_LRU_LISTS
];
137 unsigned long count
[NR_LRU_LISTS
];
139 struct zone_reclaim_stat reclaim_stat
;
140 struct rb_node tree_node
; /* RB tree node */
141 unsigned long long usage_in_excess
;/* Set to the value by which */
142 /* the soft limit is exceeded*/
144 struct mem_cgroup
*mem
; /* Back pointer, we cannot */
145 /* use container_of */
147 /* Macro for accessing counter */
148 #define MEM_CGROUP_ZSTAT(mz, idx) ((mz)->count[(idx)])
150 struct mem_cgroup_per_node
{
151 struct mem_cgroup_per_zone zoneinfo
[MAX_NR_ZONES
];
154 struct mem_cgroup_lru_info
{
155 struct mem_cgroup_per_node
*nodeinfo
[MAX_NUMNODES
];
159 * Cgroups above their limits are maintained in a RB-Tree, independent of
160 * their hierarchy representation
163 struct mem_cgroup_tree_per_zone
{
164 struct rb_root rb_root
;
168 struct mem_cgroup_tree_per_node
{
169 struct mem_cgroup_tree_per_zone rb_tree_per_zone
[MAX_NR_ZONES
];
172 struct mem_cgroup_tree
{
173 struct mem_cgroup_tree_per_node
*rb_tree_per_node
[MAX_NUMNODES
];
176 static struct mem_cgroup_tree soft_limit_tree __read_mostly
;
179 * The memory controller data structure. The memory controller controls both
180 * page cache and RSS per cgroup. We would eventually like to provide
181 * statistics based on the statistics developed by Rik Van Riel for clock-pro,
182 * to help the administrator determine what knobs to tune.
184 * TODO: Add a water mark for the memory controller. Reclaim will begin when
185 * we hit the water mark. May be even add a low water mark, such that
186 * no reclaim occurs from a cgroup at it's low water mark, this is
187 * a feature that will be implemented much later in the future.
190 struct cgroup_subsys_state css
;
192 * the counter to account for memory usage
194 struct res_counter res
;
196 * the counter to account for mem+swap usage.
198 struct res_counter memsw
;
200 * Per cgroup active and inactive list, similar to the
201 * per zone LRU lists.
203 struct mem_cgroup_lru_info info
;
206 protect against reclaim related member.
208 spinlock_t reclaim_param_lock
;
210 int prev_priority
; /* for recording reclaim priority */
213 * While reclaiming in a hierarchy, we cache the last child we
216 int last_scanned_child
;
218 * Should the accounting and control be hierarchical, per subtree?
221 unsigned long last_oom_jiffies
;
224 unsigned int swappiness
;
226 /* set when res.limit == memsw.limit */
227 bool memsw_is_minimum
;
230 * statistics. This must be placed at the end of memcg.
232 struct mem_cgroup_stat stat
;
236 * Maximum loops in mem_cgroup_hierarchical_reclaim(), used for soft
237 * limit reclaim to prevent infinite loops, if they ever occur.
239 #define MEM_CGROUP_MAX_RECLAIM_LOOPS (100)
240 #define MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS (2)
243 MEM_CGROUP_CHARGE_TYPE_CACHE
= 0,
244 MEM_CGROUP_CHARGE_TYPE_MAPPED
,
245 MEM_CGROUP_CHARGE_TYPE_SHMEM
, /* used by page migration of shmem */
246 MEM_CGROUP_CHARGE_TYPE_FORCE
, /* used by force_empty */
247 MEM_CGROUP_CHARGE_TYPE_SWAPOUT
, /* for accounting swapcache */
248 MEM_CGROUP_CHARGE_TYPE_DROP
, /* a page was unused swap cache */
252 /* only for here (for easy reading.) */
253 #define PCGF_CACHE (1UL << PCG_CACHE)
254 #define PCGF_USED (1UL << PCG_USED)
255 #define PCGF_LOCK (1UL << PCG_LOCK)
256 /* Not used, but added here for completeness */
257 #define PCGF_ACCT (1UL << PCG_ACCT)
259 /* for encoding cft->private value on file */
262 #define MEMFILE_PRIVATE(x, val) (((x) << 16) | (val))
263 #define MEMFILE_TYPE(val) (((val) >> 16) & 0xffff)
264 #define MEMFILE_ATTR(val) ((val) & 0xffff)
267 * Reclaim flags for mem_cgroup_hierarchical_reclaim
269 #define MEM_CGROUP_RECLAIM_NOSWAP_BIT 0x0
270 #define MEM_CGROUP_RECLAIM_NOSWAP (1 << MEM_CGROUP_RECLAIM_NOSWAP_BIT)
271 #define MEM_CGROUP_RECLAIM_SHRINK_BIT 0x1
272 #define MEM_CGROUP_RECLAIM_SHRINK (1 << MEM_CGROUP_RECLAIM_SHRINK_BIT)
273 #define MEM_CGROUP_RECLAIM_SOFT_BIT 0x2
274 #define MEM_CGROUP_RECLAIM_SOFT (1 << MEM_CGROUP_RECLAIM_SOFT_BIT)
276 static void mem_cgroup_get(struct mem_cgroup
*mem
);
277 static void mem_cgroup_put(struct mem_cgroup
*mem
);
278 static struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*mem
);
279 static void drain_all_stock_async(void);
281 static struct mem_cgroup_per_zone
*
282 mem_cgroup_zoneinfo(struct mem_cgroup
*mem
, int nid
, int zid
)
284 return &mem
->info
.nodeinfo
[nid
]->zoneinfo
[zid
];
287 static struct mem_cgroup_per_zone
*
288 page_cgroup_zoneinfo(struct page_cgroup
*pc
)
290 struct mem_cgroup
*mem
= pc
->mem_cgroup
;
291 int nid
= page_cgroup_nid(pc
);
292 int zid
= page_cgroup_zid(pc
);
297 return mem_cgroup_zoneinfo(mem
, nid
, zid
);
300 static struct mem_cgroup_tree_per_zone
*
301 soft_limit_tree_node_zone(int nid
, int zid
)
303 return &soft_limit_tree
.rb_tree_per_node
[nid
]->rb_tree_per_zone
[zid
];
306 static struct mem_cgroup_tree_per_zone
*
307 soft_limit_tree_from_page(struct page
*page
)
309 int nid
= page_to_nid(page
);
310 int zid
= page_zonenum(page
);
312 return &soft_limit_tree
.rb_tree_per_node
[nid
]->rb_tree_per_zone
[zid
];
316 __mem_cgroup_insert_exceeded(struct mem_cgroup
*mem
,
317 struct mem_cgroup_per_zone
*mz
,
318 struct mem_cgroup_tree_per_zone
*mctz
,
319 unsigned long long new_usage_in_excess
)
321 struct rb_node
**p
= &mctz
->rb_root
.rb_node
;
322 struct rb_node
*parent
= NULL
;
323 struct mem_cgroup_per_zone
*mz_node
;
328 mz
->usage_in_excess
= new_usage_in_excess
;
329 if (!mz
->usage_in_excess
)
333 mz_node
= rb_entry(parent
, struct mem_cgroup_per_zone
,
335 if (mz
->usage_in_excess
< mz_node
->usage_in_excess
)
338 * We can't avoid mem cgroups that are over their soft
339 * limit by the same amount
341 else if (mz
->usage_in_excess
>= mz_node
->usage_in_excess
)
344 rb_link_node(&mz
->tree_node
, parent
, p
);
345 rb_insert_color(&mz
->tree_node
, &mctz
->rb_root
);
350 __mem_cgroup_remove_exceeded(struct mem_cgroup
*mem
,
351 struct mem_cgroup_per_zone
*mz
,
352 struct mem_cgroup_tree_per_zone
*mctz
)
356 rb_erase(&mz
->tree_node
, &mctz
->rb_root
);
361 mem_cgroup_remove_exceeded(struct mem_cgroup
*mem
,
362 struct mem_cgroup_per_zone
*mz
,
363 struct mem_cgroup_tree_per_zone
*mctz
)
365 spin_lock(&mctz
->lock
);
366 __mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
367 spin_unlock(&mctz
->lock
);
370 static bool mem_cgroup_soft_limit_check(struct mem_cgroup
*mem
)
375 struct mem_cgroup_stat_cpu
*cpustat
;
378 cpustat
= &mem
->stat
.cpustat
[cpu
];
379 val
= __mem_cgroup_stat_read_local(cpustat
, MEM_CGROUP_STAT_EVENTS
);
380 if (unlikely(val
> SOFTLIMIT_EVENTS_THRESH
)) {
381 __mem_cgroup_stat_reset_safe(cpustat
, MEM_CGROUP_STAT_EVENTS
);
388 static void mem_cgroup_update_tree(struct mem_cgroup
*mem
, struct page
*page
)
390 unsigned long long excess
;
391 struct mem_cgroup_per_zone
*mz
;
392 struct mem_cgroup_tree_per_zone
*mctz
;
393 int nid
= page_to_nid(page
);
394 int zid
= page_zonenum(page
);
395 mctz
= soft_limit_tree_from_page(page
);
398 * Necessary to update all ancestors when hierarchy is used.
399 * because their event counter is not touched.
401 for (; mem
; mem
= parent_mem_cgroup(mem
)) {
402 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
403 excess
= res_counter_soft_limit_excess(&mem
->res
);
405 * We have to update the tree if mz is on RB-tree or
406 * mem is over its softlimit.
408 if (excess
|| mz
->on_tree
) {
409 spin_lock(&mctz
->lock
);
410 /* if on-tree, remove it */
412 __mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
414 * Insert again. mz->usage_in_excess will be updated.
415 * If excess is 0, no tree ops.
417 __mem_cgroup_insert_exceeded(mem
, mz
, mctz
, excess
);
418 spin_unlock(&mctz
->lock
);
423 static void mem_cgroup_remove_from_trees(struct mem_cgroup
*mem
)
426 struct mem_cgroup_per_zone
*mz
;
427 struct mem_cgroup_tree_per_zone
*mctz
;
429 for_each_node_state(node
, N_POSSIBLE
) {
430 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
431 mz
= mem_cgroup_zoneinfo(mem
, node
, zone
);
432 mctz
= soft_limit_tree_node_zone(node
, zone
);
433 mem_cgroup_remove_exceeded(mem
, mz
, mctz
);
438 static inline unsigned long mem_cgroup_get_excess(struct mem_cgroup
*mem
)
440 return res_counter_soft_limit_excess(&mem
->res
) >> PAGE_SHIFT
;
443 static struct mem_cgroup_per_zone
*
444 __mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone
*mctz
)
446 struct rb_node
*rightmost
= NULL
;
447 struct mem_cgroup_per_zone
*mz
;
451 rightmost
= rb_last(&mctz
->rb_root
);
453 goto done
; /* Nothing to reclaim from */
455 mz
= rb_entry(rightmost
, struct mem_cgroup_per_zone
, tree_node
);
457 * Remove the node now but someone else can add it back,
458 * we will to add it back at the end of reclaim to its correct
459 * position in the tree.
461 __mem_cgroup_remove_exceeded(mz
->mem
, mz
, mctz
);
462 if (!res_counter_soft_limit_excess(&mz
->mem
->res
) ||
463 !css_tryget(&mz
->mem
->css
))
469 static struct mem_cgroup_per_zone
*
470 mem_cgroup_largest_soft_limit_node(struct mem_cgroup_tree_per_zone
*mctz
)
472 struct mem_cgroup_per_zone
*mz
;
474 spin_lock(&mctz
->lock
);
475 mz
= __mem_cgroup_largest_soft_limit_node(mctz
);
476 spin_unlock(&mctz
->lock
);
480 static void mem_cgroup_swap_statistics(struct mem_cgroup
*mem
,
483 int val
= (charge
) ? 1 : -1;
484 struct mem_cgroup_stat
*stat
= &mem
->stat
;
485 struct mem_cgroup_stat_cpu
*cpustat
;
488 cpustat
= &stat
->cpustat
[cpu
];
489 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_SWAPOUT
, val
);
493 static void mem_cgroup_charge_statistics(struct mem_cgroup
*mem
,
494 struct page_cgroup
*pc
,
497 int val
= (charge
) ? 1 : -1;
498 struct mem_cgroup_stat
*stat
= &mem
->stat
;
499 struct mem_cgroup_stat_cpu
*cpustat
;
502 cpustat
= &stat
->cpustat
[cpu
];
503 if (PageCgroupCache(pc
))
504 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_CACHE
, val
);
506 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_RSS
, val
);
509 __mem_cgroup_stat_add_safe(cpustat
,
510 MEM_CGROUP_STAT_PGPGIN_COUNT
, 1);
512 __mem_cgroup_stat_add_safe(cpustat
,
513 MEM_CGROUP_STAT_PGPGOUT_COUNT
, 1);
514 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_EVENTS
, 1);
518 static unsigned long mem_cgroup_get_local_zonestat(struct mem_cgroup
*mem
,
522 struct mem_cgroup_per_zone
*mz
;
525 for_each_online_node(nid
)
526 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++) {
527 mz
= mem_cgroup_zoneinfo(mem
, nid
, zid
);
528 total
+= MEM_CGROUP_ZSTAT(mz
, idx
);
533 static struct mem_cgroup
*mem_cgroup_from_cont(struct cgroup
*cont
)
535 return container_of(cgroup_subsys_state(cont
,
536 mem_cgroup_subsys_id
), struct mem_cgroup
,
540 struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
)
543 * mm_update_next_owner() may clear mm->owner to NULL
544 * if it races with swapoff, page migration, etc.
545 * So this can be called with p == NULL.
550 return container_of(task_subsys_state(p
, mem_cgroup_subsys_id
),
551 struct mem_cgroup
, css
);
554 static struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
)
556 struct mem_cgroup
*mem
= NULL
;
561 * Because we have no locks, mm->owner's may be being moved to other
562 * cgroup. We use css_tryget() here even if this looks
563 * pessimistic (rather than adding locks here).
567 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
570 } while (!css_tryget(&mem
->css
));
576 * Call callback function against all cgroup under hierarchy tree.
578 static int mem_cgroup_walk_tree(struct mem_cgroup
*root
, void *data
,
579 int (*func
)(struct mem_cgroup
*, void *))
581 int found
, ret
, nextid
;
582 struct cgroup_subsys_state
*css
;
583 struct mem_cgroup
*mem
;
585 if (!root
->use_hierarchy
)
586 return (*func
)(root
, data
);
594 css
= css_get_next(&mem_cgroup_subsys
, nextid
, &root
->css
,
596 if (css
&& css_tryget(css
))
597 mem
= container_of(css
, struct mem_cgroup
, css
);
601 ret
= (*func
)(mem
, data
);
605 } while (!ret
&& css
);
610 static inline bool mem_cgroup_is_root(struct mem_cgroup
*mem
)
612 return (mem
== root_mem_cgroup
);
616 * Following LRU functions are allowed to be used without PCG_LOCK.
617 * Operations are called by routine of global LRU independently from memcg.
618 * What we have to take care of here is validness of pc->mem_cgroup.
620 * Changes to pc->mem_cgroup happens when
623 * In typical case, "charge" is done before add-to-lru. Exception is SwapCache.
624 * It is added to LRU before charge.
625 * If PCG_USED bit is not set, page_cgroup is not added to this private LRU.
626 * When moving account, the page is not on LRU. It's isolated.
629 void mem_cgroup_del_lru_list(struct page
*page
, enum lru_list lru
)
631 struct page_cgroup
*pc
;
632 struct mem_cgroup_per_zone
*mz
;
634 if (mem_cgroup_disabled())
636 pc
= lookup_page_cgroup(page
);
637 /* can happen while we handle swapcache. */
638 if (!TestClearPageCgroupAcctLRU(pc
))
640 VM_BUG_ON(!pc
->mem_cgroup
);
642 * We don't check PCG_USED bit. It's cleared when the "page" is finally
643 * removed from global LRU.
645 mz
= page_cgroup_zoneinfo(pc
);
646 MEM_CGROUP_ZSTAT(mz
, lru
) -= 1;
647 if (mem_cgroup_is_root(pc
->mem_cgroup
))
649 VM_BUG_ON(list_empty(&pc
->lru
));
650 list_del_init(&pc
->lru
);
654 void mem_cgroup_del_lru(struct page
*page
)
656 mem_cgroup_del_lru_list(page
, page_lru(page
));
659 void mem_cgroup_rotate_lru_list(struct page
*page
, enum lru_list lru
)
661 struct mem_cgroup_per_zone
*mz
;
662 struct page_cgroup
*pc
;
664 if (mem_cgroup_disabled())
667 pc
= lookup_page_cgroup(page
);
669 * Used bit is set without atomic ops but after smp_wmb().
670 * For making pc->mem_cgroup visible, insert smp_rmb() here.
673 /* unused or root page is not rotated. */
674 if (!PageCgroupUsed(pc
) || mem_cgroup_is_root(pc
->mem_cgroup
))
676 mz
= page_cgroup_zoneinfo(pc
);
677 list_move(&pc
->lru
, &mz
->lists
[lru
]);
680 void mem_cgroup_add_lru_list(struct page
*page
, enum lru_list lru
)
682 struct page_cgroup
*pc
;
683 struct mem_cgroup_per_zone
*mz
;
685 if (mem_cgroup_disabled())
687 pc
= lookup_page_cgroup(page
);
688 VM_BUG_ON(PageCgroupAcctLRU(pc
));
690 * Used bit is set without atomic ops but after smp_wmb().
691 * For making pc->mem_cgroup visible, insert smp_rmb() here.
694 if (!PageCgroupUsed(pc
))
697 mz
= page_cgroup_zoneinfo(pc
);
698 MEM_CGROUP_ZSTAT(mz
, lru
) += 1;
699 SetPageCgroupAcctLRU(pc
);
700 if (mem_cgroup_is_root(pc
->mem_cgroup
))
702 list_add(&pc
->lru
, &mz
->lists
[lru
]);
706 * At handling SwapCache, pc->mem_cgroup may be changed while it's linked to
707 * lru because the page may.be reused after it's fully uncharged (because of
708 * SwapCache behavior).To handle that, unlink page_cgroup from LRU when charge
709 * it again. This function is only used to charge SwapCache. It's done under
710 * lock_page and expected that zone->lru_lock is never held.
712 static void mem_cgroup_lru_del_before_commit_swapcache(struct page
*page
)
715 struct zone
*zone
= page_zone(page
);
716 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
718 spin_lock_irqsave(&zone
->lru_lock
, flags
);
720 * Forget old LRU when this page_cgroup is *not* used. This Used bit
721 * is guarded by lock_page() because the page is SwapCache.
723 if (!PageCgroupUsed(pc
))
724 mem_cgroup_del_lru_list(page
, page_lru(page
));
725 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
728 static void mem_cgroup_lru_add_after_commit_swapcache(struct page
*page
)
731 struct zone
*zone
= page_zone(page
);
732 struct page_cgroup
*pc
= lookup_page_cgroup(page
);
734 spin_lock_irqsave(&zone
->lru_lock
, flags
);
735 /* link when the page is linked to LRU but page_cgroup isn't */
736 if (PageLRU(page
) && !PageCgroupAcctLRU(pc
))
737 mem_cgroup_add_lru_list(page
, page_lru(page
));
738 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
742 void mem_cgroup_move_lists(struct page
*page
,
743 enum lru_list from
, enum lru_list to
)
745 if (mem_cgroup_disabled())
747 mem_cgroup_del_lru_list(page
, from
);
748 mem_cgroup_add_lru_list(page
, to
);
751 int task_in_mem_cgroup(struct task_struct
*task
, const struct mem_cgroup
*mem
)
754 struct mem_cgroup
*curr
= NULL
;
758 curr
= try_get_mem_cgroup_from_mm(task
->mm
);
763 if (curr
->use_hierarchy
)
764 ret
= css_is_ancestor(&curr
->css
, &mem
->css
);
772 * prev_priority control...this will be used in memory reclaim path.
774 int mem_cgroup_get_reclaim_priority(struct mem_cgroup
*mem
)
778 spin_lock(&mem
->reclaim_param_lock
);
779 prev_priority
= mem
->prev_priority
;
780 spin_unlock(&mem
->reclaim_param_lock
);
782 return prev_priority
;
785 void mem_cgroup_note_reclaim_priority(struct mem_cgroup
*mem
, int priority
)
787 spin_lock(&mem
->reclaim_param_lock
);
788 if (priority
< mem
->prev_priority
)
789 mem
->prev_priority
= priority
;
790 spin_unlock(&mem
->reclaim_param_lock
);
793 void mem_cgroup_record_reclaim_priority(struct mem_cgroup
*mem
, int priority
)
795 spin_lock(&mem
->reclaim_param_lock
);
796 mem
->prev_priority
= priority
;
797 spin_unlock(&mem
->reclaim_param_lock
);
800 static int calc_inactive_ratio(struct mem_cgroup
*memcg
, unsigned long *present_pages
)
802 unsigned long active
;
803 unsigned long inactive
;
805 unsigned long inactive_ratio
;
807 inactive
= mem_cgroup_get_local_zonestat(memcg
, LRU_INACTIVE_ANON
);
808 active
= mem_cgroup_get_local_zonestat(memcg
, LRU_ACTIVE_ANON
);
810 gb
= (inactive
+ active
) >> (30 - PAGE_SHIFT
);
812 inactive_ratio
= int_sqrt(10 * gb
);
817 present_pages
[0] = inactive
;
818 present_pages
[1] = active
;
821 return inactive_ratio
;
824 int mem_cgroup_inactive_anon_is_low(struct mem_cgroup
*memcg
)
826 unsigned long active
;
827 unsigned long inactive
;
828 unsigned long present_pages
[2];
829 unsigned long inactive_ratio
;
831 inactive_ratio
= calc_inactive_ratio(memcg
, present_pages
);
833 inactive
= present_pages
[0];
834 active
= present_pages
[1];
836 if (inactive
* inactive_ratio
< active
)
842 int mem_cgroup_inactive_file_is_low(struct mem_cgroup
*memcg
)
844 unsigned long active
;
845 unsigned long inactive
;
847 inactive
= mem_cgroup_get_local_zonestat(memcg
, LRU_INACTIVE_FILE
);
848 active
= mem_cgroup_get_local_zonestat(memcg
, LRU_ACTIVE_FILE
);
850 return (active
> inactive
);
853 unsigned long mem_cgroup_zone_nr_pages(struct mem_cgroup
*memcg
,
857 int nid
= zone
->zone_pgdat
->node_id
;
858 int zid
= zone_idx(zone
);
859 struct mem_cgroup_per_zone
*mz
= mem_cgroup_zoneinfo(memcg
, nid
, zid
);
861 return MEM_CGROUP_ZSTAT(mz
, lru
);
864 struct zone_reclaim_stat
*mem_cgroup_get_reclaim_stat(struct mem_cgroup
*memcg
,
867 int nid
= zone
->zone_pgdat
->node_id
;
868 int zid
= zone_idx(zone
);
869 struct mem_cgroup_per_zone
*mz
= mem_cgroup_zoneinfo(memcg
, nid
, zid
);
871 return &mz
->reclaim_stat
;
874 struct zone_reclaim_stat
*
875 mem_cgroup_get_reclaim_stat_from_page(struct page
*page
)
877 struct page_cgroup
*pc
;
878 struct mem_cgroup_per_zone
*mz
;
880 if (mem_cgroup_disabled())
883 pc
= lookup_page_cgroup(page
);
885 * Used bit is set without atomic ops but after smp_wmb().
886 * For making pc->mem_cgroup visible, insert smp_rmb() here.
889 if (!PageCgroupUsed(pc
))
892 mz
= page_cgroup_zoneinfo(pc
);
896 return &mz
->reclaim_stat
;
899 unsigned long mem_cgroup_isolate_pages(unsigned long nr_to_scan
,
900 struct list_head
*dst
,
901 unsigned long *scanned
, int order
,
902 int mode
, struct zone
*z
,
903 struct mem_cgroup
*mem_cont
,
904 int active
, int file
)
906 unsigned long nr_taken
= 0;
910 struct list_head
*src
;
911 struct page_cgroup
*pc
, *tmp
;
912 int nid
= z
->zone_pgdat
->node_id
;
913 int zid
= zone_idx(z
);
914 struct mem_cgroup_per_zone
*mz
;
915 int lru
= LRU_FILE
* file
+ active
;
919 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
920 src
= &mz
->lists
[lru
];
923 list_for_each_entry_safe_reverse(pc
, tmp
, src
, lru
) {
924 if (scan
>= nr_to_scan
)
928 if (unlikely(!PageCgroupUsed(pc
)))
930 if (unlikely(!PageLRU(page
)))
934 ret
= __isolate_lru_page(page
, mode
, file
);
937 list_move(&page
->lru
, dst
);
938 mem_cgroup_del_lru(page
);
942 /* we don't affect global LRU but rotate in our LRU */
943 mem_cgroup_rotate_lru_list(page
, page_lru(page
));
954 #define mem_cgroup_from_res_counter(counter, member) \
955 container_of(counter, struct mem_cgroup, member)
957 static bool mem_cgroup_check_under_limit(struct mem_cgroup
*mem
)
959 if (do_swap_account
) {
960 if (res_counter_check_under_limit(&mem
->res
) &&
961 res_counter_check_under_limit(&mem
->memsw
))
964 if (res_counter_check_under_limit(&mem
->res
))
969 static unsigned int get_swappiness(struct mem_cgroup
*memcg
)
971 struct cgroup
*cgrp
= memcg
->css
.cgroup
;
972 unsigned int swappiness
;
975 if (cgrp
->parent
== NULL
)
976 return vm_swappiness
;
978 spin_lock(&memcg
->reclaim_param_lock
);
979 swappiness
= memcg
->swappiness
;
980 spin_unlock(&memcg
->reclaim_param_lock
);
985 static int mem_cgroup_count_children_cb(struct mem_cgroup
*mem
, void *data
)
993 * mem_cgroup_print_mem_info: Called from OOM with tasklist_lock held in read mode.
994 * @memcg: The memory cgroup that went over limit
995 * @p: Task that is going to be killed
997 * NOTE: @memcg and @p's mem_cgroup can be different when hierarchy is
1000 void mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
, struct task_struct
*p
)
1002 struct cgroup
*task_cgrp
;
1003 struct cgroup
*mem_cgrp
;
1005 * Need a buffer in BSS, can't rely on allocations. The code relies
1006 * on the assumption that OOM is serialized for memory controller.
1007 * If this assumption is broken, revisit this code.
1009 static char memcg_name
[PATH_MAX
];
1018 mem_cgrp
= memcg
->css
.cgroup
;
1019 task_cgrp
= task_cgroup(p
, mem_cgroup_subsys_id
);
1021 ret
= cgroup_path(task_cgrp
, memcg_name
, PATH_MAX
);
1024 * Unfortunately, we are unable to convert to a useful name
1025 * But we'll still print out the usage information
1032 printk(KERN_INFO
"Task in %s killed", memcg_name
);
1035 ret
= cgroup_path(mem_cgrp
, memcg_name
, PATH_MAX
);
1043 * Continues from above, so we don't need an KERN_ level
1045 printk(KERN_CONT
" as a result of limit of %s\n", memcg_name
);
1048 printk(KERN_INFO
"memory: usage %llukB, limit %llukB, failcnt %llu\n",
1049 res_counter_read_u64(&memcg
->res
, RES_USAGE
) >> 10,
1050 res_counter_read_u64(&memcg
->res
, RES_LIMIT
) >> 10,
1051 res_counter_read_u64(&memcg
->res
, RES_FAILCNT
));
1052 printk(KERN_INFO
"memory+swap: usage %llukB, limit %llukB, "
1054 res_counter_read_u64(&memcg
->memsw
, RES_USAGE
) >> 10,
1055 res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
) >> 10,
1056 res_counter_read_u64(&memcg
->memsw
, RES_FAILCNT
));
1060 * This function returns the number of memcg under hierarchy tree. Returns
1061 * 1(self count) if no children.
1063 static int mem_cgroup_count_children(struct mem_cgroup
*mem
)
1066 mem_cgroup_walk_tree(mem
, &num
, mem_cgroup_count_children_cb
);
1071 * Visit the first child (need not be the first child as per the ordering
1072 * of the cgroup list, since we track last_scanned_child) of @mem and use
1073 * that to reclaim free pages from.
1075 static struct mem_cgroup
*
1076 mem_cgroup_select_victim(struct mem_cgroup
*root_mem
)
1078 struct mem_cgroup
*ret
= NULL
;
1079 struct cgroup_subsys_state
*css
;
1082 if (!root_mem
->use_hierarchy
) {
1083 css_get(&root_mem
->css
);
1089 nextid
= root_mem
->last_scanned_child
+ 1;
1090 css
= css_get_next(&mem_cgroup_subsys
, nextid
, &root_mem
->css
,
1092 if (css
&& css_tryget(css
))
1093 ret
= container_of(css
, struct mem_cgroup
, css
);
1096 /* Updates scanning parameter */
1097 spin_lock(&root_mem
->reclaim_param_lock
);
1099 /* this means start scan from ID:1 */
1100 root_mem
->last_scanned_child
= 0;
1102 root_mem
->last_scanned_child
= found
;
1103 spin_unlock(&root_mem
->reclaim_param_lock
);
1110 * Scan the hierarchy if needed to reclaim memory. We remember the last child
1111 * we reclaimed from, so that we don't end up penalizing one child extensively
1112 * based on its position in the children list.
1114 * root_mem is the original ancestor that we've been reclaim from.
1116 * We give up and return to the caller when we visit root_mem twice.
1117 * (other groups can be removed while we're walking....)
1119 * If shrink==true, for avoiding to free too much, this returns immedieately.
1121 static int mem_cgroup_hierarchical_reclaim(struct mem_cgroup
*root_mem
,
1124 unsigned long reclaim_options
)
1126 struct mem_cgroup
*victim
;
1129 bool noswap
= reclaim_options
& MEM_CGROUP_RECLAIM_NOSWAP
;
1130 bool shrink
= reclaim_options
& MEM_CGROUP_RECLAIM_SHRINK
;
1131 bool check_soft
= reclaim_options
& MEM_CGROUP_RECLAIM_SOFT
;
1132 unsigned long excess
= mem_cgroup_get_excess(root_mem
);
1134 /* If memsw_is_minimum==1, swap-out is of-no-use. */
1135 if (root_mem
->memsw_is_minimum
)
1139 victim
= mem_cgroup_select_victim(root_mem
);
1140 if (victim
== root_mem
) {
1143 drain_all_stock_async();
1146 * If we have not been able to reclaim
1147 * anything, it might because there are
1148 * no reclaimable pages under this hierarchy
1150 if (!check_soft
|| !total
) {
1151 css_put(&victim
->css
);
1155 * We want to do more targetted reclaim.
1156 * excess >> 2 is not to excessive so as to
1157 * reclaim too much, nor too less that we keep
1158 * coming back to reclaim from this cgroup
1160 if (total
>= (excess
>> 2) ||
1161 (loop
> MEM_CGROUP_MAX_RECLAIM_LOOPS
)) {
1162 css_put(&victim
->css
);
1167 if (!mem_cgroup_local_usage(&victim
->stat
)) {
1168 /* this cgroup's local usage == 0 */
1169 css_put(&victim
->css
);
1172 /* we use swappiness of local cgroup */
1174 ret
= mem_cgroup_shrink_node_zone(victim
, gfp_mask
,
1175 noswap
, get_swappiness(victim
), zone
,
1176 zone
->zone_pgdat
->node_id
);
1178 ret
= try_to_free_mem_cgroup_pages(victim
, gfp_mask
,
1179 noswap
, get_swappiness(victim
));
1180 css_put(&victim
->css
);
1182 * At shrinking usage, we can't check we should stop here or
1183 * reclaim more. It's depends on callers. last_scanned_child
1184 * will work enough for keeping fairness under tree.
1190 if (res_counter_check_under_soft_limit(&root_mem
->res
))
1192 } else if (mem_cgroup_check_under_limit(root_mem
))
1198 bool mem_cgroup_oom_called(struct task_struct
*task
)
1201 struct mem_cgroup
*mem
;
1202 struct mm_struct
*mm
;
1208 mem
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
1209 if (mem
&& time_before(jiffies
, mem
->last_oom_jiffies
+ HZ
/10))
1215 static int record_last_oom_cb(struct mem_cgroup
*mem
, void *data
)
1217 mem
->last_oom_jiffies
= jiffies
;
1221 static void record_last_oom(struct mem_cgroup
*mem
)
1223 mem_cgroup_walk_tree(mem
, NULL
, record_last_oom_cb
);
1227 * Currently used to update mapped file statistics, but the routine can be
1228 * generalized to update other statistics as well.
1230 void mem_cgroup_update_file_mapped(struct page
*page
, int val
)
1232 struct mem_cgroup
*mem
;
1233 struct mem_cgroup_stat
*stat
;
1234 struct mem_cgroup_stat_cpu
*cpustat
;
1236 struct page_cgroup
*pc
;
1238 pc
= lookup_page_cgroup(page
);
1242 lock_page_cgroup(pc
);
1243 mem
= pc
->mem_cgroup
;
1247 if (!PageCgroupUsed(pc
))
1251 * Preemption is already disabled, we don't need get_cpu()
1253 cpu
= smp_processor_id();
1255 cpustat
= &stat
->cpustat
[cpu
];
1257 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_FILE_MAPPED
, val
);
1259 unlock_page_cgroup(pc
);
1263 * size of first charge trial. "32" comes from vmscan.c's magic value.
1264 * TODO: maybe necessary to use big numbers in big irons.
1266 #define CHARGE_SIZE (32 * PAGE_SIZE)
1267 struct memcg_stock_pcp
{
1268 struct mem_cgroup
*cached
; /* this never be root cgroup */
1270 struct work_struct work
;
1272 static DEFINE_PER_CPU(struct memcg_stock_pcp
, memcg_stock
);
1273 static atomic_t memcg_drain_count
;
1276 * Try to consume stocked charge on this cpu. If success, PAGE_SIZE is consumed
1277 * from local stock and true is returned. If the stock is 0 or charges from a
1278 * cgroup which is not current target, returns false. This stock will be
1281 static bool consume_stock(struct mem_cgroup
*mem
)
1283 struct memcg_stock_pcp
*stock
;
1286 stock
= &get_cpu_var(memcg_stock
);
1287 if (mem
== stock
->cached
&& stock
->charge
)
1288 stock
->charge
-= PAGE_SIZE
;
1289 else /* need to call res_counter_charge */
1291 put_cpu_var(memcg_stock
);
1296 * Returns stocks cached in percpu to res_counter and reset cached information.
1298 static void drain_stock(struct memcg_stock_pcp
*stock
)
1300 struct mem_cgroup
*old
= stock
->cached
;
1302 if (stock
->charge
) {
1303 res_counter_uncharge(&old
->res
, stock
->charge
);
1304 if (do_swap_account
)
1305 res_counter_uncharge(&old
->memsw
, stock
->charge
);
1307 stock
->cached
= NULL
;
1312 * This must be called under preempt disabled or must be called by
1313 * a thread which is pinned to local cpu.
1315 static void drain_local_stock(struct work_struct
*dummy
)
1317 struct memcg_stock_pcp
*stock
= &__get_cpu_var(memcg_stock
);
1322 * Cache charges(val) which is from res_counter, to local per_cpu area.
1323 * This will be consumed by consumt_stock() function, later.
1325 static void refill_stock(struct mem_cgroup
*mem
, int val
)
1327 struct memcg_stock_pcp
*stock
= &get_cpu_var(memcg_stock
);
1329 if (stock
->cached
!= mem
) { /* reset if necessary */
1331 stock
->cached
= mem
;
1333 stock
->charge
+= val
;
1334 put_cpu_var(memcg_stock
);
1338 * Tries to drain stocked charges in other cpus. This function is asynchronous
1339 * and just put a work per cpu for draining localy on each cpu. Caller can
1340 * expects some charges will be back to res_counter later but cannot wait for
1343 static void drain_all_stock_async(void)
1346 /* This function is for scheduling "drain" in asynchronous way.
1347 * The result of "drain" is not directly handled by callers. Then,
1348 * if someone is calling drain, we don't have to call drain more.
1349 * Anyway, WORK_STRUCT_PENDING check in queue_work_on() will catch if
1350 * there is a race. We just do loose check here.
1352 if (atomic_read(&memcg_drain_count
))
1354 /* Notify other cpus that system-wide "drain" is running */
1355 atomic_inc(&memcg_drain_count
);
1357 for_each_online_cpu(cpu
) {
1358 struct memcg_stock_pcp
*stock
= &per_cpu(memcg_stock
, cpu
);
1359 schedule_work_on(cpu
, &stock
->work
);
1362 atomic_dec(&memcg_drain_count
);
1363 /* We don't wait for flush_work */
1366 /* This is a synchronous drain interface. */
1367 static void drain_all_stock_sync(void)
1369 /* called when force_empty is called */
1370 atomic_inc(&memcg_drain_count
);
1371 schedule_on_each_cpu(drain_local_stock
);
1372 atomic_dec(&memcg_drain_count
);
1375 static int __cpuinit
memcg_stock_cpu_callback(struct notifier_block
*nb
,
1376 unsigned long action
,
1379 int cpu
= (unsigned long)hcpu
;
1380 struct memcg_stock_pcp
*stock
;
1382 if (action
!= CPU_DEAD
)
1384 stock
= &per_cpu(memcg_stock
, cpu
);
1390 * Unlike exported interface, "oom" parameter is added. if oom==true,
1391 * oom-killer can be invoked.
1393 static int __mem_cgroup_try_charge(struct mm_struct
*mm
,
1394 gfp_t gfp_mask
, struct mem_cgroup
**memcg
,
1395 bool oom
, struct page
*page
)
1397 struct mem_cgroup
*mem
, *mem_over_limit
;
1398 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
1399 struct res_counter
*fail_res
;
1400 int csize
= CHARGE_SIZE
;
1402 if (unlikely(test_thread_flag(TIF_MEMDIE
))) {
1403 /* Don't account this! */
1409 * We always charge the cgroup the mm_struct belongs to.
1410 * The mm_struct's mem_cgroup changes on task migration if the
1411 * thread group leader migrates. It's possible that mm is not
1412 * set, if so charge the init_mm (happens for pagecache usage).
1416 mem
= try_get_mem_cgroup_from_mm(mm
);
1424 VM_BUG_ON(css_is_removed(&mem
->css
));
1425 if (mem_cgroup_is_root(mem
))
1430 unsigned long flags
= 0;
1432 if (consume_stock(mem
))
1435 ret
= res_counter_charge(&mem
->res
, csize
, &fail_res
);
1437 if (!do_swap_account
)
1439 ret
= res_counter_charge(&mem
->memsw
, csize
, &fail_res
);
1442 /* mem+swap counter fails */
1443 res_counter_uncharge(&mem
->res
, csize
);
1444 flags
|= MEM_CGROUP_RECLAIM_NOSWAP
;
1445 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
,
1448 /* mem counter fails */
1449 mem_over_limit
= mem_cgroup_from_res_counter(fail_res
,
1452 /* reduce request size and retry */
1453 if (csize
> PAGE_SIZE
) {
1457 if (!(gfp_mask
& __GFP_WAIT
))
1460 ret
= mem_cgroup_hierarchical_reclaim(mem_over_limit
, NULL
,
1466 * try_to_free_mem_cgroup_pages() might not give us a full
1467 * picture of reclaim. Some pages are reclaimed and might be
1468 * moved to swap cache or just unmapped from the cgroup.
1469 * Check the limit again to see if the reclaim reduced the
1470 * current usage of the cgroup before giving up
1473 if (mem_cgroup_check_under_limit(mem_over_limit
))
1476 if (!nr_retries
--) {
1478 mutex_lock(&memcg_tasklist
);
1479 mem_cgroup_out_of_memory(mem_over_limit
, gfp_mask
);
1480 mutex_unlock(&memcg_tasklist
);
1481 record_last_oom(mem_over_limit
);
1486 if (csize
> PAGE_SIZE
)
1487 refill_stock(mem
, csize
- PAGE_SIZE
);
1490 * Insert ancestor (and ancestor's ancestors), to softlimit RB-tree.
1491 * if they exceeds softlimit.
1493 if (mem_cgroup_soft_limit_check(mem
))
1494 mem_cgroup_update_tree(mem
, page
);
1503 * Somemtimes we have to undo a charge we got by try_charge().
1504 * This function is for that and do uncharge, put css's refcnt.
1505 * gotten by try_charge().
1507 static void mem_cgroup_cancel_charge(struct mem_cgroup
*mem
)
1509 if (!mem_cgroup_is_root(mem
)) {
1510 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
1511 if (do_swap_account
)
1512 res_counter_uncharge(&mem
->memsw
, PAGE_SIZE
);
1518 * A helper function to get mem_cgroup from ID. must be called under
1519 * rcu_read_lock(). The caller must check css_is_removed() or some if
1520 * it's concern. (dropping refcnt from swap can be called against removed
1523 static struct mem_cgroup
*mem_cgroup_lookup(unsigned short id
)
1525 struct cgroup_subsys_state
*css
;
1527 /* ID 0 is unused ID */
1530 css
= css_lookup(&mem_cgroup_subsys
, id
);
1533 return container_of(css
, struct mem_cgroup
, css
);
1536 static struct mem_cgroup
*try_get_mem_cgroup_from_swapcache(struct page
*page
)
1538 struct mem_cgroup
*mem
;
1539 struct page_cgroup
*pc
;
1543 VM_BUG_ON(!PageLocked(page
));
1545 if (!PageSwapCache(page
))
1548 pc
= lookup_page_cgroup(page
);
1549 lock_page_cgroup(pc
);
1550 if (PageCgroupUsed(pc
)) {
1551 mem
= pc
->mem_cgroup
;
1552 if (mem
&& !css_tryget(&mem
->css
))
1555 ent
.val
= page_private(page
);
1556 id
= lookup_swap_cgroup(ent
);
1558 mem
= mem_cgroup_lookup(id
);
1559 if (mem
&& !css_tryget(&mem
->css
))
1563 unlock_page_cgroup(pc
);
1568 * commit a charge got by __mem_cgroup_try_charge() and makes page_cgroup to be
1569 * USED state. If already USED, uncharge and return.
1572 static void __mem_cgroup_commit_charge(struct mem_cgroup
*mem
,
1573 struct page_cgroup
*pc
,
1574 enum charge_type ctype
)
1576 /* try_charge() can return NULL to *memcg, taking care of it. */
1580 lock_page_cgroup(pc
);
1581 if (unlikely(PageCgroupUsed(pc
))) {
1582 unlock_page_cgroup(pc
);
1583 mem_cgroup_cancel_charge(mem
);
1587 pc
->mem_cgroup
= mem
;
1589 * We access a page_cgroup asynchronously without lock_page_cgroup().
1590 * Especially when a page_cgroup is taken from a page, pc->mem_cgroup
1591 * is accessed after testing USED bit. To make pc->mem_cgroup visible
1592 * before USED bit, we need memory barrier here.
1593 * See mem_cgroup_add_lru_list(), etc.
1597 case MEM_CGROUP_CHARGE_TYPE_CACHE
:
1598 case MEM_CGROUP_CHARGE_TYPE_SHMEM
:
1599 SetPageCgroupCache(pc
);
1600 SetPageCgroupUsed(pc
);
1602 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
1603 ClearPageCgroupCache(pc
);
1604 SetPageCgroupUsed(pc
);
1610 mem_cgroup_charge_statistics(mem
, pc
, true);
1612 unlock_page_cgroup(pc
);
1616 * __mem_cgroup_move_account - move account of the page
1617 * @pc: page_cgroup of the page.
1618 * @from: mem_cgroup which the page is moved from.
1619 * @to: mem_cgroup which the page is moved to. @from != @to.
1621 * The caller must confirm following.
1622 * - page is not on LRU (isolate_page() is useful.)
1623 * - the pc is locked, used, and ->mem_cgroup points to @from.
1625 * This function does "uncharge" from old cgroup but doesn't do "charge" to
1626 * new cgroup. It should be done by a caller.
1629 static void __mem_cgroup_move_account(struct page_cgroup
*pc
,
1630 struct mem_cgroup
*from
, struct mem_cgroup
*to
)
1634 struct mem_cgroup_stat
*stat
;
1635 struct mem_cgroup_stat_cpu
*cpustat
;
1637 VM_BUG_ON(from
== to
);
1638 VM_BUG_ON(PageLRU(pc
->page
));
1639 VM_BUG_ON(!PageCgroupLocked(pc
));
1640 VM_BUG_ON(!PageCgroupUsed(pc
));
1641 VM_BUG_ON(pc
->mem_cgroup
!= from
);
1643 if (!mem_cgroup_is_root(from
))
1644 res_counter_uncharge(&from
->res
, PAGE_SIZE
);
1645 mem_cgroup_charge_statistics(from
, pc
, false);
1648 if (page_mapped(page
) && !PageAnon(page
)) {
1649 cpu
= smp_processor_id();
1650 /* Update mapped_file data for mem_cgroup "from" */
1652 cpustat
= &stat
->cpustat
[cpu
];
1653 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_FILE_MAPPED
,
1656 /* Update mapped_file data for mem_cgroup "to" */
1658 cpustat
= &stat
->cpustat
[cpu
];
1659 __mem_cgroup_stat_add_safe(cpustat
, MEM_CGROUP_STAT_FILE_MAPPED
,
1663 if (do_swap_account
&& !mem_cgroup_is_root(from
))
1664 res_counter_uncharge(&from
->memsw
, PAGE_SIZE
);
1665 css_put(&from
->css
);
1668 pc
->mem_cgroup
= to
;
1669 mem_cgroup_charge_statistics(to
, pc
, true);
1671 * We charges against "to" which may not have any tasks. Then, "to"
1672 * can be under rmdir(). But in current implementation, caller of
1673 * this function is just force_empty() and it's garanteed that
1674 * "to" is never removed. So, we don't check rmdir status here.
1679 * check whether the @pc is valid for moving account and call
1680 * __mem_cgroup_move_account()
1682 static int mem_cgroup_move_account(struct page_cgroup
*pc
,
1683 struct mem_cgroup
*from
, struct mem_cgroup
*to
)
1686 lock_page_cgroup(pc
);
1687 if (PageCgroupUsed(pc
) && pc
->mem_cgroup
== from
) {
1688 __mem_cgroup_move_account(pc
, from
, to
);
1691 unlock_page_cgroup(pc
);
1696 * move charges to its parent.
1699 static int mem_cgroup_move_parent(struct page_cgroup
*pc
,
1700 struct mem_cgroup
*child
,
1703 struct page
*page
= pc
->page
;
1704 struct cgroup
*cg
= child
->css
.cgroup
;
1705 struct cgroup
*pcg
= cg
->parent
;
1706 struct mem_cgroup
*parent
;
1714 if (!get_page_unless_zero(page
))
1716 if (isolate_lru_page(page
))
1719 parent
= mem_cgroup_from_cont(pcg
);
1720 ret
= __mem_cgroup_try_charge(NULL
, gfp_mask
, &parent
, false, page
);
1724 ret
= mem_cgroup_move_account(pc
, child
, parent
);
1726 css_put(&parent
->css
); /* drop extra refcnt by try_charge() */
1728 mem_cgroup_cancel_charge(parent
); /* does css_put */
1730 putback_lru_page(page
);
1738 * Charge the memory controller for page usage.
1740 * 0 if the charge was successful
1741 * < 0 if the cgroup is over its limit
1743 static int mem_cgroup_charge_common(struct page
*page
, struct mm_struct
*mm
,
1744 gfp_t gfp_mask
, enum charge_type ctype
,
1745 struct mem_cgroup
*memcg
)
1747 struct mem_cgroup
*mem
;
1748 struct page_cgroup
*pc
;
1751 pc
= lookup_page_cgroup(page
);
1752 /* can happen at boot */
1758 ret
= __mem_cgroup_try_charge(mm
, gfp_mask
, &mem
, true, page
);
1762 __mem_cgroup_commit_charge(mem
, pc
, ctype
);
1766 int mem_cgroup_newpage_charge(struct page
*page
,
1767 struct mm_struct
*mm
, gfp_t gfp_mask
)
1769 if (mem_cgroup_disabled())
1771 if (PageCompound(page
))
1774 * If already mapped, we don't have to account.
1775 * If page cache, page->mapping has address_space.
1776 * But page->mapping may have out-of-use anon_vma pointer,
1777 * detecit it by PageAnon() check. newly-mapped-anon's page->mapping
1780 if (page_mapped(page
) || (page
->mapping
&& !PageAnon(page
)))
1784 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
1785 MEM_CGROUP_CHARGE_TYPE_MAPPED
, NULL
);
1789 __mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
,
1790 enum charge_type ctype
);
1792 int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
1795 struct mem_cgroup
*mem
= NULL
;
1798 if (mem_cgroup_disabled())
1800 if (PageCompound(page
))
1803 * Corner case handling. This is called from add_to_page_cache()
1804 * in usual. But some FS (shmem) precharges this page before calling it
1805 * and call add_to_page_cache() with GFP_NOWAIT.
1807 * For GFP_NOWAIT case, the page may be pre-charged before calling
1808 * add_to_page_cache(). (See shmem.c) check it here and avoid to call
1809 * charge twice. (It works but has to pay a bit larger cost.)
1810 * And when the page is SwapCache, it should take swap information
1811 * into account. This is under lock_page() now.
1813 if (!(gfp_mask
& __GFP_WAIT
)) {
1814 struct page_cgroup
*pc
;
1817 pc
= lookup_page_cgroup(page
);
1820 lock_page_cgroup(pc
);
1821 if (PageCgroupUsed(pc
)) {
1822 unlock_page_cgroup(pc
);
1825 unlock_page_cgroup(pc
);
1828 if (unlikely(!mm
&& !mem
))
1831 if (page_is_file_cache(page
))
1832 return mem_cgroup_charge_common(page
, mm
, gfp_mask
,
1833 MEM_CGROUP_CHARGE_TYPE_CACHE
, NULL
);
1836 if (PageSwapCache(page
)) {
1837 ret
= mem_cgroup_try_charge_swapin(mm
, page
, gfp_mask
, &mem
);
1839 __mem_cgroup_commit_charge_swapin(page
, mem
,
1840 MEM_CGROUP_CHARGE_TYPE_SHMEM
);
1842 ret
= mem_cgroup_charge_common(page
, mm
, gfp_mask
,
1843 MEM_CGROUP_CHARGE_TYPE_SHMEM
, mem
);
1849 * While swap-in, try_charge -> commit or cancel, the page is locked.
1850 * And when try_charge() successfully returns, one refcnt to memcg without
1851 * struct page_cgroup is acquired. This refcnt will be consumed by
1852 * "commit()" or removed by "cancel()"
1854 int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
1856 gfp_t mask
, struct mem_cgroup
**ptr
)
1858 struct mem_cgroup
*mem
;
1861 if (mem_cgroup_disabled())
1864 if (!do_swap_account
)
1867 * A racing thread's fault, or swapoff, may have already updated
1868 * the pte, and even removed page from swap cache: in those cases
1869 * do_swap_page()'s pte_same() test will fail; but there's also a
1870 * KSM case which does need to charge the page.
1872 if (!PageSwapCache(page
))
1874 mem
= try_get_mem_cgroup_from_swapcache(page
);
1878 ret
= __mem_cgroup_try_charge(NULL
, mask
, ptr
, true, page
);
1879 /* drop extra refcnt from tryget */
1885 return __mem_cgroup_try_charge(mm
, mask
, ptr
, true, page
);
1889 __mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
,
1890 enum charge_type ctype
)
1892 struct page_cgroup
*pc
;
1894 if (mem_cgroup_disabled())
1898 cgroup_exclude_rmdir(&ptr
->css
);
1899 pc
= lookup_page_cgroup(page
);
1900 mem_cgroup_lru_del_before_commit_swapcache(page
);
1901 __mem_cgroup_commit_charge(ptr
, pc
, ctype
);
1902 mem_cgroup_lru_add_after_commit_swapcache(page
);
1904 * Now swap is on-memory. This means this page may be
1905 * counted both as mem and swap....double count.
1906 * Fix it by uncharging from memsw. Basically, this SwapCache is stable
1907 * under lock_page(). But in do_swap_page()::memory.c, reuse_swap_page()
1908 * may call delete_from_swap_cache() before reach here.
1910 if (do_swap_account
&& PageSwapCache(page
)) {
1911 swp_entry_t ent
= {.val
= page_private(page
)};
1913 struct mem_cgroup
*memcg
;
1915 id
= swap_cgroup_record(ent
, 0);
1917 memcg
= mem_cgroup_lookup(id
);
1920 * This recorded memcg can be obsolete one. So, avoid
1921 * calling css_tryget
1923 if (!mem_cgroup_is_root(memcg
))
1924 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
1925 mem_cgroup_swap_statistics(memcg
, false);
1926 mem_cgroup_put(memcg
);
1931 * At swapin, we may charge account against cgroup which has no tasks.
1932 * So, rmdir()->pre_destroy() can be called while we do this charge.
1933 * In that case, we need to call pre_destroy() again. check it here.
1935 cgroup_release_and_wakeup_rmdir(&ptr
->css
);
1938 void mem_cgroup_commit_charge_swapin(struct page
*page
, struct mem_cgroup
*ptr
)
1940 __mem_cgroup_commit_charge_swapin(page
, ptr
,
1941 MEM_CGROUP_CHARGE_TYPE_MAPPED
);
1944 void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*mem
)
1946 if (mem_cgroup_disabled())
1950 mem_cgroup_cancel_charge(mem
);
1954 __do_uncharge(struct mem_cgroup
*mem
, const enum charge_type ctype
)
1956 struct memcg_batch_info
*batch
= NULL
;
1957 bool uncharge_memsw
= true;
1958 /* If swapout, usage of swap doesn't decrease */
1959 if (!do_swap_account
|| ctype
== MEM_CGROUP_CHARGE_TYPE_SWAPOUT
)
1960 uncharge_memsw
= false;
1962 * do_batch > 0 when unmapping pages or inode invalidate/truncate.
1963 * In those cases, all pages freed continously can be expected to be in
1964 * the same cgroup and we have chance to coalesce uncharges.
1965 * But we do uncharge one by one if this is killed by OOM(TIF_MEMDIE)
1966 * because we want to do uncharge as soon as possible.
1968 if (!current
->memcg_batch
.do_batch
|| test_thread_flag(TIF_MEMDIE
))
1969 goto direct_uncharge
;
1971 batch
= ¤t
->memcg_batch
;
1973 * In usual, we do css_get() when we remember memcg pointer.
1974 * But in this case, we keep res->usage until end of a series of
1975 * uncharges. Then, it's ok to ignore memcg's refcnt.
1980 * In typical case, batch->memcg == mem. This means we can
1981 * merge a series of uncharges to an uncharge of res_counter.
1982 * If not, we uncharge res_counter ony by one.
1984 if (batch
->memcg
!= mem
)
1985 goto direct_uncharge
;
1986 /* remember freed charge and uncharge it later */
1987 batch
->bytes
+= PAGE_SIZE
;
1989 batch
->memsw_bytes
+= PAGE_SIZE
;
1992 res_counter_uncharge(&mem
->res
, PAGE_SIZE
);
1994 res_counter_uncharge(&mem
->memsw
, PAGE_SIZE
);
1999 * uncharge if !page_mapped(page)
2001 static struct mem_cgroup
*
2002 __mem_cgroup_uncharge_common(struct page
*page
, enum charge_type ctype
)
2004 struct page_cgroup
*pc
;
2005 struct mem_cgroup
*mem
= NULL
;
2006 struct mem_cgroup_per_zone
*mz
;
2008 if (mem_cgroup_disabled())
2011 if (PageSwapCache(page
))
2015 * Check if our page_cgroup is valid
2017 pc
= lookup_page_cgroup(page
);
2018 if (unlikely(!pc
|| !PageCgroupUsed(pc
)))
2021 lock_page_cgroup(pc
);
2023 mem
= pc
->mem_cgroup
;
2025 if (!PageCgroupUsed(pc
))
2029 case MEM_CGROUP_CHARGE_TYPE_MAPPED
:
2030 case MEM_CGROUP_CHARGE_TYPE_DROP
:
2031 if (page_mapped(page
))
2034 case MEM_CGROUP_CHARGE_TYPE_SWAPOUT
:
2035 if (!PageAnon(page
)) { /* Shared memory */
2036 if (page
->mapping
&& !page_is_file_cache(page
))
2038 } else if (page_mapped(page
)) /* Anon */
2045 if (!mem_cgroup_is_root(mem
))
2046 __do_uncharge(mem
, ctype
);
2047 if (ctype
== MEM_CGROUP_CHARGE_TYPE_SWAPOUT
)
2048 mem_cgroup_swap_statistics(mem
, true);
2049 mem_cgroup_charge_statistics(mem
, pc
, false);
2051 ClearPageCgroupUsed(pc
);
2053 * pc->mem_cgroup is not cleared here. It will be accessed when it's
2054 * freed from LRU. This is safe because uncharged page is expected not
2055 * to be reused (freed soon). Exception is SwapCache, it's handled by
2056 * special functions.
2059 mz
= page_cgroup_zoneinfo(pc
);
2060 unlock_page_cgroup(pc
);
2062 if (mem_cgroup_soft_limit_check(mem
))
2063 mem_cgroup_update_tree(mem
, page
);
2064 /* at swapout, this memcg will be accessed to record to swap */
2065 if (ctype
!= MEM_CGROUP_CHARGE_TYPE_SWAPOUT
)
2071 unlock_page_cgroup(pc
);
2075 void mem_cgroup_uncharge_page(struct page
*page
)
2078 if (page_mapped(page
))
2080 if (page
->mapping
&& !PageAnon(page
))
2082 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_MAPPED
);
2085 void mem_cgroup_uncharge_cache_page(struct page
*page
)
2087 VM_BUG_ON(page_mapped(page
));
2088 VM_BUG_ON(page
->mapping
);
2089 __mem_cgroup_uncharge_common(page
, MEM_CGROUP_CHARGE_TYPE_CACHE
);
2093 * Batch_start/batch_end is called in unmap_page_range/invlidate/trucate.
2094 * In that cases, pages are freed continuously and we can expect pages
2095 * are in the same memcg. All these calls itself limits the number of
2096 * pages freed at once, then uncharge_start/end() is called properly.
2097 * This may be called prural(2) times in a context,
2100 void mem_cgroup_uncharge_start(void)
2102 current
->memcg_batch
.do_batch
++;
2103 /* We can do nest. */
2104 if (current
->memcg_batch
.do_batch
== 1) {
2105 current
->memcg_batch
.memcg
= NULL
;
2106 current
->memcg_batch
.bytes
= 0;
2107 current
->memcg_batch
.memsw_bytes
= 0;
2111 void mem_cgroup_uncharge_end(void)
2113 struct memcg_batch_info
*batch
= ¤t
->memcg_batch
;
2115 if (!batch
->do_batch
)
2119 if (batch
->do_batch
) /* If stacked, do nothing. */
2125 * This "batch->memcg" is valid without any css_get/put etc...
2126 * bacause we hide charges behind us.
2129 res_counter_uncharge(&batch
->memcg
->res
, batch
->bytes
);
2130 if (batch
->memsw_bytes
)
2131 res_counter_uncharge(&batch
->memcg
->memsw
, batch
->memsw_bytes
);
2132 /* forget this pointer (for sanity check) */
2133 batch
->memcg
= NULL
;
2138 * called after __delete_from_swap_cache() and drop "page" account.
2139 * memcg information is recorded to swap_cgroup of "ent"
2142 mem_cgroup_uncharge_swapcache(struct page
*page
, swp_entry_t ent
, bool swapout
)
2144 struct mem_cgroup
*memcg
;
2145 int ctype
= MEM_CGROUP_CHARGE_TYPE_SWAPOUT
;
2147 if (!swapout
) /* this was a swap cache but the swap is unused ! */
2148 ctype
= MEM_CGROUP_CHARGE_TYPE_DROP
;
2150 memcg
= __mem_cgroup_uncharge_common(page
, ctype
);
2152 /* record memcg information */
2153 if (do_swap_account
&& swapout
&& memcg
) {
2154 swap_cgroup_record(ent
, css_id(&memcg
->css
));
2155 mem_cgroup_get(memcg
);
2157 if (swapout
&& memcg
)
2158 css_put(&memcg
->css
);
2162 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
2164 * called from swap_entry_free(). remove record in swap_cgroup and
2165 * uncharge "memsw" account.
2167 void mem_cgroup_uncharge_swap(swp_entry_t ent
)
2169 struct mem_cgroup
*memcg
;
2172 if (!do_swap_account
)
2175 id
= swap_cgroup_record(ent
, 0);
2177 memcg
= mem_cgroup_lookup(id
);
2180 * We uncharge this because swap is freed.
2181 * This memcg can be obsolete one. We avoid calling css_tryget
2183 if (!mem_cgroup_is_root(memcg
))
2184 res_counter_uncharge(&memcg
->memsw
, PAGE_SIZE
);
2185 mem_cgroup_swap_statistics(memcg
, false);
2186 mem_cgroup_put(memcg
);
2193 * Before starting migration, account PAGE_SIZE to mem_cgroup that the old
2196 int mem_cgroup_prepare_migration(struct page
*page
, struct mem_cgroup
**ptr
)
2198 struct page_cgroup
*pc
;
2199 struct mem_cgroup
*mem
= NULL
;
2202 if (mem_cgroup_disabled())
2205 pc
= lookup_page_cgroup(page
);
2206 lock_page_cgroup(pc
);
2207 if (PageCgroupUsed(pc
)) {
2208 mem
= pc
->mem_cgroup
;
2211 unlock_page_cgroup(pc
);
2214 ret
= __mem_cgroup_try_charge(NULL
, GFP_KERNEL
, &mem
, false,
2222 /* remove redundant charge if migration failed*/
2223 void mem_cgroup_end_migration(struct mem_cgroup
*mem
,
2224 struct page
*oldpage
, struct page
*newpage
)
2226 struct page
*target
, *unused
;
2227 struct page_cgroup
*pc
;
2228 enum charge_type ctype
;
2232 cgroup_exclude_rmdir(&mem
->css
);
2233 /* at migration success, oldpage->mapping is NULL. */
2234 if (oldpage
->mapping
) {
2242 if (PageAnon(target
))
2243 ctype
= MEM_CGROUP_CHARGE_TYPE_MAPPED
;
2244 else if (page_is_file_cache(target
))
2245 ctype
= MEM_CGROUP_CHARGE_TYPE_CACHE
;
2247 ctype
= MEM_CGROUP_CHARGE_TYPE_SHMEM
;
2249 /* unused page is not on radix-tree now. */
2251 __mem_cgroup_uncharge_common(unused
, ctype
);
2253 pc
= lookup_page_cgroup(target
);
2255 * __mem_cgroup_commit_charge() check PCG_USED bit of page_cgroup.
2256 * So, double-counting is effectively avoided.
2258 __mem_cgroup_commit_charge(mem
, pc
, ctype
);
2261 * Both of oldpage and newpage are still under lock_page().
2262 * Then, we don't have to care about race in radix-tree.
2263 * But we have to be careful that this page is unmapped or not.
2265 * There is a case for !page_mapped(). At the start of
2266 * migration, oldpage was mapped. But now, it's zapped.
2267 * But we know *target* page is not freed/reused under us.
2268 * mem_cgroup_uncharge_page() does all necessary checks.
2270 if (ctype
== MEM_CGROUP_CHARGE_TYPE_MAPPED
)
2271 mem_cgroup_uncharge_page(target
);
2273 * At migration, we may charge account against cgroup which has no tasks
2274 * So, rmdir()->pre_destroy() can be called while we do this charge.
2275 * In that case, we need to call pre_destroy() again. check it here.
2277 cgroup_release_and_wakeup_rmdir(&mem
->css
);
2281 * A call to try to shrink memory usage on charge failure at shmem's swapin.
2282 * Calling hierarchical_reclaim is not enough because we should update
2283 * last_oom_jiffies to prevent pagefault_out_of_memory from invoking global OOM.
2284 * Moreover considering hierarchy, we should reclaim from the mem_over_limit,
2285 * not from the memcg which this page would be charged to.
2286 * try_charge_swapin does all of these works properly.
2288 int mem_cgroup_shmem_charge_fallback(struct page
*page
,
2289 struct mm_struct
*mm
,
2292 struct mem_cgroup
*mem
= NULL
;
2295 if (mem_cgroup_disabled())
2298 ret
= mem_cgroup_try_charge_swapin(mm
, page
, gfp_mask
, &mem
);
2300 mem_cgroup_cancel_charge_swapin(mem
); /* it does !mem check */
2305 static DEFINE_MUTEX(set_limit_mutex
);
2307 static int mem_cgroup_resize_limit(struct mem_cgroup
*memcg
,
2308 unsigned long long val
)
2314 int children
= mem_cgroup_count_children(memcg
);
2315 u64 curusage
, oldusage
;
2318 * For keeping hierarchical_reclaim simple, how long we should retry
2319 * is depends on callers. We set our retry-count to be function
2320 * of # of children which we should visit in this loop.
2322 retry_count
= MEM_CGROUP_RECLAIM_RETRIES
* children
;
2324 oldusage
= res_counter_read_u64(&memcg
->res
, RES_USAGE
);
2326 while (retry_count
) {
2327 if (signal_pending(current
)) {
2332 * Rather than hide all in some function, I do this in
2333 * open coded manner. You see what this really does.
2334 * We have to guarantee mem->res.limit < mem->memsw.limit.
2336 mutex_lock(&set_limit_mutex
);
2337 memswlimit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
2338 if (memswlimit
< val
) {
2340 mutex_unlock(&set_limit_mutex
);
2343 ret
= res_counter_set_limit(&memcg
->res
, val
);
2345 if (memswlimit
== val
)
2346 memcg
->memsw_is_minimum
= true;
2348 memcg
->memsw_is_minimum
= false;
2350 mutex_unlock(&set_limit_mutex
);
2355 progress
= mem_cgroup_hierarchical_reclaim(memcg
, NULL
,
2357 MEM_CGROUP_RECLAIM_SHRINK
);
2358 curusage
= res_counter_read_u64(&memcg
->res
, RES_USAGE
);
2359 /* Usage is reduced ? */
2360 if (curusage
>= oldusage
)
2363 oldusage
= curusage
;
2369 static int mem_cgroup_resize_memsw_limit(struct mem_cgroup
*memcg
,
2370 unsigned long long val
)
2373 u64 memlimit
, oldusage
, curusage
;
2374 int children
= mem_cgroup_count_children(memcg
);
2377 /* see mem_cgroup_resize_res_limit */
2378 retry_count
= children
* MEM_CGROUP_RECLAIM_RETRIES
;
2379 oldusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
2380 while (retry_count
) {
2381 if (signal_pending(current
)) {
2386 * Rather than hide all in some function, I do this in
2387 * open coded manner. You see what this really does.
2388 * We have to guarantee mem->res.limit < mem->memsw.limit.
2390 mutex_lock(&set_limit_mutex
);
2391 memlimit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
2392 if (memlimit
> val
) {
2394 mutex_unlock(&set_limit_mutex
);
2397 ret
= res_counter_set_limit(&memcg
->memsw
, val
);
2399 if (memlimit
== val
)
2400 memcg
->memsw_is_minimum
= true;
2402 memcg
->memsw_is_minimum
= false;
2404 mutex_unlock(&set_limit_mutex
);
2409 mem_cgroup_hierarchical_reclaim(memcg
, NULL
, GFP_KERNEL
,
2410 MEM_CGROUP_RECLAIM_NOSWAP
|
2411 MEM_CGROUP_RECLAIM_SHRINK
);
2412 curusage
= res_counter_read_u64(&memcg
->memsw
, RES_USAGE
);
2413 /* Usage is reduced ? */
2414 if (curusage
>= oldusage
)
2417 oldusage
= curusage
;
2422 unsigned long mem_cgroup_soft_limit_reclaim(struct zone
*zone
, int order
,
2423 gfp_t gfp_mask
, int nid
,
2426 unsigned long nr_reclaimed
= 0;
2427 struct mem_cgroup_per_zone
*mz
, *next_mz
= NULL
;
2428 unsigned long reclaimed
;
2430 struct mem_cgroup_tree_per_zone
*mctz
;
2431 unsigned long long excess
;
2436 mctz
= soft_limit_tree_node_zone(nid
, zid
);
2438 * This loop can run a while, specially if mem_cgroup's continuously
2439 * keep exceeding their soft limit and putting the system under
2446 mz
= mem_cgroup_largest_soft_limit_node(mctz
);
2450 reclaimed
= mem_cgroup_hierarchical_reclaim(mz
->mem
, zone
,
2452 MEM_CGROUP_RECLAIM_SOFT
);
2453 nr_reclaimed
+= reclaimed
;
2454 spin_lock(&mctz
->lock
);
2457 * If we failed to reclaim anything from this memory cgroup
2458 * it is time to move on to the next cgroup
2464 * Loop until we find yet another one.
2466 * By the time we get the soft_limit lock
2467 * again, someone might have aded the
2468 * group back on the RB tree. Iterate to
2469 * make sure we get a different mem.
2470 * mem_cgroup_largest_soft_limit_node returns
2471 * NULL if no other cgroup is present on
2475 __mem_cgroup_largest_soft_limit_node(mctz
);
2476 if (next_mz
== mz
) {
2477 css_put(&next_mz
->mem
->css
);
2479 } else /* next_mz == NULL or other memcg */
2483 __mem_cgroup_remove_exceeded(mz
->mem
, mz
, mctz
);
2484 excess
= res_counter_soft_limit_excess(&mz
->mem
->res
);
2486 * One school of thought says that we should not add
2487 * back the node to the tree if reclaim returns 0.
2488 * But our reclaim could return 0, simply because due
2489 * to priority we are exposing a smaller subset of
2490 * memory to reclaim from. Consider this as a longer
2493 /* If excess == 0, no tree ops */
2494 __mem_cgroup_insert_exceeded(mz
->mem
, mz
, mctz
, excess
);
2495 spin_unlock(&mctz
->lock
);
2496 css_put(&mz
->mem
->css
);
2499 * Could not reclaim anything and there are no more
2500 * mem cgroups to try or we seem to be looping without
2501 * reclaiming anything.
2503 if (!nr_reclaimed
&&
2505 loop
> MEM_CGROUP_MAX_SOFT_LIMIT_RECLAIM_LOOPS
))
2507 } while (!nr_reclaimed
);
2509 css_put(&next_mz
->mem
->css
);
2510 return nr_reclaimed
;
2514 * This routine traverse page_cgroup in given list and drop them all.
2515 * *And* this routine doesn't reclaim page itself, just removes page_cgroup.
2517 static int mem_cgroup_force_empty_list(struct mem_cgroup
*mem
,
2518 int node
, int zid
, enum lru_list lru
)
2521 struct mem_cgroup_per_zone
*mz
;
2522 struct page_cgroup
*pc
, *busy
;
2523 unsigned long flags
, loop
;
2524 struct list_head
*list
;
2527 zone
= &NODE_DATA(node
)->node_zones
[zid
];
2528 mz
= mem_cgroup_zoneinfo(mem
, node
, zid
);
2529 list
= &mz
->lists
[lru
];
2531 loop
= MEM_CGROUP_ZSTAT(mz
, lru
);
2532 /* give some margin against EBUSY etc...*/
2537 spin_lock_irqsave(&zone
->lru_lock
, flags
);
2538 if (list_empty(list
)) {
2539 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
2542 pc
= list_entry(list
->prev
, struct page_cgroup
, lru
);
2544 list_move(&pc
->lru
, list
);
2546 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
2549 spin_unlock_irqrestore(&zone
->lru_lock
, flags
);
2551 ret
= mem_cgroup_move_parent(pc
, mem
, GFP_KERNEL
);
2555 if (ret
== -EBUSY
|| ret
== -EINVAL
) {
2556 /* found lock contention or "pc" is obsolete. */
2563 if (!ret
&& !list_empty(list
))
2569 * make mem_cgroup's charge to be 0 if there is no task.
2570 * This enables deleting this mem_cgroup.
2572 static int mem_cgroup_force_empty(struct mem_cgroup
*mem
, bool free_all
)
2575 int node
, zid
, shrink
;
2576 int nr_retries
= MEM_CGROUP_RECLAIM_RETRIES
;
2577 struct cgroup
*cgrp
= mem
->css
.cgroup
;
2582 /* should free all ? */
2586 while (mem
->res
.usage
> 0) {
2588 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
))
2591 if (signal_pending(current
))
2593 /* This is for making all *used* pages to be on LRU. */
2594 lru_add_drain_all();
2595 drain_all_stock_sync();
2597 for_each_node_state(node
, N_HIGH_MEMORY
) {
2598 for (zid
= 0; !ret
&& zid
< MAX_NR_ZONES
; zid
++) {
2601 ret
= mem_cgroup_force_empty_list(mem
,
2610 /* it seems parent cgroup doesn't have enough mem */
2621 /* returns EBUSY if there is a task or if we come here twice. */
2622 if (cgroup_task_count(cgrp
) || !list_empty(&cgrp
->children
) || shrink
) {
2626 /* we call try-to-free pages for make this cgroup empty */
2627 lru_add_drain_all();
2628 /* try to free all pages in this cgroup */
2630 while (nr_retries
&& mem
->res
.usage
> 0) {
2633 if (signal_pending(current
)) {
2637 progress
= try_to_free_mem_cgroup_pages(mem
, GFP_KERNEL
,
2638 false, get_swappiness(mem
));
2641 /* maybe some writeback is necessary */
2642 congestion_wait(BLK_RW_ASYNC
, HZ
/10);
2647 /* try move_account...there may be some *locked* pages. */
2654 int mem_cgroup_force_empty_write(struct cgroup
*cont
, unsigned int event
)
2656 return mem_cgroup_force_empty(mem_cgroup_from_cont(cont
), true);
2660 static u64
mem_cgroup_hierarchy_read(struct cgroup
*cont
, struct cftype
*cft
)
2662 return mem_cgroup_from_cont(cont
)->use_hierarchy
;
2665 static int mem_cgroup_hierarchy_write(struct cgroup
*cont
, struct cftype
*cft
,
2669 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
2670 struct cgroup
*parent
= cont
->parent
;
2671 struct mem_cgroup
*parent_mem
= NULL
;
2674 parent_mem
= mem_cgroup_from_cont(parent
);
2678 * If parent's use_hierarchy is set, we can't make any modifications
2679 * in the child subtrees. If it is unset, then the change can
2680 * occur, provided the current cgroup has no children.
2682 * For the root cgroup, parent_mem is NULL, we allow value to be
2683 * set if there are no children.
2685 if ((!parent_mem
|| !parent_mem
->use_hierarchy
) &&
2686 (val
== 1 || val
== 0)) {
2687 if (list_empty(&cont
->children
))
2688 mem
->use_hierarchy
= val
;
2698 struct mem_cgroup_idx_data
{
2700 enum mem_cgroup_stat_index idx
;
2704 mem_cgroup_get_idx_stat(struct mem_cgroup
*mem
, void *data
)
2706 struct mem_cgroup_idx_data
*d
= data
;
2707 d
->val
+= mem_cgroup_read_stat(&mem
->stat
, d
->idx
);
2712 mem_cgroup_get_recursive_idx_stat(struct mem_cgroup
*mem
,
2713 enum mem_cgroup_stat_index idx
, s64
*val
)
2715 struct mem_cgroup_idx_data d
;
2718 mem_cgroup_walk_tree(mem
, &d
, mem_cgroup_get_idx_stat
);
2722 static u64
mem_cgroup_read(struct cgroup
*cont
, struct cftype
*cft
)
2724 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
2728 type
= MEMFILE_TYPE(cft
->private);
2729 name
= MEMFILE_ATTR(cft
->private);
2732 if (name
== RES_USAGE
&& mem_cgroup_is_root(mem
)) {
2733 mem_cgroup_get_recursive_idx_stat(mem
,
2734 MEM_CGROUP_STAT_CACHE
, &idx_val
);
2736 mem_cgroup_get_recursive_idx_stat(mem
,
2737 MEM_CGROUP_STAT_RSS
, &idx_val
);
2741 val
= res_counter_read_u64(&mem
->res
, name
);
2744 if (name
== RES_USAGE
&& mem_cgroup_is_root(mem
)) {
2745 mem_cgroup_get_recursive_idx_stat(mem
,
2746 MEM_CGROUP_STAT_CACHE
, &idx_val
);
2748 mem_cgroup_get_recursive_idx_stat(mem
,
2749 MEM_CGROUP_STAT_RSS
, &idx_val
);
2751 mem_cgroup_get_recursive_idx_stat(mem
,
2752 MEM_CGROUP_STAT_SWAPOUT
, &idx_val
);
2756 val
= res_counter_read_u64(&mem
->memsw
, name
);
2765 * The user of this function is...
2768 static int mem_cgroup_write(struct cgroup
*cont
, struct cftype
*cft
,
2771 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cont
);
2773 unsigned long long val
;
2776 type
= MEMFILE_TYPE(cft
->private);
2777 name
= MEMFILE_ATTR(cft
->private);
2780 if (mem_cgroup_is_root(memcg
)) { /* Can't set limit on root */
2784 /* This function does all necessary parse...reuse it */
2785 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
2789 ret
= mem_cgroup_resize_limit(memcg
, val
);
2791 ret
= mem_cgroup_resize_memsw_limit(memcg
, val
);
2793 case RES_SOFT_LIMIT
:
2794 ret
= res_counter_memparse_write_strategy(buffer
, &val
);
2798 * For memsw, soft limits are hard to implement in terms
2799 * of semantics, for now, we support soft limits for
2800 * control without swap
2803 ret
= res_counter_set_soft_limit(&memcg
->res
, val
);
2808 ret
= -EINVAL
; /* should be BUG() ? */
2814 static void memcg_get_hierarchical_limit(struct mem_cgroup
*memcg
,
2815 unsigned long long *mem_limit
, unsigned long long *memsw_limit
)
2817 struct cgroup
*cgroup
;
2818 unsigned long long min_limit
, min_memsw_limit
, tmp
;
2820 min_limit
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
2821 min_memsw_limit
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
2822 cgroup
= memcg
->css
.cgroup
;
2823 if (!memcg
->use_hierarchy
)
2826 while (cgroup
->parent
) {
2827 cgroup
= cgroup
->parent
;
2828 memcg
= mem_cgroup_from_cont(cgroup
);
2829 if (!memcg
->use_hierarchy
)
2831 tmp
= res_counter_read_u64(&memcg
->res
, RES_LIMIT
);
2832 min_limit
= min(min_limit
, tmp
);
2833 tmp
= res_counter_read_u64(&memcg
->memsw
, RES_LIMIT
);
2834 min_memsw_limit
= min(min_memsw_limit
, tmp
);
2837 *mem_limit
= min_limit
;
2838 *memsw_limit
= min_memsw_limit
;
2842 static int mem_cgroup_reset(struct cgroup
*cont
, unsigned int event
)
2844 struct mem_cgroup
*mem
;
2847 mem
= mem_cgroup_from_cont(cont
);
2848 type
= MEMFILE_TYPE(event
);
2849 name
= MEMFILE_ATTR(event
);
2853 res_counter_reset_max(&mem
->res
);
2855 res_counter_reset_max(&mem
->memsw
);
2859 res_counter_reset_failcnt(&mem
->res
);
2861 res_counter_reset_failcnt(&mem
->memsw
);
2869 /* For read statistics */
2885 struct mcs_total_stat
{
2886 s64 stat
[NR_MCS_STAT
];
2892 } memcg_stat_strings
[NR_MCS_STAT
] = {
2893 {"cache", "total_cache"},
2894 {"rss", "total_rss"},
2895 {"mapped_file", "total_mapped_file"},
2896 {"pgpgin", "total_pgpgin"},
2897 {"pgpgout", "total_pgpgout"},
2898 {"swap", "total_swap"},
2899 {"inactive_anon", "total_inactive_anon"},
2900 {"active_anon", "total_active_anon"},
2901 {"inactive_file", "total_inactive_file"},
2902 {"active_file", "total_active_file"},
2903 {"unevictable", "total_unevictable"}
2907 static int mem_cgroup_get_local_stat(struct mem_cgroup
*mem
, void *data
)
2909 struct mcs_total_stat
*s
= data
;
2913 val
= mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_CACHE
);
2914 s
->stat
[MCS_CACHE
] += val
* PAGE_SIZE
;
2915 val
= mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_RSS
);
2916 s
->stat
[MCS_RSS
] += val
* PAGE_SIZE
;
2917 val
= mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_FILE_MAPPED
);
2918 s
->stat
[MCS_FILE_MAPPED
] += val
* PAGE_SIZE
;
2919 val
= mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_PGPGIN_COUNT
);
2920 s
->stat
[MCS_PGPGIN
] += val
;
2921 val
= mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_PGPGOUT_COUNT
);
2922 s
->stat
[MCS_PGPGOUT
] += val
;
2923 if (do_swap_account
) {
2924 val
= mem_cgroup_read_stat(&mem
->stat
, MEM_CGROUP_STAT_SWAPOUT
);
2925 s
->stat
[MCS_SWAP
] += val
* PAGE_SIZE
;
2929 val
= mem_cgroup_get_local_zonestat(mem
, LRU_INACTIVE_ANON
);
2930 s
->stat
[MCS_INACTIVE_ANON
] += val
* PAGE_SIZE
;
2931 val
= mem_cgroup_get_local_zonestat(mem
, LRU_ACTIVE_ANON
);
2932 s
->stat
[MCS_ACTIVE_ANON
] += val
* PAGE_SIZE
;
2933 val
= mem_cgroup_get_local_zonestat(mem
, LRU_INACTIVE_FILE
);
2934 s
->stat
[MCS_INACTIVE_FILE
] += val
* PAGE_SIZE
;
2935 val
= mem_cgroup_get_local_zonestat(mem
, LRU_ACTIVE_FILE
);
2936 s
->stat
[MCS_ACTIVE_FILE
] += val
* PAGE_SIZE
;
2937 val
= mem_cgroup_get_local_zonestat(mem
, LRU_UNEVICTABLE
);
2938 s
->stat
[MCS_UNEVICTABLE
] += val
* PAGE_SIZE
;
2943 mem_cgroup_get_total_stat(struct mem_cgroup
*mem
, struct mcs_total_stat
*s
)
2945 mem_cgroup_walk_tree(mem
, s
, mem_cgroup_get_local_stat
);
2948 static int mem_control_stat_show(struct cgroup
*cont
, struct cftype
*cft
,
2949 struct cgroup_map_cb
*cb
)
2951 struct mem_cgroup
*mem_cont
= mem_cgroup_from_cont(cont
);
2952 struct mcs_total_stat mystat
;
2955 memset(&mystat
, 0, sizeof(mystat
));
2956 mem_cgroup_get_local_stat(mem_cont
, &mystat
);
2958 for (i
= 0; i
< NR_MCS_STAT
; i
++) {
2959 if (i
== MCS_SWAP
&& !do_swap_account
)
2961 cb
->fill(cb
, memcg_stat_strings
[i
].local_name
, mystat
.stat
[i
]);
2964 /* Hierarchical information */
2966 unsigned long long limit
, memsw_limit
;
2967 memcg_get_hierarchical_limit(mem_cont
, &limit
, &memsw_limit
);
2968 cb
->fill(cb
, "hierarchical_memory_limit", limit
);
2969 if (do_swap_account
)
2970 cb
->fill(cb
, "hierarchical_memsw_limit", memsw_limit
);
2973 memset(&mystat
, 0, sizeof(mystat
));
2974 mem_cgroup_get_total_stat(mem_cont
, &mystat
);
2975 for (i
= 0; i
< NR_MCS_STAT
; i
++) {
2976 if (i
== MCS_SWAP
&& !do_swap_account
)
2978 cb
->fill(cb
, memcg_stat_strings
[i
].total_name
, mystat
.stat
[i
]);
2981 #ifdef CONFIG_DEBUG_VM
2982 cb
->fill(cb
, "inactive_ratio", calc_inactive_ratio(mem_cont
, NULL
));
2986 struct mem_cgroup_per_zone
*mz
;
2987 unsigned long recent_rotated
[2] = {0, 0};
2988 unsigned long recent_scanned
[2] = {0, 0};
2990 for_each_online_node(nid
)
2991 for (zid
= 0; zid
< MAX_NR_ZONES
; zid
++) {
2992 mz
= mem_cgroup_zoneinfo(mem_cont
, nid
, zid
);
2994 recent_rotated
[0] +=
2995 mz
->reclaim_stat
.recent_rotated
[0];
2996 recent_rotated
[1] +=
2997 mz
->reclaim_stat
.recent_rotated
[1];
2998 recent_scanned
[0] +=
2999 mz
->reclaim_stat
.recent_scanned
[0];
3000 recent_scanned
[1] +=
3001 mz
->reclaim_stat
.recent_scanned
[1];
3003 cb
->fill(cb
, "recent_rotated_anon", recent_rotated
[0]);
3004 cb
->fill(cb
, "recent_rotated_file", recent_rotated
[1]);
3005 cb
->fill(cb
, "recent_scanned_anon", recent_scanned
[0]);
3006 cb
->fill(cb
, "recent_scanned_file", recent_scanned
[1]);
3013 static u64
mem_cgroup_swappiness_read(struct cgroup
*cgrp
, struct cftype
*cft
)
3015 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
3017 return get_swappiness(memcg
);
3020 static int mem_cgroup_swappiness_write(struct cgroup
*cgrp
, struct cftype
*cft
,
3023 struct mem_cgroup
*memcg
= mem_cgroup_from_cont(cgrp
);
3024 struct mem_cgroup
*parent
;
3029 if (cgrp
->parent
== NULL
)
3032 parent
= mem_cgroup_from_cont(cgrp
->parent
);
3036 /* If under hierarchy, only empty-root can set this value */
3037 if ((parent
->use_hierarchy
) ||
3038 (memcg
->use_hierarchy
&& !list_empty(&cgrp
->children
))) {
3043 spin_lock(&memcg
->reclaim_param_lock
);
3044 memcg
->swappiness
= val
;
3045 spin_unlock(&memcg
->reclaim_param_lock
);
3053 static struct cftype mem_cgroup_files
[] = {
3055 .name
= "usage_in_bytes",
3056 .private = MEMFILE_PRIVATE(_MEM
, RES_USAGE
),
3057 .read_u64
= mem_cgroup_read
,
3060 .name
= "max_usage_in_bytes",
3061 .private = MEMFILE_PRIVATE(_MEM
, RES_MAX_USAGE
),
3062 .trigger
= mem_cgroup_reset
,
3063 .read_u64
= mem_cgroup_read
,
3066 .name
= "limit_in_bytes",
3067 .private = MEMFILE_PRIVATE(_MEM
, RES_LIMIT
),
3068 .write_string
= mem_cgroup_write
,
3069 .read_u64
= mem_cgroup_read
,
3072 .name
= "soft_limit_in_bytes",
3073 .private = MEMFILE_PRIVATE(_MEM
, RES_SOFT_LIMIT
),
3074 .write_string
= mem_cgroup_write
,
3075 .read_u64
= mem_cgroup_read
,
3079 .private = MEMFILE_PRIVATE(_MEM
, RES_FAILCNT
),
3080 .trigger
= mem_cgroup_reset
,
3081 .read_u64
= mem_cgroup_read
,
3085 .read_map
= mem_control_stat_show
,
3088 .name
= "force_empty",
3089 .trigger
= mem_cgroup_force_empty_write
,
3092 .name
= "use_hierarchy",
3093 .write_u64
= mem_cgroup_hierarchy_write
,
3094 .read_u64
= mem_cgroup_hierarchy_read
,
3097 .name
= "swappiness",
3098 .read_u64
= mem_cgroup_swappiness_read
,
3099 .write_u64
= mem_cgroup_swappiness_write
,
3103 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3104 static struct cftype memsw_cgroup_files
[] = {
3106 .name
= "memsw.usage_in_bytes",
3107 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_USAGE
),
3108 .read_u64
= mem_cgroup_read
,
3111 .name
= "memsw.max_usage_in_bytes",
3112 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_MAX_USAGE
),
3113 .trigger
= mem_cgroup_reset
,
3114 .read_u64
= mem_cgroup_read
,
3117 .name
= "memsw.limit_in_bytes",
3118 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_LIMIT
),
3119 .write_string
= mem_cgroup_write
,
3120 .read_u64
= mem_cgroup_read
,
3123 .name
= "memsw.failcnt",
3124 .private = MEMFILE_PRIVATE(_MEMSWAP
, RES_FAILCNT
),
3125 .trigger
= mem_cgroup_reset
,
3126 .read_u64
= mem_cgroup_read
,
3130 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
3132 if (!do_swap_account
)
3134 return cgroup_add_files(cont
, ss
, memsw_cgroup_files
,
3135 ARRAY_SIZE(memsw_cgroup_files
));
3138 static int register_memsw_files(struct cgroup
*cont
, struct cgroup_subsys
*ss
)
3144 static int alloc_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
3146 struct mem_cgroup_per_node
*pn
;
3147 struct mem_cgroup_per_zone
*mz
;
3149 int zone
, tmp
= node
;
3151 * This routine is called against possible nodes.
3152 * But it's BUG to call kmalloc() against offline node.
3154 * TODO: this routine can waste much memory for nodes which will
3155 * never be onlined. It's better to use memory hotplug callback
3158 if (!node_state(node
, N_NORMAL_MEMORY
))
3160 pn
= kmalloc_node(sizeof(*pn
), GFP_KERNEL
, tmp
);
3164 mem
->info
.nodeinfo
[node
] = pn
;
3165 memset(pn
, 0, sizeof(*pn
));
3167 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
3168 mz
= &pn
->zoneinfo
[zone
];
3170 INIT_LIST_HEAD(&mz
->lists
[l
]);
3171 mz
->usage_in_excess
= 0;
3172 mz
->on_tree
= false;
3178 static void free_mem_cgroup_per_zone_info(struct mem_cgroup
*mem
, int node
)
3180 kfree(mem
->info
.nodeinfo
[node
]);
3183 static int mem_cgroup_size(void)
3185 int cpustat_size
= nr_cpu_ids
* sizeof(struct mem_cgroup_stat_cpu
);
3186 return sizeof(struct mem_cgroup
) + cpustat_size
;
3189 static struct mem_cgroup
*mem_cgroup_alloc(void)
3191 struct mem_cgroup
*mem
;
3192 int size
= mem_cgroup_size();
3194 if (size
< PAGE_SIZE
)
3195 mem
= kmalloc(size
, GFP_KERNEL
);
3197 mem
= vmalloc(size
);
3200 memset(mem
, 0, size
);
3205 * At destroying mem_cgroup, references from swap_cgroup can remain.
3206 * (scanning all at force_empty is too costly...)
3208 * Instead of clearing all references at force_empty, we remember
3209 * the number of reference from swap_cgroup and free mem_cgroup when
3210 * it goes down to 0.
3212 * Removal of cgroup itself succeeds regardless of refs from swap.
3215 static void __mem_cgroup_free(struct mem_cgroup
*mem
)
3219 mem_cgroup_remove_from_trees(mem
);
3220 free_css_id(&mem_cgroup_subsys
, &mem
->css
);
3222 for_each_node_state(node
, N_POSSIBLE
)
3223 free_mem_cgroup_per_zone_info(mem
, node
);
3225 if (mem_cgroup_size() < PAGE_SIZE
)
3231 static void mem_cgroup_get(struct mem_cgroup
*mem
)
3233 atomic_inc(&mem
->refcnt
);
3236 static void mem_cgroup_put(struct mem_cgroup
*mem
)
3238 if (atomic_dec_and_test(&mem
->refcnt
)) {
3239 struct mem_cgroup
*parent
= parent_mem_cgroup(mem
);
3240 __mem_cgroup_free(mem
);
3242 mem_cgroup_put(parent
);
3247 * Returns the parent mem_cgroup in memcgroup hierarchy with hierarchy enabled.
3249 static struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*mem
)
3251 if (!mem
->res
.parent
)
3253 return mem_cgroup_from_res_counter(mem
->res
.parent
, res
);
3256 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3257 static void __init
enable_swap_cgroup(void)
3259 if (!mem_cgroup_disabled() && really_do_swap_account
)
3260 do_swap_account
= 1;
3263 static void __init
enable_swap_cgroup(void)
3268 static int mem_cgroup_soft_limit_tree_init(void)
3270 struct mem_cgroup_tree_per_node
*rtpn
;
3271 struct mem_cgroup_tree_per_zone
*rtpz
;
3272 int tmp
, node
, zone
;
3274 for_each_node_state(node
, N_POSSIBLE
) {
3276 if (!node_state(node
, N_NORMAL_MEMORY
))
3278 rtpn
= kzalloc_node(sizeof(*rtpn
), GFP_KERNEL
, tmp
);
3282 soft_limit_tree
.rb_tree_per_node
[node
] = rtpn
;
3284 for (zone
= 0; zone
< MAX_NR_ZONES
; zone
++) {
3285 rtpz
= &rtpn
->rb_tree_per_zone
[zone
];
3286 rtpz
->rb_root
= RB_ROOT
;
3287 spin_lock_init(&rtpz
->lock
);
3293 static struct cgroup_subsys_state
* __ref
3294 mem_cgroup_create(struct cgroup_subsys
*ss
, struct cgroup
*cont
)
3296 struct mem_cgroup
*mem
, *parent
;
3297 long error
= -ENOMEM
;
3300 mem
= mem_cgroup_alloc();
3302 return ERR_PTR(error
);
3304 for_each_node_state(node
, N_POSSIBLE
)
3305 if (alloc_mem_cgroup_per_zone_info(mem
, node
))
3309 if (cont
->parent
== NULL
) {
3311 enable_swap_cgroup();
3313 root_mem_cgroup
= mem
;
3314 if (mem_cgroup_soft_limit_tree_init())
3316 for_each_possible_cpu(cpu
) {
3317 struct memcg_stock_pcp
*stock
=
3318 &per_cpu(memcg_stock
, cpu
);
3319 INIT_WORK(&stock
->work
, drain_local_stock
);
3321 hotcpu_notifier(memcg_stock_cpu_callback
, 0);
3324 parent
= mem_cgroup_from_cont(cont
->parent
);
3325 mem
->use_hierarchy
= parent
->use_hierarchy
;
3328 if (parent
&& parent
->use_hierarchy
) {
3329 res_counter_init(&mem
->res
, &parent
->res
);
3330 res_counter_init(&mem
->memsw
, &parent
->memsw
);
3332 * We increment refcnt of the parent to ensure that we can
3333 * safely access it on res_counter_charge/uncharge.
3334 * This refcnt will be decremented when freeing this
3335 * mem_cgroup(see mem_cgroup_put).
3337 mem_cgroup_get(parent
);
3339 res_counter_init(&mem
->res
, NULL
);
3340 res_counter_init(&mem
->memsw
, NULL
);
3342 mem
->last_scanned_child
= 0;
3343 spin_lock_init(&mem
->reclaim_param_lock
);
3346 mem
->swappiness
= get_swappiness(parent
);
3347 atomic_set(&mem
->refcnt
, 1);
3350 __mem_cgroup_free(mem
);
3351 root_mem_cgroup
= NULL
;
3352 return ERR_PTR(error
);
3355 static int mem_cgroup_pre_destroy(struct cgroup_subsys
*ss
,
3356 struct cgroup
*cont
)
3358 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
3360 return mem_cgroup_force_empty(mem
, false);
3363 static void mem_cgroup_destroy(struct cgroup_subsys
*ss
,
3364 struct cgroup
*cont
)
3366 struct mem_cgroup
*mem
= mem_cgroup_from_cont(cont
);
3368 mem_cgroup_put(mem
);
3371 static int mem_cgroup_populate(struct cgroup_subsys
*ss
,
3372 struct cgroup
*cont
)
3376 ret
= cgroup_add_files(cont
, ss
, mem_cgroup_files
,
3377 ARRAY_SIZE(mem_cgroup_files
));
3380 ret
= register_memsw_files(cont
, ss
);
3384 static void mem_cgroup_move_task(struct cgroup_subsys
*ss
,
3385 struct cgroup
*cont
,
3386 struct cgroup
*old_cont
,
3387 struct task_struct
*p
,
3390 mutex_lock(&memcg_tasklist
);
3392 * FIXME: It's better to move charges of this process from old
3393 * memcg to new memcg. But it's just on TODO-List now.
3395 mutex_unlock(&memcg_tasklist
);
3398 struct cgroup_subsys mem_cgroup_subsys
= {
3400 .subsys_id
= mem_cgroup_subsys_id
,
3401 .create
= mem_cgroup_create
,
3402 .pre_destroy
= mem_cgroup_pre_destroy
,
3403 .destroy
= mem_cgroup_destroy
,
3404 .populate
= mem_cgroup_populate
,
3405 .attach
= mem_cgroup_move_task
,
3410 #ifdef CONFIG_CGROUP_MEM_RES_CTLR_SWAP
3412 static int __init
disable_swap_account(char *s
)
3414 really_do_swap_account
= 0;
3417 __setup("noswapaccount", disable_swap_account
);