1 /* memcontrol.h - Memory Controller
3 * Copyright IBM Corporation, 2007
6 * Copyright 2007 OpenVZ SWsoft Inc
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 * The corresponding mem_cgroup_stat_names is defined in mm/memcontrol.c,
34 * These two lists should keep in accord with each other.
36 enum mem_cgroup_stat_index {
38 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
40 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
41 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
42 MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */
43 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
44 MEM_CGROUP_STAT_WRITEBACK, /* # of pages under writeback */
45 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
46 MEM_CGROUP_STAT_NSTATS,
49 struct mem_cgroup_reclaim_cookie {
52 unsigned int generation;
55 enum mem_cgroup_events_index {
56 MEM_CGROUP_EVENTS_PGPGIN, /* # of pages paged in */
57 MEM_CGROUP_EVENTS_PGPGOUT, /* # of pages paged out */
58 MEM_CGROUP_EVENTS_PGFAULT, /* # of page-faults */
59 MEM_CGROUP_EVENTS_PGMAJFAULT, /* # of major page-faults */
60 MEM_CGROUP_EVENTS_NSTATS,
61 /* default hierarchy events */
62 MEMCG_LOW = MEM_CGROUP_EVENTS_NSTATS,
70 void mem_cgroup_events(struct mem_cgroup *memcg,
71 enum mem_cgroup_events_index idx,
74 bool mem_cgroup_low(struct mem_cgroup *root, struct mem_cgroup *memcg);
76 int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
77 gfp_t gfp_mask, struct mem_cgroup **memcgp);
78 void mem_cgroup_commit_charge(struct page *page, struct mem_cgroup *memcg,
80 void mem_cgroup_cancel_charge(struct page *page, struct mem_cgroup *memcg);
81 void mem_cgroup_uncharge(struct page *page);
82 void mem_cgroup_uncharge_list(struct list_head *page_list);
84 void mem_cgroup_migrate(struct page *oldpage, struct page *newpage,
87 struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
88 struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
90 bool mem_cgroup_is_descendant(struct mem_cgroup *memcg,
91 struct mem_cgroup *root);
92 bool task_in_mem_cgroup(struct task_struct *task, struct mem_cgroup *memcg);
94 extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
95 extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
97 extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
98 extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
100 static inline bool mm_match_cgroup(struct mm_struct *mm,
101 struct mem_cgroup *memcg)
103 struct mem_cgroup *task_memcg;
107 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
109 match = mem_cgroup_is_descendant(task_memcg, memcg);
114 extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
116 struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
118 struct mem_cgroup_reclaim_cookie *);
119 void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
122 * For memory reclaim.
124 int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
125 bool mem_cgroup_lruvec_online(struct lruvec *lruvec);
126 int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
127 unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
128 void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
129 extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
130 struct task_struct *p);
132 static inline void mem_cgroup_oom_enable(void)
134 WARN_ON(current->memcg_oom.may_oom);
135 current->memcg_oom.may_oom = 1;
138 static inline void mem_cgroup_oom_disable(void)
140 WARN_ON(!current->memcg_oom.may_oom);
141 current->memcg_oom.may_oom = 0;
144 static inline bool task_in_memcg_oom(struct task_struct *p)
146 return p->memcg_oom.memcg;
149 bool mem_cgroup_oom_synchronize(bool wait);
151 #ifdef CONFIG_MEMCG_SWAP
152 extern int do_swap_account;
155 static inline bool mem_cgroup_disabled(void)
157 if (memory_cgrp_subsys.disabled)
162 struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page);
163 void mem_cgroup_update_page_stat(struct mem_cgroup *memcg,
164 enum mem_cgroup_stat_index idx, int val);
165 void mem_cgroup_end_page_stat(struct mem_cgroup *memcg);
167 static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg,
168 enum mem_cgroup_stat_index idx)
170 mem_cgroup_update_page_stat(memcg, idx, 1);
173 static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg,
174 enum mem_cgroup_stat_index idx)
176 mem_cgroup_update_page_stat(memcg, idx, -1);
179 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
181 unsigned long *total_scanned);
183 void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
184 static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
185 enum vm_event_item idx)
187 if (mem_cgroup_disabled())
189 __mem_cgroup_count_vm_event(mm, idx);
191 #ifdef CONFIG_TRANSPARENT_HUGEPAGE
192 void mem_cgroup_split_huge_fixup(struct page *head);
195 #else /* CONFIG_MEMCG */
198 static inline void mem_cgroup_events(struct mem_cgroup *memcg,
199 enum mem_cgroup_events_index idx,
204 static inline bool mem_cgroup_low(struct mem_cgroup *root,
205 struct mem_cgroup *memcg)
210 static inline int mem_cgroup_try_charge(struct page *page, struct mm_struct *mm,
212 struct mem_cgroup **memcgp)
218 static inline void mem_cgroup_commit_charge(struct page *page,
219 struct mem_cgroup *memcg,
224 static inline void mem_cgroup_cancel_charge(struct page *page,
225 struct mem_cgroup *memcg)
229 static inline void mem_cgroup_uncharge(struct page *page)
233 static inline void mem_cgroup_uncharge_list(struct list_head *page_list)
237 static inline void mem_cgroup_migrate(struct page *oldpage,
238 struct page *newpage,
243 static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
244 struct mem_cgroup *memcg)
246 return &zone->lruvec;
249 static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
252 return &zone->lruvec;
255 static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
260 static inline bool mm_match_cgroup(struct mm_struct *mm,
261 struct mem_cgroup *memcg)
266 static inline bool task_in_mem_cgroup(struct task_struct *task,
267 const struct mem_cgroup *memcg)
272 static inline struct cgroup_subsys_state
273 *mem_cgroup_css(struct mem_cgroup *memcg)
278 static inline struct mem_cgroup *
279 mem_cgroup_iter(struct mem_cgroup *root,
280 struct mem_cgroup *prev,
281 struct mem_cgroup_reclaim_cookie *reclaim)
286 static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
287 struct mem_cgroup *prev)
291 static inline bool mem_cgroup_disabled(void)
297 mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
302 static inline bool mem_cgroup_lruvec_online(struct lruvec *lruvec)
307 static inline unsigned long
308 mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
314 mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
320 mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
324 static inline struct mem_cgroup *mem_cgroup_begin_page_stat(struct page *page)
329 static inline void mem_cgroup_end_page_stat(struct mem_cgroup *memcg)
333 static inline void mem_cgroup_oom_enable(void)
337 static inline void mem_cgroup_oom_disable(void)
341 static inline bool task_in_memcg_oom(struct task_struct *p)
346 static inline bool mem_cgroup_oom_synchronize(bool wait)
351 static inline void mem_cgroup_inc_page_stat(struct mem_cgroup *memcg,
352 enum mem_cgroup_stat_index idx)
356 static inline void mem_cgroup_dec_page_stat(struct mem_cgroup *memcg,
357 enum mem_cgroup_stat_index idx)
362 unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
364 unsigned long *total_scanned)
369 static inline void mem_cgroup_split_huge_fixup(struct page *head)
374 void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
377 #endif /* CONFIG_MEMCG */
386 #if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
387 void sock_update_memcg(struct sock *sk);
388 void sock_release_memcg(struct sock *sk);
390 static inline void sock_update_memcg(struct sock *sk)
393 static inline void sock_release_memcg(struct sock *sk)
396 #endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
398 #ifdef CONFIG_MEMCG_KMEM
399 extern struct static_key memcg_kmem_enabled_key;
401 extern int memcg_nr_cache_ids;
402 extern void memcg_get_cache_ids(void);
403 extern void memcg_put_cache_ids(void);
406 * Helper macro to loop through all memcg-specific caches. Callers must still
407 * check if the cache is valid (it is either valid or NULL).
408 * the slab_mutex must be held when looping through those caches
410 #define for_each_memcg_cache_index(_idx) \
411 for ((_idx) = 0; (_idx) < memcg_nr_cache_ids; (_idx)++)
413 static inline bool memcg_kmem_enabled(void)
415 return static_key_false(&memcg_kmem_enabled_key);
418 bool memcg_kmem_is_active(struct mem_cgroup *memcg);
421 * In general, we'll do everything in our power to not incur in any overhead
422 * for non-memcg users for the kmem functions. Not even a function call, if we
425 * Therefore, we'll inline all those functions so that in the best case, we'll
426 * see that kmemcg is off for everybody and proceed quickly. If it is on,
427 * we'll still do most of the flag checking inline. We check a lot of
428 * conditions, but because they are pretty simple, they are expected to be
431 bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
433 void __memcg_kmem_commit_charge(struct page *page,
434 struct mem_cgroup *memcg, int order);
435 void __memcg_kmem_uncharge_pages(struct page *page, int order);
437 int memcg_cache_id(struct mem_cgroup *memcg);
439 struct kmem_cache *__memcg_kmem_get_cache(struct kmem_cache *cachep);
440 void __memcg_kmem_put_cache(struct kmem_cache *cachep);
442 struct mem_cgroup *__mem_cgroup_from_kmem(void *ptr);
444 int memcg_charge_kmem(struct mem_cgroup *memcg, gfp_t gfp,
445 unsigned long nr_pages);
446 void memcg_uncharge_kmem(struct mem_cgroup *memcg, unsigned long nr_pages);
449 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
450 * @gfp: the gfp allocation flags.
451 * @memcg: a pointer to the memcg this was charged against.
452 * @order: allocation order.
454 * returns true if the memcg where the current task belongs can hold this
457 * We return true automatically if this allocation is not to be accounted to
461 memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
463 if (!memcg_kmem_enabled())
467 * __GFP_NOFAIL allocations will move on even if charging is not
468 * possible. Therefore we don't even try, and have this allocation
469 * unaccounted. We could in theory charge it forcibly, but we hope
470 * those allocations are rare, and won't be worth the trouble.
472 if (gfp & __GFP_NOFAIL)
474 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
477 /* If the test is dying, just let it go. */
478 if (unlikely(fatal_signal_pending(current)))
481 return __memcg_kmem_newpage_charge(gfp, memcg, order);
485 * memcg_kmem_uncharge_pages: uncharge pages from memcg
486 * @page: pointer to struct page being freed
487 * @order: allocation order.
490 memcg_kmem_uncharge_pages(struct page *page, int order)
492 if (memcg_kmem_enabled())
493 __memcg_kmem_uncharge_pages(page, order);
497 * memcg_kmem_commit_charge: embeds correct memcg in a page
498 * @page: pointer to struct page recently allocated
499 * @memcg: the memcg structure we charged against
500 * @order: allocation order.
502 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
503 * failure of the allocation. if @page is NULL, this function will revert the
504 * charges. Otherwise, it will commit @page to @memcg.
507 memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
509 if (memcg_kmem_enabled() && memcg)
510 __memcg_kmem_commit_charge(page, memcg, order);
514 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
515 * @cachep: the original global kmem cache
516 * @gfp: allocation flags.
518 * All memory allocated from a per-memcg cache is charged to the owner memcg.
520 static __always_inline struct kmem_cache *
521 memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
523 if (!memcg_kmem_enabled())
525 if (gfp & __GFP_NOFAIL)
527 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
529 if (unlikely(fatal_signal_pending(current)))
532 return __memcg_kmem_get_cache(cachep);
535 static __always_inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
537 if (memcg_kmem_enabled())
538 __memcg_kmem_put_cache(cachep);
541 static __always_inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr)
543 if (!memcg_kmem_enabled())
545 return __mem_cgroup_from_kmem(ptr);
548 #define for_each_memcg_cache_index(_idx) \
551 static inline bool memcg_kmem_enabled(void)
556 static inline bool memcg_kmem_is_active(struct mem_cgroup *memcg)
562 memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
567 static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
572 memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
576 static inline int memcg_cache_id(struct mem_cgroup *memcg)
581 static inline void memcg_get_cache_ids(void)
585 static inline void memcg_put_cache_ids(void)
589 static inline struct kmem_cache *
590 memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
595 static inline void memcg_kmem_put_cache(struct kmem_cache *cachep)
599 static inline struct mem_cgroup *mem_cgroup_from_kmem(void *ptr)
603 #endif /* CONFIG_MEMCG_KMEM */
604 #endif /* _LINUX_MEMCONTROL_H */