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_inactive_file_is_low(struct lruvec *lruvec);
120 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
121 unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
122 void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
123 extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
124 struct task_struct *p);
125 extern void mem_cgroup_replace_page_cache(struct page *oldpage,
126 struct page *newpage);
128 #ifdef CONFIG_MEMCG_SWAP
129 extern int do_swap_account;
132 static inline bool mem_cgroup_disabled(void)
134 if (mem_cgroup_subsys.disabled)
139 void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
140 unsigned long *flags);
142 extern atomic_t memcg_moving;
144 static inline void mem_cgroup_begin_update_page_stat(struct page *page,
145 bool *locked, unsigned long *flags)
147 if (mem_cgroup_disabled())
151 if (atomic_read(&memcg_moving))
152 __mem_cgroup_begin_update_page_stat(page, locked, flags);
155 void __mem_cgroup_end_update_page_stat(struct page *page,
156 unsigned long *flags);
157 static inline void mem_cgroup_end_update_page_stat(struct page *page,
158 bool *locked, unsigned long *flags)
160 if (mem_cgroup_disabled())
163 __mem_cgroup_end_update_page_stat(page, flags);
167 void mem_cgroup_update_page_stat(struct page *page,
168 enum mem_cgroup_page_stat_item idx,
171 static inline void mem_cgroup_inc_page_stat(struct page *page,
172 enum mem_cgroup_page_stat_item idx)
174 mem_cgroup_update_page_stat(page, idx, 1);
177 static inline void mem_cgroup_dec_page_stat(struct page *page,
178 enum mem_cgroup_page_stat_item idx)
180 mem_cgroup_update_page_stat(page, idx, -1);
183 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
185 unsigned long *total_scanned);
187 void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
188 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
189 void mem_cgroup_split_huge_fixup(struct page *head);
192 #ifdef CONFIG_DEBUG_VM
193 bool mem_cgroup_bad_page_check(struct page *page);
194 void mem_cgroup_print_bad_page(struct page *page);
196 #else /* CONFIG_MEMCG */
199 static inline int mem_cgroup_newpage_charge(struct page *page,
200 struct mm_struct *mm, gfp_t gfp_mask)
205 static inline int mem_cgroup_cache_charge(struct page *page,
206 struct mm_struct *mm, gfp_t gfp_mask)
211 static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
212 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
217 static inline void mem_cgroup_commit_charge_swapin(struct page *page,
218 struct mem_cgroup *memcg)
222 static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
226 static inline void mem_cgroup_uncharge_start(void)
230 static inline void mem_cgroup_uncharge_end(void)
234 static inline void mem_cgroup_uncharge_page(struct page *page)
238 static inline void mem_cgroup_uncharge_cache_page(struct page *page)
242 static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
243 struct mem_cgroup *memcg)
245 return &zone->lruvec;
248 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
251 return &zone->lruvec;
254 static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
259 static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
264 static inline bool mm_match_cgroup(struct mm_struct *mm,
265 struct mem_cgroup *memcg)
270 static inline int task_in_mem_cgroup(struct task_struct *task,
271 const struct mem_cgroup *memcg)
276 static inline struct cgroup_subsys_state
277 *mem_cgroup_css(struct mem_cgroup *memcg)
283 mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
284 struct mem_cgroup **memcgp)
288 static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
289 struct page *oldpage, struct page *newpage, bool migration_ok)
293 static inline struct mem_cgroup *
294 mem_cgroup_iter(struct mem_cgroup *root,
295 struct mem_cgroup *prev,
296 struct mem_cgroup_reclaim_cookie *reclaim)
301 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
302 struct mem_cgroup *prev)
306 static inline bool mem_cgroup_disabled(void)
312 mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
318 mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
323 static inline unsigned long
324 mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
330 mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
336 mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
340 static inline void mem_cgroup_begin_update_page_stat(struct page *page,
341 bool *locked, unsigned long *flags)
345 static inline void mem_cgroup_end_update_page_stat(struct page *page,
346 bool *locked, unsigned long *flags)
350 static inline void mem_cgroup_inc_page_stat(struct page *page,
351 enum mem_cgroup_page_stat_item idx)
355 static inline void mem_cgroup_dec_page_stat(struct page *page,
356 enum mem_cgroup_page_stat_item idx)
361 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
363 unsigned long *total_scanned)
368 static inline void mem_cgroup_split_huge_fixup(struct page *head)
373 void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
376 static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
377 struct page *newpage)
380 #endif /* CONFIG_MEMCG */
382 #if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
384 mem_cgroup_bad_page_check(struct page *page)
390 mem_cgroup_print_bad_page(struct page *page)
402 #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
403 void sock_update_memcg(struct sock *sk);
404 void sock_release_memcg(struct sock *sk);
406 static inline void sock_update_memcg(struct sock *sk)
409 static inline void sock_release_memcg(struct sock *sk)
412 #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
414 #ifdef CONFIG_MEMCG_KMEM
415 extern struct static_key memcg_kmem_enabled_key;
416 static inline bool memcg_kmem_enabled(void)
418 return static_key_false(&memcg_kmem_enabled_key);
422 * In general, we'll do everything in our power to not incur in any overhead
423 * for non-memcg users for the kmem functions. Not even a function call, if we
426 * Therefore, we'll inline all those functions so that in the best case, we'll
427 * see that kmemcg is off for everybody and proceed quickly. If it is on,
428 * we'll still do most of the flag checking inline. We check a lot of
429 * conditions, but because they are pretty simple, they are expected to be
432 bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
434 void __memcg_kmem_commit_charge(struct page *page,
435 struct mem_cgroup *memcg, int order);
436 void __memcg_kmem_uncharge_pages(struct page *page, int order);
438 int memcg_cache_id(struct mem_cgroup *memcg);
439 int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s);
440 void memcg_release_cache(struct kmem_cache *cachep);
441 void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
443 int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
444 void memcg_update_array_size(int num_groups);
447 __memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
449 void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
450 void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
453 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
454 * @gfp: the gfp allocation flags.
455 * @memcg: a pointer to the memcg this was charged against.
456 * @order: allocation order.
458 * returns true if the memcg where the current task belongs can hold this
461 * We return true automatically if this allocation is not to be accounted to
464 static __always_inline bool
465 memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
467 if (!memcg_kmem_enabled())
471 * __GFP_NOFAIL allocations will move on even if charging is not
472 * possible. Therefore we don't even try, and have this allocation
473 * unaccounted. We could in theory charge it with
474 * res_counter_charge_nofail, but we hope those allocations are rare,
475 * and won't be worth the trouble.
477 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
479 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
482 /* If the test is dying, just let it go. */
483 if (unlikely(fatal_signal_pending(current)))
486 return __memcg_kmem_newpage_charge(gfp, memcg, order);
490 * memcg_kmem_uncharge_pages: uncharge pages from memcg
491 * @page: pointer to struct page being freed
492 * @order: allocation order.
494 * there is no need to specify memcg here, since it is embedded in page_cgroup
496 static __always_inline void
497 memcg_kmem_uncharge_pages(struct page *page, int order)
499 if (memcg_kmem_enabled())
500 __memcg_kmem_uncharge_pages(page, order);
504 * memcg_kmem_commit_charge: embeds correct memcg in a page
505 * @page: pointer to struct page recently allocated
506 * @memcg: the memcg structure we charged against
507 * @order: allocation order.
509 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
510 * failure of the allocation. if @page is NULL, this function will revert the
511 * charges. Otherwise, it will commit the memcg given by @memcg to the
512 * corresponding page_cgroup.
514 static __always_inline void
515 memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
517 if (memcg_kmem_enabled() && memcg)
518 __memcg_kmem_commit_charge(page, memcg, order);
522 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
523 * @cachep: the original global kmem cache
524 * @gfp: allocation flags.
526 * This function assumes that the task allocating, which determines the memcg
527 * in the page allocator, belongs to the same cgroup throughout the whole
528 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
529 * while belonging to a cgroup, and later on changes. This is considered
530 * acceptable, and should only happen upon task migration.
532 * Before the cache is created by the memcg core, there is also a possible
533 * imbalance: the task belongs to a memcg, but the cache being allocated from
534 * is the global cache, since the child cache is not yet guaranteed to be
535 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
536 * passed and the page allocator will not attempt any cgroup accounting.
538 static __always_inline struct kmem_cache *
539 memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
541 if (!memcg_kmem_enabled())
543 if (gfp & __GFP_NOFAIL)
545 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
547 if (unlikely(fatal_signal_pending(current)))
550 return __memcg_kmem_get_cache(cachep, gfp);
553 static inline bool memcg_kmem_enabled(void)
559 memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
564 static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
569 memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
573 static inline int memcg_cache_id(struct mem_cgroup *memcg)
578 static inline int memcg_register_cache(struct mem_cgroup *memcg,
579 struct kmem_cache *s)
584 static inline void memcg_release_cache(struct kmem_cache *cachep)
588 static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
589 struct kmem_cache *s)
593 static inline struct kmem_cache *
594 memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
599 static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
602 #endif /* CONFIG_MEMCG_KMEM */
603 #endif /* _LINUX_MEMCONTROL_H */