Skip to contentSkip to conversation

memcg: Fix memcg_kmem_bypass() for remote memcg charging

PATCH Superseded Zefan Li 5 participants 20 messages May 13, 2020 – May 28, 2020

Subsystems (3)
lore
Share & tools
lore

Zefan Li

author
#0 lore raw

PATCH 1/1 memcg: Fix memcg_kmem_bypass() for remote memcg charging

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

---

No need to queue this for v5.7 as currently no upstream users of this memcg feature suffer from this bug.

---
 mm/memcontrol.c | 2 ++
 1 file changed, 2 insertions(+)
mm/memcontrol.c +2 -0
index a3b97f1..db836fc 100644
@@ -2802,6 +2802,8 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	if (unlikely(current->active_memcg))
+		return false;
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 	return false;
Signature · 1 line
2.7.4
maintainer
#1 lore raw
Quoted 18 lines · Li Zefan

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

---

No need to queue this for v5.7 as currently no upstream users of this memcg feature suffer from this bug.

---
 mm/memcontrol.c | 2 ++
 1 file changed, 2 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..db836fc 100644

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2806

Quoted 5 lines
mm/memcontrol.c +2 -0
@@ -2802,6 +2802,8 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	if (unlikely(current->active_memcg))
+		return false;

I am confused. Why the check below is insufficient? It checks for both mm and PF_KTHREAD?

Quoted 5 lines
mm/memcontrol.c +0 -0
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 	return false;
Signature · 1 line
2.7.4
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#2 lore raw
Quoted 20 lines · Michal Hocko
Quoted 19 lines · Li Zefan

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

---

No need to queue this for v5.7 as currently no upstream users of this memcg feature suffer from this bug.

---
 mm/memcontrol.c | 2 ++
 1 file changed, 2 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..db836fc 100644
@@ -2802,6 +2802,8 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
Quoted 8 lines

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2806

