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