memcg: kmem accounting lifecycle management
[linux-block.git] / include / linux / memcontrol.h
CommitLineData
8cdea7c0
BS
1/* memcontrol.h - Memory Controller
2 *
3 * Copyright IBM Corporation, 2007
4 * Author Balbir Singh <balbir@linux.vnet.ibm.com>
5 *
78fb7466
PE
6 * Copyright 2007 OpenVZ SWsoft Inc
7 * Author: Pavel Emelianov <xemul@openvz.org>
8 *
8cdea7c0
BS
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.
13 *
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.
18 */
19
20#ifndef _LINUX_MEMCONTROL_H
21#define _LINUX_MEMCONTROL_H
f8d66542 22#include <linux/cgroup.h>
456f998e 23#include <linux/vm_event_item.h>
7ae1e1d0 24#include <linux/hardirq.h>
456f998e 25
78fb7466
PE
26struct mem_cgroup;
27struct page_cgroup;
8697d331
BS
28struct page;
29struct mm_struct;
78fb7466 30
2a7106f2
GT
31/* Stats that can be updated by kernel. */
32enum mem_cgroup_page_stat_item {
33 MEMCG_NR_FILE_MAPPED, /* # of pages charged as file rss */
34};
35
5660048c
JW
36struct mem_cgroup_reclaim_cookie {
37 struct zone *zone;
38 int priority;
39 unsigned int generation;
40};
41
c255a458 42#ifdef CONFIG_MEMCG
2c26fdd7
KH
43/*
44 * All "charge" functions with gfp_mask should use GFP_KERNEL or
45 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
46 * alloc memory but reclaims memory from all available zones. So, "where I want
47 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
48 * available but adding a rule is better. charge functions' gfp_mask should
49 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
50 * codes.
51 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
52 */
78fb7466 53
7a81b88c 54extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
e1a1cd59 55 gfp_t gfp_mask);
7a81b88c 56/* for swap handling */
8c7c6e34 57extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
72835c86 58 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
7a81b88c 59extern void mem_cgroup_commit_charge_swapin(struct page *page,
72835c86
JW
60 struct mem_cgroup *memcg);
61extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
7a81b88c 62
8289546e
HD
63extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
64 gfp_t gfp_mask);
925b7673
JW
65
66struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
fa9add64 67struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
569b846d
KH
68
69/* For coalescing uncharge for reducing memcg' overhead*/
70extern void mem_cgroup_uncharge_start(void);
71extern void mem_cgroup_uncharge_end(void);
72
3c541e14 73extern void mem_cgroup_uncharge_page(struct page *page);
69029cd5 74extern void mem_cgroup_uncharge_cache_page(struct page *page);
c9b0ed51 75
c3ac9a8a
JW
76bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
77 struct mem_cgroup *memcg);
c0ff4b85 78int task_in_mem_cgroup(struct task_struct *task, const struct mem_cgroup *memcg);
3062fc67 79
e42d9d5d 80extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
cf475ad2 81extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
a433658c 82extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
cf475ad2 83
e1aab161 84extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
d1a4c0b3 85extern struct mem_cgroup *mem_cgroup_from_cont(struct cgroup *cont);
e1aab161 86
2e4d4091 87static inline
587af308 88bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
2e4d4091 89{
587af308
JW
90 struct mem_cgroup *task_memcg;
91 bool match;
c3ac9a8a 92
2e4d4091 93 rcu_read_lock();
587af308
JW
94 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
95 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
2e4d4091 96 rcu_read_unlock();
c3ac9a8a 97 return match;
2e4d4091 98}
8a9f3ccd 99
c0ff4b85 100extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
d324236b 101
0030f535
JW
102extern void
103mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
104 struct mem_cgroup **memcgp);
c0ff4b85 105extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
50de1dd9 106 struct page *oldpage, struct page *newpage, bool migration_ok);
ae41be37 107
5660048c
JW
108struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *,
109 struct mem_cgroup *,
110 struct mem_cgroup_reclaim_cookie *);
111void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
112
58ae83db
KH
113/*
114 * For memory reclaim.
115 */
c56d5c7d
KK
116int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
117int mem_cgroup_inactive_file_is_low(struct lruvec *lruvec);
889976db 118int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
4d7dcca2 119unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
fa9add64 120void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
e222432b
BS
121extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
122 struct task_struct *p);
ab936cbc
KH
123extern void mem_cgroup_replace_page_cache(struct page *oldpage,
124 struct page *newpage);
58ae83db 125
c255a458 126#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
127extern int do_swap_account;
128#endif
f8d66542
HT
129
130static inline bool mem_cgroup_disabled(void)
131{
132 if (mem_cgroup_subsys.disabled)
133 return true;
134 return false;
135}
136
89c06bd5
KH
137void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
138 unsigned long *flags);
139
4331f7d3
KH
140extern atomic_t memcg_moving;
141
89c06bd5
KH
142static inline void mem_cgroup_begin_update_page_stat(struct page *page,
143 bool *locked, unsigned long *flags)
144{
145 if (mem_cgroup_disabled())
146 return;
147 rcu_read_lock();
148 *locked = false;
4331f7d3
KH
149 if (atomic_read(&memcg_moving))
150 __mem_cgroup_begin_update_page_stat(page, locked, flags);
89c06bd5
KH
151}
152
153void __mem_cgroup_end_update_page_stat(struct page *page,
154 unsigned long *flags);
155static inline void mem_cgroup_end_update_page_stat(struct page *page,
156 bool *locked, unsigned long *flags)
157{
158 if (mem_cgroup_disabled())
159 return;
160 if (*locked)
161 __mem_cgroup_end_update_page_stat(page, flags);
162 rcu_read_unlock();
163}
164
2a7106f2
GT
165void mem_cgroup_update_page_stat(struct page *page,
166 enum mem_cgroup_page_stat_item idx,
167 int val);
168
169static inline void mem_cgroup_inc_page_stat(struct page *page,
170 enum mem_cgroup_page_stat_item idx)
171{
172 mem_cgroup_update_page_stat(page, idx, 1);
173}
174
175static inline void mem_cgroup_dec_page_stat(struct page *page,
176 enum mem_cgroup_page_stat_item idx)
177{
178 mem_cgroup_update_page_stat(page, idx, -1);
179}
180
4e416953 181unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
0ae5e89c
YH
182 gfp_t gfp_mask,
183 unsigned long *total_scanned);
a63d83f4 184
68ae564b
DR
185void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
186static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
187 enum vm_event_item idx)
188{
189 if (mem_cgroup_disabled())
190 return;
191 __mem_cgroup_count_vm_event(mm, idx);
192}
ca3e0214 193#ifdef CONFIG_TRANSPARENT_HUGEPAGE
e94c8a9c 194void mem_cgroup_split_huge_fixup(struct page *head);
ca3e0214
KH
195#endif
196
f212ad7c
DN
197#ifdef CONFIG_DEBUG_VM
198bool mem_cgroup_bad_page_check(struct page *page);
199void mem_cgroup_print_bad_page(struct page *page);
200#endif
c255a458 201#else /* CONFIG_MEMCG */
7a81b88c
KH
202struct mem_cgroup;
203
204static inline int mem_cgroup_newpage_charge(struct page *page,
8289546e 205 struct mm_struct *mm, gfp_t gfp_mask)
8a9f3ccd
BS
206{
207 return 0;
208}
209
8289546e
HD
210static inline int mem_cgroup_cache_charge(struct page *page,
211 struct mm_struct *mm, gfp_t gfp_mask)
8a9f3ccd 212{
8289546e 213 return 0;
8a9f3ccd
BS
214}
215
8c7c6e34 216static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
72835c86 217 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
7a81b88c
KH
218{
219 return 0;
220}
221
222static inline void mem_cgroup_commit_charge_swapin(struct page *page,
72835c86 223 struct mem_cgroup *memcg)
7a81b88c
KH
224{
225}
226
72835c86 227static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
7a81b88c
KH
228{
229}
230
569b846d
KH
231static inline void mem_cgroup_uncharge_start(void)
232{
233}
234
235static inline void mem_cgroup_uncharge_end(void)
236{
237}
238
8a9f3ccd
BS
239static inline void mem_cgroup_uncharge_page(struct page *page)
240{
241}
242
69029cd5
KH
243static inline void mem_cgroup_uncharge_cache_page(struct page *page)
244{
245}
246
925b7673
JW
247static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
248 struct mem_cgroup *memcg)
08e552c6 249{
925b7673 250 return &zone->lruvec;
08e552c6
KH
251}
252
fa9add64
HD
253static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
254 struct zone *zone)
66e1707b 255{
925b7673 256 return &zone->lruvec;
66e1707b
BS
257}
258
e42d9d5d
WF
259static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
260{
261 return NULL;
262}
263
a433658c
KM
264static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
265{
266 return NULL;
267}
268
587af308 269static inline bool mm_match_cgroup(struct mm_struct *mm,
c0ff4b85 270 struct mem_cgroup *memcg)
bed7161a 271{
587af308 272 return true;
bed7161a
BS
273}
274
4c4a2214 275static inline int task_in_mem_cgroup(struct task_struct *task,
c0ff4b85 276 const struct mem_cgroup *memcg)
4c4a2214
DR
277{
278 return 1;
279}
280
c0ff4b85
R
281static inline struct cgroup_subsys_state
282 *mem_cgroup_css(struct mem_cgroup *memcg)
d324236b
WF
283{
284 return NULL;
285}
286
0030f535 287static inline void
ac39cf8c 288mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
0030f535 289 struct mem_cgroup **memcgp)
ae41be37 290{
ae41be37
KH
291}
292
c0ff4b85 293static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
50de1dd9 294 struct page *oldpage, struct page *newpage, bool migration_ok)
ae41be37
KH
295{
296}
297
5660048c
JW
298static inline struct mem_cgroup *
299mem_cgroup_iter(struct mem_cgroup *root,
300 struct mem_cgroup *prev,
301 struct mem_cgroup_reclaim_cookie *reclaim)
302{
303 return NULL;
304}
305
306static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
307 struct mem_cgroup *prev)
308{
309}
310
f8d66542
HT
311static inline bool mem_cgroup_disabled(void)
312{
313 return true;
314}
a636b327 315
14797e23 316static inline int
c56d5c7d 317mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
14797e23
KM
318{
319 return 1;
320}
321
56e49d21 322static inline int
c56d5c7d 323mem_cgroup_inactive_file_is_low(struct lruvec *lruvec)
56e49d21
RR
324{
325 return 1;
326}
327
a3d8e054 328static inline unsigned long
4d7dcca2 329mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
a3d8e054
KM
330{
331 return 0;
332}
333
fa9add64
HD
334static inline void
335mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
336 int increment)
3e2f41f1 337{
3e2f41f1
KM
338}
339
e222432b
BS
340static inline void
341mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
342{
343}
344
89c06bd5
KH
345static inline void mem_cgroup_begin_update_page_stat(struct page *page,
346 bool *locked, unsigned long *flags)
347{
348}
349
350static inline void mem_cgroup_end_update_page_stat(struct page *page,
351 bool *locked, unsigned long *flags)
352{
353}
354
2a7106f2
GT
355static inline void mem_cgroup_inc_page_stat(struct page *page,
356 enum mem_cgroup_page_stat_item idx)
357{
358}
359
360static inline void mem_cgroup_dec_page_stat(struct page *page,
361 enum mem_cgroup_page_stat_item idx)
d69b042f
BS
362{
363}
364
4e416953
BS
365static inline
366unsigned long mem_cgroup_soft_limit_reclaim(struct zone *zone, int order,
0ae5e89c
YH
367 gfp_t gfp_mask,
368 unsigned long *total_scanned)
4e416953
BS
369{
370 return 0;
371}
372
e94c8a9c 373static inline void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
374{
375}
376
456f998e
YH
377static inline
378void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
379{
380}
ab936cbc
KH
381static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
382 struct page *newpage)
383{
384}
c255a458 385#endif /* CONFIG_MEMCG */
78fb7466 386
c255a458 387#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
f212ad7c
DN
388static inline bool
389mem_cgroup_bad_page_check(struct page *page)
390{
391 return false;
392}
393
394static inline void
395mem_cgroup_print_bad_page(struct page *page)
396{
397}
398#endif
399
e1aab161
GC
400enum {
401 UNDER_LIMIT,
402 SOFT_LIMIT,
403 OVER_LIMIT,
404};
405
406struct sock;
cd59085a 407#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
e1aab161
GC
408void sock_update_memcg(struct sock *sk);
409void sock_release_memcg(struct sock *sk);
410#else
411static inline void sock_update_memcg(struct sock *sk)
412{
413}
414static inline void sock_release_memcg(struct sock *sk)
415{
416}
cd59085a 417#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
7ae1e1d0
GC
418
419#ifdef CONFIG_MEMCG_KMEM
420static inline bool memcg_kmem_enabled(void)
421{
422 return true;
423}
424
425/*
426 * In general, we'll do everything in our power to not incur in any overhead
427 * for non-memcg users for the kmem functions. Not even a function call, if we
428 * can avoid it.
429 *
430 * Therefore, we'll inline all those functions so that in the best case, we'll
431 * see that kmemcg is off for everybody and proceed quickly. If it is on,
432 * we'll still do most of the flag checking inline. We check a lot of
433 * conditions, but because they are pretty simple, they are expected to be
434 * fast.
435 */
436bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
437 int order);
438void __memcg_kmem_commit_charge(struct page *page,
439 struct mem_cgroup *memcg, int order);
440void __memcg_kmem_uncharge_pages(struct page *page, int order);
441
442/**
443 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
444 * @gfp: the gfp allocation flags.
445 * @memcg: a pointer to the memcg this was charged against.
446 * @order: allocation order.
447 *
448 * returns true if the memcg where the current task belongs can hold this
449 * allocation.
450 *
451 * We return true automatically if this allocation is not to be accounted to
452 * any memcg.
453 */
454static inline bool
455memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
456{
457 if (!memcg_kmem_enabled())
458 return true;
459
460 /*
461 * __GFP_NOFAIL allocations will move on even if charging is not
462 * possible. Therefore we don't even try, and have this allocation
463 * unaccounted. We could in theory charge it with
464 * res_counter_charge_nofail, but we hope those allocations are rare,
465 * and won't be worth the trouble.
466 */
467 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
468 return true;
469 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
470 return true;
471
472 /* If the test is dying, just let it go. */
473 if (unlikely(fatal_signal_pending(current)))
474 return true;
475
476 return __memcg_kmem_newpage_charge(gfp, memcg, order);
477}
478
479/**
480 * memcg_kmem_uncharge_pages: uncharge pages from memcg
481 * @page: pointer to struct page being freed
482 * @order: allocation order.
483 *
484 * there is no need to specify memcg here, since it is embedded in page_cgroup
485 */
486static inline void
487memcg_kmem_uncharge_pages(struct page *page, int order)
488{
489 if (memcg_kmem_enabled())
490 __memcg_kmem_uncharge_pages(page, order);
491}
492
493/**
494 * memcg_kmem_commit_charge: embeds correct memcg in a page
495 * @page: pointer to struct page recently allocated
496 * @memcg: the memcg structure we charged against
497 * @order: allocation order.
498 *
499 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
500 * failure of the allocation. if @page is NULL, this function will revert the
501 * charges. Otherwise, it will commit the memcg given by @memcg to the
502 * corresponding page_cgroup.
503 */
504static inline void
505memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
506{
507 if (memcg_kmem_enabled() && memcg)
508 __memcg_kmem_commit_charge(page, memcg, order);
509}
510
511#else
512static inline bool
513memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
514{
515 return true;
516}
517
518static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
519{
520}
521
522static inline void
523memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
524{
525}
526#endif /* CONFIG_MEMCG_KMEM */
8cdea7c0
BS
527#endif /* _LINUX_MEMCONTROL_H */
528