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
;
46 * All "charge" functions with gfp_mask should use GFP_KERNEL or
47 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
48 * alloc memory but reclaims memory from all available zones. So, "where I want
49 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
50 * available but adding a rule is better. charge functions' gfp_mask should
51 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
53 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
56 extern int mem_cgroup_newpage_charge(struct page
*page
, struct mm_struct
*mm
,
58 /* for swap handling */
59 extern int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
60 struct page
*page
, gfp_t mask
, struct mem_cgroup
**memcgp
);
61 extern void mem_cgroup_commit_charge_swapin(struct page
*page
,
62 struct mem_cgroup
*memcg
);
63 extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*memcg
);
65 extern int mem_cgroup_cache_charge(struct page
*page
, struct mm_struct
*mm
,
68 struct lruvec
*mem_cgroup_zone_lruvec(struct zone
*, struct mem_cgroup
*);
69 struct lruvec
*mem_cgroup_page_lruvec(struct page
*, struct zone
*);
71 /* For coalescing uncharge for reducing memcg' overhead*/
72 extern void mem_cgroup_uncharge_start(void);
73 extern void mem_cgroup_uncharge_end(void);
75 extern void mem_cgroup_uncharge_page(struct page
*page
);
76 extern void mem_cgroup_uncharge_cache_page(struct page
*page
);
78 bool __mem_cgroup_same_or_subtree(const struct mem_cgroup
*root_memcg
,
79 struct mem_cgroup
*memcg
);
80 int task_in_mem_cgroup(struct task_struct
*task
, const struct mem_cgroup
*memcg
);
82 extern struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
);
83 extern struct mem_cgroup
*mem_cgroup_from_task(struct task_struct
*p
);
84 extern struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
);
86 extern struct mem_cgroup
*parent_mem_cgroup(struct mem_cgroup
*memcg
);
87 extern struct mem_cgroup
*mem_cgroup_from_cont(struct cgroup
*cont
);
90 bool mm_match_cgroup(const struct mm_struct
*mm
, const struct mem_cgroup
*memcg
)
92 struct mem_cgroup
*task_memcg
;
96 task_memcg
= mem_cgroup_from_task(rcu_dereference(mm
->owner
));
97 match
= __mem_cgroup_same_or_subtree(memcg
, task_memcg
);
102 extern struct cgroup_subsys_state
*mem_cgroup_css(struct mem_cgroup
*memcg
);
105 mem_cgroup_prepare_migration(struct page
*page
, struct page
*newpage
,
106 struct mem_cgroup
**memcgp
);
107 extern void mem_cgroup_end_migration(struct mem_cgroup
*memcg
,
108 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
);
110 struct mem_cgroup
*mem_cgroup_iter(struct mem_cgroup
*,
112 struct mem_cgroup_reclaim_cookie
*);
113 void mem_cgroup_iter_break(struct mem_cgroup
*, struct mem_cgroup
*);
116 * For memory reclaim.
118 int mem_cgroup_inactive_anon_is_low(struct lruvec
*lruvec
);
119 int mem_cgroup_select_victim_node(struct mem_cgroup
*memcg
);
120 unsigned long mem_cgroup_get_lru_size(struct lruvec
*lruvec
, enum lru_list
);
121 void mem_cgroup_update_lru_size(struct lruvec
*, enum lru_list
, int);
122 extern void mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
,
123 struct task_struct
*p
);
124 extern void mem_cgroup_replace_page_cache(struct page
*oldpage
,
125 struct page
*newpage
);
127 static inline void mem_cgroup_oom_enable(void)
129 WARN_ON(current
->memcg_oom
.may_oom
);
130 current
->memcg_oom
.may_oom
= 1;
133 static inline void mem_cgroup_oom_disable(void)
135 WARN_ON(!current
->memcg_oom
.may_oom
);
136 current
->memcg_oom
.may_oom
= 0;
139 static inline bool task_in_memcg_oom(struct task_struct
*p
)
141 return p
->memcg_oom
.memcg
;
144 bool mem_cgroup_oom_synchronize(bool wait
);
146 #ifdef CONFIG_MEMCG_SWAP
147 extern int do_swap_account
;
150 static inline bool mem_cgroup_disabled(void)
152 if (mem_cgroup_subsys
.disabled
)
157 void __mem_cgroup_begin_update_page_stat(struct page
*page
, bool *locked
,
158 unsigned long *flags
);
160 extern atomic_t memcg_moving
;
162 static inline void mem_cgroup_begin_update_page_stat(struct page
*page
,
163 bool *locked
, unsigned long *flags
)
165 if (mem_cgroup_disabled())
169 if (atomic_read(&memcg_moving
))
170 __mem_cgroup_begin_update_page_stat(page
, locked
, flags
);
173 void __mem_cgroup_end_update_page_stat(struct page
*page
,
174 unsigned long *flags
);
175 static inline void mem_cgroup_end_update_page_stat(struct page
*page
,
176 bool *locked
, unsigned long *flags
)
178 if (mem_cgroup_disabled())
181 __mem_cgroup_end_update_page_stat(page
, flags
);
185 void mem_cgroup_update_page_stat(struct page
*page
,
186 enum mem_cgroup_page_stat_item idx
,
189 static inline void mem_cgroup_inc_page_stat(struct page
*page
,
190 enum mem_cgroup_page_stat_item idx
)
192 mem_cgroup_update_page_stat(page
, idx
, 1);
195 static inline void mem_cgroup_dec_page_stat(struct page
*page
,
196 enum mem_cgroup_page_stat_item idx
)
198 mem_cgroup_update_page_stat(page
, idx
, -1);
201 unsigned long mem_cgroup_soft_limit_reclaim(struct zone
*zone
, int order
,
203 unsigned long *total_scanned
);
205 void __mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
);
206 static inline void mem_cgroup_count_vm_event(struct mm_struct
*mm
,
207 enum vm_event_item idx
)
209 if (mem_cgroup_disabled())
211 __mem_cgroup_count_vm_event(mm
, idx
);
213 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
214 void mem_cgroup_split_huge_fixup(struct page
*head
);
217 #ifdef CONFIG_DEBUG_VM
218 bool mem_cgroup_bad_page_check(struct page
*page
);
219 void mem_cgroup_print_bad_page(struct page
*page
);
221 #else /* CONFIG_MEMCG */
224 static inline int mem_cgroup_newpage_charge(struct page
*page
,
225 struct mm_struct
*mm
, gfp_t gfp_mask
)
230 static inline int mem_cgroup_cache_charge(struct page
*page
,
231 struct mm_struct
*mm
, gfp_t gfp_mask
)
236 static inline int mem_cgroup_try_charge_swapin(struct mm_struct
*mm
,
237 struct page
*page
, gfp_t gfp_mask
, struct mem_cgroup
**memcgp
)
242 static inline void mem_cgroup_commit_charge_swapin(struct page
*page
,
243 struct mem_cgroup
*memcg
)
247 static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup
*memcg
)
251 static inline void mem_cgroup_uncharge_start(void)
255 static inline void mem_cgroup_uncharge_end(void)
259 static inline void mem_cgroup_uncharge_page(struct page
*page
)
263 static inline void mem_cgroup_uncharge_cache_page(struct page
*page
)
267 static inline struct lruvec
*mem_cgroup_zone_lruvec(struct zone
*zone
,
268 struct mem_cgroup
*memcg
)
270 return &zone
->lruvec
;
273 static inline struct lruvec
*mem_cgroup_page_lruvec(struct page
*page
,
276 return &zone
->lruvec
;
279 static inline struct mem_cgroup
*try_get_mem_cgroup_from_page(struct page
*page
)
284 static inline struct mem_cgroup
*try_get_mem_cgroup_from_mm(struct mm_struct
*mm
)
289 static inline bool mm_match_cgroup(struct mm_struct
*mm
,
290 struct mem_cgroup
*memcg
)
295 static inline int task_in_mem_cgroup(struct task_struct
*task
,
296 const struct mem_cgroup
*memcg
)
301 static inline struct cgroup_subsys_state
302 *mem_cgroup_css(struct mem_cgroup
*memcg
)
308 mem_cgroup_prepare_migration(struct page
*page
, struct page
*newpage
,
309 struct mem_cgroup
**memcgp
)
313 static inline void mem_cgroup_end_migration(struct mem_cgroup
*memcg
,
314 struct page
*oldpage
, struct page
*newpage
, bool migration_ok
)
318 static inline struct mem_cgroup
*
319 mem_cgroup_iter(struct mem_cgroup
*root
,
320 struct mem_cgroup
*prev
,
321 struct mem_cgroup_reclaim_cookie
*reclaim
)
326 static inline void mem_cgroup_iter_break(struct mem_cgroup
*root
,
327 struct mem_cgroup
*prev
)
331 static inline bool mem_cgroup_disabled(void)
337 mem_cgroup_inactive_anon_is_low(struct lruvec
*lruvec
)
342 static inline unsigned long
343 mem_cgroup_get_lru_size(struct lruvec
*lruvec
, enum lru_list lru
)
349 mem_cgroup_update_lru_size(struct lruvec
*lruvec
, enum lru_list lru
,
355 mem_cgroup_print_oom_info(struct mem_cgroup
*memcg
, struct task_struct
*p
)
359 static inline void mem_cgroup_begin_update_page_stat(struct page
*page
,
360 bool *locked
, unsigned long *flags
)
364 static inline void mem_cgroup_end_update_page_stat(struct page
*page
,
365 bool *locked
, unsigned long *flags
)
369 static inline void mem_cgroup_oom_enable(void)
373 static inline void mem_cgroup_oom_disable(void)
377 static inline bool task_in_memcg_oom(struct task_struct
*p
)
382 static inline bool mem_cgroup_oom_synchronize(bool wait
)
387 static inline void mem_cgroup_inc_page_stat(struct page
*page
,
388 enum mem_cgroup_page_stat_item idx
)
392 static inline void mem_cgroup_dec_page_stat(struct page
*page
,
393 enum mem_cgroup_page_stat_item idx
)
398 unsigned long mem_cgroup_soft_limit_reclaim(struct zone
*zone
, int order
,
400 unsigned long *total_scanned
)
405 static inline void mem_cgroup_split_huge_fixup(struct page
*head
)
410 void mem_cgroup_count_vm_event(struct mm_struct
*mm
, enum vm_event_item idx
)
413 static inline void mem_cgroup_replace_page_cache(struct page
*oldpage
,
414 struct page
*newpage
)
417 #endif /* CONFIG_MEMCG */
419 #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
421 mem_cgroup_bad_page_check(struct page
*page
)
427 mem_cgroup_print_bad_page(struct page
*page
)
439 #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
440 void sock_update_memcg(struct sock
*sk
);
441 void sock_release_memcg(struct sock
*sk
);
443 static inline void sock_update_memcg(struct sock
*sk
)
446 static inline void sock_release_memcg(struct sock
*sk
)
449 #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
451 #ifdef CONFIG_MEMCG_KMEM
452 extern struct static_key memcg_kmem_enabled_key
;
454 extern int memcg_limited_groups_array_size
;
457 * Helper macro to loop through all memcg-specific caches. Callers must still
458 * check if the cache is valid (it is either valid or NULL).
459 * the slab_mutex must be held when looping through those caches
461 #define for_each_memcg_cache_index(_idx) \
462 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
464 static inline bool memcg_kmem_enabled(void)
466 return static_key_false(&memcg_kmem_enabled_key
);
470 * In general, we'll do everything in our power to not incur in any overhead
471 * for non-memcg users for the kmem functions. Not even a function call, if we
474 * Therefore, we'll inline all those functions so that in the best case, we'll
475 * see that kmemcg is off for everybody and proceed quickly. If it is on,
476 * we'll still do most of the flag checking inline. We check a lot of
477 * conditions, but because they are pretty simple, they are expected to be
480 bool __memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
,
482 void __memcg_kmem_commit_charge(struct page
*page
,
483 struct mem_cgroup
*memcg
, int order
);
484 void __memcg_kmem_uncharge_pages(struct page
*page
, int order
);
486 int memcg_cache_id(struct mem_cgroup
*memcg
);
487 int memcg_register_cache(struct mem_cgroup
*memcg
, struct kmem_cache
*s
,
488 struct kmem_cache
*root_cache
);
489 void memcg_release_cache(struct kmem_cache
*cachep
);
490 void memcg_cache_list_add(struct mem_cgroup
*memcg
, struct kmem_cache
*cachep
);
492 int memcg_update_cache_size(struct kmem_cache
*s
, int num_groups
);
493 void memcg_update_array_size(int num_groups
);
496 __memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
);
498 void mem_cgroup_destroy_cache(struct kmem_cache
*cachep
);
499 void kmem_cache_destroy_memcg_children(struct kmem_cache
*s
);
502 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
503 * @gfp: the gfp allocation flags.
504 * @memcg: a pointer to the memcg this was charged against.
505 * @order: allocation order.
507 * returns true if the memcg where the current task belongs can hold this
510 * We return true automatically if this allocation is not to be accounted to
514 memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
, int order
)
516 if (!memcg_kmem_enabled())
520 * __GFP_NOFAIL allocations will move on even if charging is not
521 * possible. Therefore we don't even try, and have this allocation
522 * unaccounted. We could in theory charge it with
523 * res_counter_charge_nofail, but we hope those allocations are rare,
524 * and won't be worth the trouble.
526 if (!(gfp
& __GFP_KMEMCG
) || (gfp
& __GFP_NOFAIL
))
528 if (in_interrupt() || (!current
->mm
) || (current
->flags
& PF_KTHREAD
))
531 /* If the test is dying, just let it go. */
532 if (unlikely(fatal_signal_pending(current
)))
535 return __memcg_kmem_newpage_charge(gfp
, memcg
, order
);
539 * memcg_kmem_uncharge_pages: uncharge pages from memcg
540 * @page: pointer to struct page being freed
541 * @order: allocation order.
543 * there is no need to specify memcg here, since it is embedded in page_cgroup
546 memcg_kmem_uncharge_pages(struct page
*page
, int order
)
548 if (memcg_kmem_enabled())
549 __memcg_kmem_uncharge_pages(page
, order
);
553 * memcg_kmem_commit_charge: embeds correct memcg in a page
554 * @page: pointer to struct page recently allocated
555 * @memcg: the memcg structure we charged against
556 * @order: allocation order.
558 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
559 * failure of the allocation. if @page is NULL, this function will revert the
560 * charges. Otherwise, it will commit the memcg given by @memcg to the
561 * corresponding page_cgroup.
564 memcg_kmem_commit_charge(struct page
*page
, struct mem_cgroup
*memcg
, int order
)
566 if (memcg_kmem_enabled() && memcg
)
567 __memcg_kmem_commit_charge(page
, memcg
, order
);
571 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
572 * @cachep: the original global kmem cache
573 * @gfp: allocation flags.
575 * This function assumes that the task allocating, which determines the memcg
576 * in the page allocator, belongs to the same cgroup throughout the whole
577 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
578 * while belonging to a cgroup, and later on changes. This is considered
579 * acceptable, and should only happen upon task migration.
581 * Before the cache is created by the memcg core, there is also a possible
582 * imbalance: the task belongs to a memcg, but the cache being allocated from
583 * is the global cache, since the child cache is not yet guaranteed to be
584 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
585 * passed and the page allocator will not attempt any cgroup accounting.
587 static __always_inline
struct kmem_cache
*
588 memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
590 if (!memcg_kmem_enabled())
592 if (gfp
& __GFP_NOFAIL
)
594 if (in_interrupt() || (!current
->mm
) || (current
->flags
& PF_KTHREAD
))
596 if (unlikely(fatal_signal_pending(current
)))
599 return __memcg_kmem_get_cache(cachep
, gfp
);
602 #define for_each_memcg_cache_index(_idx) \
605 static inline bool memcg_kmem_enabled(void)
611 memcg_kmem_newpage_charge(gfp_t gfp
, struct mem_cgroup
**memcg
, int order
)
616 static inline void memcg_kmem_uncharge_pages(struct page
*page
, int order
)
621 memcg_kmem_commit_charge(struct page
*page
, struct mem_cgroup
*memcg
, int order
)
625 static inline int memcg_cache_id(struct mem_cgroup
*memcg
)
631 memcg_register_cache(struct mem_cgroup
*memcg
, struct kmem_cache
*s
,
632 struct kmem_cache
*root_cache
)
637 static inline void memcg_release_cache(struct kmem_cache
*cachep
)
641 static inline void memcg_cache_list_add(struct mem_cgroup
*memcg
,
642 struct kmem_cache
*s
)
646 static inline struct kmem_cache
*
647 memcg_kmem_get_cache(struct kmem_cache
*cachep
, gfp_t gfp
)
652 static inline void kmem_cache_destroy_memcg_children(struct kmem_cache
*s
)
655 #endif /* CONFIG_MEMCG_KMEM */
656 #endif /* _LINUX_MEMCONTROL_H */