1 /* memcontrol.h - 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 #ifndef _LINUX_MEMCONTROL_H
21 #define _LINUX_MEMCONTROL_H
22 #include <linux/cgroup.h>
23 #include <linux/vm_event_item.h>
24 #include <linux/hardirq.h>
25 #include <linux/jump_label.h>
34 * The corresponding mem_cgroup_stat_names is defined in mm/memcontrol.c,
35 * These two lists should keep in accord with each other.
37 enum mem_cgroup_stat_index
{
39 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
41 MEM_CGROUP_STAT_CACHE
, /* # of pages charged as cache */
42 MEM_CGROUP_STAT_RSS
, /* # of pages charged as anon rss */
43 MEM_CGROUP_STAT_RSS_HUGE
, /* # of pages charged as anon huge */
44 MEM_CGROUP_STAT_FILE_MAPPED
, /* # of pages charged as file rss */
45 MEM_CGROUP_STAT_WRITEBACK
, /* # of pages under writeback */
46 MEM_CGROUP_STAT_SWAP
, /* # of pages, swapped out */
47 MEM_CGROUP_STAT_NSTATS
,
50 struct mem_cgroup_reclaim_cookie
{
53 unsigned int generation
;
56 enum mem_cgroup_filter_t
{
57 VISIT
, /* visit current node */
58 SKIP
, /* skip the current node and continue traversal */
59 SKIP_TREE
, /* skip the whole subtree and continue traversal */
63 * mem_cgroup_filter_t predicate might instruct mem_cgroup_iter_cond how to
64 * iterate through the hierarchy tree. Each tree element is checked by the
65 * predicate before it is returned by the iterator. If a filter returns
66 * SKIP or SKIP_TREE then the iterator code continues traversal (with the
67 * next node down the hierarchy or the next node that doesn't belong under the
70 typedef enum mem_cgroup_filter_t
71 (*mem_cgroup_iter_filter
)(struct mem_cgroup
*memcg
, struct mem_cgroup
*root
);
75 * All "charge" functions with gfp_mask should use GFP_KERNEL or
76 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
77 * alloc memory but reclaims memory from all available zones. So, "where I want
78 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
79 * available but adding a rule is better. charge functions' gfp_mask should
80 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
82 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
85 extern int mem_cgroup_newpage_charge(struct page
*page
, struct mm_struct
*mm
,
87 /* for swap handling */
88 extern int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
89 struct page
*page
, gfp_t mask
, struct mem_cgroup
**memcgp
);
90 extern void mem_cgroup_commit_charge_swapin(struct page
*page
,
91 struct mem_cgroup
*memcg
);
92 extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*memcg
);
94 extern int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
97 struct lruvec
*mem_cgroup_zone_lruvec(struct zone
*, struct mem_cgroup
*);
98 struct lruvec
*mem_cgroup_page_lruvec(struct page
*, struct zone
*);
100 /* For coalescing uncharge for reducing memcg' overhead*/
101 extern void mem_cgroup_uncharge_start(void);
102 extern void mem_cgroup_uncharge_end(void);
104 extern void mem_cgroup_uncharge_page(struct page
*page
);
105 extern void mem_cgroup_uncharge_cache_page(struct page
*page
);
107 bool __mem_cgroup_same_or_subtree(const struct mem_cgroup
*root_memcg
,
108 struct mem_cgroup
*memcg
);
109 bool task_in_mem_cgroup(struct task_struct
*task
,
110 const struct mem_cgroup
*memcg
);
112 extern struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
);
113 extern struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
);
114 extern struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
);
116 extern struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*memcg
);
117 extern struct mem_cgroup
*mem_cgroup_from_css(struct cgroup_subsys_state
*css
);
120 bool mm_match_cgroup(const struct mm_struct
*mm
, const struct mem_cgroup
*memcg
)
122 struct mem_cgroup
*task_memcg
;
126 task_memcg
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
127 match
= __mem_cgroup_same_or_subtree(memcg
, task_memcg
);
132 extern struct cgroup_subsys_state
*mem_cgroup_css(struct mem_cgroup
*memcg
);
135 mem_cgroup_prepare_migration(struct page
*page
, struct page
*newpage
,
136 struct mem_cgroup
**memcgp
);
137 extern void mem_cgroup_end_migration(struct mem_cgroup
*memcg
,
138 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
);
140 struct mem_cgroup
*mem_cgroup_iter_cond(struct mem_cgroup
*root
,
141 struct mem_cgroup
*prev
,
142 struct mem_cgroup_reclaim_cookie
*reclaim
,
143 mem_cgroup_iter_filter cond
);
145 static inline struct mem_cgroup
*mem_cgroup_iter(struct mem_cgroup
*root
,
146 struct mem_cgroup
*prev
,
147 struct mem_cgroup_reclaim_cookie
*reclaim
)
149 return mem_cgroup_iter_cond(root
, prev
, reclaim
, NULL
);
152 void mem_cgroup_iter_break(struct mem_cgroup
*, struct mem_cgroup
*);
155 * For memory reclaim.
157 int mem_cgroup_inactive_anon_is_low(struct lruvec
*lruvec
);
158 int mem_cgroup_select_victim_node(struct mem_cgroup
*memcg
);
159 unsigned long mem_cgroup_get_lru_size(struct lruvec
*lruvec
, enum lru_list
);
160 void mem_cgroup_update_lru_size(struct lruvec
*, enum lru_list
, int);
161 extern void mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
,
162 struct task_struct
*p
);
163 extern void mem_cgroup_replace_page_cache(struct page
*oldpage
,
164 struct page
*newpage
);
167 * mem_cgroup_toggle_oom - toggle the memcg OOM killer for the current task
168 * @new: true to enable, false to disable
170 * Toggle whether a failed memcg charge should invoke the OOM killer
171 * or just return -ENOMEM. Returns the previous toggle state.
173 * NOTE: Any path that enables the OOM killer before charging must
174 * call mem_cgroup_oom_synchronize() afterward to finalize the
175 * OOM handling and clean up.
177 static inline bool mem_cgroup_toggle_oom(bool new)
181 old
= current
->memcg_oom
.may_oom
;
182 current
->memcg_oom
.may_oom
= new;
187 static inline void mem_cgroup_enable_oom(void)
189 bool old
= mem_cgroup_toggle_oom(true);
191 WARN_ON(old
== true);
194 static inline void mem_cgroup_disable_oom(void)
196 bool old
= mem_cgroup_toggle_oom(false);
198 WARN_ON(old
== false);
201 static inline bool task_in_memcg_oom(struct task_struct
*p
)
203 return p
->memcg_oom
.in_memcg_oom
;
206 bool mem_cgroup_oom_synchronize(void);
208 #ifdef CONFIG_MEMCG_SWAP
209 extern int do_swap_account
;
212 static inline bool mem_cgroup_disabled(void)
214 if (mem_cgroup_subsys
.disabled
)
219 void __mem_cgroup_begin_update_page_stat(struct page
*page
, bool *locked
,
220 unsigned long *flags
);
222 extern atomic_t memcg_moving
;
224 static inline void mem_cgroup_begin_update_page_stat(struct page
*page
,
225 bool *locked
, unsigned long *flags
)
227 if (mem_cgroup_disabled())
231 if (atomic_read(&memcg_moving
))
232 __mem_cgroup_begin_update_page_stat(page
, locked
, flags
);
235 void __mem_cgroup_end_update_page_stat(struct page
*page
,
236 unsigned long *flags
);
237 static inline void mem_cgroup_end_update_page_stat(struct page
*page
,
238 bool *locked
, unsigned long *flags
)
240 if (mem_cgroup_disabled())
243 __mem_cgroup_end_update_page_stat(page
, flags
);
247 void mem_cgroup_update_page_stat(struct page
*page
,
248 enum mem_cgroup_stat_index idx
,
251 static inline void mem_cgroup_inc_page_stat(struct page
*page
,
252 enum mem_cgroup_stat_index idx
)
254 mem_cgroup_update_page_stat(page
, idx
, 1);
257 static inline void mem_cgroup_dec_page_stat(struct page
*page
,
258 enum mem_cgroup_stat_index idx
)
260 mem_cgroup_update_page_stat(page
, idx
, -1);
263 enum mem_cgroup_filter_t
264 mem_cgroup_soft_reclaim_eligible(struct mem_cgroup
*memcg
,
265 struct mem_cgroup
*root
);
267 void __mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
);
268 static inline void mem_cgroup_count_vm_event(struct mm_struct
*mm
,
269 enum vm_event_item idx
)
271 if (mem_cgroup_disabled())
273 __mem_cgroup_count_vm_event(mm
, idx
);
275 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
276 void mem_cgroup_split_huge_fixup(struct page
*head
);
279 #ifdef CONFIG_DEBUG_VM
280 bool mem_cgroup_bad_page_check(struct page
*page
);
281 void mem_cgroup_print_bad_page(struct page
*page
);
283 #else /* CONFIG_MEMCG */
286 static inline int mem_cgroup_newpage_charge(struct page
*page
,
287 struct mm_struct
*mm
, gfp_t gfp_mask
)
292 static inline int mem_cgroup_cache_charge(struct page
*page
,
293 struct mm_struct
*mm
, gfp_t gfp_mask
)
298 static inline int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
299 struct page
*page
, gfp_t gfp_mask
, struct mem_cgroup
**memcgp
)
304 static inline void mem_cgroup_commit_charge_swapin(struct page
*page
,
305 struct mem_cgroup
*memcg
)
309 static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*memcg
)
313 static inline void mem_cgroup_uncharge_start(void)
317 static inline void mem_cgroup_uncharge_end(void)
321 static inline void mem_cgroup_uncharge_page(struct page
*page
)
325 static inline void mem_cgroup_uncharge_cache_page(struct page
*page
)
329 static inline struct lruvec
*mem_cgroup_zone_lruvec(struct zone
*zone
,
330 struct mem_cgroup
*memcg
)
332 return &zone
->lruvec
;
335 static inline struct lruvec
*mem_cgroup_page_lruvec(struct page
*page
,
338 return &zone
->lruvec
;
341 static inline struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
)
346 static inline struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
)
351 static inline bool mm_match_cgroup(struct mm_struct
*mm
,
352 struct mem_cgroup
*memcg
)
357 static inline bool task_in_mem_cgroup(struct task_struct
*task
,
358 const struct mem_cgroup
*memcg
)
363 static inline struct cgroup_subsys_state
364 *mem_cgroup_css(struct mem_cgroup
*memcg
)
370 mem_cgroup_prepare_migration(struct page
*page
, struct page
*newpage
,
371 struct mem_cgroup
**memcgp
)
375 static inline void mem_cgroup_end_migration(struct mem_cgroup
*memcg
,
376 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
)
379 static inline struct mem_cgroup
*
380 mem_cgroup_iter_cond(struct mem_cgroup
*root
,
381 struct mem_cgroup
*prev
,
382 struct mem_cgroup_reclaim_cookie
*reclaim
,
383 mem_cgroup_iter_filter cond
)
385 /* first call must return non-NULL, second return NULL */
386 return (struct mem_cgroup
*)(unsigned long)!prev
;
389 static inline struct mem_cgroup
*
390 mem_cgroup_iter(struct mem_cgroup
*root
,
391 struct mem_cgroup
*prev
,
392 struct mem_cgroup_reclaim_cookie
*reclaim
)
397 static inline void mem_cgroup_iter_break(struct mem_cgroup
*root
,
398 struct mem_cgroup
*prev
)
402 static inline bool mem_cgroup_disabled(void)
408 mem_cgroup_inactive_anon_is_low(struct lruvec
*lruvec
)
413 static inline unsigned long
414 mem_cgroup_get_lru_size(struct lruvec
*lruvec
, enum lru_list lru
)
420 mem_cgroup_update_lru_size(struct lruvec
*lruvec
, enum lru_list lru
,
426 mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
, struct task_struct
*p
)
430 static inline void mem_cgroup_begin_update_page_stat(struct page
*page
,
431 bool *locked
, unsigned long *flags
)
435 static inline void mem_cgroup_end_update_page_stat(struct page
*page
,
436 bool *locked
, unsigned long *flags
)
440 static inline bool mem_cgroup_toggle_oom(bool new)
445 static inline void mem_cgroup_enable_oom(void)
449 static inline void mem_cgroup_disable_oom(void)
453 static inline bool task_in_memcg_oom(struct task_struct
*p
)
458 static inline bool mem_cgroup_oom_synchronize(void)
463 static inline void mem_cgroup_inc_page_stat(struct page
*page
,
464 enum mem_cgroup_stat_index idx
)
468 static inline void mem_cgroup_dec_page_stat(struct page
*page
,
469 enum mem_cgroup_stat_index idx
)
474 enum mem_cgroup_filter_t
475 mem_cgroup_soft_reclaim_eligible(struct mem_cgroup
*memcg
,
476 struct mem_cgroup
*root
)
481 static inline void mem_cgroup_split_huge_fixup(struct page
*head
)
486 void mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
)
489 static inline void mem_cgroup_replace_page_cache(struct page
*oldpage
,
490 struct page
*newpage
)
493 #endif /* CONFIG_MEMCG */
495 #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
497 mem_cgroup_bad_page_check(struct page
*page
)
503 mem_cgroup_print_bad_page(struct page
*page
)
515 #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
516 void sock_update_memcg(struct sock
*sk
);
517 void sock_release_memcg(struct sock
*sk
);
519 static inline void sock_update_memcg(struct sock
*sk
)
522 static inline void sock_release_memcg(struct sock
*sk
)
525 #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
527 #ifdef CONFIG_MEMCG_KMEM
528 extern struct static_key memcg_kmem_enabled_key
;
530 extern int memcg_limited_groups_array_size
;
533 * Helper macro to loop through all memcg-specific caches. Callers must still
534 * check if the cache is valid (it is either valid or NULL).
535 * the slab_mutex must be held when looping through those caches
537 #define for_each_memcg_cache_index(_idx) \
538 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
540 static inline bool memcg_kmem_enabled(void)
542 return static_key_false(&memcg_kmem_enabled_key
);
546 * In general, we'll do everything in our power to not incur in any overhead
547 * for non-memcg users for the kmem functions. Not even a function call, if we
550 * Therefore, we'll inline all those functions so that in the best case, we'll
551 * see that kmemcg is off for everybody and proceed quickly. If it is on,
552 * we'll still do most of the flag checking inline. We check a lot of
553 * conditions, but because they are pretty simple, they are expected to be
556 bool __memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
,
558 void __memcg_kmem_commit_charge(struct page
*page
,
559 struct mem_cgroup
*memcg
, int order
);
560 void __memcg_kmem_uncharge_pages(struct page
*page
, int order
);
562 int memcg_cache_id(struct mem_cgroup
*memcg
);
563 int memcg_register_cache(struct mem_cgroup
*memcg
, struct kmem_cache
*s
,
564 struct kmem_cache
*root_cache
);
565 void memcg_release_cache(struct kmem_cache
*cachep
);
566 void memcg_cache_list_add(struct mem_cgroup
*memcg
, struct kmem_cache
*cachep
);
568 int memcg_update_cache_size(struct kmem_cache
*s
, int num_groups
);
569 void memcg_update_array_size(int num_groups
);
572 __memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
);
574 void mem_cgroup_destroy_cache(struct kmem_cache
*cachep
);
575 void kmem_cache_destroy_memcg_children(struct kmem_cache
*s
);
578 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
579 * @gfp: the gfp allocation flags.
580 * @memcg: a pointer to the memcg this was charged against.
581 * @order: allocation order.
583 * returns true if the memcg where the current task belongs can hold this
586 * We return true automatically if this allocation is not to be accounted to
590 memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
, int order
)
592 if (!memcg_kmem_enabled())
596 * __GFP_NOFAIL allocations will move on even if charging is not
597 * possible. Therefore we don't even try, and have this allocation
598 * unaccounted. We could in theory charge it with
599 * res_counter_charge_nofail, but we hope those allocations are rare,
600 * and won't be worth the trouble.
602 if (!(gfp
& __GFP_KMEMCG
) || (gfp
& __GFP_NOFAIL
))
604 if (in_interrupt() || (!current
->mm
) || (current
->flags
& PF_KTHREAD
))
607 /* If the test is dying, just let it go. */
608 if (unlikely(fatal_signal_pending(current
)))
611 return __memcg_kmem_newpage_charge(gfp
, memcg
, order
);
615 * memcg_kmem_uncharge_pages: uncharge pages from memcg
616 * @page: pointer to struct page being freed
617 * @order: allocation order.
619 * there is no need to specify memcg here, since it is embedded in page_cgroup
622 memcg_kmem_uncharge_pages(struct page
*page
, int order
)
624 if (memcg_kmem_enabled())
625 __memcg_kmem_uncharge_pages(page
, order
);
629 * memcg_kmem_commit_charge: embeds correct memcg in a page
630 * @page: pointer to struct page recently allocated
631 * @memcg: the memcg structure we charged against
632 * @order: allocation order.
634 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
635 * failure of the allocation. if @page is NULL, this function will revert the
636 * charges. Otherwise, it will commit the memcg given by @memcg to the
637 * corresponding page_cgroup.
640 memcg_kmem_commit_charge(struct page
*page
, struct mem_cgroup
*memcg
, int order
)
642 if (memcg_kmem_enabled() && memcg
)
643 __memcg_kmem_commit_charge(page
, memcg
, order
);
647 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
648 * @cachep: the original global kmem cache
649 * @gfp: allocation flags.
651 * This function assumes that the task allocating, which determines the memcg
652 * in the page allocator, belongs to the same cgroup throughout the whole
653 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
654 * while belonging to a cgroup, and later on changes. This is considered
655 * acceptable, and should only happen upon task migration.
657 * Before the cache is created by the memcg core, there is also a possible
658 * imbalance: the task belongs to a memcg, but the cache being allocated from
659 * is the global cache, since the child cache is not yet guaranteed to be
660 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
661 * passed and the page allocator will not attempt any cgroup accounting.
663 static __always_inline
struct kmem_cache
*
664 memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
666 if (!memcg_kmem_enabled())
668 if (gfp
& __GFP_NOFAIL
)
670 if (in_interrupt() || (!current
->mm
) || (current
->flags
& PF_KTHREAD
))
672 if (unlikely(fatal_signal_pending(current
)))
675 return __memcg_kmem_get_cache(cachep
, gfp
);
678 #define for_each_memcg_cache_index(_idx) \
681 static inline bool memcg_kmem_enabled(void)
687 memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
, int order
)
692 static inline void memcg_kmem_uncharge_pages(struct page
*page
, int order
)
697 memcg_kmem_commit_charge(struct page
*page
, struct mem_cgroup
*memcg
, int order
)
701 static inline int memcg_cache_id(struct mem_cgroup
*memcg
)
707 memcg_register_cache(struct mem_cgroup
*memcg
, struct kmem_cache
*s
,
708 struct kmem_cache
*root_cache
)
713 static inline void memcg_release_cache(struct kmem_cache
*cachep
)
717 static inline void memcg_cache_list_add(struct mem_cgroup
*memcg
,
718 struct kmem_cache
*s
)
722 static inline struct kmem_cache
*
723 memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
728 static inline void kmem_cache_destroy_memcg_children(struct kmem_cache
*s
)
731 #endif /* CONFIG_MEMCG_KMEM */
732 #endif /* _LINUX_MEMCONTROL_H */