Skip to content

Commit 37d5985

Browse files
Roman Gushchintorvalds
authored andcommitted
mm: kmem: prepare remote memcg charging infra for interrupt contexts
Remote memcg charging API uses current->active_memcg to store the currently active memory cgroup, which overwrites the memory cgroup of the current process. It works well for normal contexts, but doesn't work for interrupt contexts: indeed, if an interrupt occurs during the execution of a section with an active memcg set, all allocations inside the interrupt will be charged to the active memcg set (given that we'll enable accounting for allocations from an interrupt context). But because the interrupt might have no relation to the active memcg set outside, it's obviously wrong from the accounting prospective. To resolve this problem, let's add a global percpu int_active_memcg variable, which will be used to store an active memory cgroup which will be used from interrupt contexts. set_active_memcg() will transparently use current->active_memcg or int_active_memcg depending on the context. To make the read part simple and transparent for the caller, let's introduce two new functions: - struct mem_cgroup *active_memcg(void), - struct mem_cgroup *get_active_memcg(void). They are returning the active memcg if it's set, hiding all implementation details: where to get it depending on the current context. Signed-off-by: Roman Gushchin <guro@fb.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Reviewed-by: Shakeel Butt <shakeelb@google.com> Cc: Johannes Weiner <hannes@cmpxchg.org> Cc: Michal Hocko <mhocko@kernel.org> Link: http://lkml.kernel.org/r/20200827225843.1270629-4-guro@fb.com Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 67f0286 commit 37d5985

2 files changed

Lines changed: 45 additions & 16 deletions

File tree

‎include/linux/sched/mm.h‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,7 @@ static inline void memalloc_nocma_restore(unsigned int flags)
279279
#endif
280280

281281
#ifdef CONFIG_MEMCG
282+
DECLARE_PER_CPU(struct mem_cgroup *, int_active_memcg);
282283
/**
283284
* set_active_memcg - Starts the remote memcg charging scope.
284285
* @memcg: memcg to charge.
@@ -293,8 +294,16 @@ static inline void memalloc_nocma_restore(unsigned int flags)
293294
static inline struct mem_cgroup *
294295
set_active_memcg(struct mem_cgroup *memcg)
295296
{
296-
struct mem_cgroup *old = current->active_memcg;
297-
current->active_memcg = memcg;
297+
struct mem_cgroup *old;
298+
299+
if (in_interrupt()) {
300+
old = this_cpu_read(int_active_memcg);
301+
this_cpu_write(int_active_memcg, memcg);
302+
} else {
303+
old = current->active_memcg;
304+
current->active_memcg = memcg;
305+
}
306+
298307
return old;
299308
}
300309
#else

‎mm/memcontrol.c‎

Lines changed: 34 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,9 @@ EXPORT_SYMBOL(memory_cgrp_subsys);
7373

7474
struct mem_cgroup *root_mem_cgroup __read_mostly;
7575

76+
/* Active memory cgroup to use from an interrupt context */
77+
DEFINE_PER_CPU(struct mem_cgroup *, int_active_memcg);
78+
7679
/* Socket memory accounting disabled? */
7780
static bool cgroup_memory_nosocket;
7881

@@ -1061,26 +1064,43 @@ struct mem_cgroup *get_mem_cgroup_from_page(struct page *page)
10611064
}
10621065
EXPORT_SYMBOL(get_mem_cgroup_from_page);
10631066

1064-
/**
1065-
* If current->active_memcg is non-NULL, do not fallback to current->mm->memcg.
1066-
*/
1067-
static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void)
1067+
static __always_inline struct mem_cgroup *active_memcg(void)
10681068
{
1069-
if (memcg_kmem_bypass())
1070-
return NULL;
1069+
if (in_interrupt())
1070+
return this_cpu_read(int_active_memcg);
1071+
else
1072+
return current->active_memcg;
1073+
}
10711074

1072-
if (unlikely(current->active_memcg)) {
1073-
struct mem_cgroup *memcg;
1075+
static __always_inline struct mem_cgroup *get_active_memcg(void)
1076+
{
1077+
struct mem_cgroup *memcg;
10741078

1075-
rcu_read_lock();
1079+
rcu_read_lock();
1080+
memcg = active_memcg();
1081+
if (memcg) {
10761082
/* current->active_memcg must hold a ref. */
1077-
if (WARN_ON_ONCE(!css_tryget(&current->active_memcg->css)))
1083+
if (WARN_ON_ONCE(!css_tryget(&memcg->css)))
10781084
memcg = root_mem_cgroup;
10791085
else
10801086
memcg = current->active_memcg;
1081-
rcu_read_unlock();
1082-
return memcg;
10831087
}
1088+
rcu_read_unlock();
1089+
1090+
return memcg;
1091+
}
1092+
1093+
/**
1094+
* If active memcg is set, do not fallback to current->mm->memcg.
1095+
*/
1096+
static __always_inline struct mem_cgroup *get_mem_cgroup_from_current(void)
1097+
{
1098+
if (memcg_kmem_bypass())
1099+
return NULL;
1100+
1101+
if (unlikely(active_memcg()))
1102+
return get_active_memcg();
1103+
10841104
return get_mem_cgroup_from_mm(current->mm);
10851105
}
10861106

@@ -2940,8 +2960,8 @@ __always_inline struct obj_cgroup *get_obj_cgroup_from_current(void)
29402960
return NULL;
29412961

29422962
rcu_read_lock();
2943-
if (unlikely(current->active_memcg))
2944-
memcg = rcu_dereference(current->active_memcg);
2963+
if (unlikely(active_memcg()))
2964+
memcg = active_memcg();
29452965
else
29462966
memcg = mem_cgroup_from_task(current);
29472967

0 commit comments

Comments
 (0)