Quoted 4 lines
mm/memcontrol.c +2 -0
 static inline bool memcg_kmem_bypass(void)
 {
+	if (unlikely(current->active_memcg))
+		return false;

I am confused. Why the check below is insufficient? It checks for both mm and PF_KTHREAD?

memalloc_use_memcg(memcg);
alloc_page(GFP_KERNEL_ACCOUNT);
memalloc_unuse_memcg();

If we run above code in a workqueue thread the memory won't be charged to the specific memcg, because memcg_kmem_bypass() returns true in this case.

Quoted 3 lines
Quoted 3 lines
mm/memcontrol.c +0 -0
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 	return false;
maintainer
#3 lore raw

[Cc Roman - initial email is http://lkml.kernel.org/r/e6927a82-949c-bdfd-d717-0a14743c6759-hv44wF8Li93QT0dZR+AlfA@public.gmane.org]

Quoted 30 lines · Li Zefan
Quoted 20 lines · Michal Hocko
Quoted 19 lines · Li Zefan

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

---

No need to queue this for v5.7 as currently no upstream users of this memcg feature suffer from this bug.

---
 mm/memcontrol.c | 2 ++
 1 file changed, 2 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..db836fc 100644
@@ -2802,6 +2802,8 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
Quoted 8 lines

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2806

Quoted 4 lines
mm/memcontrol.c +2 -0
 static inline bool memcg_kmem_bypass(void)
 {
+	if (unlikely(current->active_memcg))
+		return false;

I am confused. Why the check below is insufficient? It checks for both mm and PF_KTHREAD?

Quoted 6 lines

memalloc_use_memcg(memcg);
alloc_page(GFP_KERNEL_ACCOUNT);
memalloc_unuse_memcg();

If we run above code in a workqueue thread the memory won't be charged to the specific memcg, because memcg_kmem_bypass() returns true in this case.

Ohh, right I have misread your patch. Sorry about that. A comment for the above branch would make it more clear. Something like

	/* Allow memalloc_use_memcg usage from kthread contexts */

On the other hand adding a code for an out of tree code is usually not welcome. But in this particular case the branch is correct for the existing code already so I am OK with it. Roman is de-facto kmem implementation maintainer so I will defer to him.

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines
mm/memcontrol.c +0 -0
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 	return false;
Signature · 2 lines
Michal Hocko
SUSE Labs
author
#4 lore raw

PATCH v2 1/1 memcg: Fix memcg_kmem_bypass() for remote memcg charging

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, it's better we fix it now.

---

v2: add a comment as sugguested by Michal. and add changelog to explain why upstream kernel needs this fix.

---

 mm/memcontrol.c | 3 +++
 1 file changed, 3 insertions(+)
mm/memcontrol.c +3 -0
index a3b97f1..43a12ed 100644
@@ -2802,6 +2802,9 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	/* Allow remote memcg charging in kthread contexts. */
+	if (unlikely(current->active_memcg))
+		return false;
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 	return false;
Signature · 1 line
2.7.4
#5 lore raw
Quoted 23 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, it's better we fix it now.

---

v2: add a comment as sugguested by Michal. and add changelog to explain why upstream kernel needs this fix.

---

 mm/memcontrol.c | 3 +++
 1 file changed, 3 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..43a12ed 100644

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2807

Quoted 6 lines
mm/memcontrol.c +3 -0
@@ -2802,6 +2802,9 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+       /* Allow remote memcg charging in kthread contexts. */
+       if (unlikely(current->active_memcg))
+               return false;

What about __GFP_ACCOUNT allocations in the interrupt context?

e.g.

memalloc_use_memcg(memcg);
--->interrupt --->alloc_page(GFP_KERNEL_ACCOUNT) in interrupt context. alloc_page(GFP_KERNEL_ACCOUNT); memalloc_unuse_memcg();

Quoted 6 lines
mm/memcontrol.c +0 -1
        if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
                return true;
        return false;
--

2.7.4

#7 lore raw
In reply to Zefan Li
Quoted 22 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, it's better we fix it now.

---

v2: add a comment as sugguested by Michal. and add changelog to explain why upstream kernel needs this fix.

---

 mm/memcontrol.c | 3 +++
 1 file changed, 3 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..43a12ed 100644

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2809

Quoted 8 lines
mm/memcontrol.c +3 -0
@@ -2802,6 +2802,9 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	/* Allow remote memcg charging in kthread contexts. */
+	if (unlikely(current->active_memcg))
+		return false;
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;

Shakeel is right about interrupts. How about something like this?

static inline bool memcg_kmem_bypass(void)

{
	if (in_interrupt())
		return true;

	if ((!current->mm || current->flags & PF_KTHREAD) && !current->active_memcg)
		return true;

	return false;
}

Thanks!

author
#8 lore raw
Quoted 35 lines · Roman Gushchin
Quoted 22 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, it's better we fix it now.

---

v2: add a comment as sugguested by Michal. and add changelog to explain why upstream kernel needs this fix.

---

 mm/memcontrol.c | 3 +++
 1 file changed, 3 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..43a12ed 100644

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2809

Quoted 8 lines
mm/memcontrol.c +3 -0
@@ -2802,6 +2802,9 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	/* Allow remote memcg charging in kthread contexts. */
+	if (unlikely(current->active_memcg))
+		return false;
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;

Shakeel is right about interrupts. How about something like this?

static inline bool memcg_kmem_bypass(void)

Quoted 10 lines
{
	if (in_interrupt())
		return true;

	if ((!current->mm || current->flags & PF_KTHREAD) && !current->active_memcg)
		return true;

	return false;
}

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

#9 lore raw
Quoted 43 lines · Zefan Li
Quoted 42 lines · Roman Gushchin
Quoted 22 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, it's better we fix it now.

---

v2: add a comment as sugguested by Michal. and add changelog to explain why upstream kernel needs this fix.

---

 mm/memcontrol.c | 3 +++
 1 file changed, 3 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..43a12ed 100644

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2809

Quoted 8 lines
mm/memcontrol.c +3 -0
@@ -2802,6 +2802,9 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	/* Allow remote memcg charging in kthread contexts. */
+	if (unlikely(current->active_memcg))
+		return false;
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;

Shakeel is right about interrupts. How about something like this?

static inline bool memcg_kmem_bypass(void)

{
	if (in_interrupt())
		return true;

	if ((!current->mm || current->flags & PF_KTHREAD) && !current->active_memcg)
		return true;
Quoted 6 lines
Quoted 3 lines
	return false;
}

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

I think now it's mostly a legacy of the opt-out kernel memory accounting. Actually we can relax this requirement by forcibly overcommit the memory cgroup if the allocation is happening from the irq context, and punish it afterwards. Idk how much we wanna this, hopefully nobody is allocating large non-temporarily objects from an irq.

Will you send a v3?

Thanks!

maintainer
#10 lore raw
Quoted 48 lines · Roman Gushchin
Quoted 47 lines · Zefan Li
Quoted 45 lines · Roman Gushchin
Quoted 22 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, it's better we fix it now.

---

v2: add a comment as sugguested by Michal. and add changelog to explain why upstream kernel needs this fix.

---

 mm/memcontrol.c | 3 +++
 1 file changed, 3 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..43a12ed 100644

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2809

Quoted 8 lines
mm/memcontrol.c +3 -0
@@ -2802,6 +2802,9 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	/* Allow remote memcg charging in kthread contexts. */
+	if (unlikely(current->active_memcg))
+		return false;
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;

Shakeel is right about interrupts. How about something like this?

static inline bool memcg_kmem_bypass(void)

{
	if (in_interrupt())
		return true;

	if ((!current->mm || current->flags & PF_KTHREAD) && !current->active_memcg)
		return true;

	return false;
}
Quoted 8 lines
Quoted 2 lines

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

I think now it's mostly a legacy of the opt-out kernel memory accounting. Actually we can relax this requirement by forcibly overcommit the memory cgroup if the allocation is happening from the irq context, and punish it afterwards. Idk how much we wanna this, hopefully nobody is allocating large non-temporarily objects from an irq.

I do not think we want to pretend that remote charging from the IRQ context is supported. Why don't we simply WARN_ON(in_interrupt()) there?

Quoted 4 lines

Will you send a v3?

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#11 lore raw
Quoted 52 lines · Michal Hocko
Quoted 51 lines · Roman Gushchin
Quoted 49 lines · Zefan Li
Quoted 35 lines · Roman Gushchin
Quoted 22 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, it's better we fix it now.

---

v2: add a comment as sugguested by Michal. and add changelog to explain why upstream kernel needs this fix.

---

 mm/memcontrol.c | 3 +++
 1 file changed, 3 insertions(+)
mm/memcontrol.c +0 -0
index a3b97f1..43a12ed 100644

mm/memcontrol.c static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg, · line 2809

Quoted 8 lines
mm/memcontrol.c +3 -0
@@ -2802,6 +2802,9 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
+	/* Allow remote memcg charging in kthread contexts. */
+	if (unlikely(current->active_memcg))
+		return false;
 	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;

Shakeel is right about interrupts. How about something like this?

static inline bool memcg_kmem_bypass(void)

Quoted 10 lines
{
	if (in_interrupt())
		return true;

	if ((!current->mm || current->flags & PF_KTHREAD) && !current->active_memcg)
		return true;

	return false;
}

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

Quoted 9 lines
Quoted 5 lines

I think now it's mostly a legacy of the opt-out kernel memory accounting. Actually we can relax this requirement by forcibly overcommit the memory cgroup if the allocation is happening from the irq context, and punish it afterwards. Idk how much we wanna this, hopefully nobody is allocating large non-temporarily objects from an irq.

I do not think we want to pretend that remote charging from the IRQ context is supported. Why don't we simply WARN_ON(in_interrupt()) there?

How about:

static inline bool memcg_kmem_bypass(void)

{
        if (in_interrupt()) {
                WARN_ON(current->active_memcg);
                return true;
        }

        /* Allow remote memcg charging in kthread contexts. */
        if ((!current->mm || (current->flags & PF_KTHREAD)) && !current->active_memcg)
                return true;
        return false;
}
maintainer
#12 lore raw
Quoted 2 lines · Li Zefan
Quoted 1 line · Michal Hocko

On Thu 14-05-20 15:52:59, Roman Gushchin wrote:

[...]

Quoted 13 lines
Quoted 12 lines
Quoted 8 lines
Quoted 2 lines

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

I think now it's mostly a legacy of the opt-out kernel memory accounting. Actually we can relax this requirement by forcibly overcommit the memory cgroup if the allocation is happening from the irq context, and punish it afterwards. Idk how much we wanna this, hopefully nobody is allocating large non-temporarily objects from an irq.

I do not think we want to pretend that remote charging from the IRQ context is supported. Why don't we simply WARN_ON(in_interrupt()) there?

Quoted 8 lines

How about:

static inline bool memcg_kmem_bypass(void)

{
        if (in_interrupt()) {
                WARN_ON(current->active_memcg);
                return true;
        }

Why not simply

	if (WARN_ON_ONCE(in_interrupt())
		return true;

the idea is that we want to catch any __GFP_ACCOUNT user from IRQ context because this just doesn't work and we do not plan to support it for now and foreseeable future. If this is reduced only to active_memcg then we are only getting a partial picture.

Quoted 6 lines
        /* Allow remote memcg charging in kthread contexts. */
        if ((!current->mm || (current->flags & PF_KTHREAD)) && !current->active_memcg)
                return true;
        return false;
}
Signature · 2 lines
Michal Hocko
SUSE Labs
#13 lore raw
Quoted 29 lines · Michal Hocko
Quoted 2 lines · Li Zefan
Quoted 1 line · Michal Hocko

On Thu 14-05-20 15:52:59, Roman Gushchin wrote:

[...]

Quoted 13 lines
Quoted 12 lines
Quoted 8 lines
Quoted 2 lines

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

I think now it's mostly a legacy of the opt-out kernel memory accounting. Actually we can relax this requirement by forcibly overcommit the memory cgroup if the allocation is happening from the irq context, and punish it afterwards. Idk how much we wanna this, hopefully nobody is allocating large non-temporarily objects from an irq.

I do not think we want to pretend that remote charging from the IRQ context is supported. Why don't we simply WARN_ON(in_interrupt()) there?

Quoted 8 lines

How about:

static inline bool memcg_kmem_bypass(void)

{
        if (in_interrupt()) {
                WARN_ON(current->active_memcg);
                return true;
        }

Why not simply

Quoted 8 lines
        if (WARN_ON_ONCE(in_interrupt())
                return true;

the idea is that we want to catch any __GFP_ACCOUNT user from IRQ context because this just doesn't work and we do not plan to support it for now and foreseeable future. If this is reduced only to active_memcg then we are only getting a partial picture.

There are SLAB_ACCOUNT kmem caches which do allocations in IRQ context (see sk_prot_alloc()), so, either we make charging work in IRQ or no warnings at all.

#14 lore raw
Quoted 28 lines · Shakeel Butt
Quoted 27 lines · Michal Hocko
Quoted 2 lines · Li Zefan
Quoted 1 line · Michal Hocko

On Thu 14-05-20 15:52:59, Roman Gushchin wrote:

[...]

Quoted 21 lines
Quoted 12 lines
Quoted 8 lines
Quoted 2 lines

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

I think now it's mostly a legacy of the opt-out kernel memory accounting. Actually we can relax this requirement by forcibly overcommit the memory cgroup if the allocation is happening from the irq context, and punish it afterwards. Idk how much we wanna this, hopefully nobody is allocating large non-temporarily objects from an irq.

I do not think we want to pretend that remote charging from the IRQ context is supported. Why don't we simply WARN_ON(in_interrupt()) there?

How about:

static inline bool memcg_kmem_bypass(void)

{
        if (in_interrupt()) {
                WARN_ON(current->active_memcg);
                return true;
        }
Quoted 8 lines
Quoted 8 lines

Why not simply

        if (WARN_ON_ONCE(in_interrupt())
                return true;

the idea is that we want to catch any __GFP_ACCOUNT user from IRQ context because this just doesn't work and we do not plan to support it for now and foreseeable future.

Actually, why not?
It should be fairly simple, especially after the rework of the slab controller.

Quoted 7 lines

If this is reduced only to active_memcg

Quoted 2 lines

then we are only getting a partial picture.

There are SLAB_ACCOUNT kmem caches which do allocations in IRQ context (see sk_prot_alloc()), so, either we make charging work in IRQ or no warnings at all.

I agree. Actually, there is nothing wrong to warn about, it's just a limitation of the current accounting implementation.

Thanks!

maintainer
#15 lore raw
In reply to Shakeel Butt
Quoted 39 lines · Shakeel Butt
Quoted 29 lines · Michal Hocko
Quoted 2 lines · Li Zefan
Quoted 1 line · Michal Hocko

On Thu 14-05-20 15:52:59, Roman Gushchin wrote:

[...]

Quoted 13 lines
Quoted 12 lines
Quoted 8 lines
Quoted 2 lines

I thought the user should ensure not do this, but now I think it makes sense to just bypass the interrupt case.

I think now it's mostly a legacy of the opt-out kernel memory accounting. Actually we can relax this requirement by forcibly overcommit the memory cgroup if the allocation is happening from the irq context, and punish it afterwards. Idk how much we wanna this, hopefully nobody is allocating large non-temporarily objects from an irq.

I do not think we want to pretend that remote charging from the IRQ context is supported. Why don't we simply WARN_ON(in_interrupt()) there?

Quoted 8 lines

How about:

static inline bool memcg_kmem_bypass(void)

{
        if (in_interrupt()) {
                WARN_ON(current->active_memcg);
                return true;
        }

Why not simply

Quoted 8 lines
        if (WARN_ON_ONCE(in_interrupt())
                return true;

the idea is that we want to catch any __GFP_ACCOUNT user from IRQ context because this just doesn't work and we do not plan to support it for now and foreseeable future. If this is reduced only to active_memcg then we are only getting a partial picture.

Quoted 3 lines

There are SLAB_ACCOUNT kmem caches which do allocations in IRQ context (see sk_prot_alloc()), so, either we make charging work in IRQ or no warnings at all.

OK, I see. I wasn't aware that those caches are used from IRQ context.

Signature · 2 lines
Michal Hocko
SUSE Labs
author
#16 lore raw

PATCH v3 1/1 memcg: Fix memcg_kmem_bypass() for remote memcg charging

In reply to Michal Hocko

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, and it's nothing wrong for this usage, it's better we fix it now.

---

v3: bypass __GFP_ACCOUNT allocations in interrupt contexts.

---
 mm/memcontrol.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/memcontrol.c +6 -1
index a3b97f1..3c7717a 100644
@@ -2802,7 +2802,12 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (in_interrupt())
+		return true;
+
+	/* Allow remote memcg charging in kthread contexts. */
+	if ((!current->mm || (current->flags & PF_KTHREAD)) &&
+	     !current->active_memcg)
 		return true;
 	return false;
 }
Signature · 1 line
2.7.4
Reviewed-by on v3 1/1
#17 lore raw
Quoted 10 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, and it's nothing wrong for this usage, it's better we fix it now.

---

Thanks!

Quoted 28 lines

v3: bypass __GFP_ACCOUNT allocations in interrupt contexts.

---
 mm/memcontrol.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/memcontrol.c +6 -1
index a3b97f1..3c7717a 100644
@@ -2802,7 +2802,12 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (in_interrupt())
+		return true;
+
+	/* Allow remote memcg charging in kthread contexts. */
+	if ((!current->mm || (current->flags & PF_KTHREAD)) &&
+	     !current->active_memcg)
 		return true;
 	return false;
 }
Signature · 1 line
2.7.4
Reviewed-by on v3 1/1
#18 lore raw
In reply to Zefan Li
Quoted 10 lines · Zefan Li

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, and it's nothing wrong for this usage, it's better we fix it now.

maintainer Acked-by on v3 1/1
#19 lore raw
In reply to Zefan Li
Quoted 9 lines · Li Zefan

While trying to use remote memcg charging in an out-of-tree kernel module I found it's not working, because the current thread is a workqueue thread.

As we will probably encounter this issue in the future as the users of memalloc_use_memcg() grow, and it's nothing wrong for this usage, it's better we fix it now.

Quoted 28 lines

---

v3: bypass __GFP_ACCOUNT allocations in interrupt contexts.

---
 mm/memcontrol.c | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
mm/memcontrol.c +6 -1
index a3b97f1..3c7717a 100644
@@ -2802,7 +2802,12 @@ static void memcg_schedule_kmem_cache_create(struct mem_cgroup *memcg,
 
 static inline bool memcg_kmem_bypass(void)
 {
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (in_interrupt())
+		return true;
+
+	/* Allow remote memcg charging in kthread contexts. */
+	if ((!current->mm || (current->flags & PF_KTHREAD)) &&
+	     !current->active_memcg)
 		return true;
 	return false;
 }
Signature · 1 line
2.7.4
Signature · 2 lines
Michal Hocko
SUSE Labs