Skip to contentSkip to conversation

memcg: enable accounting for pids in nested pid namespaces

PATCH In review Vasily Averin 6 participants 12 messages Apr 22, 2021 – Jul 14, 2021 3 messages not in the archive

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 memcg: enable accounting for pids in nested pid namespaces

init_pid_ns.pid_cachep have enabled memcg accounting, though this setting was disabled for nested pid namespaces.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 7b777e22-5b0d-7444-343d-92cbfae5f8b4-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#2 lore raw
Quoted 25 lines · Vasily Averin

init_pid_ns.pid_cachep have enabled memcg accounting, though this setting was disabled for nested pid namespaces.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);
Signature · 1 line
1.8.3.1

It looks good to me! It makes total sense to apply the same rules to the root and non-root levels.

Btw, is there any reason why this patch is not included into the series?

Thanks!

#3 lore raw
Quoted 30 lines · Roman Gushchin
Quoted 25 lines · Vasily Averin

init_pid_ns.pid_cachep have enabled memcg accounting, though this setting was disabled for nested pid namespaces.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);
Signature · 1 line
1.8.3.1

It looks good to me! It makes total sense to apply the same rules to the root and non-root levels.

Quoted 3 lines

Btw, is there any reason why this patch is not included into the series?

It is a bugfix and I think it should be added to upstream ASAP. Another patches adds a new functionality, they can cause questions or objections and anyway can wait.

Thank you,
Vasily Averin

A message that is not in the archive (sent off-list or not yet received): 38945563-59ad-fb5e-9f7f-eb65ae4bf55e-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#5 lore raw
Quoted 31 lines · Vasily Averin
Quoted 30 lines · Roman Gushchin
Quoted 25 lines · Vasily Averin

init_pid_ns.pid_cachep have enabled memcg accounting, though this setting was disabled for nested pid namespaces.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);
Signature · 1 line
1.8.3.1

It looks good to me! It makes total sense to apply the same rules to the root and non-root levels.

Quoted 5 lines
Quoted 3 lines

Btw, is there any reason why this patch is not included into the series?

It is a bugfix and I think it should be added to upstream ASAP.

Then it would be really useful to add some details on why it's a bug, what kind of problems it causes, etc. If it has to be backported to stable, please, add cc stable/fixes tag.

Thanks!

#6 lore raw
Quoted 34 lines · Roman Gushchin
Quoted 33 lines · Vasily Averin
Quoted 32 lines · Roman Gushchin
Quoted 25 lines · Vasily Averin

init_pid_ns.pid_cachep have enabled memcg accounting, though this setting was disabled for nested pid namespaces.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);
Signature · 1 line
1.8.3.1

It looks good to me! It makes total sense to apply the same rules to the root and non-root levels.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Btw, is there any reason why this patch is not included into the series?

It is a bugfix and I think it should be added to upstream ASAP.

Then it would be really useful to add some details on why it's a bug, what kind of problems it causes, etc. If it has to be backported to stable, please, add cc stable/fixes tag.

I mean, in this case we already decided to account pids, but forget to do it. In another cases we did not have final decision about accounting.

I doubt we specially denied accounting for pids frem nested pid namespaces, especially because they consumes more memory.
We can expect that all pids are accounted -- but it does not happen in fact.

A message that is not in the archive (sent off-list or not yet received): cd6680e3-edd0-88fa-bb83-b9f2d5a65d5b-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#8 lore raw
Quoted 43 lines · Vasily Averin
Quoted 34 lines · Roman Gushchin
Quoted 33 lines · Vasily Averin
Quoted 32 lines · Roman Gushchin
Quoted 25 lines · Vasily Averin

init_pid_ns.pid_cachep have enabled memcg accounting, though this setting was disabled for nested pid namespaces.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);
Signature · 1 line
1.8.3.1

It looks good to me! It makes total sense to apply the same rules to the root and non-root levels.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

Btw, is there any reason why this patch is not included into the series?

It is a bugfix and I think it should be added to upstream ASAP.

Then it would be really useful to add some details on why it's a bug, what kind of problems it causes, etc. If it has to be backported to stable, please, add cc stable/fixes tag.

Quoted 6 lines

I mean, in this case we already decided to account pids, but forget to do it. In another cases we did not have final decision about accounting.

I doubt we specially denied accounting for pids frem nested pid namespaces, especially because they consumes more memory.
We can expect that all pids are accounted -- but it does not happen in fact.

As Roman said you should probably explain this in the cover letter and essentially justify why this should be backported. The thing with changes such as this is that it's easy for someone to reply and go "noone noticed for <n> years so why do we care now?". Otherwise:

Christian

#11 lore raw

v2 cover memcg: enable accounting for pids in nested pid namespaces

In reply to Vasily Averin

Pid was one the first kernel objects enabled for memcg accounting, see 5d097056c9a0 ("kmemcg: account certain kmem allocations to memcg")

init_pid_ns.pid_cachep marked by SLAB_ACCOUNT and we can expect that any new pids in the system are memcg-accounted.

