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