Skip to contentSkip to conversation

mm: percpu: fix incorrect size in pcpu_obj_full_size()

PATCH Superseded Yafang Shao 4 participants 9 messages Feb 10, 2023 – Feb 14, 2023

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mm: percpu: fix incorrect size in pcpu_obj_full_size()

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.
This helper is also used in non-memcg code, for example the tracepoint, so we should fix it.

It was found by code review when I was implementing bpf memory usage[1]. No real issue happens in production environment.

[1]. https://lwn.net/Articles/921991/

Cc Dennis Zhou <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Vasily Averin <[email protected]>
---
 mm/percpu-internal.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)
mm/percpu-internal.h +3 -1
index 70b1ea2..2a95b1f 100644
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/percpu.h>
+#include <linux/memcontrol.h>
 
 /*
  * pcpu_block_md is the metadata block struct.
@@ -125,7 +126,8 @@ static inline size_t pcpu_obj_full_size(size_t size)
 	size_t extra_size = 0;
 
 #ifdef CONFIG_MEMCG_KMEM
-	extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
+	if (!mem_cgroup_kmem_disabled())
+		extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
 #endif
 
 	return size * num_possible_cpus() + extra_size;
Signature · 1 line
1.8.3.1
maintainer
#1 lore raw
Quoted 22 lines · On Fri, 10 Feb 2023 15:49:47 +0000 Yafang Shao <[email protected]> wrote:

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.
This helper is also used in non-memcg code, for example the tracepoint, so we should fix it.

It was found by code review when I was implementing bpf memory usage[1]. No real issue happens in production environment.

...

mm/percpu-internal.h +1 -0
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/percpu.h>
+#include <linux/memcontrol.h>
 
 /*
  * pcpu_block_md is the metadata block struct.

mm/percpu-internal.h static inline size_t pcpu_obj_full_size(size_t size) · line 133

Quoted 9 lines
mm/percpu-internal.h +2 -1
@@ -125,7 +126,8 @@ static inline size_t pcpu_obj_full_size(size_t size)
 	size_t extra_size = 0;
 
 #ifdef CONFIG_MEMCG_KMEM
-	extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
+	if (!mem_cgroup_kmem_disabled())
+		extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
 #endif
 
 	return size * num_possible_cpus() + extra_size;

Seems risky at the first look - enabling kmemcg at runtime will make prior calculations based on pcpu_obj_full_size) incorrect. But as long as this is only used for accounting I guess that's OK.

What happens if we do a bunch of allocations with kmemcg enabled, then disable kmemcg then free those allocations, or some such thing. Does the accounting end up wrong?

The final sentence in the pcpu_obj_full_size() kerneldoc could do with an update - it still implies that the extra_size accounting is unconditional.

maintainer
#2 lore raw

Hello,

Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Fri, 10 Feb 2023 15:49:47 +0000 Yafang Shao <[email protected]> wrote:

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.
This helper is also used in non-memcg code, for example the tracepoint, so we should fix it.

It was found by code review when I was implementing bpf memory usage[1]. No real issue happens in production environment.

...

mm/percpu-internal.h +1 -0
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/percpu.h>
+#include <linux/memcontrol.h>
 
 /*
  * pcpu_block_md is the metadata block struct.
@@ -125,7 +126,8 @@ static inline size_t pcpu_obj_full_size(size_t size)
 	size_t extra_size = 0;
 
Quoted 8 lines

mm/percpu-internal.h static inline size_t pcpu_obj_full_size(size_t size) · line 133

Quoted 7 lines
mm/percpu-internal.h +2 -1
 #ifdef CONFIG_MEMCG_KMEM
-	extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
+	if (!mem_cgroup_kmem_disabled())
+		extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
 #endif
 
 	return size * num_possible_cpus() + extra_size;

Sorry I've been a bit mia...

Quoted 8 lines

Seems risky at the first look - enabling kmemcg at runtime will make prior calculations based on pcpu_obj_full_size) incorrect. But as long as this is only used for accounting I guess that's OK.

What happens if we do a bunch of allocations with kmemcg enabled, then disable kmemcg then free those allocations, or some such thing. Does the accounting end up wrong?

For now it works correctly because of 2 things. 1 - the function is only called by accounting. 2 - the free path doesn't consult mem_cgroup_kmem_disabled() but consults if a memcg exists for a percpu allocation. If accounting is enabled, we'd always account the additional memory for the memcg accounting. If it's not enabled, then percpu is well unaccounted for.

This function probably needs to be renamed a bit more carefully so it doesn't bleed outside of mm/percpu.c.

In short, I don't think this change is correct.

Quoted 4 lines

The final sentence in the pcpu_obj_full_size() kerneldoc could do with an update - it still implies that the extra_size accounting is unconditional.

Thanks,
Dennis

#3 lore raw
Quoted 57 lines · Dennis Zhou

Hello,

Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Fri, 10 Feb 2023 15:49:47 +0000 Yafang Shao <[email protected]> wrote:

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.
This helper is also used in non-memcg code, for example the tracepoint, so we should fix it.

It was found by code review when I was implementing bpf memory usage[1]. No real issue happens in production environment.

...

mm/percpu-internal.h +1 -0
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/percpu.h>
+#include <linux/memcontrol.h>
 
 /*
  * pcpu_block_md is the metadata block struct.
@@ -125,7 +126,8 @@ static inline size_t pcpu_obj_full_size(size_t size)
    size_t extra_size = 0;
 
Quoted 8 lines

mm/percpu-internal.h static inline size_t pcpu_obj_full_size(size_t size) · line 133

Quoted 7 lines
mm/percpu-internal.h +2 -1
 #ifdef CONFIG_MEMCG_KMEM
-   extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
+   if (!mem_cgroup_kmem_disabled())
+           extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
 #endif
 
    return size * num_possible_cpus() + extra_size;

Sorry I've been a bit mia...

Quoted 8 lines

Seems risky at the first look - enabling kmemcg at runtime will make prior calculations based on pcpu_obj_full_size) incorrect. But as long as this is only used for accounting I guess that's OK.

What happens if we do a bunch of allocations with kmemcg enabled, then disable kmemcg then free those allocations, or some such thing. Does the accounting end up wrong?

For now it works correctly because of 2 things. 1 - the function is only called by accounting. 2 - the free path doesn't consult mem_cgroup_kmem_disabled() but consults if a memcg exists for a percpu allocation. If accounting is enabled, we'd always account the additional memory for the memcg accounting. If it's not enabled, then percpu is well unaccounted for.

Quoted 3 lines

This function probably needs to be renamed a bit more carefully so it doesn't bleed outside of mm/percpu.c.

Do you have any suggestions on the new name ?

Quoted 1 line

In short, I don't think this change is correct.

Could you pls be more specific ?

Quoted 8 lines
Quoted 5 lines

The final sentence in the pcpu_obj_full_size() kerneldoc could do with an update - it still implies that the extra_size accounting is unconditional.

Thanks,
Dennis

Signature · 2 lines
Regards
Yafang
maintainer Acked-by
#4 lore raw
Quoted 62 lines · Yafang Shao
Quoted 57 lines · Dennis Zhou

Hello,

Quoted 26 lines · Andrew Morton
Quoted 24 lines · On Fri, 10 Feb 2023 15:49:47 +0000 Yafang Shao <[email protected]> wrote:

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.
This helper is also used in non-memcg code, for example the tracepoint, so we should fix it.

It was found by code review when I was implementing bpf memory usage[1]. No real issue happens in production environment.

...

mm/percpu-internal.h +1 -0
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/percpu.h>
+#include <linux/memcontrol.h>
 
 /*
  * pcpu_block_md is the metadata block struct.
@@ -125,7 +126,8 @@ static inline size_t pcpu_obj_full_size(size_t size)
    size_t extra_size = 0;
 
Quoted 8 lines

mm/percpu-internal.h static inline size_t pcpu_obj_full_size(size_t size) · line 133

Quoted 7 lines
mm/percpu-internal.h +2 -1
 #ifdef CONFIG_MEMCG_KMEM
-   extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
+   if (!mem_cgroup_kmem_disabled())
+           extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
 #endif
 
    return size * num_possible_cpus() + extra_size;

Sorry I've been a bit mia...

Quoted 8 lines

Seems risky at the first look - enabling kmemcg at runtime will make prior calculations based on pcpu_obj_full_size) incorrect. But as long as this is only used for accounting I guess that's OK.

What happens if we do a bunch of allocations with kmemcg enabled, then disable kmemcg then free those allocations, or some such thing. Does the accounting end up wrong?

For now it works correctly because of 2 things. 1 - the function is only called by accounting. 2 - the free path doesn't consult mem_cgroup_kmem_disabled() but consults if a memcg exists for a percpu allocation. If accounting is enabled, we'd always account the additional memory for the memcg accounting. If it's not enabled, then percpu is well unaccounted for.

Quoted 3 lines

This function probably needs to be renamed a bit more carefully so it doesn't bleed outside of mm/percpu.c.

Quoted 6 lines

Do you have any suggestions on the new name ?

Quoted 1 line

In short, I don't think this change is correct.

Could you pls be more specific ?

Hmmm I got ahead of myself. I misunderstood memcg_*_enabled() vs memcg_*_disabled(). Roman clarified it just now in [1]. I was imagining a world where we add disabled here and then eventually enabled would propagate here too.

Anothing that was on my mind is, should a percpu object be charged for the memcg space even if it's not in use. I now think it's yes and then for general accounting outside of memcg, this function is correct.

Andrew, I have nothing queued. Do you mind picking this up?

[1] https://lore.kernel.org/linux-mm/[email protected]/T/#u

Thanks,
Dennis

#5 lore raw
In reply to Andrew Morton
Quoted 39 lines · Andrew Morton
Quoted 22 lines · On Fri, 10 Feb 2023 15:49:47 +0000 Yafang Shao <[email protected]> wrote:

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.
This helper is also used in non-memcg code, for example the tracepoint, so we should fix it.

It was found by code review when I was implementing bpf memory usage[1]. No real issue happens in production environment.

...

mm/percpu-internal.h +1 -0
@@ -4,6 +4,7 @@
 
 #include <linux/types.h>
 #include <linux/percpu.h>
+#include <linux/memcontrol.h>
 
 /*
  * pcpu_block_md is the metadata block struct.

mm/percpu-internal.h static inline size_t pcpu_obj_full_size(size_t size) · line 133

Quoted 9 lines
mm/percpu-internal.h +2 -1
@@ -125,7 +126,8 @@ static inline size_t pcpu_obj_full_size(size_t size)
      size_t extra_size = 0;
 
 #ifdef CONFIG_MEMCG_KMEM
-     extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
+     if (!mem_cgroup_kmem_disabled())
+             extra_size += size / PCPU_MIN_ALLOC_SIZE * sizeof(struct obj_cgroup *);
 #endif
 
      return size * num_possible_cpus() + extra_size;

Seems risky at the first look - enabling kmemcg at runtime will make prior calculations based on pcpu_obj_full_size) incorrect. But as long as this is only used for accounting I guess that's OK.

Quoted 4 lines

What happens if we do a bunch of allocations with kmemcg enabled, then disable kmemcg then free those allocations, or some such thing. Does the accounting end up wrong?

That won't happen, because we can only enable and disable kmemcg at boot time.

Quoted 4 lines

The final sentence in the pcpu_obj_full_size() kerneldoc could do with an update - it still implies that the extra_size accounting is unconditional.

Sure.

Signature · 2 lines
Regards
Yafang
maintainer
#6 lore raw
Quoted 11 lines · On Sun, 12 Feb 2023 22:05:42 +0800 Yafang Shao <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Fri, 10 Feb 2023 15:49:47 +0000 Yafang Shao <[email protected]> wrote:

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.

...

Quoted 1 line
Quoted 6 lines
Quoted 4 lines

What happens if we do a bunch of allocations with kmemcg enabled, then disable kmemcg then free those allocations, or some such thing. Does the accounting end up wrong?

That won't happen, because we can only enable and disable kmemcg at boot time.

Ah, I misread the changelog. I suggest that "disabled via the kernel parameter ... at runtime" be "... at boot time".

#7 lore raw
Quoted 19 lines · Andrew Morton
Quoted 16 lines · On Sun, 12 Feb 2023 22:05:42 +0800 Yafang Shao <[email protected]> wrote:
Quoted 6 lines · Andrew Morton
Quoted 3 lines · On Fri, 10 Feb 2023 15:49:47 +0000 Yafang Shao <[email protected]> wrote:

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.

...

Quoted 5 lines

What happens if we do a bunch of allocations with kmemcg enabled, then disable kmemcg then free those allocations, or some such thing. Does the accounting end up wrong?

Quoted 5 lines
Quoted 1 line

That won't happen, because we can only enable and disable kmemcg at boot time.

Ah, I misread the changelog. I suggest that "disabled via the kernel parameter ... at runtime" be "... at boot time".

Thanks for the suggestion. I will change it.

Signature · 2 lines
Regards
Yafang
Reviewed-by on v1 1/1
#8 lore raw
In reply to Yafang Shao
Quoted 8 lines · Yafang Shao

The extra space which is used to store the obj_cgroup membership is only valid when kmemcg is enabled. The kmemcg can be disabled via the kernel parameter "cgroup.memory=nokmem" at runtime.
This helper is also used in non-memcg code, for example the tracepoint, so we should fix it.

It was found by code review when I was implementing bpf memory usage[1]. No real issue happens in production environment.

Probably not a stable material, but nice to have it fixed. Thanks!

Quoted 9 lines

[1]. https://lwn.net/Articles/921991/

Cc Dennis Zhou <[email protected]>
Cc Tejun Heo <[email protected]>
Cc Christoph Lameter <[email protected]>
Cc Roman Gushchin <[email protected]>
Cc Vasily Averin <[email protected]>

Thanks!