Though recently I've noticed that it is wrong. nested pid namespaces creates own slab caches for pid objects, nested pids have increased size because contain id both for all parent and for own pid namespaces. The problem is that these slab caches are _NOT_ marked by SLAB_ACCOUNT,as a result any pids allocated in nested pid namespaces are not memcg-accounted.

Pid struct in nested pid namespace consumes up to 500 bytes memory, 100000 such objects gives us up to ~50Mb unaccounted memory. This allow container to exceed assigned memcg limits.

For me this issue lookslike bug and I would like to ask to push this fix both to upstream and to stable

Vasily Averin (1):
memcg: enable accounting for pids in nested pid namespaces

 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
Signature · 1 line
1.8.3.1
#12 lore raw

PATCH v2 1/1 memcg: enable accounting for pids in nested pid namespaces

In reply to Vasily Averin

Commit 5d097056c9a0 ("kmemcg: account certain kmem allocations to memcg") enabled memcg accounting for pids allocated from init_pid_ns.pid_cachep, but forgot to adjust the setting for nested pid namespaces.
As a result, pid memory is not accounted exactly where it is really needed, inside memcg-limited containers with their own pid namespaces.

Pid was one the first kernel objects enabled for memcg accounting. init_pid_ns.pid_cachep marked by SLAB_ACCOUNT and we can expect that any new pids in the system are memcg-accounted.

Though recently I've noticed that it is wrong. nested pid namespaces creates own slab caches for pid objects, nested pids have increased size because contain id both for all parent and for own pid namespaces. The problem is that these slab caches are _NOT_ marked by SLAB_ACCOUNT, as a result any pids allocated in nested pid namespaces are not memcg-accounted.

Pid struct in nested pid namespace consumes up to 500 bytes memory, 100000 such objects gives us up to ~50Mb unaccounted memory, this allow container to exceed assigned memcg limits.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);
Signature · 1 line
1.8.3.1
Reviewed-by on v2 1/1
#13 lore raw
Quoted 21 lines · Vasily Averin

Commit 5d097056c9a0 ("kmemcg: account certain kmem allocations to memcg") enabled memcg accounting for pids allocated from init_pid_ns.pid_cachep, but forgot to adjust the setting for nested pid namespaces.
As a result, pid memory is not accounted exactly where it is really needed, inside memcg-limited containers with their own pid namespaces.

Pid was one the first kernel objects enabled for memcg accounting. init_pid_ns.pid_cachep marked by SLAB_ACCOUNT and we can expect that any new pids in the system are memcg-accounted.

Though recently I've noticed that it is wrong. nested pid namespaces creates own slab caches for pid objects, nested pids have increased size because contain id both for all parent and for own pid namespaces. The problem is that these slab caches are _NOT_ marked by SLAB_ACCOUNT, as a result any pids allocated in nested pid namespaces are not memcg-accounted.

Pid struct in nested pid namespace consumes up to 500 bytes memory, 100000 such objects gives us up to ~50Mb unaccounted memory, this allow container to exceed assigned memcg limits.

Quoted 6 lines
Fixes 5d097056c9a0 ("kmemcg: account certain kmem allocations to memcg")
Reviewed-by Shakeel Butt on v2 1/1
#14 lore raw
In reply to Vasily Averin

Dear Andrew,
could you please pick up this patch and add

Thank you,
Vasily Averin

Quoted 45 lines · Vasily Averin

Commit 5d097056c9a0 ("kmemcg: account certain kmem allocations to memcg") enabled memcg accounting for pids allocated from init_pid_ns.pid_cachep, but forgot to adjust the setting for nested pid namespaces.
As a result, pid memory is not accounted exactly where it is really needed, inside memcg-limited containers with their own pid namespaces.

Pid was one the first kernel objects enabled for memcg accounting. init_pid_ns.pid_cachep marked by SLAB_ACCOUNT and we can expect that any new pids in the system are memcg-accounted.

Though recently I've noticed that it is wrong. nested pid namespaces creates own slab caches for pid objects, nested pids have increased size because contain id both for all parent and for own pid namespaces. The problem is that these slab caches are _NOT_ marked by SLAB_ACCOUNT, as a result any pids allocated in nested pid namespaces are not memcg-accounted.

Pid struct in nested pid namespace consumes up to 500 bytes memory, 100000 such objects gives us up to ~50Mb unaccounted memory, this allow container to exceed assigned memcg limits.

---
 kernel/pid_namespace.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
kernel/pid_namespace.c +2 -1
index 6cd6715..a46a372 100644
@@ -51,7 +51,8 @@ static struct kmem_cache *create_pid_cachep(unsigned int level)
 	mutex_lock(&pid_caches_mutex);
 	/* Name collision forces to do allocation under mutex. */
 	if (!*pkc)
-		*pkc = kmem_cache_create(name, len, 0, SLAB_HWCACHE_ALIGN, 0);
+		*pkc = kmem_cache_create(name, len, 0,
+					 SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, 0);
 	mutex_unlock(&pid_caches_mutex);
 	/* current can fail, but someone else can succeed. */
 	return READ_ONCE(*pkc);