]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - include/linux/memcontrol.h
Merge tag 'writeback-fixes' of git://git.kernel.org/pub/scm/linux/kernel/git/wfg...
[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;
2633d7a0 31struct kmem_cache;
78fb7466 32
68b4876d
SZ
33/*
34 * The corresponding mem_cgroup_stat_names is defined in mm/memcontrol.c,
35 * These two lists should keep in accord with each other.
36 */
37enum mem_cgroup_stat_index {
38 /*
39 * For MEM_CONTAINER_TYPE_ALL, usage = pagecache + rss.
40 */
41 MEM_CGROUP_STAT_CACHE, /* # of pages charged as cache */
42 MEM_CGROUP_STAT_RSS, /* # of pages charged as anon rss */
43 MEM_CGROUP_STAT_RSS_HUGE, /* # of pages charged as anon huge */
44 MEM_CGROUP_STAT_FILE_MAPPED, /* # of pages charged as file rss */
3ea67d06 45 MEM_CGROUP_STAT_WRITEBACK, /* # of pages under writeback */
68b4876d
SZ
46 MEM_CGROUP_STAT_SWAP, /* # of pages, swapped out */
47 MEM_CGROUP_STAT_NSTATS,
2a7106f2
GT
48};
49
5660048c
JW
50struct mem_cgroup_reclaim_cookie {
51 struct zone *zone;
52 int priority;
53 unsigned int generation;
54};
55
de57780d
MH
56enum mem_cgroup_filter_t {
57 VISIT, /* visit current node */
58 SKIP, /* skip the current node and continue traversal */
59 SKIP_TREE, /* skip the whole subtree and continue traversal */
60};
61
62/*
63 * mem_cgroup_filter_t predicate might instruct mem_cgroup_iter_cond how to
64 * iterate through the hierarchy tree. Each tree element is checked by the
65 * predicate before it is returned by the iterator. If a filter returns
66 * SKIP or SKIP_TREE then the iterator code continues traversal (with the
67 * next node down the hierarchy or the next node that doesn't belong under the
68 * memcg's subtree).
69 */
70typedef enum mem_cgroup_filter_t
71(*mem_cgroup_iter_filter)(struct mem_cgroup *memcg, struct mem_cgroup *root);
72
c255a458 73#ifdef CONFIG_MEMCG
2c26fdd7
KH
74/*
75 * All "charge" functions with gfp_mask should use GFP_KERNEL or
76 * (gfp_mask & GFP_RECLAIM_MASK). In current implementatin, memcg doesn't
77 * alloc memory but reclaims memory from all available zones. So, "where I want
78 * memory from" bits of gfp_mask has no meaning. So any bits of that field is
79 * available but adding a rule is better. charge functions' gfp_mask should
80 * be set to GFP_KERNEL or gfp_mask & GFP_RECLAIM_MASK for avoiding ambiguous
81 * codes.
82 * (Of course, if memcg does memory allocation in future, GFP_KERNEL is sane.)
83 */
78fb7466 84
7a81b88c 85extern int mem_cgroup_newpage_charge(struct page *page, struct mm_struct *mm,
e1a1cd59 86 gfp_t gfp_mask);
7a81b88c 87/* for swap handling */
8c7c6e34 88extern int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
72835c86 89 struct page *page, gfp_t mask, struct mem_cgroup **memcgp);
7a81b88c 90extern void mem_cgroup_commit_charge_swapin(struct page *page,
72835c86
JW
91 struct mem_cgroup *memcg);
92extern void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg);
7a81b88c 93
8289546e
HD
94extern int mem_cgroup_cache_charge(struct page *page, struct mm_struct *mm,
95 gfp_t gfp_mask);
925b7673
JW
96
97struct lruvec *mem_cgroup_zone_lruvec(struct zone *, struct mem_cgroup *);
fa9add64 98struct lruvec *mem_cgroup_page_lruvec(struct page *, struct zone *);
569b846d
KH
99
100/* For coalescing uncharge for reducing memcg' overhead*/
101extern void mem_cgroup_uncharge_start(void);
102extern void mem_cgroup_uncharge_end(void);
103
3c541e14 104extern void mem_cgroup_uncharge_page(struct page *page);
69029cd5 105extern void mem_cgroup_uncharge_cache_page(struct page *page);
c9b0ed51 106
c3ac9a8a
JW
107bool __mem_cgroup_same_or_subtree(const struct mem_cgroup *root_memcg,
108 struct mem_cgroup *memcg);
ffbdccf5
DR
109bool task_in_mem_cgroup(struct task_struct *task,
110 const struct mem_cgroup *memcg);
3062fc67 111
e42d9d5d 112extern struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page);
cf475ad2 113extern struct mem_cgroup *mem_cgroup_from_task(struct task_struct *p);
a433658c 114extern struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm);
cf475ad2 115
e1aab161 116extern struct mem_cgroup *parent_mem_cgroup(struct mem_cgroup *memcg);
182446d0 117extern struct mem_cgroup *mem_cgroup_from_css(struct cgroup_subsys_state *css);
e1aab161 118
2e4d4091 119static inline
587af308 120bool mm_match_cgroup(const struct mm_struct *mm, const struct mem_cgroup *memcg)
2e4d4091 121{
587af308
JW
122 struct mem_cgroup *task_memcg;
123 bool match;
c3ac9a8a 124
2e4d4091 125 rcu_read_lock();
587af308
JW
126 task_memcg = mem_cgroup_from_task(rcu_dereference(mm->owner));
127 match = __mem_cgroup_same_or_subtree(memcg, task_memcg);
2e4d4091 128 rcu_read_unlock();
c3ac9a8a 129 return match;
2e4d4091 130}
8a9f3ccd 131
c0ff4b85 132extern struct cgroup_subsys_state *mem_cgroup_css(struct mem_cgroup *memcg);
d324236b 133
0030f535
JW
134extern void
135mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
136 struct mem_cgroup **memcgp);
c0ff4b85 137extern void mem_cgroup_end_migration(struct mem_cgroup *memcg,
50de1dd9 138 struct page *oldpage, struct page *newpage, bool migration_ok);
ae41be37 139
de57780d
MH
140struct mem_cgroup *mem_cgroup_iter_cond(struct mem_cgroup *root,
141 struct mem_cgroup *prev,
142 struct mem_cgroup_reclaim_cookie *reclaim,
143 mem_cgroup_iter_filter cond);
144
145static inline struct mem_cgroup *mem_cgroup_iter(struct mem_cgroup *root,
146 struct mem_cgroup *prev,
147 struct mem_cgroup_reclaim_cookie *reclaim)
148{
149 return mem_cgroup_iter_cond(root, prev, reclaim, NULL);
150}
151
5660048c
JW
152void mem_cgroup_iter_break(struct mem_cgroup *, struct mem_cgroup *);
153
58ae83db
KH
154/*
155 * For memory reclaim.
156 */
c56d5c7d 157int mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec);
889976db 158int mem_cgroup_select_victim_node(struct mem_cgroup *memcg);
4d7dcca2 159unsigned long mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list);
fa9add64 160void mem_cgroup_update_lru_size(struct lruvec *, enum lru_list, int);
e222432b
BS
161extern void mem_cgroup_print_oom_info(struct mem_cgroup *memcg,
162 struct task_struct *p);
ab936cbc
KH
163extern void mem_cgroup_replace_page_cache(struct page *oldpage,
164 struct page *newpage);
58ae83db 165
519e5247
JW
166/**
167 * mem_cgroup_toggle_oom - toggle the memcg OOM killer for the current task
168 * @new: true to enable, false to disable
169 *
170 * Toggle whether a failed memcg charge should invoke the OOM killer
171 * or just return -ENOMEM. Returns the previous toggle state.
3812c8c8
JW
172 *
173 * NOTE: Any path that enables the OOM killer before charging must
174 * call mem_cgroup_oom_synchronize() afterward to finalize the
175 * OOM handling and clean up.
519e5247
JW
176 */
177static inline bool mem_cgroup_toggle_oom(bool new)
178{
179 bool old;
180
181 old = current->memcg_oom.may_oom;
182 current->memcg_oom.may_oom = new;
183
184 return old;
185}
186
187static inline void mem_cgroup_enable_oom(void)
188{
189 bool old = mem_cgroup_toggle_oom(true);
190
191 WARN_ON(old == true);
192}
193
194static inline void mem_cgroup_disable_oom(void)
195{
196 bool old = mem_cgroup_toggle_oom(false);
197
198 WARN_ON(old == false);
199}
200
3812c8c8
JW
201static inline bool task_in_memcg_oom(struct task_struct *p)
202{
203 return p->memcg_oom.in_memcg_oom;
204}
205
206bool mem_cgroup_oom_synchronize(void);
207
c255a458 208#ifdef CONFIG_MEMCG_SWAP
c077719b
KH
209extern int do_swap_account;
210#endif
f8d66542
HT
211
212static inline bool mem_cgroup_disabled(void)
213{
214 if (mem_cgroup_subsys.disabled)
215 return true;
216 return false;
217}
218
89c06bd5
KH
219void __mem_cgroup_begin_update_page_stat(struct page *page, bool *locked,
220 unsigned long *flags);
221
4331f7d3
KH
222extern atomic_t memcg_moving;
223
89c06bd5
KH
224static inline void mem_cgroup_begin_update_page_stat(struct page *page,
225 bool *locked, unsigned long *flags)
226{
227 if (mem_cgroup_disabled())
228 return;
229 rcu_read_lock();
230 *locked = false;
4331f7d3
KH
231 if (atomic_read(&memcg_moving))
232 __mem_cgroup_begin_update_page_stat(page, locked, flags);
89c06bd5
KH
233}
234
235void __mem_cgroup_end_update_page_stat(struct page *page,
236 unsigned long *flags);
237static inline void mem_cgroup_end_update_page_stat(struct page *page,
238 bool *locked, unsigned long *flags)
239{
240 if (mem_cgroup_disabled())
241 return;
242 if (*locked)
243 __mem_cgroup_end_update_page_stat(page, flags);
244 rcu_read_unlock();
245}
246
2a7106f2 247void mem_cgroup_update_page_stat(struct page *page,
68b4876d 248 enum mem_cgroup_stat_index idx,
2a7106f2
GT
249 int val);
250
251static inline void mem_cgroup_inc_page_stat(struct page *page,
68b4876d 252 enum mem_cgroup_stat_index idx)
2a7106f2
GT
253{
254 mem_cgroup_update_page_stat(page, idx, 1);
255}
256
257static inline void mem_cgroup_dec_page_stat(struct page *page,
68b4876d 258 enum mem_cgroup_stat_index idx)
2a7106f2
GT
259{
260 mem_cgroup_update_page_stat(page, idx, -1);
261}
262
de57780d
MH
263enum mem_cgroup_filter_t
264mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
a5b7c87f 265 struct mem_cgroup *root);
a63d83f4 266
68ae564b
DR
267void __mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx);
268static inline void mem_cgroup_count_vm_event(struct mm_struct *mm,
269 enum vm_event_item idx)
270{
271 if (mem_cgroup_disabled())
272 return;
273 __mem_cgroup_count_vm_event(mm, idx);
274}
ca3e0214 275#ifdef CONFIG_TRANSPARENT_HUGEPAGE
e94c8a9c 276void mem_cgroup_split_huge_fixup(struct page *head);
ca3e0214
KH
277#endif
278
f212ad7c
DN
279#ifdef CONFIG_DEBUG_VM
280bool mem_cgroup_bad_page_check(struct page *page);
281void mem_cgroup_print_bad_page(struct page *page);
282#endif
c255a458 283#else /* CONFIG_MEMCG */
7a81b88c
KH
284struct mem_cgroup;
285
286static inline int mem_cgroup_newpage_charge(struct page *page,
8289546e 287 struct mm_struct *mm, gfp_t gfp_mask)
8a9f3ccd
BS
288{
289 return 0;
290}
291
8289546e
HD
292static inline int mem_cgroup_cache_charge(struct page *page,
293 struct mm_struct *mm, gfp_t gfp_mask)
8a9f3ccd 294{
8289546e 295 return 0;
8a9f3ccd
BS
296}
297
8c7c6e34 298static inline int mem_cgroup_try_charge_swapin(struct mm_struct *mm,
72835c86 299 struct page *page, gfp_t gfp_mask, struct mem_cgroup **memcgp)
7a81b88c
KH
300{
301 return 0;
302}
303
304static inline void mem_cgroup_commit_charge_swapin(struct page *page,
72835c86 305 struct mem_cgroup *memcg)
7a81b88c
KH
306{
307}
308
72835c86 309static inline void mem_cgroup_cancel_charge_swapin(struct mem_cgroup *memcg)
7a81b88c
KH
310{
311}
312
569b846d
KH
313static inline void mem_cgroup_uncharge_start(void)
314{
315}
316
317static inline void mem_cgroup_uncharge_end(void)
318{
319}
320
8a9f3ccd
BS
321static inline void mem_cgroup_uncharge_page(struct page *page)
322{
323}
324
69029cd5
KH
325static inline void mem_cgroup_uncharge_cache_page(struct page *page)
326{
327}
328
925b7673
JW
329static inline struct lruvec *mem_cgroup_zone_lruvec(struct zone *zone,
330 struct mem_cgroup *memcg)
08e552c6 331{
925b7673 332 return &zone->lruvec;
08e552c6
KH
333}
334
fa9add64
HD
335static inline struct lruvec *mem_cgroup_page_lruvec(struct page *page,
336 struct zone *zone)
66e1707b 337{
925b7673 338 return &zone->lruvec;
66e1707b
BS
339}
340
e42d9d5d
WF
341static inline struct mem_cgroup *try_get_mem_cgroup_from_page(struct page *page)
342{
343 return NULL;
344}
345
a433658c
KM
346static inline struct mem_cgroup *try_get_mem_cgroup_from_mm(struct mm_struct *mm)
347{
348 return NULL;
349}
350
587af308 351static inline bool mm_match_cgroup(struct mm_struct *mm,
c0ff4b85 352 struct mem_cgroup *memcg)
bed7161a 353{
587af308 354 return true;
bed7161a
BS
355}
356
ffbdccf5
DR
357static inline bool task_in_mem_cgroup(struct task_struct *task,
358 const struct mem_cgroup *memcg)
4c4a2214 359{
ffbdccf5 360 return true;
4c4a2214
DR
361}
362
c0ff4b85
R
363static inline struct cgroup_subsys_state
364 *mem_cgroup_css(struct mem_cgroup *memcg)
d324236b
WF
365{
366 return NULL;
367}
368
0030f535 369static inline void
ac39cf8c 370mem_cgroup_prepare_migration(struct page *page, struct page *newpage,
0030f535 371 struct mem_cgroup **memcgp)
ae41be37 372{
ae41be37
KH
373}
374
c0ff4b85 375static inline void mem_cgroup_end_migration(struct mem_cgroup *memcg,
50de1dd9 376 struct page *oldpage, struct page *newpage, bool migration_ok)
ae41be37
KH
377{
378}
de57780d
MH
379static inline struct mem_cgroup *
380mem_cgroup_iter_cond(struct mem_cgroup *root,
381 struct mem_cgroup *prev,
382 struct mem_cgroup_reclaim_cookie *reclaim,
383 mem_cgroup_iter_filter cond)
384{
385 /* first call must return non-NULL, second return NULL */
386 return (struct mem_cgroup *)(unsigned long)!prev;
387}
ae41be37 388
5660048c
JW
389static inline struct mem_cgroup *
390mem_cgroup_iter(struct mem_cgroup *root,
391 struct mem_cgroup *prev,
392 struct mem_cgroup_reclaim_cookie *reclaim)
393{
394 return NULL;
395}
396
397static inline void mem_cgroup_iter_break(struct mem_cgroup *root,
398 struct mem_cgroup *prev)
399{
400}
401
f8d66542
HT
402static inline bool mem_cgroup_disabled(void)
403{
404 return true;
405}
a636b327 406
14797e23 407static inline int
c56d5c7d 408mem_cgroup_inactive_anon_is_low(struct lruvec *lruvec)
14797e23
KM
409{
410 return 1;
411}
412
a3d8e054 413static inline unsigned long
4d7dcca2 414mem_cgroup_get_lru_size(struct lruvec *lruvec, enum lru_list lru)
a3d8e054
KM
415{
416 return 0;
417}
418
fa9add64
HD
419static inline void
420mem_cgroup_update_lru_size(struct lruvec *lruvec, enum lru_list lru,
421 int increment)
3e2f41f1 422{
3e2f41f1
KM
423}
424
e222432b
BS
425static inline void
426mem_cgroup_print_oom_info(struct mem_cgroup *memcg, struct task_struct *p)
427{
428}
429
89c06bd5
KH
430static inline void mem_cgroup_begin_update_page_stat(struct page *page,
431 bool *locked, unsigned long *flags)
432{
433}
434
435static inline void mem_cgroup_end_update_page_stat(struct page *page,
436 bool *locked, unsigned long *flags)
437{
438}
439
519e5247
JW
440static inline bool mem_cgroup_toggle_oom(bool new)
441{
442 return false;
443}
444
445static inline void mem_cgroup_enable_oom(void)
446{
447}
448
449static inline void mem_cgroup_disable_oom(void)
450{
451}
452
3812c8c8
JW
453static inline bool task_in_memcg_oom(struct task_struct *p)
454{
455 return false;
456}
457
458static inline bool mem_cgroup_oom_synchronize(void)
459{
460 return false;
461}
462
2a7106f2 463static inline void mem_cgroup_inc_page_stat(struct page *page,
68b4876d 464 enum mem_cgroup_stat_index idx)
2a7106f2
GT
465{
466}
467
468static inline void mem_cgroup_dec_page_stat(struct page *page,
68b4876d 469 enum mem_cgroup_stat_index idx)
d69b042f
BS
470{
471}
472
4e416953 473static inline
de57780d
MH
474enum mem_cgroup_filter_t
475mem_cgroup_soft_reclaim_eligible(struct mem_cgroup *memcg,
a5b7c87f 476 struct mem_cgroup *root)
4e416953 477{
de57780d 478 return VISIT;
4e416953
BS
479}
480
e94c8a9c 481static inline void mem_cgroup_split_huge_fixup(struct page *head)
ca3e0214
KH
482{
483}
484
456f998e
YH
485static inline
486void mem_cgroup_count_vm_event(struct mm_struct *mm, enum vm_event_item idx)
487{
488}
ab936cbc
KH
489static inline void mem_cgroup_replace_page_cache(struct page *oldpage,
490 struct page *newpage)
491{
492}
c255a458 493#endif /* CONFIG_MEMCG */
78fb7466 494
c255a458 495#if !defined(CONFIG_MEMCG) || !defined(CONFIG_DEBUG_VM)
f212ad7c
DN
496static inline bool
497mem_cgroup_bad_page_check(struct page *page)
498{
499 return false;
500}
501
502static inline void
503mem_cgroup_print_bad_page(struct page *page)
504{
505}
506#endif
507
e1aab161
GC
508enum {
509 UNDER_LIMIT,
510 SOFT_LIMIT,
511 OVER_LIMIT,
512};
513
514struct sock;
cd59085a 515#if defined(CONFIG_INET) && defined(CONFIG_MEMCG_KMEM)
e1aab161
GC
516void sock_update_memcg(struct sock *sk);
517void sock_release_memcg(struct sock *sk);
518#else
519static inline void sock_update_memcg(struct sock *sk)
520{
521}
522static inline void sock_release_memcg(struct sock *sk)
523{
524}
cd59085a 525#endif /* CONFIG_INET && CONFIG_MEMCG_KMEM */
7ae1e1d0
GC
526
527#ifdef CONFIG_MEMCG_KMEM
a8964b9b 528extern struct static_key memcg_kmem_enabled_key;
749c5415
GC
529
530extern int memcg_limited_groups_array_size;
ebe945c2
GC
531
532/*
533 * Helper macro to loop through all memcg-specific caches. Callers must still
534 * check if the cache is valid (it is either valid or NULL).
535 * the slab_mutex must be held when looping through those caches
536 */
749c5415 537#define for_each_memcg_cache_index(_idx) \
91c777d8 538 for ((_idx) = 0; (_idx) < memcg_limited_groups_array_size; (_idx)++)
749c5415 539
7ae1e1d0
GC
540static inline bool memcg_kmem_enabled(void)
541{
a8964b9b 542 return static_key_false(&memcg_kmem_enabled_key);
7ae1e1d0
GC
543}
544
545/*
546 * In general, we'll do everything in our power to not incur in any overhead
547 * for non-memcg users for the kmem functions. Not even a function call, if we
548 * can avoid it.
549 *
550 * Therefore, we'll inline all those functions so that in the best case, we'll
551 * see that kmemcg is off for everybody and proceed quickly. If it is on,
552 * we'll still do most of the flag checking inline. We check a lot of
553 * conditions, but because they are pretty simple, they are expected to be
554 * fast.
555 */
556bool __memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg,
557 int order);
558void __memcg_kmem_commit_charge(struct page *page,
559 struct mem_cgroup *memcg, int order);
560void __memcg_kmem_uncharge_pages(struct page *page, int order);
561
2633d7a0 562int memcg_cache_id(struct mem_cgroup *memcg);
943a451a
GC
563int memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
564 struct kmem_cache *root_cache);
2633d7a0
GC
565void memcg_release_cache(struct kmem_cache *cachep);
566void memcg_cache_list_add(struct mem_cgroup *memcg, struct kmem_cache *cachep);
567
55007d84
GC
568int memcg_update_cache_size(struct kmem_cache *s, int num_groups);
569void memcg_update_array_size(int num_groups);
d7f25f8a
GC
570
571struct kmem_cache *
572__memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp);
573
1f458cbf 574void mem_cgroup_destroy_cache(struct kmem_cache *cachep);
7cf27982 575void kmem_cache_destroy_memcg_children(struct kmem_cache *s);
1f458cbf 576
7ae1e1d0
GC
577/**
578 * memcg_kmem_newpage_charge: verify if a new kmem allocation is allowed.
579 * @gfp: the gfp allocation flags.
580 * @memcg: a pointer to the memcg this was charged against.
581 * @order: allocation order.
582 *
583 * returns true if the memcg where the current task belongs can hold this
584 * allocation.
585 *
586 * We return true automatically if this allocation is not to be accounted to
587 * any memcg.
588 */
589static inline bool
590memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
591{
592 if (!memcg_kmem_enabled())
593 return true;
594
595 /*
596 * __GFP_NOFAIL allocations will move on even if charging is not
597 * possible. Therefore we don't even try, and have this allocation
598 * unaccounted. We could in theory charge it with
599 * res_counter_charge_nofail, but we hope those allocations are rare,
600 * and won't be worth the trouble.
601 */
602 if (!(gfp & __GFP_KMEMCG) || (gfp & __GFP_NOFAIL))
603 return true;
604 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
605 return true;
606
607 /* If the test is dying, just let it go. */
608 if (unlikely(fatal_signal_pending(current)))
609 return true;
610
611 return __memcg_kmem_newpage_charge(gfp, memcg, order);
612}
613
614/**
615 * memcg_kmem_uncharge_pages: uncharge pages from memcg
616 * @page: pointer to struct page being freed
617 * @order: allocation order.
618 *
619 * there is no need to specify memcg here, since it is embedded in page_cgroup
620 */
621static inline void
622memcg_kmem_uncharge_pages(struct page *page, int order)
623{
624 if (memcg_kmem_enabled())
625 __memcg_kmem_uncharge_pages(page, order);
626}
627
628/**
629 * memcg_kmem_commit_charge: embeds correct memcg in a page
630 * @page: pointer to struct page recently allocated
631 * @memcg: the memcg structure we charged against
632 * @order: allocation order.
633 *
634 * Needs to be called after memcg_kmem_newpage_charge, regardless of success or
635 * failure of the allocation. if @page is NULL, this function will revert the
636 * charges. Otherwise, it will commit the memcg given by @memcg to the
637 * corresponding page_cgroup.
638 */
639static inline void
640memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
641{
642 if (memcg_kmem_enabled() && memcg)
643 __memcg_kmem_commit_charge(page, memcg, order);
644}
645
d7f25f8a
GC
646/**
647 * memcg_kmem_get_cache: selects the correct per-memcg cache for allocation
648 * @cachep: the original global kmem cache
649 * @gfp: allocation flags.
650 *
651 * This function assumes that the task allocating, which determines the memcg
652 * in the page allocator, belongs to the same cgroup throughout the whole
653 * process. Misacounting can happen if the task calls memcg_kmem_get_cache()
654 * while belonging to a cgroup, and later on changes. This is considered
655 * acceptable, and should only happen upon task migration.
656 *
657 * Before the cache is created by the memcg core, there is also a possible
658 * imbalance: the task belongs to a memcg, but the cache being allocated from
659 * is the global cache, since the child cache is not yet guaranteed to be
660 * ready. This case is also fine, since in this case the GFP_KMEMCG will not be
661 * passed and the page allocator will not attempt any cgroup accounting.
662 */
663static __always_inline struct kmem_cache *
664memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
665{
666 if (!memcg_kmem_enabled())
667 return cachep;
668 if (gfp & __GFP_NOFAIL)
669 return cachep;
670 if (in_interrupt() || (!current->mm) || (current->flags & PF_KTHREAD))
671 return cachep;
672 if (unlikely(fatal_signal_pending(current)))
673 return cachep;
674
675 return __memcg_kmem_get_cache(cachep, gfp);
676}
7ae1e1d0 677#else
749c5415
GC
678#define for_each_memcg_cache_index(_idx) \
679 for (; NULL; )
680
b9ce5ef4
GC
681static inline bool memcg_kmem_enabled(void)
682{
683 return false;
684}
685
7ae1e1d0
GC
686static inline bool
687memcg_kmem_newpage_charge(gfp_t gfp, struct mem_cgroup **memcg, int order)
688{
689 return true;
690}
691
692static inline void memcg_kmem_uncharge_pages(struct page *page, int order)
693{
694}
695
696static inline void
697memcg_kmem_commit_charge(struct page *page, struct mem_cgroup *memcg, int order)
698{
699}
2633d7a0
GC
700
701static inline int memcg_cache_id(struct mem_cgroup *memcg)
702{
703 return -1;
704}
705
943a451a
GC
706static inline int
707memcg_register_cache(struct mem_cgroup *memcg, struct kmem_cache *s,
708 struct kmem_cache *root_cache)
2633d7a0
GC
709{
710 return 0;
711}
712
713static inline void memcg_release_cache(struct kmem_cache *cachep)
714{
715}
716
717static inline void memcg_cache_list_add(struct mem_cgroup *memcg,
718 struct kmem_cache *s)
719{
720}
d7f25f8a
GC
721
722static inline struct kmem_cache *
723memcg_kmem_get_cache(struct kmem_cache *cachep, gfp_t gfp)
724{
725 return cachep;
726}
7cf27982
GC
727
728static inline void kmem_cache_destroy_memcg_children(struct kmem_cache *s)
729{
730}
7ae1e1d0 731#endif /* CONFIG_MEMCG_KMEM */
8cdea7c0
BS
732#endif /* _LINUX_MEMCONTROL_H */
733