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>
33 /* Stats that can be updated by kernel. */
34 enum mem_cgroup_page_stat_item
{
35 MEMCG_NR_FILE_MAPPED
, /* # of pages charged as file rss */
38 struct mem_cgroup_reclaim_cookie
{
41 unsigned int generation
;
44 enum mem_cgroup_filter_t
{
45 VISIT
, /* visit current node */
46 SKIP
, /* skip the current node and continue traversal */
47 SKIP_TREE
, /* skip the whole subtree and continue traversal */
51 * mem_cgroup_filter_t predicate might instruct mem_cgroup_iter_cond how to
52 * iterate through the hierarchy tree. Each tree element is checked by the
53 * predicate before it is returned by the iterator. If a filter returns
54 * SKIP or SKIP_TREE then the iterator code continues traversal (with the
55 * next node down the hierarchy or the next node that doesn't belong under the
58 typedef enum mem_cgroup_filter_t
59 (*mem_cgroup_iter_filter
)(struct mem_cgroup
*memcg
, struct mem_cgroup
*root
);
63 * All "charge" functions with gfp_mask should use GFP_KERNEL or
64 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
65 * alloc memory but reclaims memory from all available zones. So, "where I want
66 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
67 * available but adding a rule is better. charge functions' gfp_mask should
68 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
70 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
73 extern int mem_cgroup_newpage_charge(struct page
*page
, struct mm_struct
*mm
,
75 /* for swap handling */
76 extern int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
77 struct page
*page
, gfp_t mask
, struct mem_cgroup
**memcgp
);
78 extern void mem_cgroup_commit_charge_swapin(struct page
*page
,
79 struct mem_cgroup
*memcg
);
80 extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*memcg
);
82 extern int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
85 struct lruvec
*mem_cgroup_zone_lruvec(struct zone
*, struct mem_cgroup
*);
86 struct lruvec
*mem_cgroup_page_lruvec(struct page
*, struct zone
*);
88 /* For coalescing uncharge for reducing memcg' overhead*/
89 extern void mem_cgroup_uncharge_start(void);
90 extern void mem_cgroup_uncharge_end(void);
92 extern void mem_cgroup_uncharge_page(struct page
*page
);
93 extern void mem_cgroup_uncharge_cache_page(struct page
*page
);
95 bool __mem_cgroup_same_or_subtree(const struct mem_cgroup
*root_memcg
,
96 struct mem_cgroup
*memcg
);
97 bool task_in_mem_cgroup(struct task_struct
*task
,
98 const struct mem_cgroup
*memcg
);
100 extern struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
);
101 extern struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
);
102 extern struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
);
104 extern struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*memcg
);
105 extern struct mem_cgroup
*mem_cgroup_from_css(struct cgroup_subsys_state
*css
);
108 bool mm_match_cgroup(const struct mm_struct
*mm
, const struct mem_cgroup
*memcg
)
110 struct mem_cgroup
*task_memcg
;
114 task_memcg
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
115 match
= __mem_cgroup_same_or_subtree(memcg
, task_memcg
);
120 extern struct cgroup_subsys_state
*mem_cgroup_css(struct mem_cgroup
*memcg
);
123 mem_cgroup_prepare_migration(struct page
*page
, struct page
*newpage
,
124 struct mem_cgroup
**memcgp
);
125 extern void mem_cgroup_end_migration(struct mem_cgroup
*memcg
,
126 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
);
128 struct mem_cgroup
*mem_cgroup_iter_cond(struct mem_cgroup
*root
,
129 struct mem_cgroup
*prev
,
130 struct mem_cgroup_reclaim_cookie
*reclaim
,
131 mem_cgroup_iter_filter cond
);
133 static inline struct mem_cgroup
*mem_cgroup_iter(struct mem_cgroup
*root
,
134 struct mem_cgroup
*prev
,
135 struct mem_cgroup_reclaim_cookie
*reclaim
)
137 return mem_cgroup_iter_cond(root
, prev
, reclaim
, NULL
);
140 void mem_cgroup_iter_break(struct mem_cgroup
*, struct mem_cgroup
*);
143 * For memory reclaim.
145 int mem_cgroup_inactive_anon_is_low(struct lruvec
*lruvec
);
146 int mem_cgroup_select_victim_node(struct mem_cgroup
*memcg
);
147 unsigned long mem_cgroup_get_lru_size(struct lruvec
*lruvec
, enum lru_list
);
148 void mem_cgroup_update_lru_size(struct lruvec
*, enum lru_list
, int);
149 extern void mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
,
150 struct task_struct
*p
);
151 extern void mem_cgroup_replace_page_cache(struct page
*oldpage
,
152 struct page
*newpage
);
154 #ifdef CONFIG_MEMCG_SWAP
155 extern int do_swap_account
;
158 static inline bool mem_cgroup_disabled(void)
160 if (mem_cgroup_subsys
.disabled
)
165 void __mem_cgroup_begin_update_page_stat(struct page
*page
, bool *locked
,
166 unsigned long *flags
);
168 extern atomic_t memcg_moving
;
170 static inline void mem_cgroup_begin_update_page_stat(struct page
*page
,
171 bool *locked
, unsigned long *flags
)
173 if (mem_cgroup_disabled())
177 if (atomic_read(&memcg_moving
))
178 __mem_cgroup_begin_update_page_stat(page
, locked
, flags
);
181 void __mem_cgroup_end_update_page_stat(struct page
*page
,
182 unsigned long *flags
);
183 static inline void mem_cgroup_end_update_page_stat(struct page
*page
,
184 bool *locked
, unsigned long *flags
)
186 if (mem_cgroup_disabled())
189 __mem_cgroup_end_update_page_stat(page
, flags
);
193 void mem_cgroup_update_page_stat(struct page
*page
,
194 enum mem_cgroup_page_stat_item idx
,
197 static inline void mem_cgroup_inc_page_stat(struct page
*page
,
198 enum mem_cgroup_page_stat_item idx
)
200 mem_cgroup_update_page_stat(page
, idx
, 1);
203 static inline void mem_cgroup_dec_page_stat(struct page
*page
,
204 enum mem_cgroup_page_stat_item idx
)
206 mem_cgroup_update_page_stat(page
, idx
, -1);
209 enum mem_cgroup_filter_t
210 mem_cgroup_soft_reclaim_eligible(struct mem_cgroup
*memcg
,
211 struct mem_cgroup
*root
);
213 void __mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
);
214 static inline void mem_cgroup_count_vm_event(struct mm_struct
*mm
,
215 enum vm_event_item idx
)
217 if (mem_cgroup_disabled())
219 __mem_cgroup_count_vm_event(mm
, idx
);
221 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
222 void mem_cgroup_split_huge_fixup(struct page
*head
);
225 #ifdef CONFIG_DEBUG_VM
226 bool mem_cgroup_bad_page_check(struct page
*page
);
227 void mem_cgroup_print_bad_page(struct page
*page
);
229 #else /* CONFIG_MEMCG */
232 static inline int mem_cgroup_newpage_charge(struct page
*page
,
233 struct mm_struct
*mm
, gfp_t gfp_mask
)
238 static inline int mem_cgroup_cache_charge(struct page
*page
,
239 struct mm_struct
*mm
, gfp_t gfp_mask
)
244 static inline int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
245 struct page
*page
, gfp_t gfp_mask
, struct mem_cgroup
**memcgp
)
250 static inline void mem_cgroup_commit_charge_swapin(struct page
*page
,
251 struct mem_cgroup
*memcg
)
255 static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*memcg
)
259 static inline void mem_cgroup_uncharge_start(void)
263 static inline void mem_cgroup_uncharge_end(void)
267 static inline void mem_cgroup_uncharge_page(struct page
*page
)
271 static inline void mem_cgroup_uncharge_cache_page(struct page
*page
)
275 static inline struct lruvec
*mem_cgroup_zone_lruvec(struct zone
*zone
,
276 struct mem_cgroup
*memcg
)
278 return &zone
->lruvec
;
281 static inline struct lruvec
*mem_cgroup_page_lruvec(struct page
*page
,
284 return &zone
->lruvec
;
287 static inline struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
)
292 static inline struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
)
297 static inline bool mm_match_cgroup(struct mm_struct
*mm
,
298 struct mem_cgroup
*memcg
)
303 static inline bool task_in_mem_cgroup(struct task_struct
*task
,
304 const struct mem_cgroup
*memcg
)
309 static inline struct cgroup_subsys_state
310 *mem_cgroup_css(struct mem_cgroup
*memcg
)
316 mem_cgroup_prepare_migration(struct page
*page
, struct page
*newpage
,
317 struct mem_cgroup
**memcgp
)
321 static inline void mem_cgroup_end_migration(struct mem_cgroup
*memcg
,
322 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
)
325 static inline struct mem_cgroup
*
326 mem_cgroup_iter_cond(struct mem_cgroup
*root
,
327 struct mem_cgroup
*prev
,
328 struct mem_cgroup_reclaim_cookie
*reclaim
,
329 mem_cgroup_iter_filter cond
)
331 /* first call must return non-NULL, second return NULL */
332 return (struct mem_cgroup
*)(unsigned long)!prev
;
335 static inline struct mem_cgroup
*
336 mem_cgroup_iter(struct mem_cgroup
*root
,
337 struct mem_cgroup
*prev
,
338 struct mem_cgroup_reclaim_cookie
*reclaim
)
343 static inline void mem_cgroup_iter_break(struct mem_cgroup
*root
,
344 struct mem_cgroup
*prev
)
348 static inline bool mem_cgroup_disabled(void)
354 mem_cgroup_inactive_anon_is_low(struct lruvec
*lruvec
)
359 static inline unsigned long
360 mem_cgroup_get_lru_size(struct lruvec
*lruvec
, enum lru_list lru
)
366 mem_cgroup_update_lru_size(struct lruvec
*lruvec
, enum lru_list lru
,
372 mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
, struct task_struct
*p
)
376 static inline void mem_cgroup_begin_update_page_stat(struct page
*page
,
377 bool *locked
, unsigned long *flags
)
381 static inline void mem_cgroup_end_update_page_stat(struct page
*page
,
382 bool *locked
, unsigned long *flags
)
386 static inline void mem_cgroup_inc_page_stat(struct page
*page
,
387 enum mem_cgroup_page_stat_item idx
)
391 static inline void mem_cgroup_dec_page_stat(struct page
*page
,
392 enum mem_cgroup_page_stat_item idx
)
397 enum mem_cgroup_filter_t
398 mem_cgroup_soft_reclaim_eligible(struct mem_cgroup
*memcg
,
399 struct mem_cgroup
*root
)
404 static inline void mem_cgroup_split_huge_fixup(struct page
*head
)
409 void mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
)
412 static inline void mem_cgroup_replace_page_cache(struct page
*oldpage
,
413 struct page
*newpage
)
416 #endif /* CONFIG_MEMCG */
418 #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
420 mem_cgroup_bad_page_check(struct page
*page
)
426 mem_cgroup_print_bad_page(struct page
*page
)
438 #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
439 void sock_update_memcg(struct sock
*sk
);
440 void sock_release_memcg(struct sock
*sk
);
442 static inline void sock_update_memcg(struct sock
*sk
)
445 static inline void sock_release_memcg(struct sock
*sk
)
448 #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
450 #ifdef CONFIG_MEMCG_KMEM
451 extern struct static_key memcg_kmem_enabled_key
;
453 extern int memcg_limited_groups_array_size
;
456 * Helper macro to loop through all memcg-specific caches. Callers must still
457 * check if the cache is valid (it is either valid or NULL).
458 * the slab_mutex must be held when looping through those caches
460 #define for_each_memcg_cache_index(_idx) \
461 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
463 static inline bool memcg_kmem_enabled(void)
465 return static_key_false(&memcg_kmem_enabled_key
);
469 * In general, we'll do everything in our power to not incur in any overhead
470 * for non-memcg users for the kmem functions. Not even a function call, if we
473 * Therefore, we'll inline all those functions so that in the best case, we'll
474 * see that kmemcg is off for everybody and proceed quickly. If it is on,
475 * we'll still do most of the flag checking inline. We check a lot of
476 * conditions, but because they are pretty simple, they are expected to be
479 bool __memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
,
481 void __memcg_kmem_commit_charge(struct page
*page
,
482 struct mem_cgroup
*memcg
, int order
);
483 void __memcg_kmem_uncharge_pages(struct page
*page
, int order
);
485 int memcg_cache_id(struct mem_cgroup
*memcg
);
486 int memcg_register_cache(struct mem_cgroup
*memcg
, struct kmem_cache
*s
,
487 struct kmem_cache
*root_cache
);
488 void memcg_release_cache(struct kmem_cache
*cachep
);
489 void memcg_cache_list_add(struct mem_cgroup
*memcg
, struct kmem_cache
*cachep
);
491 int memcg_update_cache_size(struct kmem_cache
*s
, int num_groups
);
492 void memcg_update_array_size(int num_groups
);
495 __memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
);
497 void mem_cgroup_destroy_cache(struct kmem_cache
*cachep
);
498 void kmem_cache_destroy_memcg_children(struct kmem_cache
*s
);
501 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
502 * @gfp: the gfp allocation flags.
503 * @memcg: a pointer to the memcg this was charged against.
504 * @order: allocation order.
506 * returns true if the memcg where the current task belongs can hold this
509 * We return true automatically if this allocation is not to be accounted to
513 memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
, int order
)
515 if (!memcg_kmem_enabled())
519 * __GFP_NOFAIL allocations will move on even if charging is not
520 * possible. Therefore we don't even try, and have this allocation
521 * unaccounted. We could in theory charge it with
522 * res_counter_charge_nofail, but we hope those allocations are rare,
523 * and won't be worth the trouble.
525 if (!(gfp
& __GFP_KMEMCG
) || (gfp
& __GFP_NOFAIL
))
527 if (in_interrupt() || (!current
->mm
) || (current
->flags
& PF_KTHREAD
))
530 /* If the test is dying, just let it go. */
531 if (unlikely(fatal_signal_pending(current
)))
534 return __memcg_kmem_newpage_charge(gfp
, memcg
, order
);
538 * memcg_kmem_uncharge_pages: uncharge pages from memcg
539 * @page: pointer to struct page being freed
540 * @order: allocation order.
542 * there is no need to specify memcg here, since it is embedded in page_cgroup
545 memcg_kmem_uncharge_pages(struct page
*page
, int order
)
547 if (memcg_kmem_enabled())
548 __memcg_kmem_uncharge_pages(page
, order
);
552 * memcg_kmem_commit_charge: embeds correct memcg in a page
553 * @page: pointer to struct page recently allocated
554 * @memcg: the memcg structure we charged against
555 * @order: allocation order.
557 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
558 * failure of the allocation. if @page is NULL, this function will revert the
559 * charges. Otherwise, it will commit the memcg given by @memcg to the
560 * corresponding page_cgroup.
563 memcg_kmem_commit_charge(struct page
*page
, struct mem_cgroup
*memcg
, int order
)
565 if (memcg_kmem_enabled() && memcg
)
566 __memcg_kmem_commit_charge(page
, memcg
, order
);
570 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
571 * @cachep: the original global kmem cache
572 * @gfp: allocation flags.
574 * This function assumes that the task allocating, which determines the memcg
575 * in the page allocator, belongs to the same cgroup throughout the whole
576 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
577 * while belonging to a cgroup, and later on changes. This is considered
578 * acceptable, and should only happen upon task migration.
580 * Before the cache is created by the memcg core, there is also a possible
581 * imbalance: the task belongs to a memcg, but the cache being allocated from
582 * is the global cache, since the child cache is not yet guaranteed to be
583 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
584 * passed and the page allocator will not attempt any cgroup accounting.
586 static __always_inline
struct kmem_cache
*
587 memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
589 if (!memcg_kmem_enabled())
591 if (gfp
& __GFP_NOFAIL
)
593 if (in_interrupt() || (!current
->mm
) || (current
->flags
& PF_KTHREAD
))
595 if (unlikely(fatal_signal_pending(current
)))
598 return __memcg_kmem_get_cache(cachep
, gfp
);
601 #define for_each_memcg_cache_index(_idx) \
604 static inline bool memcg_kmem_enabled(void)
610 memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
, int order
)
615 static inline void memcg_kmem_uncharge_pages(struct page
*page
, int order
)
620 memcg_kmem_commit_charge(struct page
*page
, struct mem_cgroup
*memcg
, int order
)
624 static inline int memcg_cache_id(struct mem_cgroup
*memcg
)
630 memcg_register_cache(struct mem_cgroup
*memcg
, struct kmem_cache
*s
,
631 struct kmem_cache
*root_cache
)
636 static inline void memcg_release_cache(struct kmem_cache
*cachep
)
640 static inline void memcg_cache_list_add(struct mem_cgroup
*memcg
,
641 struct kmem_cache
*s
)
645 static inline struct kmem_cache
*
646 memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
651 static inline void kmem_cache_destroy_memcg_children(struct kmem_cache
*s
)
654 #endif /* CONFIG_MEMCG_KMEM */
655 #endif /* _LINUX_MEMCONTROL_H */