Skip to contentSkip to conversation

memcg accounting from OpenVZ

PATCH In review Vasily Averin 21 participants 181 messages Mar 9, 2021 – Feb 28, 2022 22 messages not in the archive

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

cover memcg accounting from OpenVZ

OpenVZ many years accounted memory of few kernel objects,
this helps us to prevent host memory abuse from inside memcg-limited container.

Vasily Averin (9):
memcg: accounting for allocations called with disabled BH
memcg: accounting for fib6_nodes cache
memcg: accounting for ip6_dst_cache
memcg: accounting for fib_rules
memcg: accounting for ip_fib caches
memcg: accounting for fasync_cache
memcg: accounting for mnt_cache entries
memcg: accounting for tty_struct objects
memcg: accounting for ldt_struct objects

 arch/x86/kernel/ldt.c | 7 ++++---
 drivers/tty/tty_io.c  | 4 ++--
 fs/fcntl.c            | 3 ++-
 fs/namespace.c        | 5 +++--
 mm/memcontrol.c       | 2 +-
 net/core/fib_rules.c  | 4 ++--
 net/ipv4/fib_trie.c   | 4 ++--
 net/ipv6/ip6_fib.c    | 2 +-
 net/ipv6/route.c      | 2 +-
 9 files changed, 18 insertions(+), 15 deletions(-)
Signature · 1 line
1.8.3.1
#1 lore raw
Quoted 4 lines · Vasily Averin

OpenVZ many years accounted memory of few kernel objects,
this helps us to prevent host memory abuse from inside memcg-limited container.

The text is cryptic but I am assuming you wanted to say that OpenVZ has remained on a kernel which was still on opt-out kmem accounting i.e. <4.5. Now OpenVZ wants to move to a newer kernel and thus these patches are needed, right?

#2 lore raw
Quoted 10 lines · Shakeel Butt
Quoted 4 lines · Vasily Averin

OpenVZ many years accounted memory of few kernel objects,
this helps us to prevent host memory abuse from inside memcg-limited container.

The text is cryptic but I am assuming you wanted to say that OpenVZ has remained on a kernel which was still on opt-out kmem accounting i.e. <4.5. Now OpenVZ wants to move to a newer kernel and thus these patches are needed, right?

Something like this.
Frankly speaking I badly understand which arguments should I provide to upstream to enable accounting for some new king of objects.

OpenVZ used own accounting subsystem since 2001 (i.e. since v2.2.x linux kernels) and we have accounted all required kernel objects by using our own patches. When memcg was added to upstream Vladimir Davydov added accounting of some objects to upstream but did not skipped another ones.
Now OpenVZ uses RHEL7-based kernels with cgroup v1 in production, and we still account "skipped" objects by our own patches just because we accounted such objects before. We're working on rebase to new kernels and we prefer to push our old patches to upstream.

Thank you,
Vasily Averin

#3 lore raw
Quoted 16 lines · Vasily Averin
Quoted 10 lines · Shakeel Butt
Quoted 4 lines · Vasily Averin

OpenVZ many years accounted memory of few kernel objects,
this helps us to prevent host memory abuse from inside memcg-limited container.

The text is cryptic but I am assuming you wanted to say that OpenVZ has remained on a kernel which was still on opt-out kmem accounting i.e. <4.5. Now OpenVZ wants to move to a newer kernel and thus these patches are needed, right?

Something like this.
Frankly speaking I badly understand which arguments should I provide to upstream to enable accounting for some new king of objects.

Quoted 7 lines

OpenVZ used own accounting subsystem since 2001 (i.e. since v2.2.x linux kernels) and we have accounted all required kernel objects by using our own patches. When memcg was added to upstream Vladimir Davydov added accounting of some objects to upstream but did not skipped another ones.
Now OpenVZ uses RHEL7-based kernels with cgroup v1 in production, and we still account "skipped" objects by our own patches just because we accounted such objects before. We're working on rebase to new kernels and we prefer to push our old patches to upstream.

That is certainly an interesting information. But for a changelog it would be more appropriate to provide information about how much memory user can induce and whether there is any way to limit that memory by other means. How practical those other means are and which usecases will benefit from the containment.

Signature · 2 lines
Michal Hocko
SUSE Labs
#4 lore raw
Quoted 24 lines · Michal Hocko
Quoted 15 lines · Vasily Averin
Quoted 9 lines · Shakeel Butt
Quoted 3 lines · Vasily Averin

OpenVZ many years accounted memory of few kernel objects,
this helps us to prevent host memory abuse from inside memcg-limited container.

The text is cryptic but I am assuming you wanted to say that OpenVZ has remained on a kernel which was still on opt-out kmem accounting i.e. <4.5. Now OpenVZ wants to move to a newer kernel and thus these patches are needed, right?

Something like this.
Frankly speaking I badly understand which arguments should I provide to upstream to enable accounting for some new king of objects.

Quoted 7 lines

OpenVZ used own accounting subsystem since 2001 (i.e. since v2.2.x linux kernels) and we have accounted all required kernel objects by using our own patches. When memcg was added to upstream Vladimir Davydov added accounting of some objects to upstream but did not skipped another ones.
Now OpenVZ uses RHEL7-based kernels with cgroup v1 in production, and we still account "skipped" objects by our own patches just because we accounted such objects before. We're working on rebase to new kernels and we prefer to push our old patches to upstream.

Quoted 5 lines

That is certainly an interesting information. But for a changelog it would be more appropriate to provide information about how much memory user can induce and whether there is any way to limit that memory by other means. How practical those other means are and which usecases will benefit from the containment.

Right now I would like to understand how should I argument my requests about accounting of new kind of objects.

Which description it enough to enable object accounting?
Could you please specify some edge rules?
Should I push such patches trough this list?
Is it probably better to send them to mailing lists of according subsystems? Should I notify them somehow at least?

"untrusted netadmin inside memcg-limited container can create unlimited number of routing entries, trigger OOM on host that will be unable to find the reason of memory shortage and kill huge"

"each mount inside memcg-limited container creates non-accounted mount object,
but new mount namespace creation consumes huge piece of non-accounted memory for cloned mounts"

"unprivileged user inside memcg-limited container can create non-accounted multi-page per-thread kernel objects for LDT"

"non-accounted multi-page tty objects can be created from inside memcg-limited container"

"unprivileged user inside memcg-limited container can trigger creation of huge number of non-accounted fasync_struct objects"

Thank you,
Vasily Averin

#5 lore raw
Quoted 1 line · Vasily Averin

On 3/10/21 1:41 PM, Michal Hocko wrote:

[...]

Quoted 10 lines
Quoted 5 lines

That is certainly an interesting information. But for a changelog it would be more appropriate to provide information about how much memory user can induce and whether there is any way to limit that memory by other means. How practical those other means are and which usecases will benefit from the containment.

Right now I would like to understand how should I argument my requests about accounting of new kind of objects.

Which description it enough to enable object accounting?

Doesn't the above paragraph give you a hint?

Quoted 1 line

Could you please specify some edge rules?

There are no strong rules AFAIK. I would say that it is important is that the user can trigger a lot of or unbound amount of objects.

Quoted 1 line

Should I push such patches trough this list?

yes linux-mm and ccing memcg maintainers is the proper way. It would be great to CC maintainers of the affected subsystem as well.

Quoted 6 lines

Is it probably better to send them to mailing lists of according subsystems?

Should I notify them somehow at least?

"untrusted netadmin inside memcg-limited container can create unlimited number of routing entries, trigger OOM on host that will be unable to find the reason of memory shortage and kill huge"

Quoted 8 lines

"each mount inside memcg-limited container creates non-accounted mount object,
but new mount namespace creation consumes huge piece of non-accounted memory for cloned mounts"

"unprivileged user inside memcg-limited container can create non-accounted multi-page per-thread kernel objects for LDT"

"non-accounted multi-page tty objects can be created from inside memcg-limited container"

"unprivileged user inside memcg-limited container can trigger creation of huge number of non-accounted fasync_struct objects"

OK, that sounds better to me. It would be also great if you can mention whether there are any other means to limit those objects if there are any.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#7 lore raw

v2 cover memcg accounting from OpenVZ

OpenVZ used own accounting subsystem since 2001 (i.e. since v2.2.x linux kernels) and we have accounted all required kernel objects by using our own patches. When memcg was added to upstream Vladimir Davydov added accounting of some objects to upstream but did not skipped another ones.
Now OpenVZ uses RHEL7-based kernels with cgroup v1 in production, and we still account "skipped" objects by our own patches just because we accounted such objects before. We're working on rebase to new kernels and we prefer to push our old patches to upstream.

v2:
- squashed old patch 1 "accounting for allocations called with disabled BH" with old patch 2 "accounting for fib6_nodes cache" used such kind of memory allocation
- improved patch description
- subsystem maintainers added to cc:

Vasily Averin (8):
memcg: accounting for fib6_nodes cache
memcg: accounting for ip6_dst_cache
memcg: accounting for fib_rules
memcg: accounting for ip_fib caches
memcg: accounting for fasync_cache
memcg: accounting for mnt_cache entries
memcg: accounting for tty_struct objects
memcg: accounting for ldt_struct objects

 arch/x86/kernel/ldt.c | 7 ++++---
 drivers/tty/tty_io.c  | 4 ++--
 fs/fcntl.c            | 3 ++-
 fs/namespace.c        | 5 +++--
 mm/memcontrol.c       | 2 +-
 net/core/fib_rules.c  | 4 ++--
 net/ipv4/fib_trie.c   | 4 ++--
 net/ipv6/ip6_fib.c    | 2 +-
 net/ipv6/route.c      | 2 +-
 9 files changed, 18 insertions(+), 15 deletions(-)
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): dddf6b29-debd-dcb5-62d0-74909d610edb-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#9 lore raw

Michal, Shakeel, Roman,
thank you very much for your help.

Quoted 35 lines · Vasily Averin

OpenVZ used own accounting subsystem since 2001 (i.e. since v2.2.x linux kernels) and we have accounted all required kernel objects by using our own patches. When memcg was added to upstream Vladimir Davydov added accounting of some objects to upstream but did not skipped another ones.
Now OpenVZ uses RHEL7-based kernels with cgroup v1 in production, and we still account "skipped" objects by our own patches just because we accounted such objects before. We're working on rebase to new kernels and we prefer to push our old patches to upstream.

v2:
- squashed old patch 1 "accounting for allocations called with disabled BH" with old patch 2 "accounting for fib6_nodes cache" used such kind of memory allocation
- improved patch description
- subsystem maintainers added to cc:

Vasily Averin (8):
memcg: accounting for fib6_nodes cache
memcg: accounting for ip6_dst_cache
memcg: accounting for fib_rules
memcg: accounting for ip_fib caches
memcg: accounting for fasync_cache
memcg: accounting for mnt_cache entries
memcg: accounting for tty_struct objects
memcg: accounting for ldt_struct objects

 arch/x86/kernel/ldt.c | 7 ++++---
 drivers/tty/tty_io.c  | 4 ++--
 fs/fcntl.c            | 3 ++-
 fs/namespace.c        | 5 +++--
 mm/memcontrol.c       | 2 +-
 net/core/fib_rules.c  | 4 ++--
 net/ipv4/fib_trie.c   | 4 ++--
 net/ipv6/ip6_fib.c    | 2 +-
 net/ipv6/route.c      | 2 +-
 9 files changed, 18 insertions(+), 15 deletions(-)
#10 lore raw

v3 cover memcg accounting from OpenVZ

In reply to a message not in the archive

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

v3:
- added new patches for other kind of accounted objects
- combined patches for ip address/routing-related objects
- improved description
- re-ordered and rebased for linux 5.12-rc8

v2:
- squashed old patch 1 "accounting for allocations called with disabled BH" with old patch 2 "accounting for fib6_nodes cache" used such kind of memory allocation
- improved patch description
- subsystem maintainers added to cc:

Vasily Averin (16):
memcg: enable accounting for net_device and Tx/Rx queues
memcg: enable accounting for IP address and routing-related objects
memcg: enable accounting for inet_bin_bucket cache
memcg: enable accounting for VLAN group array
memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs
allocation
memcg: enable accounting for scm_fp_list objects
memcg: enable accounting for new namesapces and struct nsproxy
memcg: enable accounting of ipc resources
memcg: enable accounting for mnt_cache entries
memcg: enable accounting for pollfd and select bits arrays
memcg: enable accounting for signals
memcg: enable accounting for posix_timers_cache slab
memcg: enable accounting for file lock caches
memcg: enable accounting for fasync_cache
memcg: enable accounting for tty-related objects
memcg: enable accounting for ldt_struct objects

 arch/x86/kernel/ldt.c      |  7 ++++---
 drivers/tty/tty_io.c       |  4 ++--
 fs/fcntl.c                 |  3 ++-
 fs/locks.c                 |  6 ++++--
 fs/namespace.c             |  7 ++++---
 fs/select.c                |  4 ++--
 ipc/msg.c                  |  2 +-
 ipc/namespace.c            |  2 +-
 ipc/sem.c                  | 10 ++++++----
 ipc/shm.c                  |  2 +-
 kernel/cgroup/namespace.c  |  2 +-
 kernel/nsproxy.c           |  2 +-
 kernel/pid_namespace.c     |  2 +-
 kernel/signal.c            |  2 +-
 kernel/time/namespace.c    |  4 ++--
 kernel/time/posix-timers.c |  4 ++--
 kernel/user_namespace.c    |  2 +-
 mm/memcontrol.c            |  2 +-
 net/8021q/vlan.c           |  2 +-
 net/core/dev.c             |  6 +++---
 net/core/fib_rules.c       |  4 ++--
 net/core/scm.c             |  4 ++--
 net/dccp/proto.c           |  2 +-
 net/ipv4/devinet.c         |  2 +-
 net/ipv4/fib_trie.c        |  4 ++--
 net/ipv4/tcp.c             |  4 +++-
 net/ipv6/addrconf.c        |  2 +-
 net/ipv6/ip6_fib.c         |  4 ++--
 net/ipv6/route.c           |  2 +-
 net/ipv6/sit.c             |  5 +++--
 30 files changed, 59 insertions(+), 49 deletions(-)
Signature · 1 line
1.8.3.1
#11 lore raw

v4 cover memcg accounting from OpenVZ

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

v4:
- improved description for tty patch
- minor cleanup in LDT patch
- rebased to v5.12
- resent to lkml@

v3:
- added new patches for other kind of accounted objects
- combined patches for ip address/routing-related objects
- improved description
- re-ordered and rebased for linux 5.12-rc8

v2:
- squashed old patch 1 "accounting for allocations called with disabled BH" with old patch 2 "accounting for fib6_nodes cache" used such kind of memory allocation
- improved patch description
- subsystem maintainers added to cc:

Vasily Averin (16):
memcg: enable accounting for net_device and Tx/Rx queues
memcg: enable accounting for IP address and routing-related objects
memcg: enable accounting for inet_bin_bucket cache
memcg: enable accounting for VLAN group array
memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs
allocation
memcg: enable accounting for scm_fp_list objects
memcg: enable accounting for new namesapces and struct nsproxy
memcg: enable accounting of ipc resources
memcg: enable accounting for mnt_cache entries
memcg: enable accounting for pollfd and select bits arrays
memcg: enable accounting for signals
memcg: enable accounting for posix_timers_cache slab
memcg: enable accounting for file lock caches
memcg: enable accounting for fasync_cache
memcg: enable accounting for tty-related objects
memcg: enable accounting for ldt_struct objects

 arch/x86/kernel/ldt.c      |  6 +++---
 drivers/tty/tty_io.c       |  4 ++--
 fs/fcntl.c                 |  3 ++-
 fs/locks.c                 |  6 ++++--
 fs/namespace.c             |  7 ++++---
 fs/select.c                |  4 ++--
 ipc/msg.c                  |  2 +-
 ipc/namespace.c            |  2 +-
 ipc/sem.c                  | 10 ++++++----
 ipc/shm.c                  |  2 +-
 kernel/cgroup/namespace.c  |  2 +-
 kernel/nsproxy.c           |  2 +-
 kernel/pid_namespace.c     |  2 +-
 kernel/signal.c            |  2 +-
 kernel/time/namespace.c    |  4 ++--
 kernel/time/posix-timers.c |  4 ++--
 kernel/user_namespace.c    |  2 +-
 mm/memcontrol.c            |  2 +-
 net/8021q/vlan.c           |  2 +-
 net/core/dev.c             |  6 +++---
 net/core/fib_rules.c       |  4 ++--
 net/core/scm.c             |  4 ++--
 net/dccp/proto.c           |  2 +-
 net/ipv4/devinet.c         |  2 +-
 net/ipv4/fib_trie.c        |  4 ++--
 net/ipv4/tcp.c             |  4 +++-
 net/ipv6/addrconf.c        |  2 +-
 net/ipv6/ip6_fib.c         |  4 ++--
 net/ipv6/route.c           |  2 +-
 net/ipv6/sit.c             |  5 +++--
 30 files changed, 58 insertions(+), 49 deletions(-)
Signature · 1 line
1.8.3.1
#12 lore raw
Quoted 35 lines · Vasily Averin

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Quoted 6 lines

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

Hi Vasily,

What's the status of this series? I see a couple patches did get acked/reviewed. Can you please re-send the series with updated ack tags?

thanks,
Shakeel

#13 lore raw
Quoted 42 lines · Shakeel Butt
Quoted 35 lines · Vasily Averin

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Quoted 5 lines

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

Quoted 5 lines

Hi Vasily,

What's the status of this series? I see a couple patches did get acked/reviewed. Can you please re-send the series with updated ack tags?

Technically my patches does not have any NAKs. Practically they are still them merged. I've expected Michal will push it, but he advised me to push subsystem maintainers. I've asked Tejun to pick up the whole patch set and I'm waiting for his feedback right now.

I can resend patch set once again, with collected approval and with rebase to v5.14-rc1. However I do not understand how it helps to push them if patches should be processed through subsystem maintainers. As far as I understand I'll need to split this patch set into per-subsystem pieces and sent them to corresponded maintainers.

Thank you,
Vasily Averin.

#14 lore raw
Quoted 50 lines · Vasily Averin
Quoted 42 lines · Shakeel Butt
Quoted 35 lines · Vasily Averin

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Quoted 5 lines

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

Quoted 5 lines

Hi Vasily,

What's the status of this series? I see a couple patches did get acked/reviewed. Can you please re-send the series with updated ack tags?

Quoted 9 lines

Technically my patches does not have any NAKs. Practically they are still them merged. I've expected Michal will push it, but he advised me to push subsystem maintainers. I've asked Tejun to pick up the whole patch set and I'm waiting for his feedback right now.

I can resend patch set once again, with collected approval and with rebase to v5.14-rc1. However I do not understand how it helps to push them if patches should be processed through subsystem maintainers. As far as I understand I'll need to split this patch set into per-subsystem pieces and sent them to corresponded maintainers.

Usually these kinds of patches (adding memcg accounting) go through mm tree but if there are no dependencies between the patches and a consensus that each subsystem maintainer picks the corresponding patch then that is fine too.

#15 lore raw

v5 cover memcg accounting from OpenVZ

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

v5:
- rebased to v5.14-rc1
- updated ack tags

v4:
- improved description for tty patch
- minor cleanup in LDT patch
- rebased to v5.12
- resent to lkml@

v3:
- added new patches for other kind of accounted objects
- combined patches for ip address/routing-related objects
- improved description
- re-ordered and rebased for linux 5.12-rc8

v2:
- squashed old patch 1 "accounting for allocations called with disabled BH" with old patch 2 "accounting for fib6_nodes cache" used such kind of memory allocation
- improved patch description
- subsystem maintainers added to cc:

Vasily Averin (16):
memcg: enable accounting for net_device and Tx/Rx queues
memcg: enable accounting for IP address and routing-related objects
memcg: enable accounting for inet_bin_bucket cache
memcg: enable accounting for VLAN group array
memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs
allocation
memcg: enable accounting for scm_fp_list objects
memcg: enable accounting for mnt_cache entries
memcg: enable accounting for pollfd and select bits arrays
memcg: enable accounting for file lock caches
memcg: enable accounting for fasync_cache
memcg: enable accounting for new namesapces and struct nsproxy
memcg: enable accounting of ipc resources
memcg: enable accounting for signals
memcg: enable accounting for posix_timers_cache slab
memcg: enable accounting for tty-related objects
memcg: enable accounting for ldt_struct objects

 arch/x86/kernel/ldt.c      | 6 +++---
 drivers/tty/tty_io.c       | 4 ++--
 fs/fcntl.c                 | 3 ++-
 fs/locks.c                 | 6 ++++--
 fs/namespace.c             | 7 ++++---
 fs/select.c                | 4 ++--
 ipc/msg.c                  | 2 +-
 ipc/namespace.c            | 2 +-
 ipc/sem.c                  | 9 +++++----
 ipc/shm.c                  | 2 +-
 kernel/cgroup/namespace.c  | 2 +-
 kernel/nsproxy.c           | 2 +-
 kernel/pid_namespace.c     | 2 +-
 kernel/signal.c            | 2 +-
 kernel/time/namespace.c    | 4 ++--
 kernel/time/posix-timers.c | 4 ++--
 kernel/user_namespace.c    | 2 +-
 mm/memcontrol.c            | 2 +-
 net/8021q/vlan.c           | 2 +-
 net/core/dev.c             | 6 +++---
 net/core/fib_rules.c       | 4 ++--
 net/core/scm.c             | 4 ++--
 net/dccp/proto.c           | 2 +-
 net/ipv4/devinet.c         | 2 +-
 net/ipv4/fib_trie.c        | 4 ++--
 net/ipv4/tcp.c             | 4 +++-
 net/ipv6/addrconf.c        | 2 +-
 net/ipv6/ip6_fib.c         | 4 ++--
 net/ipv6/route.c           | 2 +-
 net/ipv6/sit.c             | 5 +++--
 30 files changed, 57 insertions(+), 49 deletions(-)
Signature · 1 line
1.8.3.1
#16 lore raw

v6 cover memcg accounting from

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

v6:
- improved description of "memcg: enable accounting for signals" according to Eric Biderman's wishes
- added Reviewed-by tag from Shakeel Butt on the same patch

v5:
- rebased to v5.14-rc1
- updated ack tags

v4:
- improved description for tty patch
- minor cleanup in LDT patch
- rebased to v5.12
- resent to lkml@

v3:
- added new patches for other kind of accounted objects
- combined patches for ip address/routing-related objects
- improved description
- re-ordered and rebased for linux 5.12-rc8

v2:
- squashed old patch 1 "accounting for allocations called with disabled BH" with old patch 2 "accounting for fib6_nodes cache" used such kind of memory allocation
- improved patch description
- subsystem maintainers added to cc:

Vasily Averin (16):
memcg: enable accounting for net_device and Tx/Rx queues
memcg: enable accounting for IP address and routing-related objects
memcg: enable accounting for inet_bin_bucket cache
memcg: enable accounting for VLAN group array
memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs
allocation
memcg: enable accounting for scm_fp_list objects
memcg: enable accounting for mnt_cache entries
memcg: enable accounting for pollfd and select bits arrays
memcg: enable accounting for file lock caches
memcg: enable accounting for fasync_cache
memcg: enable accounting for new namesapces and struct nsproxy
memcg: enable accounting of ipc resources
memcg: enable accounting for signals
memcg: enable accounting for posix_timers_cache slab
memcg: enable accounting for tty-related objects
memcg: enable accounting for ldt_struct objects

 arch/x86/kernel/ldt.c      | 6 +++---
 drivers/tty/tty_io.c       | 4 ++--
 fs/fcntl.c                 | 3 ++-
 fs/locks.c                 | 6 ++++--
 fs/namespace.c             | 7 ++++---
 fs/select.c                | 4 ++--
 ipc/msg.c                  | 2 +-
 ipc/namespace.c            | 2 +-
 ipc/sem.c                  | 9 +++++----
 ipc/shm.c                  | 2 +-
 kernel/cgroup/namespace.c  | 2 +-
 kernel/nsproxy.c           | 2 +-
 kernel/pid_namespace.c     | 2 +-
 kernel/signal.c            | 2 +-
 kernel/time/namespace.c    | 4 ++--
 kernel/time/posix-timers.c | 4 ++--
 kernel/user_namespace.c    | 2 +-
 mm/memcontrol.c            | 2 +-
 net/8021q/vlan.c           | 2 +-
 net/core/dev.c             | 6 +++---
 net/core/fib_rules.c       | 4 ++--
 net/core/scm.c             | 4 ++--
 net/dccp/proto.c           | 2 +-
 net/ipv4/devinet.c         | 2 +-
 net/ipv4/fib_trie.c        | 4 ++--
 net/ipv4/tcp.c             | 4 +++-
 net/ipv6/addrconf.c        | 2 +-
 net/ipv6/ip6_fib.c         | 4 ++--
 net/ipv6/route.c           | 2 +-
 net/ipv6/sit.c             | 5 +++--
 30 files changed, 57 insertions(+), 49 deletions(-)
Signature · 1 line
1.8.3.1
maintainer
#17 lore raw

Re: [PATCH v6 00/16] memcg accounting from

This series does not apply cleanly to net-next, please respin.

Thank you.

#18 lore raw
Quoted 2 lines · David Miller

This series does not apply cleanly to net-next, please respin.

Dear David,
I found that you have already approved net-related patches of this series and included them into net-next. So I'll respin v7 without these patches.

Thank you,
Vasily Averin

#19 lore raw

v7 cover memcg accounting from OpenVZ

OpenVZ uses memory accounting 20+ years since v2.2.x linux kernels. Initially we used our own accounting subsystem, then partially committed it to upstream, and a few years ago switched to cgroups v1. Now we're rebasing again, revising our old patches and trying to push them upstream.

We try to protect the host system from any misuse of kernel memory allocation triggered by untrusted users inside the containers.

Patch-set is addressed mostly to cgroups maintainers and cgroups@ mailing list, though I would be very grateful for any comments from maintainersi of affected subsystems or other people added in cc:

Compared to the upstream, we additionally account the following kernel objects:
- network devices and its Tx/Rx queues
- ipv4/v6 addresses and routing-related objects
- inet_bind_bucket cache objects
- VLAN group arrays
- ipv6/sit: ip_tunnel_prl
- scm_fp_list objects used by SCM_RIGHTS messages of Unix sockets
- nsproxy and namespace objects itself
- IPC objects: semaphores, message queues and share memory segments
- mounts
- pollfd and select bits arrays
- signals and posix timers
- file lock
- fasync_struct used by the file lease code and driver's fasync queues
- tty objects
- per-mm LDT

We have an incorrect/incomplete/obsoleted accounting for few other kernel
objects: sk_filter, af_packets, netlink and xt_counters for iptables. They require rework and probably will be dropped at all.

Also we're going to add an accounting for nft, however it is not ready yet.

We have not tested performance on upstream, however, our performance team compares our current RHEL7-based production kernel and reports that they are at least not worse as the according original RHEL7 kernel.

v7:
- net-related patches was approved and included into net-next git
- rebase to v5.14-rc3
- added Acked-by tag from Kirill Tkhai on "memcg: enable accounting for new namesapces and struct nsproxy"

v6:
- improved description of "memcg: enable accounting for signals" according to Eric Biderman's wishes
- added Reviewed-by tag from Shakeel Butt on the same patch

v5:
- rebased to v5.14-rc1
- updated ack tags

v4:
- improved description for tty patch
- minor cleanup in LDT patch
- rebased to v5.12
- resent to lkml@

v3:
- added new patches for other kind of accounted objects
- combined patches for ip address/routing-related objects
- improved description
- re-ordered and rebased for linux 5.12-rc8

v2:
- squashed old patch 1 "accounting for allocations called with disabled BH" with old patch 2 "accounting for fib6_nodes cache" used such kind of memory allocation
- improved patch description
- subsystem maintainers added to cc:

Vasily Averin (10):
memcg: enable accounting for mnt_cache entries
memcg: enable accounting for pollfd and select bits arrays
memcg: enable accounting for file lock caches
memcg: enable accounting for fasync_cache
memcg: enable accounting for new namesapces and struct nsproxy
memcg: enable accounting of ipc resources
memcg: enable accounting for signals
memcg: enable accounting for posix_timers_cache slab
memcg: enable accounting for tty-related objects
memcg: enable accounting for ldt_struct objects

 arch/x86/kernel/ldt.c      | 6 +++---
 drivers/tty/tty_io.c       | 4 ++--
 fs/fcntl.c                 | 3 ++-
 fs/locks.c                 | 6 ++++--
 fs/namespace.c             | 7 ++++---
 fs/select.c                | 4 ++--
 ipc/msg.c                  | 2 +-
 ipc/namespace.c            | 2 +-
 ipc/sem.c                  | 9 +++++----
 ipc/shm.c                  | 2 +-
 kernel/cgroup/namespace.c  | 2 +-
 kernel/nsproxy.c           | 2 +-
 kernel/pid_namespace.c     | 2 +-
 kernel/signal.c            | 2 +-
 kernel/time/namespace.c    | 4 ++--
 kernel/time/posix-timers.c | 4 ++--
 kernel/user_namespace.c    | 2 +-
 17 files changed, 34 insertions(+), 29 deletions(-)
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#21 lore raw

PATCH v7 1/10 memcg: enable accounting for mnt_cache entries

The kernel allocates ~400 bytes of 'strcut mount' for any new mount. Creating a new mount namespace clones most of the parent mounts, and this can be repeated many times. Additionally, each mount allocates up to PATH_MAX=4096 bytes for mnt->mnt_devname.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/namespace.c +3 -2
index ab4174a..c6a74e5 100644
@@ -203,7 +203,8 @@ static struct mount *alloc_vfsmnt(const char *name)
 			goto out_free_cache;
 
 		if (name) {
-			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
+			mnt->mnt_devname = kstrdup_const(name,
+							 GFP_KERNEL_ACCOUNT);
 			if (!mnt->mnt_devname)
 				goto out_free_id;
 		}
@@ -4222,7 +4223,7 @@ void __init mnt_init(void)
 	int err;
 
 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
-			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
+			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
 	mount_hashtable = alloc_large_system_hash("Mount-cache",
 				sizeof(struct hlist_head),
Signature · 1 line
1.8.3.1
Reviewed-by on v7 1/10
#22 lore raw

Re: [PATCH v7 01/10] memcg: enable accounting for mnt_cache entries

Quoted 2 lines · Vasily Averin

The kernel allocates ~400 bytes of 'strcut mount' for any new mount.

*struct mount*

Quoted 8 lines

Creating a new mount namespace clones most of the parent mounts, and this can be repeated many times. Additionally, each mount allocates up to PATH_MAX=4096 bytes for mnt->mnt_devname.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

Acked-by on v7 1/10
#23 lore raw

Re: [PATCH v7 01/10] memcg: enable accounting for mnt_cache entries

In reply to Vasily Averin
Quoted 10 lines · Vasily Averin

The kernel allocates ~400 bytes of 'strcut mount' for any new mount. Creating a new mount namespace clones most of the parent mounts, and this can be repeated many times. Additionally, each mount allocates up to PATH_MAX=4096 bytes for mnt->mnt_devname.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---

Looks good. Thank you!

I wonder how much this increases reported memory consumption when you boot full system containers that run systemd and a bunch of systemd services that each use a separate mount namespace.

#24 lore raw

PATCH v7 2/10 memcg: enable accounting for pollfd and select bits arrays

In reply to a message not in the archive

User can call select/poll system calls with a large number of assigned file descriptors and force kernel to allocate up to several pages of memory till end of these sleeping system calls. We have here long-living unaccounted per-task allocations.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/select.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/select.c +2 -2
index 945896d..e83e563 100644
@@ -655,7 +655,7 @@ int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
 			goto out_nofds;
 
 		alloc_size = 6 * size;
-		bits = kvmalloc(alloc_size, GFP_KERNEL);
+		bits = kvmalloc(alloc_size, GFP_KERNEL_ACCOUNT);
 		if (!bits)
 			goto out_nofds;
 	}
@@ -1000,7 +1000,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 
 		len = min(todo, POLLFD_PER_PAGE);
 		walk = walk->next = kmalloc(struct_size(walk, entries, len),
-					    GFP_KERNEL);
+					    GFP_KERNEL_ACCOUNT);
 		if (!walk) {
 			err = -ENOMEM;
 			goto out_fds;
Signature · 1 line
1.8.3.1
#25 lore raw

Re: [PATCH v7 02/10] memcg: enable accounting for pollfd and select bits arrays

Quoted 19 lines · Vasily Averin

User can call select/poll system calls with a large number of assigned file descriptors and force kernel to allocate up to several pages of memory till end of these sleeping system calls. We have here long-living unaccounted per-task allocations.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/select.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/select.c +0 -0
index 945896d..e83e563 100644

fs/select.c int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp, · line 658

Quoted 5 lines
fs/select.c +1 -1
@@ -655,7 +655,7 @@ int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
                        goto out_nofds;
 
                alloc_size = 6 * size;
-               bits = kvmalloc(alloc_size, GFP_KERNEL);
+               bits = kvmalloc(alloc_size, GFP_KERNEL_ACCOUNT);

What about the similar allocation in compat_core_sys_select()? Also what about the allocation in poll_get_entry()?

Quoted 15 lines
fs/select.c +1 -2
                if (!bits)
                        goto out_nofds;
        }
@@ -1000,7 +1000,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 
                len = min(todo, POLLFD_PER_PAGE);
                walk = walk->next = kmalloc(struct_size(walk, entries, len),
-                                           GFP_KERNEL);
+                                           GFP_KERNEL_ACCOUNT);
                if (!walk) {
                        err = -ENOMEM;
                        goto out_fds;
--

1.8.3.1

#26 lore raw

PATCH v7 3/10 memcg: enable accounting for file lock caches

In reply to a message not in the archive

User can create file locks for each open file and force kernel to allocate small but long-living objects per each open file.

It makes sense to account for these objects to limit the host's memory consumption from inside the memcg-limited container.

---
 fs/locks.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/locks.c +4 -2
index 74b2a1d..1bc7ede 100644
@@ -3056,10 +3056,12 @@ static int __init filelock_init(void)
 	int i;
 
 	flctx_cache = kmem_cache_create("file_lock_ctx",
-			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock_context), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	filelock_cache = kmem_cache_create("file_lock_cache",
-			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	for_each_possible_cpu(i) {
 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
Signature · 1 line
1.8.3.1
Reviewed-by on v7 3/10
#27 lore raw

Re: [PATCH v7 03/10] memcg: enable accounting for file lock caches

Quoted 8 lines · Vasily Averin

User can create file locks for each open file and force kernel to allocate small but long-living objects per each open file.

It makes sense to account for these objects to limit the host's memory consumption from inside the memcg-limited container.

#28 lore raw

PATCH v7 4/10 memcg: enable accounting for fasync_cache

In reply to a message not in the archive

fasync_struct is used by almost all character device drivers to set up the fasync queue, and for regular files by the file lease code. This structure is quite small but long-living and it can be assigned for any open file.

It makes sense to account for its allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/fcntl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fcntl.c +2 -1
index f946bec..714e7c9 100644
@@ -1049,7 +1049,8 @@ static int __init fcntl_init(void)
 			__FMODE_EXEC | __FMODE_NONOTIFY));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
-		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
+					 sizeof(struct fasync_struct), 0,
+					 SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
Reviewed-by on v7 4/10
#29 lore raw

Re: [PATCH v7 04/10] memcg: enable accounting for fasync_cache

Quoted 10 lines · Vasily Averin

fasync_struct is used by almost all character device drivers to set up the fasync queue, and for regular files by the file lease code. This structure is quite small but long-living and it can be assigned for any open file.

It makes sense to account for its allocations to restrict the host's memory consumption from inside the memcg-limited container.

#30 lore raw

PATCH v7 5/10 memcg: enable accounting for new namesapces and struct nsproxy

In reply to a message not in the archive

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c            | 2 +-
 ipc/namespace.c           | 2 +-
 kernel/cgroup/namespace.c | 2 +-
 kernel/nsproxy.c          | 2 +-
 kernel/pid_namespace.c    | 2 +-
 kernel/time/namespace.c   | 4 ++--
 kernel/user_namespace.c   | 2 +-
 7 files changed, 8 insertions(+), 8 deletions(-)
fs/namespace.c +1 -1
index c6a74e5..e443ee6 100644
@@ -3289,7 +3289,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 	if (!ucounts)
 		return ERR_PTR(-ENOSPC);
 
-	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns) {
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
ipc/namespace.c +1 -1
index 7bd0766..ae83f0f 100644
@@ -42,7 +42,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
+	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
 	if (ns == NULL)
 		goto fail_dec;
 
kernel/cgroup/namespace.c +1 -1
index f5e8828..0d5c298 100644
@@ -24,7 +24,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	struct cgroup_namespace *new_ns;
 	int ret;
 
-	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
 	ret = ns_alloc_inum(&new_ns->ns);
kernel/nsproxy.c +1 -1
index abc01fc..eec72ca 100644
@@ -568,6 +568,6 @@ static void commit_nsset(struct nsset *nsset)
 
 int __init nsproxy_cache_init(void)
 {
-	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
+	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
 	return 0;
 }
kernel/pid_namespace.c +1 -1
index ca43239..6cd6715 100644
@@ -449,7 +449,7 @@ static struct user_namespace *pidns_owner(struct ns_common *ns)
 
 static __init int pid_namespaces_init(void)
 {
-	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
+	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
kernel/time/namespace.c +2 -2
index 12eab0d..aec8328 100644
@@ -88,13 +88,13 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
+	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
 	refcount_set(&ns->ns.count, 1);
 
-	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
kernel/user_namespace.c +1 -1
index ef82d40..6b2e3ca 100644
@@ -1385,7 +1385,7 @@ static struct user_namespace *userns_owner(struct ns_common *ns)
 
 static __init int user_namespaces_init(void)
 {
-	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
+	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
Signature · 1 line
1.8.3.1
Reviewed-by on v7 5/10
#31 lore raw

Re: [PATCH v7 05/10] memcg: enable accounting for new namesapces and struct nsproxy

Quoted 8 lines · Vasily Averin

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

Quoted 4 lines
#32 lore raw

PATCH v7 6/10 memcg: enable accounting of ipc resources

In reply to a message not in the archive

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

---
 ipc/msg.c | 2 +-
 ipc/sem.c | 9 +++++----
 ipc/shm.c | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
ipc/msg.c +1 -1
index 6810276..a0d0577 100644
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
ipc/sem.c +5 -4
index 971e75d..1a8b9f0 100644
@@ -514,7 +514,7 @@ static struct sem_array *sem_alloc(size_t nsems)
 	if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
 		return NULL;
 
-	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
+	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!sma))
 		return NULL;
 
@@ -1855,7 +1855,7 @@ static inline int get_undo_list(struct sem_undo_list **undo_listp)
 
 	undo_list = current->sysvsem.undo_list;
 	if (!undo_list) {
-		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
+		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
 		if (undo_list == NULL)
 			return -ENOMEM;
 		spin_lock_init(&undo_list->lock);
@@ -1941,7 +1941,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 
 	/* step 2: allocate new undo structure */
 	new = kvzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
-		       GFP_KERNEL);
+		       GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
 		return ERR_PTR(-ENOMEM);
@@ -2005,7 +2005,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	if (nsops > ns->sc_semopm)
 		return -E2BIG;
 	if (nsops > SEMOPM_FAST) {
-		sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
+		sops = kvmalloc_array(nsops, sizeof(*sops),
+				      GFP_KERNEL_ACCOUNT);
 		if (sops == NULL)
 			return -ENOMEM;
 	}
ipc/shm.c +1 -1
index 748933e..ab749be 100644
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
Reviewed-by on v7 6/10
#33 lore raw

Re: [PATCH v7 06/10] memcg: enable accounting of ipc resources

Quoted 11 lines · Vasily Averin

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

#34 lore raw

PATCH v7 7/10 memcg: enable accounting for signals

In reply to a message not in the archive

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 kernel/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
index a3229ad..8921c4a 100644
@@ -4663,7 +4663,7 @@ void __init signals_init(void)
 {
 	siginfo_buildtime_checks();
 
-	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
+	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC | SLAB_ACCOUNT);
 }
 
 #ifdef CONFIG_KGDB_KDB
Signature · 1 line
1.8.3.1
#35 lore raw

PATCH v7 8/10 memcg: enable accounting for posix_timers_cache slab

In reply to a message not in the archive

A program may create multiple interval timers using timer_create(). For each timer the kernel preallocates a "queued real-time signal", Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit. The allocated object is quite small, ~250 bytes, but even the default signal limits allow to consume up to 100 megabytes per user.

It makes sense to account for them to limit the host's memory consumption from inside the memcg-limited container.

---
 kernel/time/posix-timers.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/posix-timers.c +2 -2
index dd5697d..7363f81 100644
@@ -273,8 +273,8 @@ static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
 static __init int init_posix_timers(void)
 {
 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
-					sizeof (struct k_itimer), 0, SLAB_PANIC,
-					NULL);
+					sizeof(struct k_itimer), 0,
+					SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 __initcall(init_posix_timers);
Signature · 1 line
1.8.3.1
Reviewed-by on v7 8/10
#36 lore raw

Re: [PATCH v7 08/10] memcg: enable accounting for posix_timers_cache slab

Quoted 8 lines · Vasily Averin

A program may create multiple interval timers using timer_create(). For each timer the kernel preallocates a "queued real-time signal", Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit. The allocated object is quite small, ~250 bytes, but even the default signal limits allow to consume up to 100 megabytes per user.

Quoted 5 lines

It makes sense to account for them to limit the host's memory consumption from inside the memcg-limited container.

#37 lore raw

PATCH v7 9/10 memcg: enable accounting for tty-related objects

In reply to a message not in the archive

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index 26debec..e787f6f 100644
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
@@ -3119,7 +3119,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
1.8.3.1
#38 lore raw

Re: [PATCH v7 09/10] memcg: enable accounting for tty-related objects

Quoted 19 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

Quoted 8 lines

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

As this is independant of all of the rest, I'll just take this through my tree now so that you do not have to keep resending it.

thanks,

greg k-h

maintainer
#39 lore raw

Re: [PATCH v7 09/10] memcg: enable accounting for tty-related objects

In reply to Vasily Averin
Quoted 33 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
  drivers/tty/tty_io.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +0 -0
index 26debec..e787f6f 100644

drivers/tty/tty_io.c void tty_save_termios(struct tty_struct *tty) · line 1496

Quoted 5 lines
drivers/tty/tty_io.c +1 -1
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
  	/* Stash the termios data */
  	tp = tty->driver->termios[idx];
  	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);

termios are not saved for PTYs (TTY_DRIVER_RESET_TERMIOS). Am I missing something?

drivers/tty/tty_io.c struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx) · line 3125

Quoted 13 lines
drivers/tty/tty_io.c +1 -1
  		if (tp == NULL)
  			return;
  		tty->driver->termios[idx] = tp;
@@ -3119,7 +3119,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
  {
  	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
  	if (!tty)
  		return NULL;
 

thanks,

Signature · 2 lines
js
suse labs
#40 lore raw

Re: [PATCH v7 09/10] memcg: enable accounting for tty-related objects

Quoted 29 lines · Jiri Slaby
Quoted 28 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
  drivers/tty/tty_io.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines

drivers/tty/tty_io.c void tty_save_termios(struct tty_struct *tty) · line 1496

Quoted 10 lines
drivers/tty/tty_io.c +1 -1
index 26debec..e787f6f 100644
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
       /* Stash the termios data */
       tp = tty->driver->termios[idx];
       if (tp == NULL) {
-        tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+        tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);

termios are not saved for PTYs (TTY_DRIVER_RESET_TERMIOS). Am I missing something?

No, you are right, I've missed this.
Typical terminals inside containers use TTY_DRIVER_RESET_TERMIOS flag and therefore do not save termios. So its accounting have near-to-zero impact in real life.
I'll prepare fixup to drop GFP_KERNEL_ACCOUNT here.

Thank you very much,
Vasily Averin

Quoted 13 lines
Quoted 11 lines
drivers/tty/tty_io.c +1 -0
           if (tp == NULL)
               return;
           tty->driver->termios[idx] = tp;
@@ -3119,7 +3119,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
   {
       struct tty_struct *tty;
   -    tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+    tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
       if (!tty)
           return NULL;

thanks,

#41 lore raw

PATCH 1/1 memcg: drop GFP_KERNEL_ACCOUNT use in tty_save_termios()

Jiri Slaby pointed that termios are not saved for PTYs and for other terminals used inside containers. Therefore accounting for saved termios have near to zero impact in real life scenarios.

Cc Jiri Slaby <[email protected]>
Fixes 854dd8a572a0 ("memcg: enable accounting for tty-related objects")
---
 drivers/tty/tty_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/tty_io.c +1 -1
index e787f6f..a6230b2 100644
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
Signature · 1 line
1.8.3.1
#42 lore raw

Re: [PATCH TTY] memcg: drop GFP_KERNEL_ACCOUNT use in tty_save_termios()

Quoted 27 lines · Vasily Averin

Jiri Slaby pointed that termios are not saved for PTYs and for other terminals used inside containers. Therefore accounting for saved termios have near to zero impact in real life scenarios.

Cc Jiri Slaby <[email protected]>
Fixes 854dd8a572a0 ("memcg: enable accounting for tty-related objects")
---
 drivers/tty/tty_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/tty_io.c +1 -1
index e787f6f..a6230b2 100644
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
Signature · 1 line
1.8.3.1

I can just drop the original patch from my tree, it has not gone into my unmutable branch yet.

thanks,

greg k-h

#43 lore raw

PATCH v2 1/1 memcg: enable accounting for tty-related objects

In reply to Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

v2: removed hunk patched tty_save_termios()
Jiri Slaby pointed that termios are not saved for PTYs and for other terminals used inside containers. Therefore accounting for saved termios have near to zero impact in real life scenarios. v1 patch version was dropped due to noticed issue, however hunk patched alloc_tty_struct is still actual.

---
 drivers/tty/tty_io.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/tty/tty_io.c +1 -1
index 7e8b3bd59c7b..8fec1d8648f5 100644
@@ -3088,7 +3088,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
2.25.1
#44 lore raw

Re: [PATCH v7 09/10] memcg: enable accounting for tty-related objects

In reply to Vasily Averin
Quoted 43 lines · Vasily Averin
Quoted 29 lines · Jiri Slaby
Quoted 28 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
  drivers/tty/tty_io.c | 4 ++--

  1 file changed, 2 insertions(+), 2 deletions(-)
Quoted 12 lines

drivers/tty/tty_io.c void tty_save_termios(struct tty_struct *tty) · line 1496

Quoted 10 lines
drivers/tty/tty_io.c +1 -1
index 26debec..e787f6f 100644
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
       /* Stash the termios data */
       tp = tty->driver->termios[idx];
       if (tp == NULL) {
-        tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+        tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);

termios are not saved for PTYs (TTY_DRIVER_RESET_TERMIOS). Am I missing something?

Quoted 4 lines

No, you are right, I've missed this.
Typical terminals inside containers use TTY_DRIVER_RESET_TERMIOS flag and therefore do not save termios. So its accounting have near-to-zero impact in real life.
I'll prepare fixup to drop GFP_KERNEL_ACCOUNT here.

I'll go drop this patch from my tree.

thanks,

greg k-h

#45 lore raw

PATCH v7 10/10 memcg: enable accounting for ldt_struct objects

In reply to a message not in the archive

Each task can request own LDT and force the kernel to allocate up to 64Kb memory per-mm.

There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines.
The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

It makes sense to account for this objects to restrict the host's memory consumption from inside the memcg-limited container.

---
 arch/x86/kernel/ldt.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/ldt.c +3 -3
index aa15132..525876e 100644
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	if (num_entries > LDT_ENTRIES)
 		return NULL;
 
-	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
+	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
 	if (!new_ldt)
 		return NULL;
 
@@ -168,9 +168,9 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	else
-		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
+		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 
 	if (!new_ldt->entries) {
 		kfree(new_ldt);
Signature · 1 line
1.8.3.1
Reviewed-by on v7 10/10
#46 lore raw

Re: [PATCH v7 10/10] memcg: enable accounting for ldt_struct objects

Quoted 9 lines · Vasily Averin

Each task can request own LDT and force the kernel to allocate up to 64Kb memory per-mm.

There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines.
The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

Quoted 5 lines

It makes sense to account for this objects to restrict the host's memory consumption from inside the memcg-limited container.

A message that is not in the archive (sent off-list or not yet received): [email protected]

#48 lore raw

PATCH v6 1/16 memcg: enable accounting for net_device and Tx/Rx queues

Container netadmin can create a lot of fake net devices, then create a new net namespace and repeat it again and again. Net device can request the creation of up to 4096 tx and rx queues, and force kernel to allocate up to several tens of megabytes memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/dev.c +3 -3
index c253c2a..e9aa1e4 100644
@@ -10100,7 +10100,7 @@ static int netif_alloc_rx_queues(struct net_device *dev)
 
 	BUG_ON(count < 1);
 
-	rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!rx)
 		return -ENOMEM;
 
@@ -10167,7 +10167,7 @@ static int netif_alloc_netdev_queues(struct net_device *dev)
 	if (count < 1 || count > 0xffff)
 		return -EINVAL;
 
-	tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!tx)
 		return -ENOMEM;
 
@@ -10807,7 +10807,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
 	/* ensure 32-byte alignment of whole construct */
 	alloc_size += NETDEV_ALIGN - 1;
 
-	p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!p)
 		return NULL;
 
Signature · 1 line
1.8.3.1
#49 lore raw

PATCH v6 2/16 memcg: enable accounting for IP address and routing-related objects

In reply to a message not in the archive

An netadmin inside container can use 'ip a a' and 'ip r a' to assign a large number of ipv4/ipv6 addresses and routing entries and force kernel to allocate megabytes of unaccounted memory for long-lived per-netdevice related kernel objects:
'struct in_ifaddr', 'struct inet6_ifaddr', 'struct fib6_node', 'struct rt6_info', 'struct fib_rules' and ip_fib caches.

These objects can be manually removed, though usually they lives in memory till destroy of its net namespace.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

One of such objects is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

In this case it is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass().

Obsoleted in_interrupt() does not describe real execution context properly. From include/linux/preempt.h:

The following macros are deprecated and should not be used in new code:
in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled

To verify the current execution context new macro should be used instead:
in_task() - We're in task context

---
 mm/memcontrol.c      | 2 +-
 net/core/fib_rules.c | 4 ++--
 net/ipv4/devinet.c   | 2 +-
 net/ipv4/fib_trie.c  | 4 ++--
 net/ipv6/addrconf.c  | 2 +-
 net/ipv6/ip6_fib.c   | 4 ++--
 net/ipv6/route.c     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
mm/memcontrol.c +1 -1
index ae1f5d0..1bbf239 100644
@@ -968,7 +968,7 @@ static __always_inline bool memcg_kmem_bypass(void)
 		return false;
 
 	/* Memcg to charge can't be determined. */
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 
 	return false;
net/core/fib_rules.c +2 -2
index a9f9379..79df7cd 100644
@@ -57,7 +57,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 {
 	struct fib_rule *r;
 
-	r = kzalloc(ops->rule_size, GFP_KERNEL);
+	r = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (r == NULL)
 		return -ENOMEM;
 
@@ -541,7 +541,7 @@ static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
 			goto errout;
 	}
 
-	nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
+	nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (!nlrule) {
 		err = -ENOMEM;
 		goto errout;
net/ipv4/devinet.c +1 -1
index 73721a4..d38124b 100644
@@ -215,7 +215,7 @@ static void devinet_sysctl_unregister(struct in_device *idev)
 
 static struct in_ifaddr *inet_alloc_ifa(void)
 {
-	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
+	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL_ACCOUNT);
 }
 
 static void inet_rcu_free_ifa(struct rcu_head *head)
net/ipv4/fib_trie.c +2 -2
index 25cf387..8060524 100644
@@ -2380,11 +2380,11 @@ void __init fib_trie_init(void)
 {
 	fn_alias_kmem = kmem_cache_create("ip_fib_alias",
 					  sizeof(struct fib_alias),
-					  0, SLAB_PANIC, NULL);
+					  0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
 					   LEAF_SIZE,
-					   0, SLAB_PANIC, NULL);
+					   0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 }
 
 struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
net/ipv6/addrconf.c +1 -1
index 3bf685f..8eaeade 100644
@@ -1080,7 +1080,7 @@ static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
 			goto out;
 	}
 
-	ifa = kzalloc(sizeof(*ifa), gfp_flags);
+	ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
 	if (!ifa) {
 		err = -ENOBUFS;
 		goto out;
net/ipv6/ip6_fib.c +2 -2
index 2d650dc..a8f118e 100644
@@ -2449,8 +2449,8 @@ int __init fib6_init(void)
 	int ret = -ENOMEM;
 
 	fib6_node_kmem = kmem_cache_create("fib6_nodes",
-					   sizeof(struct fib6_node),
-					   0, SLAB_HWCACHE_ALIGN,
+					   sizeof(struct fib6_node), 0,
+					   SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
 					   NULL);
 	if (!fib6_node_kmem)
 		goto out;
net/ipv6/route.c +1 -1
index 7b756a7..5f7286a 100644
@@ -6638,7 +6638,7 @@ int __init ip6_route_init(void)
 	ret = -ENOMEM;
 	ip6_dst_ops_template.kmem_cachep =
 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!ip6_dst_ops_template.kmem_cachep)
 		goto out;
 
Signature · 1 line
1.8.3.1
#50 lore raw

PATCH v6 3/16 memcg: enable accounting for inet_bin_bucket cache

In reply to a message not in the archive

net namespace can create up to 64K tcp and dccp ports and force kernel to allocate up to several megabytes of memory per netns
for inet_bind_bucket objects.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/dccp/proto.c | 2 +-
 net/ipv4/tcp.c   | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
net/dccp/proto.c +1 -1
index 7eb0fb2..abb5c59 100644
@@ -1126,7 +1126,7 @@ static int __init dccp_init(void)
 	dccp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("dccp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!dccp_hashinfo.bind_bucket_cachep)
 		goto out_free_hashinfo2;
 
net/ipv4/tcp.c +3 -1
index d5ab5f2..5c0605e 100644
@@ -4509,7 +4509,9 @@ void __init tcp_init(void)
 	tcp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("tcp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_PANIC |
+				  SLAB_ACCOUNT,
+				  NULL);
 
 	/* Size and allocate the main established and bind bucket
 	 * hash tables.
Signature · 1 line
1.8.3.1
#51 lore raw

PATCH v6 4/16 memcg: enable accounting for VLAN group array

In reply to a message not in the archive

vlan array consume up to 8 pages of memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/8021q/vlan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/8021q/vlan.c +1 -1
index 4cdf841..55275ef 100644
@@ -67,7 +67,7 @@ static int vlan_group_prealloc_vid(struct vlan_group *vg,
 		return 0;
 
 	size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
-	array = kzalloc(size, GFP_KERNEL);
+	array = kzalloc(size, GFP_KERNEL_ACCOUNT);
 	if (array == NULL)
 		return -ENOBUFS;
 
Signature · 1 line
1.8.3.1
#52 lore raw

PATCH v6 5/16 memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs allocation

In reply to a message not in the archive

Author: Andrey Ryabinin <[email protected]>

The size of the ip_tunnel_prl structs allocation is controllable from user-space, thus it's better to avoid spam in dmesg if allocation failed. Also add __GFP_ACCOUNT as this is a good candidate for per-memcg accounting. Allocation is temporary and limited by 4GB.

---
 net/ipv6/sit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv6/sit.c +3 -2
index df5bea8..33adc12 100644
@@ -321,7 +321,7 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 	 * we try harder to allocate.
 	 */
 	kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
-		kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
+		kcalloc(cmax, sizeof(*kp), GFP_KERNEL_ACCOUNT | __GFP_NOWARN) :
 		NULL;
 
 	rcu_read_lock();
@@ -334,7 +334,8 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 		 * For root users, retry allocating enough memory for
 		 * the answer.
 		 */
-		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
+		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC | __GFP_ACCOUNT |
+					      __GFP_NOWARN);
 		if (!kp) {
 			ret = -ENOMEM;
 			goto out;
Signature · 1 line
1.8.3.1
#53 lore raw

PATCH v6 6/16 memcg: enable accounting for scm_fp_list objects

In reply to a message not in the archive

unix sockets allows to send file descriptors via SCM_RIGHTS type messages. Each such send call forces kernel to allocate up to 2Kb memory for struct scm_fp_list.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/scm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/scm.c +2 -2
index ae3085d..5c356f0 100644
@@ -79,7 +79,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
 
 	if (!fpl)
 	{
-		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
+		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL_ACCOUNT);
 		if (!fpl)
 			return -ENOMEM;
 		*fplp = fpl;
@@ -355,7 +355,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
 		return NULL;
 
 	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
-			  GFP_KERNEL);
+			  GFP_KERNEL_ACCOUNT);
 	if (new_fpl) {
 		for (i = 0; i < fpl->count; i++)
 			get_file(fpl->fp[i]);
Signature · 1 line
1.8.3.1
#54 lore raw

PATCH v6 7/16 memcg: enable accounting for mnt_cache entries

In reply to a message not in the archive

The kernel allocates ~400 bytes of 'strcut mount' for any new mount. Creating a new mount namespace clones most of the parent mounts, and this can be repeated many times. Additionally, each mount allocates up to PATH_MAX=4096 bytes for mnt->mnt_devname.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/namespace.c +3 -2
index ab4174a..c6a74e5 100644
@@ -203,7 +203,8 @@ static struct mount *alloc_vfsmnt(const char *name)
 			goto out_free_cache;
 
 		if (name) {
-			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
+			mnt->mnt_devname = kstrdup_const(name,
+							 GFP_KERNEL_ACCOUNT);
 			if (!mnt->mnt_devname)
 				goto out_free_id;
 		}
@@ -4222,7 +4223,7 @@ void __init mnt_init(void)
 	int err;
 
 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
-			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
+			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
 	mount_hashtable = alloc_large_system_hash("Mount-cache",
 				sizeof(struct hlist_head),
Signature · 1 line
1.8.3.1
#55 lore raw

PATCH v6 8/16 memcg: enable accounting for pollfd and select bits arrays

In reply to a message not in the archive

User can call select/poll system calls with a large number of assigned file descriptors and force kernel to allocate up to several pages of memory till end of these sleeping system calls. We have here long-living unaccounted per-task allocations.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/select.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/select.c +2 -2
index 945896d..e83e563 100644
@@ -655,7 +655,7 @@ int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
 			goto out_nofds;
 
 		alloc_size = 6 * size;
-		bits = kvmalloc(alloc_size, GFP_KERNEL);
+		bits = kvmalloc(alloc_size, GFP_KERNEL_ACCOUNT);
 		if (!bits)
 			goto out_nofds;
 	}
@@ -1000,7 +1000,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 
 		len = min(todo, POLLFD_PER_PAGE);
 		walk = walk->next = kmalloc(struct_size(walk, entries, len),
-					    GFP_KERNEL);
+					    GFP_KERNEL_ACCOUNT);
 		if (!walk) {
 			err = -ENOMEM;
 			goto out_fds;
Signature · 1 line
1.8.3.1
#56 lore raw

PATCH v6 9/16 memcg: enable accounting for file lock caches

In reply to a message not in the archive

User can create file locks for each open file and force kernel to allocate small but long-living objects per each open file.

It makes sense to account for these objects to limit the host's memory consumption from inside the memcg-limited container.

---
 fs/locks.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/locks.c +4 -2
index 74b2a1d..1bc7ede 100644
@@ -3056,10 +3056,12 @@ static int __init filelock_init(void)
 	int i;
 
 	flctx_cache = kmem_cache_create("file_lock_ctx",
-			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock_context), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	filelock_cache = kmem_cache_create("file_lock_cache",
-			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	for_each_possible_cpu(i) {
 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
Signature · 1 line
1.8.3.1
#57 lore raw

PATCH v6 10/16 memcg: enable accounting for fasync_cache

In reply to a message not in the archive

fasync_struct is used by almost all character device drivers to set up the fasync queue, and for regular files by the file lease code. This structure is quite small but long-living and it can be assigned for any open file.

It makes sense to account for its allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/fcntl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fcntl.c +2 -1
index dfc72f1..7941559 100644
@@ -1049,7 +1049,8 @@ static int __init fcntl_init(void)
 			__FMODE_EXEC | __FMODE_NONOTIFY));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
-		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
+					 sizeof(struct fasync_struct), 0,
+					 SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#58 lore raw

PATCH v6 11/16 memcg: enable accounting for new namesapces and struct nsproxy

In reply to a message not in the archive

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c            | 2 +-
 ipc/namespace.c           | 2 +-
 kernel/cgroup/namespace.c | 2 +-
 kernel/nsproxy.c          | 2 +-
 kernel/pid_namespace.c    | 2 +-
 kernel/time/namespace.c   | 4 ++--
 kernel/user_namespace.c   | 2 +-
 7 files changed, 8 insertions(+), 8 deletions(-)
fs/namespace.c +1 -1
index c6a74e5..e443ee6 100644
@@ -3289,7 +3289,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 	if (!ucounts)
 		return ERR_PTR(-ENOSPC);
 
-	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns) {
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
ipc/namespace.c +1 -1
index 7bd0766..ae83f0f 100644
@@ -42,7 +42,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
+	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
 	if (ns == NULL)
 		goto fail_dec;
 
kernel/cgroup/namespace.c +1 -1
index f5e8828..0d5c298 100644
@@ -24,7 +24,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	struct cgroup_namespace *new_ns;
 	int ret;
 
-	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
 	ret = ns_alloc_inum(&new_ns->ns);
kernel/nsproxy.c +1 -1
index abc01fc..eec72ca 100644
@@ -568,6 +568,6 @@ static void commit_nsset(struct nsset *nsset)
 
 int __init nsproxy_cache_init(void)
 {
-	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
+	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
 	return 0;
 }
kernel/pid_namespace.c +1 -1
index ca43239..6cd6715 100644
@@ -449,7 +449,7 @@ static struct user_namespace *pidns_owner(struct ns_common *ns)
 
 static __init int pid_namespaces_init(void)
 {
-	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
+	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
kernel/time/namespace.c +2 -2
index 12eab0d..aec8328 100644
@@ -88,13 +88,13 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
+	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
 	refcount_set(&ns->ns.count, 1);
 
-	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
kernel/user_namespace.c +1 -1
index ef82d40..6b2e3ca 100644
@@ -1385,7 +1385,7 @@ static struct user_namespace *userns_owner(struct ns_common *ns)
 
 static __init int user_namespaces_init(void)
 {
-	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
+	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
Signature · 1 line
1.8.3.1
Acked-by on v6 11/16
#59 lore raw

Re: [PATCH v6 11/16] memcg: enable accounting for new namesapces and struct nsproxy

Quoted 10 lines · Vasily Averin

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

Quoted 108 lines
---
 fs/namespace.c            | 2 +-
 ipc/namespace.c           | 2 +-
 kernel/cgroup/namespace.c | 2 +-
 kernel/nsproxy.c          | 2 +-
 kernel/pid_namespace.c    | 2 +-
 kernel/time/namespace.c   | 4 ++--
 kernel/user_namespace.c   | 2 +-
 7 files changed, 8 insertions(+), 8 deletions(-)
fs/namespace.c +1 -1
index c6a74e5..e443ee6 100644
@@ -3289,7 +3289,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 	if (!ucounts)
 		return ERR_PTR(-ENOSPC);
 
-	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns) {
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
ipc/namespace.c +1 -1
index 7bd0766..ae83f0f 100644
@@ -42,7 +42,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
+	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
 	if (ns == NULL)
 		goto fail_dec;
 
kernel/cgroup/namespace.c +1 -1
index f5e8828..0d5c298 100644
@@ -24,7 +24,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	struct cgroup_namespace *new_ns;
 	int ret;
 
-	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
 	ret = ns_alloc_inum(&new_ns->ns);
kernel/nsproxy.c +1 -1
index abc01fc..eec72ca 100644
@@ -568,6 +568,6 @@ static void commit_nsset(struct nsset *nsset)
 
 int __init nsproxy_cache_init(void)
 {
-	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
+	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
 	return 0;
 }
kernel/pid_namespace.c +1 -1
index ca43239..6cd6715 100644
@@ -449,7 +449,7 @@ static struct user_namespace *pidns_owner(struct ns_common *ns)
 
 static __init int pid_namespaces_init(void)
 {
-	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
+	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
kernel/time/namespace.c +2 -2
index 12eab0d..aec8328 100644
@@ -88,13 +88,13 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
+	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
 	refcount_set(&ns->ns.count, 1);
 
-	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
kernel/user_namespace.c +1 -1
index ef82d40..6b2e3ca 100644
@@ -1385,7 +1385,7 @@ static struct user_namespace *userns_owner(struct ns_common *ns)
 
 static __init int user_namespaces_init(void)
 {
-	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
+	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
#60 lore raw

PATCH v6 12/16 memcg: enable accounting of ipc resources

In reply to a message not in the archive

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

---
 ipc/msg.c | 2 +-
 ipc/sem.c | 9 +++++----
 ipc/shm.c | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
ipc/msg.c +1 -1
index 6810276..a0d0577 100644
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
ipc/sem.c +5 -4
index 971e75d..1a8b9f0 100644
@@ -514,7 +514,7 @@ static struct sem_array *sem_alloc(size_t nsems)
 	if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
 		return NULL;
 
-	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
+	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!sma))
 		return NULL;
 
@@ -1855,7 +1855,7 @@ static inline int get_undo_list(struct sem_undo_list **undo_listp)
 
 	undo_list = current->sysvsem.undo_list;
 	if (!undo_list) {
-		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
+		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
 		if (undo_list == NULL)
 			return -ENOMEM;
 		spin_lock_init(&undo_list->lock);
@@ -1941,7 +1941,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 
 	/* step 2: allocate new undo structure */
 	new = kvzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
-		       GFP_KERNEL);
+		       GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
 		return ERR_PTR(-ENOMEM);
@@ -2005,7 +2005,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	if (nsops > ns->sc_semopm)
 		return -E2BIG;
 	if (nsops > SEMOPM_FAST) {
-		sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
+		sops = kvmalloc_array(nsops, sizeof(*sops),
+				      GFP_KERNEL_ACCOUNT);
 		if (sops == NULL)
 			return -ENOMEM;
 	}
ipc/shm.c +1 -1
index 748933e..ab749be 100644
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
#61 lore raw

PATCH v6 13/16 memcg: enable accounting for signals

In reply to a message not in the archive

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 kernel/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
index a3229ad..8921c4a 100644
@@ -4663,7 +4663,7 @@ void __init signals_init(void)
 {
 	siginfo_buildtime_checks();
 
-	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
+	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC | SLAB_ACCOUNT);
 }
 
 #ifdef CONFIG_KGDB_KDB
Signature · 1 line
1.8.3.1
#62 lore raw

PATCH v6 14/16 memcg: enable accounting for posix_timers_cache slab

In reply to a message not in the archive

A program may create multiple interval timers using timer_create(). For each timer the kernel preallocates a "queued real-time signal", Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit. The allocated object is quite small, ~250 bytes, but even the default signal limits allow to consume up to 100 megabytes per user.

It makes sense to account for them to limit the host's memory consumption from inside the memcg-limited container.

---
 kernel/time/posix-timers.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/posix-timers.c +2 -2
index dd5697d..7363f81 100644
@@ -273,8 +273,8 @@ static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
 static __init int init_posix_timers(void)
 {
 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
-					sizeof (struct k_itimer), 0, SLAB_PANIC,
-					NULL);
+					sizeof(struct k_itimer), 0,
+					SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 __initcall(init_posix_timers);
Signature · 1 line
1.8.3.1
#63 lore raw

PATCH v6 15/16 memcg: enable accounting for tty-related objects

In reply to a message not in the archive

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index 26debec..e787f6f 100644
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
@@ -3119,7 +3119,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
1.8.3.1
#64 lore raw

PATCH v6 16/16 memcg: enable accounting for ldt_struct objects

In reply to a message not in the archive

Each task can request own LDT and force the kernel to allocate up to 64Kb memory per-mm.

There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines.
The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

It makes sense to account for this objects to restrict the host's memory consumption from inside the memcg-limited container.

---
 arch/x86/kernel/ldt.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/ldt.c +3 -3
index aa15132..525876e 100644
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	if (num_entries > LDT_ENTRIES)
 		return NULL;
 
-	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
+	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
 	if (!new_ldt)
 		return NULL;
 
@@ -168,9 +168,9 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	else
-		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
+		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 
 	if (!new_ldt->entries) {
 		kfree(new_ldt);
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): [email protected]

#66 lore raw

PATCH v5 1/16 memcg: enable accounting for net_device and Tx/Rx queues

Container netadmin can create a lot of fake net devices, then create a new net namespace and repeat it again and again. Net device can request the creation of up to 4096 tx and rx queues, and force kernel to allocate up to several tens of megabytes memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/dev.c +3 -3
index c253c2a..e9aa1e4 100644
@@ -10100,7 +10100,7 @@ static int netif_alloc_rx_queues(struct net_device *dev)
 
 	BUG_ON(count < 1);
 
-	rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!rx)
 		return -ENOMEM;
 
@@ -10167,7 +10167,7 @@ static int netif_alloc_netdev_queues(struct net_device *dev)
 	if (count < 1 || count > 0xffff)
 		return -EINVAL;
 
-	tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!tx)
 		return -ENOMEM;
 
@@ -10807,7 +10807,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
 	/* ensure 32-byte alignment of whole construct */
 	alloc_size += NETDEV_ALIGN - 1;
 
-	p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!p)
 		return NULL;
 
Signature · 1 line
1.8.3.1
#67 lore raw

PATCH v5 2/16 memcg: enable accounting for IP address and routing-related objects

In reply to a message not in the archive

An netadmin inside container can use 'ip a a' and 'ip r a' to assign a large number of ipv4/ipv6 addresses and routing entries and force kernel to allocate megabytes of unaccounted memory for long-lived per-netdevice related kernel objects:
'struct in_ifaddr', 'struct inet6_ifaddr', 'struct fib6_node', 'struct rt6_info', 'struct fib_rules' and ip_fib caches.

These objects can be manually removed, though usually they lives in memory till destroy of its net namespace.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

One of such objects is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

In this case it is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass().

Obsoleted in_interrupt() does not describe real execution context properly. From include/linux/preempt.h:

The following macros are deprecated and should not be used in new code:
in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled

To verify the current execution context new macro should be used instead:
in_task() - We're in task context

---
 mm/memcontrol.c      | 2 +-
 net/core/fib_rules.c | 4 ++--
 net/ipv4/devinet.c   | 2 +-
 net/ipv4/fib_trie.c  | 4 ++--
 net/ipv6/addrconf.c  | 2 +-
 net/ipv6/ip6_fib.c   | 4 ++--
 net/ipv6/route.c     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
mm/memcontrol.c +1 -1
index ae1f5d0..1bbf239 100644
@@ -968,7 +968,7 @@ static __always_inline bool memcg_kmem_bypass(void)
 		return false;
 
 	/* Memcg to charge can't be determined. */
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 
 	return false;
net/core/fib_rules.c +2 -2
index a9f9379..79df7cd 100644
@@ -57,7 +57,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 {
 	struct fib_rule *r;
 
-	r = kzalloc(ops->rule_size, GFP_KERNEL);
+	r = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (r == NULL)
 		return -ENOMEM;
 
@@ -541,7 +541,7 @@ static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
 			goto errout;
 	}
 
-	nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
+	nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (!nlrule) {
 		err = -ENOMEM;
 		goto errout;
net/ipv4/devinet.c +1 -1
index 73721a4..d38124b 100644
@@ -215,7 +215,7 @@ static void devinet_sysctl_unregister(struct in_device *idev)
 
 static struct in_ifaddr *inet_alloc_ifa(void)
 {
-	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
+	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL_ACCOUNT);
 }
 
 static void inet_rcu_free_ifa(struct rcu_head *head)
net/ipv4/fib_trie.c +2 -2
index 25cf387..8060524 100644
@@ -2380,11 +2380,11 @@ void __init fib_trie_init(void)
 {
 	fn_alias_kmem = kmem_cache_create("ip_fib_alias",
 					  sizeof(struct fib_alias),
-					  0, SLAB_PANIC, NULL);
+					  0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
 					   LEAF_SIZE,
-					   0, SLAB_PANIC, NULL);
+					   0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 }
 
 struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
net/ipv6/addrconf.c +1 -1
index 3bf685f..8eaeade 100644
@@ -1080,7 +1080,7 @@ static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
 			goto out;
 	}
 
-	ifa = kzalloc(sizeof(*ifa), gfp_flags);
+	ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
 	if (!ifa) {
 		err = -ENOBUFS;
 		goto out;
net/ipv6/ip6_fib.c +2 -2
index 2d650dc..a8f118e 100644
@@ -2449,8 +2449,8 @@ int __init fib6_init(void)
 	int ret = -ENOMEM;
 
 	fib6_node_kmem = kmem_cache_create("fib6_nodes",
-					   sizeof(struct fib6_node),
-					   0, SLAB_HWCACHE_ALIGN,
+					   sizeof(struct fib6_node), 0,
+					   SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
 					   NULL);
 	if (!fib6_node_kmem)
 		goto out;
net/ipv6/route.c +1 -1
index 7b756a7..5f7286a 100644
@@ -6638,7 +6638,7 @@ int __init ip6_route_init(void)
 	ret = -ENOMEM;
 	ip6_dst_ops_template.kmem_cachep =
 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!ip6_dst_ops_template.kmem_cachep)
 		goto out;
 
Signature · 1 line
1.8.3.1
#68 lore raw

Re: [PATCH v5 02/16] memcg: enable accounting for IP address and routing-related objects

Hi Vasily,

On Mon, 19 Jul 2021 at 11:45, Vasily Averin <[email protected]> wrote:
[..]

mm/memcontrol.c static __always_inline bool memcg_kmem_bypass(void) · line 972

Quoted 11 lines
mm/memcontrol.c +1 -1
index ae1f5d0..1bbf239 100644
@@ -968,7 +968,7 @@ static __always_inline bool memcg_kmem_bypass(void)
                return false;
 
        /* Memcg to charge can't be determined. */
-       if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+       if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
                return true;

This seems to do two separate things in one patch. Probably, it's better to separate them.
(I may miss how route changes are related to more generic __alloc_pages() change)

Thanks,
Dmitry

#69 lore raw

Re: [PATCH v5 02/16] memcg: enable accounting for IP address and routing-related objects

Quoted 12 lines · Dmitry Safonov

Hi Vasily,

On Mon, 19 Jul 2021 at 11:45, Vasily Averin <[email protected]> wrote:
[..]

Quoted 7 lines
mm/memcontrol.c +0 -0
index ae1f5d0..1bbf239 100644
@@ -968,7 +968,7 @@ static __always_inline bool memcg_kmem_bypass(void)
                return false;
 
Quoted 10 lines

mm/memcontrol.c static __always_inline bool memcg_kmem_bypass(void) · line 972

Quoted 4 lines
mm/memcontrol.c +1 -1
        /* Memcg to charge can't be determined. */
-       if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+       if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
                return true;

This seems to do two separate things in one patch. Probably, it's better to separate them.
(I may miss how route changes are related to more generic __alloc_pages() change)

It was requested to squash them together in some previous versions.
https://lore.kernel.org/linux-mm/[email protected]/

#70 lore raw

Re: [PATCH v5 02/16] memcg: enable accounting for IP address and routing-related objects

Quoted 24 lines · Shakeel Butt
Quoted 12 lines · Dmitry Safonov

Hi Vasily,

On Mon, 19 Jul 2021 at 11:45, Vasily Averin <[email protected]> wrote:
[..]

Quoted 7 lines
mm/memcontrol.c +0 -0
index ae1f5d0..1bbf239 100644
@@ -968,7 +968,7 @@ static __always_inline bool memcg_kmem_bypass(void)
                return false;
 
Quoted 10 lines

mm/memcontrol.c static __always_inline bool memcg_kmem_bypass(void) · line 972

Quoted 4 lines
mm/memcontrol.c +1 -1
        /* Memcg to charge can't be determined. */
-       if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+       if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
                return true;

This seems to do two separate things in one patch. Probably, it's better to separate them.
(I may miss how route changes are related to more generic __alloc_pages() change)

Quoted 3 lines

It was requested to squash them together in some previous versions.
https://lore.kernel.org/linux-mm/[email protected]/

Ah, alright, never mind than.

Thanks,
Dmitry

#71 lore raw

Re: [PATCH v5 02/16] memcg: enable accounting for IP address and routing-related objects

In reply to Vasily Averin
Quoted 50 lines · Vasily Averin

An netadmin inside container can use 'ip a a' and 'ip r a' to assign a large number of ipv4/ipv6 addresses and routing entries and force kernel to allocate megabytes of unaccounted memory for long-lived per-netdevice related kernel objects:
'struct in_ifaddr', 'struct inet6_ifaddr', 'struct fib6_node', 'struct rt6_info', 'struct fib_rules' and ip_fib caches.

These objects can be manually removed, though usually they lives in memory till destroy of its net namespace.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

One of such objects is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

In this case it is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass().

Obsoleted in_interrupt() does not describe real execution context properly. From include/linux/preempt.h:

The following macros are deprecated and should not be used in new code:
in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled

To verify the current execution context new macro should be used instead:
in_task() - We're in task context

---
 mm/memcontrol.c      | 2 +-
 net/core/fib_rules.c | 4 ++--
 net/ipv4/devinet.c   | 2 +-
 net/ipv4/fib_trie.c  | 4 ++--
 net/ipv6/addrconf.c  | 2 +-
 net/ipv6/ip6_fib.c   | 4 ++--
 net/ipv6/route.c     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
mm/memcontrol.c +0 -0
index ae1f5d0..1bbf239 100644

mm/memcontrol.c static __always_inline bool memcg_kmem_bypass(void) · line 974

Quoted 8 lines
mm/memcontrol.c +1 -1
@@ -968,7 +968,7 @@ static __always_inline bool memcg_kmem_bypass(void)
                return false;
 
        /* Memcg to charge can't be determined. */
-       if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+       if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
                return true;
 
        return false;

Can you please also change in_interrupt() in active_memcg() as well? There are other unrelated in_interrupt() in that file but the one in active_memcg() should be coupled with this change.

#72 lore raw

Re: [PATCH v5 02/16] memcg: enable accounting for IP address and routing-related objects

Quoted 58 lines · Shakeel Butt
Quoted 57 lines · Vasily Averin

An netadmin inside container can use 'ip a a' and 'ip r a' to assign a large number of ipv4/ipv6 addresses and routing entries and force kernel to allocate megabytes of unaccounted memory for long-lived per-netdevice related kernel objects:
'struct in_ifaddr', 'struct inet6_ifaddr', 'struct fib6_node', 'struct rt6_info', 'struct fib_rules' and ip_fib caches.

These objects can be manually removed, though usually they lives in memory till destroy of its net namespace.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

One of such objects is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

In this case it is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass().

Obsoleted in_interrupt() does not describe real execution context properly. From include/linux/preempt.h:

The following macros are deprecated and should not be used in new code:
in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled

To verify the current execution context new macro should be used instead:
in_task() - We're in task context

---
 mm/memcontrol.c      | 2 +-
 net/core/fib_rules.c | 4 ++--
 net/ipv4/devinet.c   | 2 +-
 net/ipv4/fib_trie.c  | 4 ++--
 net/ipv6/addrconf.c  | 2 +-
 net/ipv6/ip6_fib.c   | 4 ++--
 net/ipv6/route.c     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
mm/memcontrol.c +1 -1
index ae1f5d0..1bbf239 100644
@@ -968,7 +968,7 @@ static __always_inline bool memcg_kmem_bypass(void)
                return false;
 
        /* Memcg to charge can't be determined. */
-       if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+       if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
                return true;
 
Quoted 5 lines

mm/memcontrol.c static __always_inline bool memcg_kmem_bypass(void) · line 974

Quoted 1 line
mm/memcontrol.c +0 -0
        return false;

Can you please also change in_interrupt() in active_memcg() as well? There are other unrelated in_interrupt() in that file but the one in active_memcg() should be coupled with this change.

Could you please elaborate?
From my point of view active_memcg is paired with set_active_memcg() and is not related to this case. active_memcg uses memcg that was set by set_active_memcg(), either from int_active_memcg per-cpu pointer or from current->active_memcg pointer.
I'm agree, it in case of disabled BH it is incorrect to use int_active_memcg,
we still can use current->active_memcg. However it isn't a problem,
memcg will be properly provided in both cases.

I think it's better to fix set_active_memcg/active_memcg by separate patch.

Am I missed something perhaps?

Thank you,
Vasily Averin

#73 lore raw

Re: [PATCH v5 02/16] memcg: enable accounting for IP address and routing-related objects

Quoted 1 line · Vasily Averin

[...]

Quoted 13 lines
Quoted 4 lines

Can you please also change in_interrupt() in active_memcg() as well? There are other unrelated in_interrupt() in that file but the one in active_memcg() should be coupled with this change.

Could you please elaborate?
From my point of view active_memcg is paired with set_active_memcg() and is not related to this case. active_memcg uses memcg that was set by set_active_memcg(), either from int_active_memcg per-cpu pointer or from current->active_memcg pointer.
I'm agree, it in case of disabled BH it is incorrect to use int_active_memcg,
we still can use current->active_memcg. However it isn't a problem,
memcg will be properly provided in both cases.

Quoted 4 lines

I think it's better to fix set_active_memcg/active_memcg by separate patch.

Am I missed something perhaps?

No you are right. That should be a separate patch.

#74 lore raw

PATCH 1/1 memcg: replace in_interrupt() by !in_task() in active_memcg()

set_active_memcg() uses in_interrupt() check to select proper storage for
cgroup: pointer on task struct or per-cpu pointer.

It isn't fully correct: obsoleted in_interrupt() includes tasks with disabled BH. It's better to use '!in_task()' instead.

Fixes 37d5985c003d ("mm: kmem: prepare remote memcg charging infra for interrupt contexts")
---
 include/linux/sched/mm.h | 2 +-
 mm/memcontrol.c          | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
include/linux/sched/mm.h +1 -1
index e24b1fe348e3..9dd071f78dba 100644
@@ -306,7 +306,7 @@ set_active_memcg(struct mem_cgroup *memcg)
 {
 	struct mem_cgroup *old;
 
-	if (in_interrupt()) {
+	if (!in_task()) {
 		old = this_cpu_read(int_active_memcg);
 		this_cpu_write(int_active_memcg, memcg);
 	} else {
mm/memcontrol.c +1 -1
index ae1f5d0cb581..3ebf792ef2c0 100644
@@ -905,7 +905,7 @@ EXPORT_SYMBOL(mem_cgroup_from_task);
 
 static __always_inline struct mem_cgroup *active_memcg(void)
 {
-	if (in_interrupt())
+	if (!in_task())
 		return this_cpu_read(int_active_memcg);
 	else
 		return current->active_memcg;
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#75 lore raw

Re: [PATCH] memcg: replace in_interrupt() by !in_task() in active_memcg()

Quoted 10 lines · Vasily Averin

set_active_memcg() uses in_interrupt() check to select proper storage for
cgroup: pointer on task struct or per-cpu pointer.

It isn't fully correct: obsoleted in_interrupt() includes tasks with disabled BH. It's better to use '!in_task()' instead.

Fixes 37d5985c003d ("mm: kmem: prepare remote memcg charging infra for interrupt contexts")

Thanks.

#76 lore raw

PATCH v5 3/16 memcg: enable accounting for inet_bin_bucket cache

In reply to a message not in the archive

net namespace can create up to 64K tcp and dccp ports and force kernel to allocate up to several megabytes of memory per netns
for inet_bind_bucket objects.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/dccp/proto.c | 2 +-
 net/ipv4/tcp.c   | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
net/dccp/proto.c +1 -1
index 7eb0fb2..abb5c59 100644
@@ -1126,7 +1126,7 @@ static int __init dccp_init(void)
 	dccp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("dccp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!dccp_hashinfo.bind_bucket_cachep)
 		goto out_free_hashinfo2;
 
net/ipv4/tcp.c +3 -1
index d5ab5f2..5c0605e 100644
@@ -4509,7 +4509,9 @@ void __init tcp_init(void)
 	tcp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("tcp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_PANIC |
+				  SLAB_ACCOUNT,
+				  NULL);
 
 	/* Size and allocate the main established and bind bucket
 	 * hash tables.
Signature · 1 line
1.8.3.1
#77 lore raw

PATCH v5 4/16 memcg: enable accounting for VLAN group array

In reply to a message not in the archive

vlan array consume up to 8 pages of memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/8021q/vlan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/8021q/vlan.c +1 -1
index 4cdf841..55275ef 100644
@@ -67,7 +67,7 @@ static int vlan_group_prealloc_vid(struct vlan_group *vg,
 		return 0;
 
 	size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
-	array = kzalloc(size, GFP_KERNEL);
+	array = kzalloc(size, GFP_KERNEL_ACCOUNT);
 	if (array == NULL)
 		return -ENOBUFS;
 
Signature · 1 line
1.8.3.1
#78 lore raw

PATCH v5 5/16 memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs allocation

In reply to a message not in the archive

Author: Andrey Ryabinin <[email protected]>

The size of the ip_tunnel_prl structs allocation is controllable from user-space, thus it's better to avoid spam in dmesg if allocation failed. Also add __GFP_ACCOUNT as this is a good candidate for per-memcg accounting. Allocation is temporary and limited by 4GB.

---
 net/ipv6/sit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv6/sit.c +3 -2
index df5bea8..33adc12 100644
@@ -321,7 +321,7 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 	 * we try harder to allocate.
 	 */
 	kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
-		kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
+		kcalloc(cmax, sizeof(*kp), GFP_KERNEL_ACCOUNT | __GFP_NOWARN) :
 		NULL;
 
 	rcu_read_lock();
@@ -334,7 +334,8 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 		 * For root users, retry allocating enough memory for
 		 * the answer.
 		 */
-		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
+		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC | __GFP_ACCOUNT |
+					      __GFP_NOWARN);
 		if (!kp) {
 			ret = -ENOMEM;
 			goto out;
Signature · 1 line
1.8.3.1
#79 lore raw

PATCH v5 6/16 memcg: enable accounting for scm_fp_list objects

In reply to a message not in the archive

unix sockets allows to send file descriptors via SCM_RIGHTS type messages. Each such send call forces kernel to allocate up to 2Kb memory for struct scm_fp_list.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/scm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/scm.c +2 -2
index ae3085d..5c356f0 100644
@@ -79,7 +79,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
 
 	if (!fpl)
 	{
-		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
+		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL_ACCOUNT);
 		if (!fpl)
 			return -ENOMEM;
 		*fplp = fpl;
@@ -355,7 +355,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
 		return NULL;
 
 	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
-			  GFP_KERNEL);
+			  GFP_KERNEL_ACCOUNT);
 	if (new_fpl) {
 		for (i = 0; i < fpl->count; i++)
 			get_file(fpl->fp[i]);
Signature · 1 line
1.8.3.1
#80 lore raw

PATCH v5 7/16 memcg: enable accounting for mnt_cache entries

In reply to a message not in the archive

The kernel allocates ~400 bytes of 'strcut mount' for any new mount. Creating a new mount namespace clones most of the parent mounts, and this can be repeated many times. Additionally, each mount allocates up to PATH_MAX=4096 bytes for mnt->mnt_devname.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/namespace.c +3 -2
index ab4174a..c6a74e5 100644
@@ -203,7 +203,8 @@ static struct mount *alloc_vfsmnt(const char *name)
 			goto out_free_cache;
 
 		if (name) {
-			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
+			mnt->mnt_devname = kstrdup_const(name,
+							 GFP_KERNEL_ACCOUNT);
 			if (!mnt->mnt_devname)
 				goto out_free_id;
 		}
@@ -4222,7 +4223,7 @@ void __init mnt_init(void)
 	int err;
 
 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
-			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
+			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
 	mount_hashtable = alloc_large_system_hash("Mount-cache",
 				sizeof(struct hlist_head),
Signature · 1 line
1.8.3.1
#81 lore raw

PATCH v5 8/16 memcg: enable accounting for pollfd and select bits arrays

In reply to a message not in the archive

User can call select/poll system calls with a large number of assigned file descriptors and force kernel to allocate up to several pages of memory till end of these sleeping system calls. We have here long-living unaccounted per-task allocations.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/select.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/select.c +2 -2
index 945896d..e83e563 100644
@@ -655,7 +655,7 @@ int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
 			goto out_nofds;
 
 		alloc_size = 6 * size;
-		bits = kvmalloc(alloc_size, GFP_KERNEL);
+		bits = kvmalloc(alloc_size, GFP_KERNEL_ACCOUNT);
 		if (!bits)
 			goto out_nofds;
 	}
@@ -1000,7 +1000,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 
 		len = min(todo, POLLFD_PER_PAGE);
 		walk = walk->next = kmalloc(struct_size(walk, entries, len),
-					    GFP_KERNEL);
+					    GFP_KERNEL_ACCOUNT);
 		if (!walk) {
 			err = -ENOMEM;
 			goto out_fds;
Signature · 1 line
1.8.3.1
#82 lore raw

PATCH v5 9/16 memcg: enable accounting for file lock caches

In reply to a message not in the archive

User can create file locks for each open file and force kernel to allocate small but long-living objects per each open file.

It makes sense to account for these objects to limit the host's memory consumption from inside the memcg-limited container.

---
 fs/locks.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/locks.c +4 -2
index 74b2a1d..1bc7ede 100644
@@ -3056,10 +3056,12 @@ static int __init filelock_init(void)
 	int i;
 
 	flctx_cache = kmem_cache_create("file_lock_ctx",
-			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock_context), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	filelock_cache = kmem_cache_create("file_lock_cache",
-			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	for_each_possible_cpu(i) {
 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
Signature · 1 line
1.8.3.1
#83 lore raw

PATCH v5 10/16 memcg: enable accounting for fasync_cache

In reply to a message not in the archive

fasync_struct is used by almost all character device drivers to set up the fasync queue, and for regular files by the file lease code. This structure is quite small but long-living and it can be assigned for any open file.

It makes sense to account for its allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/fcntl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fcntl.c +2 -1
index dfc72f1..7941559 100644
@@ -1049,7 +1049,8 @@ static int __init fcntl_init(void)
 			__FMODE_EXEC | __FMODE_NONOTIFY));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
-		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
+					 sizeof(struct fasync_struct), 0,
+					 SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#84 lore raw

PATCH v5 11/16 memcg: enable accounting for new namesapces and struct nsproxy

In reply to a message not in the archive

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c            | 2 +-
 ipc/namespace.c           | 2 +-
 kernel/cgroup/namespace.c | 2 +-
 kernel/nsproxy.c          | 2 +-
 kernel/pid_namespace.c    | 2 +-
 kernel/time/namespace.c   | 4 ++--
 kernel/user_namespace.c   | 2 +-
 7 files changed, 8 insertions(+), 8 deletions(-)
fs/namespace.c +1 -1
index c6a74e5..e443ee6 100644
@@ -3289,7 +3289,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 	if (!ucounts)
 		return ERR_PTR(-ENOSPC);
 
-	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns) {
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
ipc/namespace.c +1 -1
index 7bd0766..ae83f0f 100644
@@ -42,7 +42,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
+	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
 	if (ns == NULL)
 		goto fail_dec;
 
kernel/cgroup/namespace.c +1 -1
index f5e8828..0d5c298 100644
@@ -24,7 +24,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	struct cgroup_namespace *new_ns;
 	int ret;
 
-	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
 	ret = ns_alloc_inum(&new_ns->ns);
kernel/nsproxy.c +1 -1
index abc01fc..eec72ca 100644
@@ -568,6 +568,6 @@ static void commit_nsset(struct nsset *nsset)
 
 int __init nsproxy_cache_init(void)
 {
-	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
+	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
 	return 0;
 }
kernel/pid_namespace.c +1 -1
index ca43239..6cd6715 100644
@@ -449,7 +449,7 @@ static struct user_namespace *pidns_owner(struct ns_common *ns)
 
 static __init int pid_namespaces_init(void)
 {
-	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
+	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
kernel/time/namespace.c +2 -2
index 12eab0d..aec8328 100644
@@ -88,13 +88,13 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
+	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
 	refcount_set(&ns->ns.count, 1);
 
-	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
kernel/user_namespace.c +1 -1
index ef82d40..6b2e3ca 100644
@@ -1385,7 +1385,7 @@ static struct user_namespace *userns_owner(struct ns_common *ns)
 
 static __init int user_namespaces_init(void)
 {
-	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
+	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
Signature · 1 line
1.8.3.1
#85 lore raw

PATCH v5 12/16 memcg: enable accounting of ipc resources

In reply to a message not in the archive

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

---
 ipc/msg.c | 2 +-
 ipc/sem.c | 9 +++++----
 ipc/shm.c | 2 +-
 3 files changed, 7 insertions(+), 6 deletions(-)
ipc/msg.c +1 -1
index 6810276..a0d0577 100644
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
ipc/sem.c +5 -4
index 971e75d..1a8b9f0 100644
@@ -514,7 +514,7 @@ static struct sem_array *sem_alloc(size_t nsems)
 	if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
 		return NULL;
 
-	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
+	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!sma))
 		return NULL;
 
@@ -1855,7 +1855,7 @@ static inline int get_undo_list(struct sem_undo_list **undo_listp)
 
 	undo_list = current->sysvsem.undo_list;
 	if (!undo_list) {
-		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
+		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
 		if (undo_list == NULL)
 			return -ENOMEM;
 		spin_lock_init(&undo_list->lock);
@@ -1941,7 +1941,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 
 	/* step 2: allocate new undo structure */
 	new = kvzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
-		       GFP_KERNEL);
+		       GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
 		return ERR_PTR(-ENOMEM);
@@ -2005,7 +2005,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	if (nsops > ns->sc_semopm)
 		return -E2BIG;
 	if (nsops > SEMOPM_FAST) {
-		sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
+		sops = kvmalloc_array(nsops, sizeof(*sops),
+				      GFP_KERNEL_ACCOUNT);
 		if (sops == NULL)
 			return -ENOMEM;
 	}
ipc/shm.c +1 -1
index 748933e..ab749be 100644
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
#86 lore raw

PATCH v5 13/16 memcg: enable accounting for signals

In reply to a message not in the archive

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 kernel/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
index a3229ad..8921c4a 100644
@@ -4663,7 +4663,7 @@ void __init signals_init(void)
 {
 	siginfo_buildtime_checks();
 
-	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
+	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC | SLAB_ACCOUNT);
 }
 
 #ifdef CONFIG_KGDB_KDB
Signature · 1 line
1.8.3.1
#87 lore raw

Re: [PATCH v5 13/16] memcg: enable accounting for signals

Quoted 6 lines · Vasily Averin

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

Not any more. Currently the number of sigqueue objects is limited by the rlimit of the creator of the user namespace of the container.

Quoted 2 lines

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

Does it? Why? The given justification appears to have bit-rotted since -rc1.

I know a lot of these things only really need a limit just to catch a program that starts malfunctioning. If that is indeed the case reasonable per-resource limits are probably better than some great big group limit that can be exhausted with any single resource in the group.

Is there a reason I am not aware of that where it makes sense to group all of the resources together and only count the number of bytes consumed?

Eric

Quoted 18 lines
---
 kernel/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
index a3229ad..8921c4a 100644
@@ -4663,7 +4663,7 @@ void __init signals_init(void)
 {
 	siginfo_buildtime_checks();
 
-	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
+	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC | SLAB_ACCOUNT);
 }
 
 #ifdef CONFIG_KGDB_KDB
#88 lore raw

Re: [PATCH v5 13/16] memcg: enable accounting for signals

Quoted 9 lines · Eric W. Biederman
Quoted 6 lines · Vasily Averin

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

Quoted 8 lines

Not any more. Currently the number of sigqueue objects is limited by the rlimit of the creator of the user namespace of the container.

Quoted 2 lines

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

Does it? Why? The given justification appears to have bit-rotted since -rc1.

Could you please explain what was changed in rc1?
From my POV accounting is required to help OOM-killer to select proper target.

Quoted 8 lines

I know a lot of these things only really need a limit just to catch a program that starts malfunctioning. If that is indeed the case reasonable per-resource limits are probably better than some great big group limit that can be exhausted with any single resource in the group.

Is there a reason I am not aware of that where it makes sense to group all of the resources together and only count the number of bytes consumed?

Any new limits:
a) should be set properly depending on huge number of incoming parameters.
b) should properly notify about hits
c) should be updated properly after b)
d) do a)-c) automatically if possible

In past OpenVz had own accounting subsystem, user beancounters (UBC). It accounted and limited 20+ resources per-container: numfiles, file locks, signals, netfilter rules, socket buffers and so on.
I assume you want to do something similar, so let me share our experience.

We had a lot of problems with UBC:
- it's quite hard to set up the limit.
Why it's good to consume N entities of some resource but it's bad to consume N+1 ones? per-process? per-user? per-thread? per-task? per-namespace? if nested? per-container? per-host? To answer the questions host admin should have additional knowledge and skills.

- Ok, we have set all limits. Some application hits it and fails.
It's quite hard to understand that application hits the limit, and failed due to this reason. From users point of view, if some application does not work (stable enough) inside container => containers are guilty.

- It's quite hard to understand that failed application just want to increase limit X up to N entities.

As result both host admins and container users was unhappy.
So after years of such fights we decided just to limit accounted memory instead.

Anyway, OOM-killer must know who consumed memory to select proper target.

Thank you,
vasily Averin

#89 lore raw

Re: [PATCH v5 13/16] memcg: enable accounting for signals

Quoted 54 lines · Vasily Averin
Quoted 9 lines · Eric W. Biederman
Quoted 6 lines · Vasily Averin

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

Quoted 8 lines

Not any more. Currently the number of sigqueue objects is limited by the rlimit of the creator of the user namespace of the container.

Quoted 2 lines

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

Does it? Why? The given justification appears to have bit-rotted since -rc1.

Could you please explain what was changed in rc1?
From my POV accounting is required to help OOM-killer to select proper target.

Quoted 8 lines

I know a lot of these things only really need a limit just to catch a program that starts malfunctioning. If that is indeed the case reasonable per-resource limits are probably better than some great big group limit that can be exhausted with any single resource in the group.

Is there a reason I am not aware of that where it makes sense to group all of the resources together and only count the number of bytes consumed?

Any new limits:
a) should be set properly depending on huge number of incoming parameters.
b) should properly notify about hits
c) should be updated properly after b)
d) do a)-c) automatically if possible

In past OpenVz had own accounting subsystem, user beancounters (UBC). It accounted and limited 20+ resources per-container: numfiles, file locks, signals, netfilter rules, socket buffers and so on.
I assume you want to do something similar, so let me share our experience.

We had a lot of problems with UBC:
- it's quite hard to set up the limit.
Why it's good to consume N entities of some resource but it's bad to consume N+1 ones? per-process? per-user? per-thread? per-task? per-namespace? if nested? per-container? per-host? To answer the questions host admin should have additional knowledge and skills.

- Ok, we have set all limits. Some application hits it and fails.
It's quite hard to understand that application hits the limit, and failed due to this reason. From users point of view, if some application does not work (stable enough) inside container => containers are guilty.

Quoted 7 lines

- It's quite hard to understand that failed application just want to increase limit X up to N entities.

As result both host admins and container users was unhappy.
So after years of such fights we decided just to limit accounted memory instead.

Anyway, OOM-killer must know who consumed memory to select proper target.

Just to support Vasily's point further, for systems running multiple workloads, it is much more preferred to be able to set one limit for each workload than to set many different limits.

One concrete example is described in commit ac7b79fd190b ("inotify,
memcg: account inotify instances to kmemcg"). The inotify instances which can be limited through fs sysctl inotify/max_user_instances and be further partitioned to users through per-user namespace specific sysctl but there is no sensible way to set a limit and partition it on a system that runs different workloads.

#90 lore raw

Re: [PATCH v5 13/16] memcg: enable accounting for signals

In reply to Vasily Averin
Quoted 13 lines · Vasily Averin
Quoted 12 lines · Eric W. Biederman
Quoted 6 lines · Vasily Averin

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

Not any more. Currently the number of sigqueue objects is limited by the rlimit of the creator of the user namespace of the container.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

Does it? Why? The given justification appears to have bit-rotted since -rc1.

Could you please explain what was changed in rc1?
From my POV accounting is required to help OOM-killer to select proper target.

You can no longer escape the rlimit of the creator of the user namespace, by creating multiple users inside of the user namespace. The users (in the user namespace) will have their individual rlimits but are jointly by the limit of the creator of the user namespace at the time the user namespace was created.

Quoted 31 lines
Quoted 8 lines

I know a lot of these things only really need a limit just to catch a program that starts malfunctioning. If that is indeed the case reasonable per-resource limits are probably better than some great big group limit that can be exhausted with any single resource in the group.

Is there a reason I am not aware of that where it makes sense to group all of the resources together and only count the number of bytes consumed?

Any new limits:
a) should be set properly depending on huge number of incoming parameters.
b) should properly notify about hits
c) should be updated properly after b)
d) do a)-c) automatically if possible

In past OpenVz had own accounting subsystem, user beancounters (UBC). It accounted and limited 20+ resources per-container: numfiles, file locks, signals, netfilter rules, socket buffers and so on.
I assume you want to do something similar, so let me share our experience.

We had a lot of problems with UBC:
- it's quite hard to set up the limit.
Why it's good to consume N entities of some resource but it's bad to consume N+1 ones? per-process? per-user? per-thread? per-task? per-namespace? if nested? per-container? per-host? To answer the questions host admin should have additional knowledge and skills.

- Ok, we have set all limits. Some application hits it and fails.
It's quite hard to understand that application hits the limit, and failed due to this reason. From users point of view, if some application does not work (stable enough) inside container => containers are guilty.

Quoted 5 lines

- It's quite hard to understand that failed application just want to increase limit X up to N entities.

As result both host admins and container users was unhappy. So after years of such fights we decided just to limit accounted memory instead.

Which is a perfectly fine justification. However the justification presented in the change log was that there is no existing limit, and that is what is factually wrong.

Different kinds of limits serve different purposes. An accounting of how many instance of a resource has been used serve the purpose of detecting when an application has gone completely out of spec.

Limits like you are implementing here are much better for just sharing and managing system resources.

Quoted 2 lines

Anyway, OOM-killer must know who consumed memory to select proper target.

The limits I am talking about are not useful to the OOM killer as usually the amount of resources that indicates something has gone wrong are too small to be a system-wide problem.

So please just correct the justification in the commit message and I will be happy.

Eric

Reviewed-by on v5 13/16
#91 lore raw

Re: [PATCH v5 13/16] memcg: enable accounting for signals

In reply to Vasily Averin
Quoted 8 lines · Vasily Averin

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

Quoted 4 lines

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

It seems like there is an agreement on this patch with the updated commit message. In next version you can add:

#92 lore raw

PATCH v5 14/16 memcg: enable accounting for posix_timers_cache slab

In reply to a message not in the archive

A program may create multiple interval timers using timer_create(). For each timer the kernel preallocates a "queued real-time signal", Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit. The allocated object is quite small, ~250 bytes, but even the default signal limits allow to consume up to 100 megabytes per user.

It makes sense to account for them to limit the host's memory consumption from inside the memcg-limited container.

---
 kernel/time/posix-timers.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/posix-timers.c +2 -2
index dd5697d..7363f81 100644
@@ -273,8 +273,8 @@ static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
 static __init int init_posix_timers(void)
 {
 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
-					sizeof (struct k_itimer), 0, SLAB_PANIC,
-					NULL);
+					sizeof(struct k_itimer), 0,
+					SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 __initcall(init_posix_timers);
Signature · 1 line
1.8.3.1
#93 lore raw

PATCH v5 15/16 memcg: enable accounting for tty-related objects

In reply to a message not in the archive

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index 26debec..e787f6f 100644
@@ -1493,7 +1493,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
@@ -3119,7 +3119,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
1.8.3.1
#94 lore raw

PATCH v5 16/16 memcg: enable accounting for ldt_struct objects

In reply to a message not in the archive

Each task can request own LDT and force the kernel to allocate up to 64Kb memory per-mm.

There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines.
The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

It makes sense to account for this objects to restrict the host's memory consumption from inside the memcg-limited container.

---
 arch/x86/kernel/ldt.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/ldt.c +3 -3
index aa15132..525876e 100644
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	if (num_entries > LDT_ENTRIES)
 		return NULL;
 
-	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
+	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
 	if (!new_ldt)
 		return NULL;
 
@@ -168,9 +168,9 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	else
-		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
+		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 
 	if (!new_ldt->entries) {
 		kfree(new_ldt);
Signature · 1 line
1.8.3.1
#95 lore raw

PATCH v4 1/16 memcg: enable accounting for net_device and Tx/Rx queues

In reply to Vasily Averin

Container netadmin can create a lot of fake net devices, then create a new net namespace and repeat it again and again. Net device can request the creation of up to 4096 tx and rx queues, and force kernel to allocate up to several tens of megabytes memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/dev.c +3 -3
index 1f79b9a..87b1e80 100644
@@ -9994,7 +9994,7 @@ static int netif_alloc_rx_queues(struct net_device *dev)
 
 	BUG_ON(count < 1);
 
-	rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!rx)
 		return -ENOMEM;
 
@@ -10061,7 +10061,7 @@ static int netif_alloc_netdev_queues(struct net_device *dev)
 	if (count < 1 || count > 0xffff)
 		return -EINVAL;
 
-	tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!tx)
 		return -ENOMEM;
 
@@ -10693,7 +10693,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
 	/* ensure 32-byte alignment of whole construct */
 	alloc_size += NETDEV_ALIGN - 1;
 
-	p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!p)
 		return NULL;
 
Signature · 1 line
1.8.3.1
#96 lore raw

PATCH v4 2/16 memcg: enable accounting for IP address and routing-related objects

In reply to Vasily Averin

Netadmin inside container can use 'ip a a' and 'ip r a' to assign a large number of ipv4/ipv6 addresses and routing entries and force kernel to allocate megabytes of unaccounted memory for long-lived per-netdevice related kernel objects:
'struct in_ifaddr', 'struct inet6_ifaddr', 'struct fib6_node', 'struct rt6_info', 'struct fib_rules' and ip_fib caches.

These objects can be manually removed, though usually they lives in memory till destroy of its net namespace.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

One of such objects is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

In this case it is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass().

Obsoleted in_interrupt() does not describe real execution context properly. From include/linux/preempt.h:

The following macros are deprecated and should not be used in new code:
in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled

To verify the current execution context new macro should be used instead:
in_task() - We're in task context

---
 mm/memcontrol.c      | 2 +-
 net/core/fib_rules.c | 4 ++--
 net/ipv4/devinet.c   | 2 +-
 net/ipv4/fib_trie.c  | 4 ++--
 net/ipv6/addrconf.c  | 2 +-
 net/ipv6/ip6_fib.c   | 4 ++--
 net/ipv6/route.c     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
mm/memcontrol.c +1 -1
index e064ac0d..15108ad 100644
@@ -1076,7 +1076,7 @@ static __always_inline bool memcg_kmem_bypass(void)
 		return false;
 
 	/* Memcg to charge can't be determined. */
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 
 	return false;
net/core/fib_rules.c +2 -2
index cd80ffe..65d8b1d 100644
@@ -57,7 +57,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 {
 	struct fib_rule *r;
 
-	r = kzalloc(ops->rule_size, GFP_KERNEL);
+	r = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (r == NULL)
 		return -ENOMEM;
 
@@ -541,7 +541,7 @@ static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
 			goto errout;
 	}
 
-	nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
+	nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (!nlrule) {
 		err = -ENOMEM;
 		goto errout;
net/ipv4/devinet.c +1 -1
index 2e35f68da..9b90413 100644
@@ -215,7 +215,7 @@ static void devinet_sysctl_unregister(struct in_device *idev)
 
 static struct in_ifaddr *inet_alloc_ifa(void)
 {
-	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
+	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL_ACCOUNT);
 }
 
 static void inet_rcu_free_ifa(struct rcu_head *head)
net/ipv4/fib_trie.c +2 -2
index 25cf387..8060524 100644
@@ -2380,11 +2380,11 @@ void __init fib_trie_init(void)
 {
 	fn_alias_kmem = kmem_cache_create("ip_fib_alias",
 					  sizeof(struct fib_alias),
-					  0, SLAB_PANIC, NULL);
+					  0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
 					   LEAF_SIZE,
-					   0, SLAB_PANIC, NULL);
+					   0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 }
 
 struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
net/ipv6/addrconf.c +1 -1
index a9e53f5..d56a15a 100644
@@ -1080,7 +1080,7 @@ static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
 			goto out;
 	}
 
-	ifa = kzalloc(sizeof(*ifa), gfp_flags);
+	ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
 	if (!ifa) {
 		err = -ENOBUFS;
 		goto out;
net/ipv6/ip6_fib.c +2 -2
index 679699e..0982b7c 100644
@@ -2444,8 +2444,8 @@ int __init fib6_init(void)
 	int ret = -ENOMEM;
 
 	fib6_node_kmem = kmem_cache_create("fib6_nodes",
-					   sizeof(struct fib6_node),
-					   0, SLAB_HWCACHE_ALIGN,
+					   sizeof(struct fib6_node), 0,
+					   SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
 					   NULL);
 	if (!fib6_node_kmem)
 		goto out;
net/ipv6/route.c +1 -1
index 373d480..5dc5c68 100644
@@ -6510,7 +6510,7 @@ int __init ip6_route_init(void)
 	ret = -ENOMEM;
 	ip6_dst_ops_template.kmem_cachep =
 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!ip6_dst_ops_template.kmem_cachep)
 		goto out;
 
Signature · 1 line
1.8.3.1
#97 lore raw

PATCH v4 3/16 memcg: enable accounting for inet_bin_bucket cache

In reply to Vasily Averin

net namespace can create up to 64K tcp and dccp ports and force kernel to allocate up to several megabytes of memory per netns
for inet_bind_bucket objects.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/dccp/proto.c | 2 +-
 net/ipv4/tcp.c   | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
net/dccp/proto.c +1 -1
index 6d705d9..f90d1e8 100644
@@ -1126,7 +1126,7 @@ static int __init dccp_init(void)
 	dccp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("dccp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!dccp_hashinfo.bind_bucket_cachep)
 		goto out_free_hashinfo2;
 
net/ipv4/tcp.c +3 -1
index de7cc84..5817a86b 100644
@@ -4498,7 +4498,9 @@ void __init tcp_init(void)
 	tcp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("tcp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_PANIC |
+				  SLAB_ACCOUNT,
+				  NULL);
 
 	/* Size and allocate the main established and bind bucket
 	 * hash tables.
Signature · 1 line
1.8.3.1
#98 lore raw

PATCH v4 4/16 memcg: enable accounting for VLAN group array

In reply to Vasily Averin

vlan array consume up to 8 pages of memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/8021q/vlan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/8021q/vlan.c +1 -1
index 8b644113..d0a579d4 100644
@@ -67,7 +67,7 @@ static int vlan_group_prealloc_vid(struct vlan_group *vg,
 		return 0;
 
 	size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
-	array = kzalloc(size, GFP_KERNEL);
+	array = kzalloc(size, GFP_KERNEL_ACCOUNT);
 	if (array == NULL)
 		return -ENOBUFS;
 
Signature · 1 line
1.8.3.1
#99 lore raw

PATCH v4 5/16 memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs allocation

In reply to Vasily Averin

Author: Andrey Ryabinin <[email protected]>

The size of the ip_tunnel_prl structs allocation is controllable from user-space, thus it's better to avoid spam in dmesg if allocation failed. Also add __GFP_ACCOUNT as this is a good candidate for per-memcg accounting. Allocation is temporary and limited by 4GB.

---
 net/ipv6/sit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv6/sit.c +3 -2
index 9fdccf0..2ba147c 100644
@@ -320,7 +320,7 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 	 * we try harder to allocate.
 	 */
 	kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
-		kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
+		kcalloc(cmax, sizeof(*kp), GFP_KERNEL_ACCOUNT | __GFP_NOWARN) :
 		NULL;
 
 	rcu_read_lock();
@@ -333,7 +333,8 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 		 * For root users, retry allocating enough memory for
 		 * the answer.
 		 */
-		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
+		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC | __GFP_ACCOUNT |
+					      __GFP_NOWARN);
 		if (!kp) {
 			ret = -ENOMEM;
 			goto out;
Signature · 1 line
1.8.3.1
#100 lore raw

PATCH v4 6/16 memcg: enable accounting for scm_fp_list objects

In reply to Vasily Averin

unix sockets allows to send file descriptors via SCM_RIGHTS type messages. Each such send call forces kernel to allocate up to 2Kb memory for struct scm_fp_list.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/scm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/scm.c +2 -2
index 8156d4f..e837e4f 100644
@@ -79,7 +79,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
 
 	if (!fpl)
 	{
-		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
+		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL_ACCOUNT);
 		if (!fpl)
 			return -ENOMEM;
 		*fplp = fpl;
@@ -348,7 +348,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
 		return NULL;
 
 	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
-			  GFP_KERNEL);
+			  GFP_KERNEL_ACCOUNT);
 	if (new_fpl) {
 		for (i = 0; i < fpl->count; i++)
 			get_file(fpl->fp[i]);
Signature · 1 line
1.8.3.1
#101 lore raw

PATCH v4 7/16 memcg: enable accounting for new namesapces and struct nsproxy

In reply to Vasily Averin

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c            | 2 +-
 ipc/namespace.c           | 2 +-
 kernel/cgroup/namespace.c | 2 +-
 kernel/nsproxy.c          | 2 +-
 kernel/pid_namespace.c    | 2 +-
 kernel/time/namespace.c   | 4 ++--
 kernel/user_namespace.c   | 2 +-
 7 files changed, 8 insertions(+), 8 deletions(-)
fs/namespace.c +1 -1
index 56bb5a5..5ecfa349 100644
@@ -3286,7 +3286,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 	if (!ucounts)
 		return ERR_PTR(-ENOSPC);
 
-	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns) {
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
ipc/namespace.c +1 -1
index 7bd0766..ae83f0f 100644
@@ -42,7 +42,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
+	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
 	if (ns == NULL)
 		goto fail_dec;
 
kernel/cgroup/namespace.c +1 -1
index f5e8828..0d5c298 100644
@@ -24,7 +24,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	struct cgroup_namespace *new_ns;
 	int ret;
 
-	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
 	ret = ns_alloc_inum(&new_ns->ns);
kernel/nsproxy.c +1 -1
index abc01fc..eec72ca 100644
@@ -568,6 +568,6 @@ static void commit_nsset(struct nsset *nsset)
 
 int __init nsproxy_cache_init(void)
 {
-	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
+	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
 	return 0;
 }
kernel/pid_namespace.c +1 -1
index ca43239..6cd6715 100644
@@ -449,7 +449,7 @@ static struct user_namespace *pidns_owner(struct ns_common *ns)
 
 static __init int pid_namespaces_init(void)
 {
-	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
+	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
kernel/time/namespace.c +2 -2
index 12eab0d..aec8328 100644
@@ -88,13 +88,13 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
+	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
 	refcount_set(&ns->ns.count, 1);
 
-	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
kernel/user_namespace.c +1 -1
index 9a4b980..9c6a42b 100644
@@ -1378,7 +1378,7 @@ static struct user_namespace *userns_owner(struct ns_common *ns)
 
 static __init int user_namespaces_init(void)
 {
-	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
+	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
Signature · 1 line
1.8.3.1
Acked-by on v4 7/16
#102 lore raw

Re: [PATCH v4 07/16] memcg: enable accounting for new namesapces and struct nsproxy

Quoted 8 lines · Vasily Averin

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

makes sense.

Quoted 109 lines
---
 fs/namespace.c            | 2 +-
 ipc/namespace.c           | 2 +-
 kernel/cgroup/namespace.c | 2 +-
 kernel/nsproxy.c          | 2 +-
 kernel/pid_namespace.c    | 2 +-
 kernel/time/namespace.c   | 4 ++--
 kernel/user_namespace.c   | 2 +-
 7 files changed, 8 insertions(+), 8 deletions(-)
fs/namespace.c +1 -1
index 56bb5a5..5ecfa349 100644
@@ -3286,7 +3286,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 	if (!ucounts)
 		return ERR_PTR(-ENOSPC);
 
-	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns) {
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
ipc/namespace.c +1 -1
index 7bd0766..ae83f0f 100644
@@ -42,7 +42,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
+	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
 	if (ns == NULL)
 		goto fail_dec;
 
kernel/cgroup/namespace.c +1 -1
index f5e8828..0d5c298 100644
@@ -24,7 +24,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	struct cgroup_namespace *new_ns;
 	int ret;
 
-	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
 	ret = ns_alloc_inum(&new_ns->ns);
kernel/nsproxy.c +1 -1
index abc01fc..eec72ca 100644
@@ -568,6 +568,6 @@ static void commit_nsset(struct nsset *nsset)
 
 int __init nsproxy_cache_init(void)
 {
-	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
+	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
 	return 0;
 }
kernel/pid_namespace.c +1 -1
index ca43239..6cd6715 100644
@@ -449,7 +449,7 @@ static struct user_namespace *pidns_owner(struct ns_common *ns)
 
 static __init int pid_namespaces_init(void)
 {
-	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
+	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
kernel/time/namespace.c +2 -2
index 12eab0d..aec8328 100644
@@ -88,13 +88,13 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
+	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
 	refcount_set(&ns->ns.count, 1);
 
-	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
kernel/user_namespace.c +1 -1
index 9a4b980..9c6a42b 100644
@@ -1378,7 +1378,7 @@ static struct user_namespace *userns_owner(struct ns_common *ns)
 
 static __init int user_namespaces_init(void)
 {
-	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
+	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
Signature · 1 line
1.8.3.1
Acked-by on v4 7/16
#103 lore raw

Re: [PATCH v4 07/16] memcg: enable accounting for new namesapces and struct nsproxy

In reply to Vasily Averin
Quoted 9 lines · Vasily Averin

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

---

Serge's ack reminded me of this. Looks good if the mm folks are fine with this too,

#104 lore raw

PATCH v4 8/16 memcg: enable accounting of ipc resources

In reply to Vasily Averin

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

---
 ipc/msg.c |  2 +-
 ipc/sem.c | 10 ++++++----
 ipc/shm.c |  2 +-
 3 files changed, 8 insertions(+), 6 deletions(-)
ipc/msg.c +1 -1
index acd1bc7..87898cb 100644
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
ipc/sem.c +6 -4
index f6c30a8..52a6599 100644
@@ -511,7 +511,7 @@ static struct sem_array *sem_alloc(size_t nsems)
 	if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
 		return NULL;
 
-	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
+	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!sma))
 		return NULL;
 
@@ -1850,7 +1850,7 @@ static inline int get_undo_list(struct sem_undo_list **undo_listp)
 
 	undo_list = current->sysvsem.undo_list;
 	if (!undo_list) {
-		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
+		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
 		if (undo_list == NULL)
 			return -ENOMEM;
 		spin_lock_init(&undo_list->lock);
@@ -1935,7 +1935,8 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	rcu_read_unlock();
 
 	/* step 2: allocate new undo structure */
-	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
+	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
+		      GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
 		return ERR_PTR(-ENOMEM);
@@ -1999,7 +2000,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	if (nsops > ns->sc_semopm)
 		return -E2BIG;
 	if (nsops > SEMOPM_FAST) {
-		sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
+		sops = kvmalloc_array(nsops, sizeof(*sops),
+				      GFP_KERNEL_ACCOUNT);
 		if (sops == NULL)
 			return -ENOMEM;
 	}
ipc/shm.c +1 -1
index febd88d..7632d72 100644
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1
#105 lore raw

PATCH v4 9/16 memcg: enable accounting for mnt_cache entries

In reply to Vasily Averin

The kernel allocates ~400 bytes of 'strcut mount' for any new mount. Creating a new mount namespace clones most of the parent mounts, and this can be repeated many times. Additionally, each mount allocates up to PATH_MAX=4096 bytes for mnt->mnt_devname.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/namespace.c +3 -2
index 5ecfa349..fc1b50d 100644
@@ -203,7 +203,8 @@ static struct mount *alloc_vfsmnt(const char *name)
 			goto out_free_cache;
 
 		if (name) {
-			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
+			mnt->mnt_devname = kstrdup_const(name,
+							 GFP_KERNEL_ACCOUNT);
 			if (!mnt->mnt_devname)
 				goto out_free_id;
 		}
@@ -4213,7 +4214,7 @@ void __init mnt_init(void)
 	int err;
 
 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
-			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
+			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
 	mount_hashtable = alloc_large_system_hash("Mount-cache",
 				sizeof(struct hlist_head),
Signature · 1 line
1.8.3.1
#106 lore raw

PATCH v4 10/16 memcg: enable accounting for pollfd and select bits arrays

In reply to Vasily Averin

User can call select/poll system calls with a large number of assigned file descriptors and force kernel to allocate up to several pages of memory till end of these sleeping system calls. We have here long-living unaccounted per-task allocations.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/select.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/select.c +2 -2
index 945896d..e83e563 100644
@@ -655,7 +655,7 @@ int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
 			goto out_nofds;
 
 		alloc_size = 6 * size;
-		bits = kvmalloc(alloc_size, GFP_KERNEL);
+		bits = kvmalloc(alloc_size, GFP_KERNEL_ACCOUNT);
 		if (!bits)
 			goto out_nofds;
 	}
@@ -1000,7 +1000,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 
 		len = min(todo, POLLFD_PER_PAGE);
 		walk = walk->next = kmalloc(struct_size(walk, entries, len),
-					    GFP_KERNEL);
+					    GFP_KERNEL_ACCOUNT);
 		if (!walk) {
 			err = -ENOMEM;
 			goto out_fds;
Signature · 1 line
1.8.3.1
#107 lore raw

PATCH v4 11/16 memcg: enable accounting for signals

In reply to Vasily Averin

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 kernel/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
index f271835..a7fa849 100644
@@ -4639,7 +4639,7 @@ void __init signals_init(void)
 {
 	siginfo_buildtime_checks();
 
-	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
+	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC | SLAB_ACCOUNT);
 }
 
 #ifdef CONFIG_KGDB_KDB
Signature · 1 line
1.8.3.1
#108 lore raw

PATCH v4 12/16 memcg: enable accounting for posix_timers_cache slab

In reply to Vasily Averin

A program may create multiple interval timers using timer_create(). For each timer the kernel preallocates a "queued real-time signal", Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit. The allocated object is quite small, ~250 bytes, but even the default signal limits allow to consume up to 100 megabytes per user.

It makes sense to account for them to limit the host's memory consumption from inside the memcg-limited container.

---
 kernel/time/posix-timers.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/posix-timers.c +2 -2
index bf540f5a..2eee615 100644
@@ -273,8 +273,8 @@ static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
 static __init int init_posix_timers(void)
 {
 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
-					sizeof (struct k_itimer), 0, SLAB_PANIC,
-					NULL);
+					sizeof(struct k_itimer), 0,
+					SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 __initcall(init_posix_timers);
Signature · 1 line
1.8.3.1
Reviewed-by on v4 12/16
#109 lore raw

Re: [PATCH v4 12/16] memcg: enable accounting for posix_timers_cache slab

Quoted 11 lines · Vasily Averin

A program may create multiple interval timers using timer_create(). For each timer the kernel preallocates a "queued real-time signal", Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit. The allocated object is quite small, ~250 bytes, but even the default signal limits allow to consume up to 100 megabytes per user.

It makes sense to account for them to limit the host's memory consumption from inside the memcg-limited container.

#110 lore raw

PATCH v4 13/16 memcg: enable accounting for file lock caches

In reply to Vasily Averin

User can create file locks for each open file and force kernel to allocate small but long-living objects per each open file.

It makes sense to account for these objects to limit the host's memory consumption from inside the memcg-limited container.

---
 fs/locks.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/locks.c +4 -2
index 6125d2d..fb751f3 100644
@@ -3007,10 +3007,12 @@ static int __init filelock_init(void)
 	int i;
 
 	flctx_cache = kmem_cache_create("file_lock_ctx",
-			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock_context), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	filelock_cache = kmem_cache_create("file_lock_cache",
-			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	for_each_possible_cpu(i) {
 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
Signature · 1 line
1.8.3.1
#111 lore raw

PATCH v4 14/16 memcg: enable accounting for fasync_cache

In reply to Vasily Averin

fasync_struct is used by almost all character device drivers to set up the fasync queue, and for regular files by the file lease code. This structure is quite small but long-living and it can be assigned for any open file.

It makes sense to account for its allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/fcntl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fcntl.c +2 -1
index dfc72f1..7941559 100644
@@ -1049,7 +1049,8 @@ static int __init fcntl_init(void)
 			__FMODE_EXEC | __FMODE_NONOTIFY));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
-		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
+					 sizeof(struct fasync_struct), 0,
+					 SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#112 lore raw

PATCH v4 15/16 memcg: enable accounting for tty-related objects

In reply to Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index 391bada..e613b8e 100644
@@ -1502,7 +1502,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
@@ -3127,7 +3127,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
1.8.3.1
maintainer Acked-by on v4 15/16
#113 lore raw

Re: [PATCH v4 15/16] memcg: enable accounting for tty-related objects

Quoted 15 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initial mount namespace only and the settings are controlled by host admin.

Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. As a result, one container can consume almost all allowed ptys
and allocate up to 1Gb of unaccounted memory.

Quoted 8 lines

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

#114 lore raw

PATCH v4 16/16 memcg: enable accounting for ldt_struct objects

In reply to Vasily Averin

Each task can request own LDT and force the kernel to allocate up to 64Kb memory per-mm.

There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines.
The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

It makes sense to account for this objects to restrict the host's memory consumption from inside the memcg-limited container.

---
 arch/x86/kernel/ldt.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
arch/x86/kernel/ldt.c +3 -3
index aa15132..525876e 100644
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	if (num_entries > LDT_ENTRIES)
 		return NULL;
 
-	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
+	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
 	if (!new_ldt)
 		return NULL;
 
@@ -168,9 +168,9 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	else
-		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
+		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 
 	if (!new_ldt->entries) {
 		kfree(new_ldt);
Signature · 1 line
1.8.3.1
#115 lore raw

PATCH v3 7/16 memcg: enable accounting for new namesapces and struct nsproxy

In reply to a message not in the archive

Container admin can create new namespaces and force kernel to allocate up to several pages of memory for the namespaces and its associated structures.
Net and uts namespaces have enabled accounting for such allocations. It makes sense to account for rest ones to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c            | 2 +-
 ipc/namespace.c           | 2 +-
 kernel/cgroup/namespace.c | 2 +-
 kernel/nsproxy.c          | 2 +-
 kernel/pid_namespace.c    | 2 +-
 kernel/time/namespace.c   | 4 ++--
 kernel/user_namespace.c   | 2 +-
 7 files changed, 8 insertions(+), 8 deletions(-)
fs/namespace.c +1 -1
index 56bb5a5..5ecfa349 100644
@@ -3286,7 +3286,7 @@ static struct mnt_namespace *alloc_mnt_ns(struct user_namespace *user_ns, bool a
 	if (!ucounts)
 		return ERR_PTR(-ENOSPC);
 
-	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct mnt_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns) {
 		dec_mnt_namespaces(ucounts);
 		return ERR_PTR(-ENOMEM);
ipc/namespace.c +1 -1
index 7bd0766..ae83f0f 100644
@@ -42,7 +42,7 @@ static struct ipc_namespace *create_ipc_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL);
+	ns = kzalloc(sizeof(struct ipc_namespace), GFP_KERNEL_ACCOUNT);
 	if (ns == NULL)
 		goto fail_dec;
 
kernel/cgroup/namespace.c +1 -1
index f5e8828..0d5c298 100644
@@ -24,7 +24,7 @@ static struct cgroup_namespace *alloc_cgroup_ns(void)
 	struct cgroup_namespace *new_ns;
 	int ret;
 
-	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL);
+	new_ns = kzalloc(sizeof(struct cgroup_namespace), GFP_KERNEL_ACCOUNT);
 	if (!new_ns)
 		return ERR_PTR(-ENOMEM);
 	ret = ns_alloc_inum(&new_ns->ns);
kernel/nsproxy.c +1 -1
index abc01fc..eec72ca 100644
@@ -568,6 +568,6 @@ static void commit_nsset(struct nsset *nsset)
 
 int __init nsproxy_cache_init(void)
 {
-	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC);
+	nsproxy_cachep = KMEM_CACHE(nsproxy, SLAB_PANIC|SLAB_ACCOUNT);
 	return 0;
 }
kernel/pid_namespace.c +1 -1
index ca43239..6cd6715 100644
@@ -449,7 +449,7 @@ static struct user_namespace *pidns_owner(struct ns_common *ns)
 
 static __init int pid_namespaces_init(void)
 {
-	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC);
+	pid_ns_cachep = KMEM_CACHE(pid_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 
 #ifdef CONFIG_CHECKPOINT_RESTORE
 	register_sysctl_paths(kern_path, pid_ns_ctl_table);
kernel/time/namespace.c +2 -2
index 12eab0d..aec8328 100644
@@ -88,13 +88,13 @@ static struct time_namespace *clone_time_ns(struct user_namespace *user_ns,
 		goto fail;
 
 	err = -ENOMEM;
-	ns = kmalloc(sizeof(*ns), GFP_KERNEL);
+	ns = kmalloc(sizeof(*ns), GFP_KERNEL_ACCOUNT);
 	if (!ns)
 		goto fail_dec;
 
 	refcount_set(&ns->ns.count, 1);
 
-	ns->vvar_page = alloc_page(GFP_KERNEL | __GFP_ZERO);
+	ns->vvar_page = alloc_page(GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	if (!ns->vvar_page)
 		goto fail_free;
 
kernel/user_namespace.c +1 -1
index af61294..886d6f9 100644
@@ -1319,7 +1319,7 @@ static struct user_namespace *userns_owner(struct ns_common *ns)
 
 static __init int user_namespaces_init(void)
 {
-	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC);
+	user_ns_cachep = KMEM_CACHE(user_namespace, SLAB_PANIC | SLAB_ACCOUNT);
 	return 0;
 }
 subsys_initcall(user_namespaces_init);
Signature · 1 line
1.8.3.1
#116 lore raw

PATCH v3 8/16 memcg: enable accounting of ipc resources

In reply to a message not in the archive

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

---
 ipc/msg.c |  2 +-
 ipc/sem.c | 10 ++++++----
 ipc/shm.c |  2 +-
 3 files changed, 8 insertions(+), 6 deletions(-)
ipc/msg.c +1 -1
index acd1bc7..87898cb 100644
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
ipc/sem.c +6 -4
index f6c30a8..52a6599 100644
@@ -511,7 +511,7 @@ static struct sem_array *sem_alloc(size_t nsems)
 	if (nsems > (INT_MAX - sizeof(*sma)) / sizeof(sma->sems[0]))
 		return NULL;
 
-	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL);
+	sma = kvzalloc(struct_size(sma, sems, nsems), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!sma))
 		return NULL;
 
@@ -1850,7 +1850,7 @@ static inline int get_undo_list(struct sem_undo_list **undo_listp)
 
 	undo_list = current->sysvsem.undo_list;
 	if (!undo_list) {
-		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL);
+		undo_list = kzalloc(sizeof(*undo_list), GFP_KERNEL_ACCOUNT);
 		if (undo_list == NULL)
 			return -ENOMEM;
 		spin_lock_init(&undo_list->lock);
@@ -1935,7 +1935,8 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	rcu_read_unlock();
 
 	/* step 2: allocate new undo structure */
-	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems, GFP_KERNEL);
+	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
+		      GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
 		return ERR_PTR(-ENOMEM);
@@ -1999,7 +2000,8 @@ static long do_semtimedop(int semid, struct sembuf __user *tsops,
 	if (nsops > ns->sc_semopm)
 		return -E2BIG;
 	if (nsops > SEMOPM_FAST) {
-		sops = kvmalloc_array(nsops, sizeof(*sops), GFP_KERNEL);
+		sops = kvmalloc_array(nsops, sizeof(*sops),
+				      GFP_KERNEL_ACCOUNT);
 		if (sops == NULL)
 			return -ENOMEM;
 	}
ipc/shm.c +1 -1
index febd88d..7632d72 100644
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 4ed65beb-bda3-1c93-fadf-296b760a32b2-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#118 lore raw

Re: [PATCH v3 08/16] memcg: enable accounting of ipc resources

Quoted 12 lines · Vasily Averin

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

ipc/msg.c static int newque(struct ipc_namespace *ns, struct ipc_params *params) · line 150

Quoted 5 lines
ipc/msg.c +1 -1
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);

Why this requires vmalloc? struct msg_queue is not big at all.

ipc/shm.c static int newseg(struct ipc_namespace *ns, struct ipc_params *params) · line 622

Quoted 8 lines
ipc/shm.c +1 -1
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);

Same question.
Kmem caches can be GFP_ACCOUNT by default.

A message that is not in the archive (sent off-list or not yet received): YIK6ttdnfjOo6XCN-bi+AKbBUZKY6gyzm1THtWbp2dZbC/[email protected]

#120 lore raw

Re: [PATCH v3 08/16] memcg: enable accounting of ipc resources

Quoted 27 lines · Alexey Dobriyan
Quoted 12 lines · Vasily Averin

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

ipc/msg.c static int newque(struct ipc_namespace *ns, struct ipc_params *params) · line 150

Quoted 5 lines
ipc/msg.c +1 -1
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);

Why this requires vmalloc? struct msg_queue is not big at all.

Quoted 6 lines
ipc/shm.c +0 -0
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
Quoted 5 lines

ipc/shm.c static int newseg(struct ipc_namespace *ns, struct ipc_params *params) · line 622

Quoted 2 lines
ipc/shm.c +1 -1
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);

Same question.
Kmem caches can be GFP_ACCOUNT by default.

It is side effect: previously all these objects was allocated via ipc_alloc/ipc_alloc_rcu function called kvmalloc inside.

Should I replace it to kmalloc right in this patch?

Thank you,
Vasily Averin

A message that is not in the archive (sent off-list or not yet received): dd9b1767-55e0-6754-3ac5-7e01de12f16e-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#122 lore raw

Re: [PATCH v3 08/16] memcg: enable accounting of ipc resources

Quoted 34 lines · Vasily Averin
Quoted 27 lines · Alexey Dobriyan
Quoted 12 lines · Vasily Averin

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

ipc/msg.c static int newque(struct ipc_namespace *ns, struct ipc_params *params) · line 150

Quoted 5 lines
ipc/msg.c +1 -1
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);

Why this requires vmalloc? struct msg_queue is not big at all.

Quoted 6 lines
ipc/shm.c +0 -0
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
Quoted 5 lines

ipc/shm.c static int newseg(struct ipc_namespace *ns, struct ipc_params *params) · line 622

Quoted 2 lines
ipc/shm.c +1 -1
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);

Same question.
Kmem caches can be GFP_ACCOUNT by default.

Quoted 4 lines

It is side effect: previously all these objects was allocated via ipc_alloc/ipc_alloc_rcu function called kvmalloc inside.

Should I replace it to kmalloc right in this patch?

I would say those are two independent things. I would agree that kvmalloc is bogus here. The allocation would try SLAB allocator first but if it fails (as kvmalloc doesn't try really hard) then it would fragment memory without a good reason which looks like a bug to me.

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): YILOab0/[email protected]

#124 lore raw

Re: [PATCH v3 08/16] memcg: enable accounting of ipc resources

Quoted 38 lines · Michal Hocko
Quoted 37 lines · Vasily Averin
Quoted 27 lines · Alexey Dobriyan
Quoted 12 lines · Vasily Averin

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

ipc/msg.c static int newque(struct ipc_namespace *ns, struct ipc_params *params) · line 150

Quoted 5 lines
ipc/msg.c +1 -1
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);

Why this requires vmalloc? struct msg_queue is not big at all.

Quoted 6 lines
ipc/shm.c +0 -0
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
Quoted 5 lines

ipc/shm.c static int newseg(struct ipc_namespace *ns, struct ipc_params *params) · line 622

Quoted 2 lines
ipc/shm.c +1 -1
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);

Same question.
Kmem caches can be GFP_ACCOUNT by default.

It is side effect: previously all these objects was allocated via ipc_alloc/ipc_alloc_rcu function called kvmalloc inside.

Quoted 6 lines
Quoted 1 line

Should I replace it to kmalloc right in this patch?

I would say those are two independent things. I would agree that kvmalloc is bogus here. The allocation would try SLAB allocator first but if it fails (as kvmalloc doesn't try really hard) then it would fragment memory without a good reason which looks like a bug to me.

I have dug into history and this all seems to be just code pattern copy&paste thing. In the past it was ipc_alloc_rcu which was a common code for all IPCs and that code was conditional on rcu_use_vmalloc (HDRLEN_KMALLOC + size > PAGE_SIZE). Later changed to kvmalloc and then helper removed and all users to use kvmalloc. It seems that only semaphores can grow large enough to care about kvmalloc though.

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#126 lore raw

Re: [PATCH v3 08/16] memcg: enable accounting of ipc resources

Quoted 46 lines · Michal Hocko
Quoted 38 lines · Michal Hocko
Quoted 37 lines · Vasily Averin
Quoted 27 lines · Alexey Dobriyan
Quoted 12 lines · Vasily Averin

When user creates IPC objects it forces kernel to allocate memory for these long-living objects.

It makes sense to account them to restrict the host's memory consumption from inside the memcg-limited container.

This patch enables accounting for IPC shared memory segments, messages semaphores and semaphore's undo lists.

ipc/msg.c static int newque(struct ipc_namespace *ns, struct ipc_params *params) · line 150

Quoted 5 lines
ipc/msg.c +1 -1
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL);
+	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);

Why this requires vmalloc? struct msg_queue is not big at all.

Quoted 6 lines
ipc/shm.c +0 -0
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
Quoted 5 lines

ipc/shm.c static int newseg(struct ipc_namespace *ns, struct ipc_params *params) · line 622

Quoted 2 lines
ipc/shm.c +1 -1
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL);
+	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);

Same question.
Kmem caches can be GFP_ACCOUNT by default.

It is side effect: previously all these objects was allocated via ipc_alloc/ipc_alloc_rcu function called kvmalloc inside.

Quoted 6 lines
Quoted 1 line

Should I replace it to kmalloc right in this patch?

I would say those are two independent things. I would agree that kvmalloc is bogus here. The allocation would try SLAB allocator first but if it fails (as kvmalloc doesn't try really hard) then it would fragment memory without a good reason which looks like a bug to me.

Quoted 6 lines

I have dug into history and this all seems to be just code pattern copy&paste thing. In the past it was ipc_alloc_rcu which was a common code for all IPCs and that code was conditional on rcu_use_vmalloc (HDRLEN_KMALLOC + size > PAGE_SIZE). Later changed to kvmalloc and then helper removed and all users to use kvmalloc. It seems that only semaphores can grow large enough to care about kvmalloc though.

I have one more cleanup for ipc,
so if no one objects, I'll fix this case, add my patch and send them together as a separate patch set a bit later.

Thank you,
Vasily Averin

#127 lore raw

cover ipc: allocations cleanup

Some ipc objects use the wrong allocation functions: small objects can use kmalloc(), and vice versa, potentially large objects can use kmalloc().

I think it's better to handle these patches via cgroups@ to avoid merge conflicts with my patches that include accounting for ipc objects.

Vasily Averin (2):
ipc sem: use kvmalloc for sem_undo allocation
ipc: use kmalloc for msg_queue and shmid_kernel

 ipc/msg.c |  6 +++---
 ipc/sem.c | 10 +++++-----
 ipc/shm.c |  6 +++---
 3 files changed, 11 insertions(+), 11 deletions(-)
Signature · 1 line
1.8.3.1
#128 lore raw

v2 cover ipc: allocations cleanup

Some ipc objects use the wrong allocation functions: small objects can use kmalloc(), and vice versa, potentially large objects can use kmalloc().

I think it's better to handle these patches via cgroups@ to avoid merge conflicts with "memcg: enable accounting of ipc resources" patch included to "memcg accounting from OpenVZ" patch set.

v2:
- improved patch description

Vasily Averin (2):
ipc sem: use kvmalloc for sem_undo allocation
ipc: use kmalloc for msg_queue and shmid_kernel

 ipc/msg.c |  6 +++---
 ipc/sem.c | 10 +++++-----
 ipc/shm.c |  6 +++---
 3 files changed, 11 insertions(+), 11 deletions(-)
Signature · 1 line
1.8.3.1
#129 lore raw

PATCH v2 1/2 ipc sem: use kvmalloc for sem_undo allocation

In reply to Vasily Averin

size of sem_undo can exceed one page and with the maximum possible nsems = 32000 it can grow up to 64Kb. Let's switch its allocation to kvmalloc to avoid user-triggered disruptive actions like OOM killer in case of high-order memory shortage.

User triggerable high order allocations are quite a problem on heavily fragmented systems. They can be a DoS vector.

---
 ipc/sem.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
ipc/sem.c +5 -5
index 52a6599..93088d6 100644
@@ -1152,7 +1152,7 @@ static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
 		un->semid = -1;
 		list_del_rcu(&un->list_proc);
 		spin_unlock(&un->ulp->lock);
-		kfree_rcu(un, rcu);
+		kvfree_rcu(un, rcu);
 	}
 
 	/* Wake up all pending processes and let them fail with EIDRM. */
@@ -1935,7 +1935,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	rcu_read_unlock();
 
 	/* step 2: allocate new undo structure */
-	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
+	new = kvzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
 		      GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
@@ -1948,7 +1948,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	if (!ipc_valid_object(&sma->sem_perm)) {
 		sem_unlock(sma, -1);
 		rcu_read_unlock();
-		kfree(new);
+		kvfree(new);
 		un = ERR_PTR(-EIDRM);
 		goto out;
 	}
@@ -1959,7 +1959,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	 */
 	un = lookup_undo(ulp, semid);
 	if (un) {
-		kfree(new);
+		kvfree(new);
 		goto success;
 	}
 	/* step 5: initialize & link new undo structure */
@@ -2420,7 +2420,7 @@ void exit_sem(struct task_struct *tsk)
 		rcu_read_unlock();
 		wake_up_q(&wake_q);
 
-		kfree_rcu(un, rcu);
+		kvfree_rcu(un, rcu);
 	}
 	kfree(ulp);
 }
Signature · 1 line
1.8.3.1
#130 lore raw

PATCH v2 2/2 ipc: use kmalloc for msg_queue and shmid_kernel

In reply to Vasily Averin

msg_queue and shmid_kernel are quite small objects, no need to use kvmalloc for them.
mhocko@: "Both of them are 256B on most 64b systems."

Previously these objects was allocated via ipc_alloc/ipc_rcu_alloc(), common function for several ipc objects. It had kvmalloc call inside(). Later, this function went away and was finally replaced by direct kvmalloc call, and now we can use more suitable kmalloc/kfree for them.

---
 ipc/msg.c | 6 +++---
 ipc/shm.c | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
ipc/msg.c +3 -3
index 87898cb..79c6625 100644
@@ -130,7 +130,7 @@ static void msg_rcu_free(struct rcu_head *head)
 	struct msg_queue *msq = container_of(p, struct msg_queue, q_perm);
 
 	security_msg_queue_free(&msq->q_perm);
-	kvfree(msq);
+	kfree(msq);
 }
 
 /**
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
+	msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
@@ -157,7 +157,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	msq->q_perm.security = NULL;
 	retval = security_msg_queue_alloc(&msq->q_perm);
 	if (retval) {
-		kvfree(msq);
+		kfree(msq);
 		return retval;
 	}
 
ipc/shm.c +3 -3
index 7632d72..85da060 100644
@@ -222,7 +222,7 @@ static void shm_rcu_free(struct rcu_head *head)
 	struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
 							shm_perm);
 	security_shm_free(&shp->shm_perm);
-	kvfree(shp);
+	kfree(shp);
 }
 
 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
+	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
@@ -630,7 +630,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 	shp->shm_perm.security = NULL;
 	error = security_shm_alloc(&shp->shm_perm);
 	if (error) {
-		kvfree(shp);
+		kfree(shp);
 		return error;
 	}
 
Signature · 1 line
1.8.3.1
#131 lore raw

PATCH 1/2 ipc sem: use kvmalloc for sem_undo allocation

In reply to Vasily Averin

size of sem_undo can exceed one page and with the maximum possible nsems = 32000 it can grow up to 64Kb. Let's switch its allocation to kvmalloc to avoid user-triggered disruptive actions like OOM killer in case of high-order memory shortage.

---
 ipc/sem.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
ipc/sem.c +5 -5
index 52a6599..93088d6 100644
@@ -1152,7 +1152,7 @@ static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
 		un->semid = -1;
 		list_del_rcu(&un->list_proc);
 		spin_unlock(&un->ulp->lock);
-		kfree_rcu(un, rcu);
+		kvfree_rcu(un, rcu);
 	}
 
 	/* Wake up all pending processes and let them fail with EIDRM. */
@@ -1935,7 +1935,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	rcu_read_unlock();
 
 	/* step 2: allocate new undo structure */
-	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
+	new = kvzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
 		      GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
@@ -1948,7 +1948,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	if (!ipc_valid_object(&sma->sem_perm)) {
 		sem_unlock(sma, -1);
 		rcu_read_unlock();
-		kfree(new);
+		kvfree(new);
 		un = ERR_PTR(-EIDRM);
 		goto out;
 	}
@@ -1959,7 +1959,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	 */
 	un = lookup_undo(ulp, semid);
 	if (un) {
-		kfree(new);
+		kvfree(new);
 		goto success;
 	}
 	/* step 5: initialize & link new undo structure */
@@ -2420,7 +2420,7 @@ void exit_sem(struct task_struct *tsk)
 		rcu_read_unlock();
 		wake_up_q(&wake_q);
 
-		kfree_rcu(un, rcu);
+		kvfree_rcu(un, rcu);
 	}
 	kfree(ulp);
 }
Signature · 1 line
1.8.3.1
Acked-by on v1 1/2
#132 lore raw

Re: [PATCH 1/2] ipc sem: use kvmalloc for sem_undo allocation

Quoted 4 lines · Vasily Averin

size of sem_undo can exceed one page and with the maximum possible nsems = 32000 it can grow up to 64Kb. Let's switch its allocation to kvmalloc to avoid user-triggered disruptive actions like OOM killer in case of high-order memory shortage.

User triggerable high order allocations are quite a problem on heavily fragmented systems. They can be a DoS vector.

Quoted 1 line
Quoted 55 lines
---
 ipc/sem.c | 10 +++++-----
 1 file changed, 5 insertions(+), 5 deletions(-)
ipc/sem.c +5 -5
index 52a6599..93088d6 100644
@@ -1152,7 +1152,7 @@ static void freeary(struct ipc_namespace *ns, struct kern_ipc_perm *ipcp)
 		un->semid = -1;
 		list_del_rcu(&un->list_proc);
 		spin_unlock(&un->ulp->lock);
-		kfree_rcu(un, rcu);
+		kvfree_rcu(un, rcu);
 	}
 
 	/* Wake up all pending processes and let them fail with EIDRM. */
@@ -1935,7 +1935,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	rcu_read_unlock();
 
 	/* step 2: allocate new undo structure */
-	new = kzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
+	new = kvzalloc(sizeof(struct sem_undo) + sizeof(short)*nsems,
 		      GFP_KERNEL_ACCOUNT);
 	if (!new) {
 		ipc_rcu_putref(&sma->sem_perm, sem_rcu_free);
@@ -1948,7 +1948,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	if (!ipc_valid_object(&sma->sem_perm)) {
 		sem_unlock(sma, -1);
 		rcu_read_unlock();
-		kfree(new);
+		kvfree(new);
 		un = ERR_PTR(-EIDRM);
 		goto out;
 	}
@@ -1959,7 +1959,7 @@ static struct sem_undo *find_alloc_undo(struct ipc_namespace *ns, int semid)
 	 */
 	un = lookup_undo(ulp, semid);
 	if (un) {
-		kfree(new);
+		kvfree(new);
 		goto success;
 	}
 	/* step 5: initialize & link new undo structure */
@@ -2420,7 +2420,7 @@ void exit_sem(struct task_struct *tsk)
 		rcu_read_unlock();
 		wake_up_q(&wake_q);
 
-		kfree_rcu(un, rcu);
+		kvfree_rcu(un, rcu);
 	}
 	kfree(ulp);
 }
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
Reviewed-by on v1 1/2
#133 lore raw

Re: [PATCH 1/2] ipc sem: use kvmalloc for sem_undo allocation

In reply to Vasily Averin
Quoted 7 lines · Vasily Averin

size of sem_undo can exceed one page and with the maximum possible nsems = 32000 it can grow up to 64Kb. Let's switch its allocation to kvmalloc to avoid user-triggered disruptive actions like OOM killer in case of high-order memory shortage.

Acked-by on v1 1/2
#134 lore raw

Re: [PATCH 1/2] ipc sem: use kvmalloc for sem_undo allocation

In reply to Vasily Averin
Quoted 6 lines · Vasily Averin

size of sem_undo can exceed one page and with the maximum possible nsems = 32000 it can grow up to 64Kb. Let's switch its allocation to kvmalloc to avoid user-triggered disruptive actions like OOM killer in case of high-order memory shortage.

#135 lore raw

PATCH 2/2 ipc: use kmalloc for msg_queue and shmid_kernel

In reply to Vasily Averin

msg_queue and shmid_kernel are quite small objects, no need to use kvmalloc for them.
Previously these objects was allocated via ipc_alloc/ipc_rcu_alloc(), common function for several ipc objects. It had kvmalloc call inside(). Later, this function went away and was finally replaced by direct kvmalloc call, and now we can use more suitable kmalloc/kfree for them.

---
 ipc/msg.c | 6 +++---
 ipc/shm.c | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
ipc/msg.c +3 -3
index 87898cb..79c6625 100644
@@ -130,7 +130,7 @@ static void msg_rcu_free(struct rcu_head *head)
 	struct msg_queue *msq = container_of(p, struct msg_queue, q_perm);
 
 	security_msg_queue_free(&msq->q_perm);
-	kvfree(msq);
+	kfree(msq);
 }
 
 /**
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
+	msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
@@ -157,7 +157,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	msq->q_perm.security = NULL;
 	retval = security_msg_queue_alloc(&msq->q_perm);
 	if (retval) {
-		kvfree(msq);
+		kfree(msq);
 		return retval;
 	}
 
ipc/shm.c +3 -3
index 7632d72..85da060 100644
@@ -222,7 +222,7 @@ static void shm_rcu_free(struct rcu_head *head)
 	struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
 							shm_perm);
 	security_shm_free(&shp->shm_perm);
-	kvfree(shp);
+	kfree(shp);
 }
 
 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
+	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
@@ -630,7 +630,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 	shp->shm_perm.security = NULL;
 	error = security_shm_alloc(&shp->shm_perm);
 	if (error) {
-		kvfree(shp);
+		kfree(shp);
 		return error;
 	}
 
Signature · 1 line
1.8.3.1
Acked-by on v1 2/2
#136 lore raw

Re: [PATCH 2/2] ipc: use kmalloc for msg_queue and shmid_kernel

Quoted 2 lines · Vasily Averin

msg_queue and shmid_kernel are quite small objects, no need to use kvmalloc for them.

Both of them are 256B on most 64b systems.

Quoted 4 lines

Previously these objects was allocated via ipc_alloc/ipc_rcu_alloc(), common function for several ipc objects. It had kvmalloc call inside(). Later, this function went away and was finally replaced by direct kvmalloc call, and now we can use more suitable kmalloc/kfree for them.

This describes the history of the code but it doesn't really explain why kvmalloc is a bad decision here. I would go with the following:
"
Using kvmalloc for sub page size objects is suboptimal because kmalloc can easily fallback into vmalloc under memory pressure and smaller objects would fragment memory. Therefore replace kvmalloc by a simple kmalloc.
"

Quoted 3 lines

With a clarified changelog, feel free to add

Thanks!

Quoted 69 lines
---
 ipc/msg.c | 6 +++---
 ipc/shm.c | 6 +++---
 2 files changed, 6 insertions(+), 6 deletions(-)
ipc/msg.c +3 -3
index 87898cb..79c6625 100644
@@ -130,7 +130,7 @@ static void msg_rcu_free(struct rcu_head *head)
 	struct msg_queue *msq = container_of(p, struct msg_queue, q_perm);
 
 	security_msg_queue_free(&msq->q_perm);
-	kvfree(msq);
+	kfree(msq);
 }
 
 /**
@@ -147,7 +147,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	key_t key = params->key;
 	int msgflg = params->flg;
 
-	msq = kvmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
+	msq = kmalloc(sizeof(*msq), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!msq))
 		return -ENOMEM;
 
@@ -157,7 +157,7 @@ static int newque(struct ipc_namespace *ns, struct ipc_params *params)
 	msq->q_perm.security = NULL;
 	retval = security_msg_queue_alloc(&msq->q_perm);
 	if (retval) {
-		kvfree(msq);
+		kfree(msq);
 		return retval;
 	}
 
ipc/shm.c +3 -3
index 7632d72..85da060 100644
@@ -222,7 +222,7 @@ static void shm_rcu_free(struct rcu_head *head)
 	struct shmid_kernel *shp = container_of(ptr, struct shmid_kernel,
 							shm_perm);
 	security_shm_free(&shp->shm_perm);
-	kvfree(shp);
+	kfree(shp);
 }
 
 static inline void shm_rmid(struct ipc_namespace *ns, struct shmid_kernel *s)
@@ -619,7 +619,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 			ns->shm_tot + numpages > ns->shm_ctlall)
 		return -ENOSPC;
 
-	shp = kvmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
+	shp = kmalloc(sizeof(*shp), GFP_KERNEL_ACCOUNT);
 	if (unlikely(!shp))
 		return -ENOMEM;
 
@@ -630,7 +630,7 @@ static int newseg(struct ipc_namespace *ns, struct ipc_params *params)
 	shp->shm_perm.security = NULL;
 	error = security_shm_alloc(&shp->shm_perm);
 	if (error) {
-		kvfree(shp);
+		kfree(shp);
 		return error;
 	}
 
Signature · 1 line
1.8.3.1
Signature · 2 lines
Michal Hocko
SUSE Labs
#137 lore raw

Re: [PATCH 2/2] ipc: use kmalloc for msg_queue and shmid_kernel

Quoted 4 lines · Michal Hocko

Using kvmalloc for sub page size objects is suboptimal because kmalloc can easily fallback into vmalloc under memory pressure and smaller objects would fragment memory. Therefore replace kvmalloc by a simple kmalloc.

I think you're wrong here:
kvmalloc can failback to vmalloc for size > PAGE_SIZE only

Please take look at mm/util.c::kvmalloc_node()

        if (size > PAGE_SIZE) {
                kmalloc_flags |= __GFP_NOWARN;

                if (!(kmalloc_flags & __GFP_RETRY_MAYFAIL))
                        kmalloc_flags |= __GFP_NORETRY;
        }

        ret = kmalloc_node(size, kmalloc_flags, node);

        /*
         * It doesn't really make sense to fallback to vmalloc for sub page
         * requests
         */
        if (ret || size <= PAGE_SIZE)
                return ret;

        return __vmalloc_node(size, 1, flags, node,
                        __builtin_return_address(0));

For small objects kvmalloc is not much different just from kmalloc, so the patch is mostly cosmetic.

#138 lore raw

Re: [PATCH 2/2] ipc: use kmalloc for msg_queue and shmid_kernel

Quoted 8 lines · Vasily Averin
Quoted 4 lines · Michal Hocko

Using kvmalloc for sub page size objects is suboptimal because kmalloc can easily fallback into vmalloc under memory pressure and smaller objects would fragment memory. Therefore replace kvmalloc by a simple kmalloc.

I think you're wrong here:
kvmalloc can failback to vmalloc for size > PAGE_SIZE only

You are right. My bad. My memory failed on me. Sorry about the confusion.

Signature · 2 lines
Michal Hocko
SUSE Labs
Reviewed-by on v1 2/2
#139 lore raw

Re: [PATCH 2/2] ipc: use kmalloc for msg_queue and shmid_kernel

In reply to Vasily Averin
Quoted 10 lines · Vasily Averin

msg_queue and shmid_kernel are quite small objects, no need to use kvmalloc for them.
Previously these objects was allocated via ipc_alloc/ipc_rcu_alloc(), common function for several ipc objects. It had kvmalloc call inside(). Later, this function went away and was finally replaced by direct kvmalloc call, and now we can use more suitable kmalloc/kfree for them.

Acked-by on v1 2/2
#140 lore raw

Re: [PATCH 2/2] ipc: use kmalloc for msg_queue and shmid_kernel

In reply to Vasily Averin
Quoted 9 lines · Vasily Averin

msg_queue and shmid_kernel are quite small objects, no need to use kvmalloc for them.
Previously these objects was allocated via ipc_alloc/ipc_rcu_alloc(), common function for several ipc objects. It had kvmalloc call inside(). Later, this function went away and was finally replaced by direct kvmalloc call, and now we can use more suitable kmalloc/kfree for them.

Thanks!

#141 lore raw

PATCH v3 9/16 memcg: enable accounting for mnt_cache entries

In reply to a message not in the archive

The kernel allocates ~400 bytes of 'strcut mount' for any new mount. Creating a new mount namespace clones most of the parent mounts, and this can be repeated many times. Additionally, each mount allocates up to PATH_MAX=4096 bytes for mnt->mnt_devname.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/namespace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/namespace.c +3 -2
index 5ecfa349..fc1b50d 100644
@@ -203,7 +203,8 @@ static struct mount *alloc_vfsmnt(const char *name)
 			goto out_free_cache;
 
 		if (name) {
-			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
+			mnt->mnt_devname = kstrdup_const(name,
+							 GFP_KERNEL_ACCOUNT);
 			if (!mnt->mnt_devname)
 				goto out_free_id;
 		}
@@ -4213,7 +4214,7 @@ void __init mnt_init(void)
 	int err;
 
 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
-			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
+			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
 	mount_hashtable = alloc_large_system_hash("Mount-cache",
 				sizeof(struct hlist_head),
Signature · 1 line
1.8.3.1
#142 lore raw

PATCH v3 10/16 memcg: enable accounting for pollfd and select bits arrays

In reply to a message not in the archive

User can call select/poll system calls with a large number of assigned file descriptors and force kernel to allocate up to several pages of memory till end of these sleeping system calls. We have here long-living unaccounted per-task allocations.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/select.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
fs/select.c +2 -2
index 945896d..e83e563 100644
@@ -655,7 +655,7 @@ int core_sys_select(int n, fd_set __user *inp, fd_set __user *outp,
 			goto out_nofds;
 
 		alloc_size = 6 * size;
-		bits = kvmalloc(alloc_size, GFP_KERNEL);
+		bits = kvmalloc(alloc_size, GFP_KERNEL_ACCOUNT);
 		if (!bits)
 			goto out_nofds;
 	}
@@ -1000,7 +1000,7 @@ static int do_sys_poll(struct pollfd __user *ufds, unsigned int nfds,
 
 		len = min(todo, POLLFD_PER_PAGE);
 		walk = walk->next = kmalloc(struct_size(walk, entries, len),
-					    GFP_KERNEL);
+					    GFP_KERNEL_ACCOUNT);
 		if (!walk) {
 			err = -ENOMEM;
 			goto out_fds;
Signature · 1 line
1.8.3.1
#143 lore raw

PATCH v3 11/16 memcg: enable accounting for signals

In reply to a message not in the archive

When a user send a signal to any another processes it forces the kernel to allocate memory for 'struct sigqueue' objects. The number of signals is limited by RLIMIT_SIGPENDING resource limit, but even the default settings allow each user to consume up to several megabytes of memory. Moreover, an untrusted admin inside container can increase the limit or create new fake users and force them to sent signals.

It makes sense to account for these allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 kernel/signal.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
kernel/signal.c +1 -1
index f271835..a7fa849 100644
@@ -4639,7 +4639,7 @@ void __init signals_init(void)
 {
 	siginfo_buildtime_checks();
 
-	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC);
+	sigqueue_cachep = KMEM_CACHE(sigqueue, SLAB_PANIC | SLAB_ACCOUNT);
 }
 
 #ifdef CONFIG_KGDB_KDB
Signature · 1 line
1.8.3.1
#144 lore raw

PATCH v3 12/16 memcg: enable accounting for posix_timers_cache slab

In reply to a message not in the archive

A program may create multiple interval timers using timer_create(). For each timer the kernel preallocates a "queued real-time signal", Consequently, the number of timers is limited by the RLIMIT_SIGPENDING resource limit. The allocated object is quite small, ~250 bytes, but even the default signal limits allow to consume up to 100 megabytes per user.

It makes sense to account for them to limit the host's memory consumption from inside the memcg-limited container.

---
 kernel/time/posix-timers.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
kernel/time/posix-timers.c +2 -2
index bf540f5a..2eee615 100644
@@ -273,8 +273,8 @@ static int posix_get_hrtimer_res(clockid_t which_clock, struct timespec64 *tp)
 static __init int init_posix_timers(void)
 {
 	posix_timers_cache = kmem_cache_create("posix_timers_cache",
-					sizeof (struct k_itimer), 0, SLAB_PANIC,
-					NULL);
+					sizeof(struct k_itimer), 0,
+					SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 __initcall(init_posix_timers);
Signature · 1 line
1.8.3.1
#145 lore raw

PATCH v3 13/16 memcg: enable accounting for file lock caches

In reply to a message not in the archive

User can create file locks for each open file and force kernel to allocate small but long-living objects per each open file.

It makes sense to account for these objects to limit the host's memory consumption from inside the memcg-limited container.

---
 fs/locks.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)
fs/locks.c +4 -2
index 6125d2d..fb751f3 100644
@@ -3007,10 +3007,12 @@ static int __init filelock_init(void)
 	int i;
 
 	flctx_cache = kmem_cache_create("file_lock_ctx",
-			sizeof(struct file_lock_context), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock_context), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	filelock_cache = kmem_cache_create("file_lock_cache",
-			sizeof(struct file_lock), 0, SLAB_PANIC, NULL);
+			sizeof(struct file_lock), 0,
+			SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	for_each_possible_cpu(i) {
 		struct file_lock_list_struct *fll = per_cpu_ptr(&file_lock_list, i);
Signature · 1 line
1.8.3.1
#146 lore raw

PATCH v3 14/16 memcg: enable accounting for fasync_cache

In reply to a message not in the archive

fasync_struct is used by almost all character device drivers to set up the fasync queue, and for regular files by the file lease code. This structure is quite small but long-living and it can be assigned for any open file.

It makes sense to account for its allocations to restrict the host's memory consumption from inside the memcg-limited container.

---
 fs/fcntl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fcntl.c +2 -1
index dfc72f1..7941559 100644
@@ -1049,7 +1049,8 @@ static int __init fcntl_init(void)
 			__FMODE_EXEC | __FMODE_NONOTIFY));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
-		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
+					 sizeof(struct fasync_struct), 0,
+					 SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#147 lore raw

PATCH v3 15/16 memcg: enable accounting for tty-related objects

In reply to a message not in the archive

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index 391bada..e613b8e 100644
@@ -1502,7 +1502,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
@@ -3127,7 +3127,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): da450388-2fbc-1bb8-0839-b6480cb0eead-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#149 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Does this tiny amount of memory actually matter? This feels like it would be lost in the noise, and not really be an issue for any real system as it's hard to abuse (i.e. if a user creates lots of tty structures, what can they do???)

So no, I do not think this patch is needed, thanks.

greg k-h

A message that is not in the archive (sent off-list or not yet received): [email protected]

#151 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 5 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Does this tiny amount of memory actually matter?

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#153 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 9 lines · Michal Hocko
Quoted 5 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Does this tiny amount of memory actually matter?

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Can they? They are not bounded by some other resource limit?

greg k-h

A message that is not in the archive (sent off-list or not yet received): [email protected]

#155 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 6 lines · Greg KH
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Does this tiny amount of memory actually matter?

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Can they? They are not bounded by some other resource limit?

I dunno. This is not my area. I am not aware of any direct rlimit (maybe RLIMIT_NPROC) and maybe pid controller would help. But the changelog should definitely mention that. Other patches tend to mention the scenario they protect from this one should be more specific.

Signature · 2 lines
Michal Hocko
SUSE Labs
#156 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

In reply to a message not in the archive
Quoted 6 lines · Greg Kroah-Hartman
Quoted 5 lines · Michal Hocko
Quoted 4 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Does this tiny amount of memory actually matter?

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Can they? They are not bounded by some other resource limit?

I'm not ready to provide usecase right now, but on the other hand I do not see any related limits. Let me take time out to dig this question.

Thank you,
Vasily Averin

A message that is not in the archive (sent off-list or not yet received): 6e697a1f-936d-5ffe-d29f-e4dcbe099799-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#158 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 9 lines · Vasily Averin
Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Does this tiny amount of memory actually matter?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Can they? They are not bounded by some other resource limit?

I'm not ready to provide usecase right now, but on the other hand I do not see any related limits. Let me take time out to dig this question.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initns only and the settings are controlled by host admin. It's OK.
Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. In result one container can consume almost all allowed ptys and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node. So I still think it makes sense to account this memory.

Btw OpenVz have per-container pty accounting and limits, but upstream does not.

Thank you,
Vasily Averin.

A message that is not in the archive (sent off-list or not yet received): 03cb1ce9-143a-1cd0-f34b-d608c3bbc66c-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#160 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 24 lines · Vasily Averin
Quoted 9 lines · Vasily Averin
Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Does this tiny amount of memory actually matter?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Can they? They are not bounded by some other resource limit?

I'm not ready to provide usecase right now, but on the other hand I do not see any related limits. Let me take time out to dig this question.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initns only and the settings are controlled by host admin. It's OK.
Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

Quoted 7 lines

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. In result one container can consume almost all allowed ptys and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node. So I still think it makes sense to account this memory.

This is a very valuable information to have in the changelog. It is not my call but if all the above is correct then the accounting is worth IMO.

Thanks!

Signature · 2 lines
Michal Hocko
SUSE Labs

A message that is not in the archive (sent off-list or not yet received): [email protected]

#162 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 29 lines · Michal Hocko
Quoted 28 lines · Vasily Averin
Quoted 9 lines · Vasily Averin
Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Does this tiny amount of memory actually matter?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Can they? They are not bounded by some other resource limit?

I'm not ready to provide usecase right now, but on the other hand I do not see any related limits. Let me take time out to dig this question.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initns only and the settings are controlled by host admin. It's OK.
Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. In result one container can consume almost all allowed ptys and allocate up to 1Gb of unaccounted memory.

Quoted 7 lines
Quoted 3 lines

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node. So I still think it makes sense to account this memory.

This is a very valuable information to have in the changelog. It is not my call but if all the above is correct then the accounting is worth IMO.

If Greg doesn't have any objections, I'll add this explanation to the next version of the patch.

A message that is not in the archive (sent off-list or not yet received): 31c49c60-44db-0363-3d07-5febe0048e86-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#164 lore raw

Re: [PATCH v3 15/16] memcg: enable accounting for tty-related objects

Quoted 34 lines · Vasily Averin
Quoted 33 lines · Michal Hocko
Quoted 31 lines · Vasily Averin
Quoted 9 lines · Vasily Averin
Quoted 8 lines · Greg Kroah-Hartman
Quoted 7 lines · Michal Hocko
Quoted 5 lines · Greg KH
Quoted 2 lines · Vasily Averin

At each login the user forces the kernel to create a new terminal and allocate up to ~1Kb memory for the tty-related structures.

Does this tiny amount of memory actually matter?

Quoted 8 lines
Quoted 4 lines
Quoted 2 lines

The primary question is whether an untrusted user can trigger an unbounded amount of these allocations.

Can they? They are not bounded by some other resource limit?

I'm not ready to provide usecase right now, but on the other hand I do not see any related limits. Let me take time out to dig this question.

By default it's allowed to create up to 4096 ptys with 1024 reserve for initns only and the settings are controlled by host admin. It's OK.
Though this default is not enough for hosters with thousands of containers per node. Host admin can be forced to increase it up to NR_UNIX98_PTY_MAX = 1<<20.

By default container is restricted by pty mount_opt.max = 1024, but admin inside container can change it via remount. In result one container can consume almost all allowed ptys and allocate up to 1Gb of unaccounted memory.

It is not enough per-se to trigger OOM on host, however anyway, it allows to significantly exceed the assigned memcg limit and leads to troubles on the over-committed node. So I still think it makes sense to account this memory.

Quoted 6 lines
Quoted 3 lines

This is a very valuable information to have in the changelog. It is not my call but if all the above is correct then the accounting is worth IMO.

If Greg doesn't have any objections, I'll add this explanation to the next version of the patch.

I object to the current text you submitted, so something has to change in order for me to be able to accept the patch :)

Seriously, yes, the above information is great, please include that and all should be fine.

thanks,

greg k-h

#165 lore raw

PATCH v3 16/16 memcg: enable accounting for ldt_struct objects

In reply to a message not in the archive

Each task can request own LDT and force the kernel to allocate up to 64Kb memory per-mm.

There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines.
The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

It makes sense to account for this objects to restrict the host's memory consumption from inside the memcg-limited container.

---
 arch/x86/kernel/ldt.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/ldt.c +4 -3
index aa15132..a1889a0 100644
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	if (num_entries > LDT_ENTRIES)
 		return NULL;
 
-	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
+	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
 	if (!new_ldt)
 		return NULL;
 
@@ -168,9 +168,10 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size,
+					     GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	else
-		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
+		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 
 	if (!new_ldt->entries) {
 		kfree(new_ldt);
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 94dd36cb-3abb-53fc-0f23-26c02094ddf4-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#167 lore raw

Re: [PATCH v3 16/16] memcg: enable accounting for ldt_struct objects

On Thu, Apr 22, 2021 at 01:38:01PM +0300, Vasily Averin wrote:

You have forgotten to Cc LKML on your submission.

Quoted 30 lines

Each task can request own LDT and force the kernel to allocate up to 64Kb memory per-mm.

There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines.
The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

It makes sense to account for this objects to restrict the host's memory consumption from inside the memcg-limited container.

---
 arch/x86/kernel/ldt.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/ldt.c +1 -1
index aa15132..a1889a0 100644
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	if (num_entries > LDT_ENTRIES)
 		return NULL;
 
-	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
+	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
 	if (!new_ldt)
 		return NULL;
 

arch/x86/kernel/ldt.c static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries) · line 172

Quoted 6 lines
arch/x86/kernel/ldt.c +2 -1
@@ -168,9 +168,10 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size,
+					     GFP_KERNEL_ACCOUNT | __GFP_ZERO);

You don't have to break that line - just let it stick out.

arch/x86/kernel/ldt.c static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries) · line 177

Quoted 7 lines
arch/x86/kernel/ldt.c +1 -1
 	else
-		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
+		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 
 	if (!new_ldt->entries) {
 		kfree(new_ldt);

In any case:

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette

A message that is not in the archive (sent off-list or not yet received): [email protected]

#169 lore raw

Re: [PATCH v3 16/16] memcg: enable accounting for ldt_struct objects

Quoted 3 lines · Borislav Petkov

On Thu, Apr 22, 2021 at 01:38:01PM +0300, Vasily Averin wrote:

You have forgotten to Cc LKML on your submission.

I think it's OK, patch set is addressed to cgroups subsystem amiling list. Am I missed something and such patches should be sent to LKML anyway?

Quoted 9 lines

arch/x86/kernel/ldt.c static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries) · line 172

Quoted 7 lines
arch/x86/kernel/ldt.c +2 -1
@@ -168,9 +168,10 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size,
+					     GFP_KERNEL_ACCOUNT | __GFP_ZERO);

You don't have to break that line - just let it stick out.

Hmm. I missed that allowed line limit was increased up to 100 bytes, Thank you, I will fix it in next patch version.

A message that is not in the archive (sent off-list or not yet received): 29fe6b29-d56a-6ea1-2fe7-2b015f6b74ef-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#171 lore raw

Re: [PATCH v3 16/16] memcg: enable accounting for ldt_struct objects

Quoted 6 lines · Vasily Averin
Quoted 3 lines · Borislav Petkov

On Thu, Apr 22, 2021 at 01:38:01PM +0300, Vasily Averin wrote:

You have forgotten to Cc LKML on your submission.

I think it's OK, patch set is addressed to cgroups subsystem amiling list. Am I missed something and such patches should be sent to LKML anyway?

Yes, it is preferable to CC all patches to lkml. These patches are not cgroup specit much. A specific subsystem knowledge is require to judge them and most people are not subscribed to the cgroup ML.

Quoted 11 lines
Quoted 9 lines

arch/x86/kernel/ldt.c static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries) · line 172

Quoted 7 lines
arch/x86/kernel/ldt.c +2 -1
@@ -168,9 +168,10 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size,
+					     GFP_KERNEL_ACCOUNT | __GFP_ZERO);

You don't have to break that line - just let it stick out.

Hmm. I missed that allowed line limit was increased up to 100 bytes, Thank you, I will fix it in next patch version.

Line limits are more of a guidance than a hard rule. Also please note that different subsystems' maintainers insist on this guidance differently.

Signature · 2 lines
Michal Hocko
SUSE Labs
#172 lore raw

PATCH v3 1/16 memcg: enable accounting for net_device and Tx/Rx queues

In reply to Vasily Averin

Container netadmin can create a lot of fake net devices, then create a new net namespace and repeat it again and again. Net device can request the creation of up to 4096 tx and rx queues, and force kernel to allocate up to several tens of megabytes memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/dev.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
net/core/dev.c +3 -3
index 1f79b9a..87b1e80 100644
@@ -9994,7 +9994,7 @@ static int netif_alloc_rx_queues(struct net_device *dev)
 
 	BUG_ON(count < 1);
 
-	rx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	rx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!rx)
 		return -ENOMEM;
 
@@ -10061,7 +10061,7 @@ static int netif_alloc_netdev_queues(struct net_device *dev)
 	if (count < 1 || count > 0xffff)
 		return -EINVAL;
 
-	tx = kvzalloc(sz, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	tx = kvzalloc(sz, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!tx)
 		return -ENOMEM;
 
@@ -10693,7 +10693,7 @@ struct net_device *alloc_netdev_mqs(int sizeof_priv, const char *name,
 	/* ensure 32-byte alignment of whole construct */
 	alloc_size += NETDEV_ALIGN - 1;
 
-	p = kvzalloc(alloc_size, GFP_KERNEL | __GFP_RETRY_MAYFAIL);
+	p = kvzalloc(alloc_size, GFP_KERNEL_ACCOUNT | __GFP_RETRY_MAYFAIL);
 	if (!p)
 		return NULL;
 
Signature · 1 line
1.8.3.1
#173 lore raw

PATCH v3 2/16 memcg: enable accounting for IP address and routing-related objects

In reply to Vasily Averin

An netadmin inside container can use 'ip a a' and 'ip r a' to assign a large number of ipv4/ipv6 addresses and routing entries and force kernel to allocate megabytes of unaccounted memory for long-lived per-netdevice related kernel objects:
'struct in_ifaddr', 'struct inet6_ifaddr', 'struct fib6_node', 'struct rt6_info', 'struct fib_rules' and ip_fib caches.

These objects can be manually removed, though usually they lives in memory till destroy of its net namespace.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

One of such objects is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

In this case it is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass().

Obsoleted in_interrupt() does not describe real execution context properly. From include/linux/preempt.h:

The following macros are deprecated and should not be used in new code:
in_interrupt() - We're in NMI,IRQ,SoftIRQ context or have BH disabled

To verify the current execution context new macro should be used instead:
in_task() - We're in task context

---
 mm/memcontrol.c      | 2 +-
 net/core/fib_rules.c | 4 ++--
 net/ipv4/devinet.c   | 2 +-
 net/ipv4/fib_trie.c  | 4 ++--
 net/ipv6/addrconf.c  | 2 +-
 net/ipv6/ip6_fib.c   | 4 ++--
 net/ipv6/route.c     | 2 +-
 7 files changed, 10 insertions(+), 10 deletions(-)
mm/memcontrol.c +1 -1
index e064ac0d..15108ad 100644
@@ -1076,7 +1076,7 @@ static __always_inline bool memcg_kmem_bypass(void)
 		return false;
 
 	/* Memcg to charge can't be determined. */
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 
 	return false;
net/core/fib_rules.c +2 -2
index cd80ffe..65d8b1d 100644
@@ -57,7 +57,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 {
 	struct fib_rule *r;
 
-	r = kzalloc(ops->rule_size, GFP_KERNEL);
+	r = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (r == NULL)
 		return -ENOMEM;
 
@@ -541,7 +541,7 @@ static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
 			goto errout;
 	}
 
-	nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
+	nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (!nlrule) {
 		err = -ENOMEM;
 		goto errout;
net/ipv4/devinet.c +1 -1
index 2e35f68da..9b90413 100644
@@ -215,7 +215,7 @@ static void devinet_sysctl_unregister(struct in_device *idev)
 
 static struct in_ifaddr *inet_alloc_ifa(void)
 {
-	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL);
+	return kzalloc(sizeof(struct in_ifaddr), GFP_KERNEL_ACCOUNT);
 }
 
 static void inet_rcu_free_ifa(struct rcu_head *head)
net/ipv4/fib_trie.c +2 -2
index 25cf387..8060524 100644
@@ -2380,11 +2380,11 @@ void __init fib_trie_init(void)
 {
 	fn_alias_kmem = kmem_cache_create("ip_fib_alias",
 					  sizeof(struct fib_alias),
-					  0, SLAB_PANIC, NULL);
+					  0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
 					   LEAF_SIZE,
-					   0, SLAB_PANIC, NULL);
+					   0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 }
 
 struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
net/ipv6/addrconf.c +1 -1
index a9e53f5..d56a15a 100644
@@ -1080,7 +1080,7 @@ static int ipv6_add_addr_hash(struct net_device *dev, struct inet6_ifaddr *ifa)
 			goto out;
 	}
 
-	ifa = kzalloc(sizeof(*ifa), gfp_flags);
+	ifa = kzalloc(sizeof(*ifa), gfp_flags | __GFP_ACCOUNT);
 	if (!ifa) {
 		err = -ENOBUFS;
 		goto out;
net/ipv6/ip6_fib.c +2 -2
index 679699e..0982b7c 100644
@@ -2444,8 +2444,8 @@ int __init fib6_init(void)
 	int ret = -ENOMEM;
 
 	fib6_node_kmem = kmem_cache_create("fib6_nodes",
-					   sizeof(struct fib6_node),
-					   0, SLAB_HWCACHE_ALIGN,
+					   sizeof(struct fib6_node), 0,
+					   SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT,
 					   NULL);
 	if (!fib6_node_kmem)
 		goto out;
net/ipv6/route.c +1 -1
index 373d480..5dc5c68 100644
@@ -6510,7 +6510,7 @@ int __init ip6_route_init(void)
 	ret = -ENOMEM;
 	ip6_dst_ops_template.kmem_cachep =
 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!ip6_dst_ops_template.kmem_cachep)
 		goto out;
 
Signature · 1 line
1.8.3.1
#174 lore raw

PATCH v3 3/16 memcg: enable accounting for inet_bin_bucket cache

In reply to Vasily Averin

net namespace can create up to 64K tcp and dccp ports and force kernel to allocate up to several megabytes of memory per netns
for inet_bind_bucket objects.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/dccp/proto.c | 2 +-
 net/ipv4/tcp.c   | 4 +++-
 2 files changed, 4 insertions(+), 2 deletions(-)
net/dccp/proto.c +1 -1
index 6d705d9..f90d1e8 100644
@@ -1126,7 +1126,7 @@ static int __init dccp_init(void)
 	dccp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("dccp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!dccp_hashinfo.bind_bucket_cachep)
 		goto out_free_hashinfo2;
 
net/ipv4/tcp.c +3 -1
index de7cc84..5817a86b 100644
@@ -4498,7 +4498,9 @@ void __init tcp_init(void)
 	tcp_hashinfo.bind_bucket_cachep =
 		kmem_cache_create("tcp_bind_bucket",
 				  sizeof(struct inet_bind_bucket), 0,
-				  SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_PANIC |
+				  SLAB_ACCOUNT,
+				  NULL);
 
 	/* Size and allocate the main established and bind bucket
 	 * hash tables.
Signature · 1 line
1.8.3.1
#175 lore raw

PATCH v3 4/16 memcg: enable accounting for VLAN group array

In reply to Vasily Averin

vlan array consume up to 8 pages of memory per net device.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/8021q/vlan.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/8021q/vlan.c +1 -1
index 8b644113..d0a579d4 100644
@@ -67,7 +67,7 @@ static int vlan_group_prealloc_vid(struct vlan_group *vg,
 		return 0;
 
 	size = sizeof(struct net_device *) * VLAN_GROUP_ARRAY_PART_LEN;
-	array = kzalloc(size, GFP_KERNEL);
+	array = kzalloc(size, GFP_KERNEL_ACCOUNT);
 	if (array == NULL)
 		return -ENOBUFS;
 
Signature · 1 line
1.8.3.1
#176 lore raw

PATCH v3 5/16 memcg: ipv6/sit: account and don't WARN on ip_tunnel_prl structs allocation

In reply to Vasily Averin

Author: Andrey Ryabinin <[email protected]>

The size of the ip_tunnel_prl structs allocation is controllable from user-space, thus it's better to avoid spam in dmesg if allocation failed. Also add __GFP_ACCOUNT as this is a good candidate for per-memcg accounting. Allocation is temporary and limited by 4GB.

---
 net/ipv6/sit.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
net/ipv6/sit.c +3 -2
index 9fdccf0..2ba147c 100644
@@ -320,7 +320,7 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 	 * we try harder to allocate.
 	 */
 	kp = (cmax <= 1 || capable(CAP_NET_ADMIN)) ?
-		kcalloc(cmax, sizeof(*kp), GFP_KERNEL | __GFP_NOWARN) :
+		kcalloc(cmax, sizeof(*kp), GFP_KERNEL_ACCOUNT | __GFP_NOWARN) :
 		NULL;
 
 	rcu_read_lock();
@@ -333,7 +333,8 @@ static int ipip6_tunnel_get_prl(struct net_device *dev, struct ifreq *ifr)
 		 * For root users, retry allocating enough memory for
 		 * the answer.
 		 */
-		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC);
+		kp = kcalloc(ca, sizeof(*kp), GFP_ATOMIC | __GFP_ACCOUNT |
+					      __GFP_NOWARN);
 		if (!kp) {
 			ret = -ENOMEM;
 			goto out;
Signature · 1 line
1.8.3.1
#177 lore raw

PATCH v3 6/16 memcg: enable accounting for scm_fp_list objects

In reply to Vasily Averin

unix sockets allows to send file descriptors via SCM_RIGHTS type messages. Each such send call forces kernel to allocate up to 2Kb memory for struct scm_fp_list.

It makes sense to account for them to restrict the host's memory consumption from inside the memcg-limited container.

---
 net/core/scm.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/scm.c +2 -2
index 8156d4f..e837e4f 100644
@@ -79,7 +79,7 @@ static int scm_fp_copy(struct cmsghdr *cmsg, struct scm_fp_list **fplp)
 
 	if (!fpl)
 	{
-		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL);
+		fpl = kmalloc(sizeof(struct scm_fp_list), GFP_KERNEL_ACCOUNT);
 		if (!fpl)
 			return -ENOMEM;
 		*fplp = fpl;
@@ -348,7 +348,7 @@ struct scm_fp_list *scm_fp_dup(struct scm_fp_list *fpl)
 		return NULL;
 
 	new_fpl = kmemdup(fpl, offsetof(struct scm_fp_list, fp[fpl->count]),
-			  GFP_KERNEL);
+			  GFP_KERNEL_ACCOUNT);
 	if (new_fpl) {
 		for (i = 0; i < fpl->count; i++)
 			get_file(fpl->fp[i]);
Signature · 1 line
1.8.3.1
#178 lore raw

PATCH v2 1/8 memcg: accounting for fib6_nodes cache

In reply to a message not in the archive

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

One such object is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

It this case is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass(). To be sure that caller is not executed in process contxt '!in_task()' check should be used instead

---
 mm/memcontrol.c    | 2 +-
 net/ipv6/ip6_fib.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/memcontrol.c +1 -1
index 845eec0..568f2cb 100644
@@ -1076,7 +1076,7 @@ static __always_inline bool memcg_kmem_bypass(void)
 		return false;
 
 	/* Memcg to charge can't be determined. */
-	if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+	if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))
 		return true;
 
 	return false;
net/ipv6/ip6_fib.c +1 -1
index ef9d022..fa92ed1 100644
@@ -2445,7 +2445,7 @@ int __init fib6_init(void)
 
 	fib6_node_kmem = kmem_cache_create("fib6_nodes",
 					   sizeof(struct fib6_node),
-					   0, SLAB_HWCACHE_ALIGN,
+					   0, SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
 					   NULL);
 	if (!fib6_node_kmem)
 		goto out;
Signature · 1 line
1.8.3.1
Acked-by on v2 1/8
#179 lore raw

Re: [PATCH v2 1/8] memcg: accounting for fib6_nodes cache

Quoted 17 lines · Vasily Averin

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

One such object is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

It this case is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass(). To be sure that caller is not executed in process contxt '!in_task()' check should be used instead

Quoted 5 lines
---
 mm/memcontrol.c    | 2 +-
 net/ipv6/ip6_fib.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
#180 lore raw

Re: [PATCH v2 1/8] memcg: accounting for fib6_nodes cache

In reply to Vasily Averin
Quoted 6 lines · Vasily Averin

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

Quoted 8 lines

One such object is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

It this case is

'In this case it is'

Quoted 4 lines

not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass(). To be sure that caller is not executed in process contxt

'context'

Quoted 1 line

'!in_task()' check should be used instead

You missed the signoff and it seems like the whole series is missing it as well. Please run scripts/checkpatch.pl on the patches before sending again.

Quoted 10 lines
---
 mm/memcontrol.c    | 2 +-
 net/ipv6/ip6_fib.c | 2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
mm/memcontrol.c +0 -0
index 845eec0..568f2cb 100644

mm/memcontrol.c static __always_inline bool memcg_kmem_bypass(void) · line 1079

Quoted 5 lines
mm/memcontrol.c +1 -1
@@ -1076,7 +1076,7 @@ static __always_inline bool memcg_kmem_bypass(void)
                return false;
 
        /* Memcg to charge can't be determined. */
-       if (in_interrupt() || !current->mm || (current->flags & PF_KTHREAD))
+       if (!in_task() || !current->mm || (current->flags & PF_KTHREAD))

Can you please also add some explanation in the commit message on the differences between in_interrupt() and in_task()? Why is in_interrupt() not correct here but !in_task() is? What about kernels with or without PREEMPT_COUNT?

Quoted 19 lines
mm/memcontrol.c +0 -0
                return true;
 
        return false;
net/ipv6/ip6_fib.c +1 -2
index ef9d022..fa92ed1 100644
@@ -2445,7 +2445,7 @@ int __init fib6_init(void)
 
        fib6_node_kmem = kmem_cache_create("fib6_nodes",
                                           sizeof(struct fib6_node),
-                                          0, SLAB_HWCACHE_ALIGN,
+                                          0, SLAB_HWCACHE_ALIGN|SLAB_ACCOUNT,
                                           NULL);
        if (!fib6_node_kmem)
                goto out;
--

1.8.3.1

maintainer
#181 lore raw

Re: [PATCH v2 1/8] memcg: accounting for fib6_nodes cache

In reply to Vasily Averin
Quoted 12 lines · On Mon, 15 Mar 2021 15:23:00 +0300 Vasily Averin wrote:

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

One such object is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

Quoted 5 lines

It this case is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass(). To be sure that caller is not executed in process contxt '!in_task()' check should be used instead

Sorry for a random question, I didn't get the cover letter.

What's the overhead of adding SLAB_ACCOUNT?

Please make sure you CC netdev on series which may impact networking.

#182 lore raw

Re: [PATCH v2 1/8] memcg: accounting for fib6_nodes cache

Quoted 20 lines · Jakub Kicinski
Quoted 12 lines · On Mon, 15 Mar 2021 15:23:00 +0300 Vasily Averin wrote:

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

One such object is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

Quoted 5 lines

It this case is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass(). To be sure that caller is not executed in process contxt '!in_task()' check should be used instead

Quoted 4 lines

Sorry for a random question, I didn't get the cover letter.

What's the overhead of adding SLAB_ACCOUNT?

The potential overhead is for MEMCG users where we need to charge/account each allocation from SLAB_ACCOUNT kmem caches. However charging is done in batches, so the cost is amortized. If there is a concern about a specific workload then it would be good to see the impact of this patch for that workload.

Quoted 1 line

Please make sure you CC netdev on series which may impact networking.

#183 lore raw

Re: [PATCH v2 1/8] memcg: accounting for fib6_nodes cache

Quoted 23 lines · Shakeel Butt
Quoted 22 lines · Jakub Kicinski
Quoted 12 lines · On Mon, 15 Mar 2021 15:23:00 +0300 Vasily Averin wrote:

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

One such object is the 'struct fib6_node' mostly allocated in net/ipv6/route.c::__ip6_ins_rt() inside the lock_bh()/unlock_bh() section:

write_lock_bh(&table->tb6_lock);
err = fib6_add(&table->tb6_root, rt, info, mxc);
write_unlock_bh(&table->tb6_lock);

Quoted 5 lines

It this case is not enough to simply add SLAB_ACCOUNT to corresponding kmem cache. The proper memory cgroup still cannot be found due to the incorrect 'in_interrupt()' check used in memcg_kmem_bypass(). To be sure that caller is not executed in process contxt '!in_task()' check should be used instead

Sorry for a random question, I didn't get the cover letter.

Quoted 10 lines
Quoted 2 lines

What's the overhead of adding SLAB_ACCOUNT?

The potential overhead is for MEMCG users where we need to charge/account each allocation from SLAB_ACCOUNT kmem caches. However charging is done in batches, so the cost is amortized. If there is a concern about a specific workload then it would be good to see the impact of this patch for that workload.

Quoted 1 line

Please make sure you CC netdev on series which may impact networking.

In general the overhead is not that big, so I don't think we should argue too much about every new case where we want to enable the accounting and rather focus on those few examples (if any?) where it actually hurts the performance in a meaningful way.

Thanks!

maintainer
#184 lore raw

Re: [PATCH v2 1/8] memcg: accounting for fib6_nodes cache

Quoted 12 lines · On Mon, 15 Mar 2021 12:32:07 -0700 Roman Gushchin wrote:
Quoted 11 lines · Shakeel Butt
Quoted 3 lines · Jakub Kicinski

Sorry for a random question, I didn't get the cover letter.

What's the overhead of adding SLAB_ACCOUNT?

The potential overhead is for MEMCG users where we need to charge/account each allocation from SLAB_ACCOUNT kmem caches. However charging is done in batches, so the cost is amortized. If there is a concern about a specific workload then it would be good to see the impact of this patch for that workload.

Quoted 6 lines
Quoted 1 line
Quoted 1 line

Please make sure you CC netdev on series which may impact networking.

In general the overhead is not that big, so I don't think we should argue too much about every new case where we want to enable the accounting and rather focus on those few examples (if any?) where it actually hurts the performance in a meaningful way.

Ack, no serious concerns about this particular case.

I was expecting you'd have micro benchmark numbers handy so I was curious to learn what they are, but that appears not to be the case.

#185 lore raw

PATCH v2 2/8 memcg: accounting for ip6_dst_cache

In reply to a message not in the archive

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

This patches enables accounting for 'struct rt6_info' allocations.

---
 net/ipv6/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
net/ipv6/route.c +1 -1
index 1536f49..d1d7cdf 100644
@@ -6526,7 +6526,7 @@ int __init ip6_route_init(void)
 	ret = -ENOMEM;
 	ip6_dst_ops_template.kmem_cachep =
 		kmem_cache_create("ip6_dst_cache", sizeof(struct rt6_info), 0,
-				  SLAB_HWCACHE_ALIGN, NULL);
+				  SLAB_HWCACHE_ALIGN | SLAB_ACCOUNT, NULL);
 	if (!ip6_dst_ops_template.kmem_cachep)
 		goto out;
 
Signature · 1 line
1.8.3.1
Acked-by on v2 2/8
#186 lore raw

Re: [PATCH v2 2/8] memcg: accounting for ip6_dst_cache

Quoted 10 lines · Vasily Averin

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

This patches enables accounting for 'struct rt6_info' allocations.

---
 net/ipv6/route.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
#187 lore raw

PATCH v2 3/8 memcg: accounting for fib_rules

In reply to a message not in the archive

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

This patch enables accounting for 'struct fib_rules'

---
 net/core/fib_rules.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/core/fib_rules.c +2 -2
index cd80ffe..65d8b1d 100644
@@ -57,7 +57,7 @@ int fib_default_rule_add(struct fib_rules_ops *ops,
 {
 	struct fib_rule *r;
 
-	r = kzalloc(ops->rule_size, GFP_KERNEL);
+	r = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (r == NULL)
 		return -ENOMEM;
 
@@ -541,7 +541,7 @@ static int fib_nl2rule(struct sk_buff *skb, struct nlmsghdr *nlh,
 			goto errout;
 	}
 
-	nlrule = kzalloc(ops->rule_size, GFP_KERNEL);
+	nlrule = kzalloc(ops->rule_size, GFP_KERNEL_ACCOUNT);
 	if (!nlrule) {
 		err = -ENOMEM;
 		goto errout;
Signature · 1 line
1.8.3.1
Acked-by on v2 3/8
#188 lore raw

Re: [PATCH v2 3/8] memcg: accounting for fib_rules

Quoted 10 lines · Vasily Averin

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

This patch enables accounting for 'struct fib_rules'

---
 net/core/fib_rules.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
#189 lore raw

PATCH v2 4/8 memcg: accounting for ip_fib caches

In reply to a message not in the archive

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

This patch enables accounting for ip_fib_alias and ip_fib_trie caches

---
 net/ipv4/fib_trie.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
net/ipv4/fib_trie.c +2 -2
index 25cf387..8060524 100644
@@ -2380,11 +2380,11 @@ void __init fib_trie_init(void)
 {
 	fn_alias_kmem = kmem_cache_create("ip_fib_alias",
 					  sizeof(struct fib_alias),
-					  0, SLAB_PANIC, NULL);
+					  0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 
 	trie_leaf_kmem = kmem_cache_create("ip_fib_trie",
 					   LEAF_SIZE,
-					   0, SLAB_PANIC, NULL);
+					   0, SLAB_PANIC | SLAB_ACCOUNT, NULL);
 }
 
 struct fib_table *fib_trie_table(u32 id, struct fib_table *alias)
Signature · 1 line
1.8.3.1
Acked-by on v2 4/8
#190 lore raw

Re: [PATCH v2 4/8] memcg: accounting for ip_fib caches

Quoted 10 lines · Vasily Averin

An untrusted netadmin inside a memcg-limited container can create a huge number of routing entries. Currently, allocated kernel objects are not accounted to proper memcg, so this can lead to global memory shortage on the host and cause lot of OOM kiils.

This patch enables accounting for ip_fib_alias and ip_fib_trie caches

---
 net/ipv4/fib_trie.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
#191 lore raw

PATCH v2 5/8 memcg: accounting for fasync_cache

In reply to a message not in the archive

unprivileged user inside memcg-limited container can trigger creation of huge number of non-accounted fasync_struct objects

---
 fs/fcntl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fcntl.c +2 -1
index dfc72f1..7941559 100644
@@ -1049,7 +1049,8 @@ static int __init fcntl_init(void)
 			__FMODE_EXEC | __FMODE_NONOTIFY));
 
 	fasync_cache = kmem_cache_create("fasync_cache",
-		sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
+					 sizeof(struct fasync_struct), 0,
+					 SLAB_PANIC | SLAB_ACCOUNT, NULL);
 	return 0;
 }
 
Signature · 1 line
1.8.3.1
#192 lore raw

Re: [PATCH v2 5/8] memcg: accounting for fasync_cache

Quoted 3 lines · Vasily Averin

unprivileged user inside memcg-limited container can trigger creation of huge number of non-accounted fasync_struct objects

You need to make each patch of this series self-contained by including the motivation behind the series (just one or two sentences). For example, for this patch include what's the potential impact of these huge numbers of unaccounted fasync_struct objects?

Quoted 21 lines
---
 fs/fcntl.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
fs/fcntl.c +2 -2
index dfc72f1..7941559 100644
@@ -1049,7 +1049,8 @@ static int __init fcntl_init(void)
                        __FMODE_EXEC | __FMODE_NONOTIFY));
 
        fasync_cache = kmem_cache_create("fasync_cache",
-               sizeof(struct fasync_struct), 0, SLAB_PANIC, NULL);
+                                        sizeof(struct fasync_struct), 0,
+                                        SLAB_PANIC | SLAB_ACCOUNT, NULL);
        return 0;
 }
 
--

1.8.3.1

#193 lore raw

PATCH v2 6/8 memcg: accounting for mnt_cache entries

In reply to a message not in the archive

Each mount inside memcg-limited container creates non-accounted mount object, each new mount namespace allocates lot of them for cloned mounts.

---
 fs/namespace.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
fs/namespace.c +3 -2
index 56bb5a5..d550c2a 100644
@@ -203,7 +203,8 @@ static struct mount *alloc_vfsmnt(const char *name)
 			goto out_free_cache;
 
 		if (name) {
-			mnt->mnt_devname = kstrdup_const(name, GFP_KERNEL);
+			mnt->mnt_devname = kstrdup_const(name,
+							 GFP_KERNEL_ACCOUNT);
 			if (!mnt->mnt_devname)
 				goto out_free_id;
 		}
@@ -4213,7 +4214,7 @@ void __init mnt_init(void)
 	int err;
 
 	mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct mount),
-			0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
+			0, SLAB_HWCACHE_ALIGN|SLAB_PANIC|SLAB_ACCOUNT, NULL);
 
 	mount_hashtable = alloc_large_system_hash("Mount-cache",
 				sizeof(struct hlist_head),
Signature · 1 line
1.8.3.1
#194 lore raw

PATCH v2 7/8 memcg: accounting for tty_struct objects

In reply to a message not in the archive

Non-accounted multi-page tty-related kenrel objects can be created from inside memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index 74733ec..a3b881b 100644
@@ -1503,7 +1503,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
@@ -3128,7 +3128,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
1.8.3.1

A message that is not in the archive (sent off-list or not yet received): 61134897-703e-a2a8-6f0b-0bf6e1b79dda-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org

#196 lore raw

Re: [PATCH v2 7/8] memcg: accounting for tty_struct objects

Quoted 31 lines · Vasily Averin

Non-accounted multi-page tty-related kenrel objects can be created from inside memcg-limited container.

---
 drivers/tty/tty_io.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/tty/tty_io.c +2 -2
index 74733ec..a3b881b 100644
@@ -1503,7 +1503,7 @@ void tty_save_termios(struct tty_struct *tty)
 	/* Stash the termios data */
 	tp = tty->driver->termios[idx];
 	if (tp == NULL) {
-		tp = kmalloc(sizeof(*tp), GFP_KERNEL);
+		tp = kmalloc(sizeof(*tp), GFP_KERNEL_ACCOUNT);
 		if (tp == NULL)
 			return;
 		tty->driver->termios[idx] = tp;
@@ -3128,7 +3128,7 @@ struct tty_struct *alloc_tty_struct(struct tty_driver *driver, int idx)
 {
 	struct tty_struct *tty;
 
-	tty = kzalloc(sizeof(*tty), GFP_KERNEL);
+	tty = kzalloc(sizeof(*tty), GFP_KERNEL_ACCOUNT);
 	if (!tty)
 		return NULL;
 
Signature · 1 line
1.8.3.1

Hi,

This is the friendly patch-bot of Greg Kroah-Hartman. You have sent him a patch that has triggered this response. He used to manually respond to these common problems, but in order to save his sanity (he kept writing the same thing over and over, yet to different people), I was created. Hopefully you will not take offence and will fix the problem in your patch and resubmit it so that it can be accepted into the Linux kernel tree.

You are receiving this message because of the following common error(s) as indicated below:

- Your patch does not have a Signed-off-by: line. Please read the kernel file, Documentation/SubmittingPatches and resend it after adding that line. Note, the line needs to be in the body of the email, before the patch, not at the bottom of the patch or in the email signature.

- You did not specify a description of why the patch is needed, or possibly, any description at all, in the email body. Please read the section entitled "The canonical patch format" in the kernel file, Documentation/SubmittingPatches for what is needed in order to properly describe the change.

If you wish to discuss this problem further, or you have questions about how to resolve this issue, please feel free to respond to this email and Greg will reply once he has dug out from the pending patches received from other developers.

thanks,

greg k-h's patch email bot

#197 lore raw

PATCH v2 8/8 memcg: accounting for ldt_struct objects

In reply to a message not in the archive

Unprivileged user inside memcg-limited container can create non-accounted multi-page per-thread kernel objects for LDT

---
 arch/x86/kernel/ldt.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)
arch/x86/kernel/ldt.c +4 -3
index aa15132..a1889a0 100644
@@ -154,7 +154,7 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	if (num_entries > LDT_ENTRIES)
 		return NULL;
 
-	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL);
+	new_ldt = kmalloc(sizeof(struct ldt_struct), GFP_KERNEL_ACCOUNT);
 	if (!new_ldt)
 		return NULL;
 
@@ -168,9 +168,10 @@ static struct ldt_struct *alloc_ldt_struct(unsigned int num_entries)
 	 * than PAGE_SIZE.
 	 */
 	if (alloc_size > PAGE_SIZE)
-		new_ldt->entries = vzalloc(alloc_size);
+		new_ldt->entries = __vmalloc(alloc_size,
+					     GFP_KERNEL_ACCOUNT | __GFP_ZERO);
 	else
-		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL);
+		new_ldt->entries = (void *)get_zeroed_page(GFP_KERNEL_ACCOUNT);
 
 	if (!new_ldt->entries) {
 		kfree(new_ldt);
Signature · 1 line
1.8.3.1
maintainer
#198 lore raw

Re: [PATCH v2 8/8] memcg: accounting for ldt_struct objects

Quoted 2 lines · Vasily Averin

Unprivileged user inside memcg-limited container can create non-accounted multi-page per-thread kernel objects for LDT

I have hard time parsing this commit message.

And I'm CCed only on patch 8 of what looks like a patchset.

And that patchset is not on lkml so I can't find the rest to read about it, perhaps linux-mm.

/me goes and finds it on lore

I can see some bits and pieces, this, for example:

https://lore.kernel.org/linux-mm/05c448c7-d992-8d80-b423-b80bf5446d7c-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org/

( Btw, that version has your SOB and this patch doesn't even have a
Signed-off-by. Next time, run your whole set through checkpatch please before sending. )

Now, this URL above talks about OOM, ok, that gets me close to the "why" this patch.

From a quick look at the ldt.c code, we allow a single LDT struct per mm. Manpage says so too:

DESCRIPTION
modify_ldt() reads or writes the local descriptor table (LDT) for a process. The LDT is an array of segment descriptors that can be referenced by user code. Linux allows processes to configure a per-process (actually per-mm) LDT.

We allow

/* Maximum number of LDT entries supported. */
#define LDT_ENTRIES 8192

so there's an upper limit per mm.

Now, please explain what is this accounting for?

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#199 lore raw

Re: [PATCH v2 8/8] memcg: accounting for ldt_struct objects

Quoted 33 lines · Borislav Petkov
Quoted 2 lines · Vasily Averin

Unprivileged user inside memcg-limited container can create non-accounted multi-page per-thread kernel objects for LDT

I have hard time parsing this commit message.

And I'm CCed only on patch 8 of what looks like a patchset.

And that patchset is not on lkml so I can't find the rest to read about it, perhaps linux-mm.

/me goes and finds it on lore

I can see some bits and pieces, this, for example:

https://lore.kernel.org/linux-mm/05c448c7-d992-8d80-b423-b80bf5446d7c-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org/

( Btw, that version has your SOB and this patch doesn't even have a
Signed-off-by. Next time, run your whole set through checkpatch please before sending. )

Now, this URL above talks about OOM, ok, that gets me close to the "why" this patch.

From a quick look at the ldt.c code, we allow a single LDT struct per mm. Manpage says so too:

DESCRIPTION
modify_ldt() reads or writes the local descriptor table (LDT) for a process. The LDT is an array of segment descriptors that can be referenced by user code. Linux allows processes to configure a per-process (actually per-mm) LDT.

Quoted 9 lines

We allow

/* Maximum number of LDT entries supported. */
#define LDT_ENTRIES 8192

so there's an upper limit per mm.

Now, please explain what is this accounting for?

Let me try to provide the reasoning at least from my perspective. There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines. The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

#200 lore raw

Re: [PATCH v2 8/8] memcg: accounting for ldt_struct objects

Quoted 41 lines · Shakeel Butt
Quoted 40 lines · Borislav Petkov
Quoted 2 lines · Vasily Averin

Unprivileged user inside memcg-limited container can create non-accounted multi-page per-thread kernel objects for LDT

I have hard time parsing this commit message.

And I'm CCed only on patch 8 of what looks like a patchset.

And that patchset is not on lkml so I can't find the rest to read about it, perhaps linux-mm.

/me goes and finds it on lore

I can see some bits and pieces, this, for example:

https://lore.kernel.org/linux-mm/05c448c7-d992-8d80-b423-b80bf5446d7c-5HdwGun5lf+gSpxsJD1C4w@public.gmane.org/

( Btw, that version has your SOB and this patch doesn't even have a
Signed-off-by. Next time, run your whole set through checkpatch please before sending. )

Now, this URL above talks about OOM, ok, that gets me close to the "why" this patch.

From a quick look at the ldt.c code, we allow a single LDT struct per mm. Manpage says so too:

DESCRIPTION
modify_ldt() reads or writes the local descriptor table (LDT) for a process. The LDT is an array of segment descriptors that can be referenced by user code. Linux allows processes to configure a per-process (actually per-mm) LDT.

We allow

/* Maximum number of LDT entries supported. */
#define LDT_ENTRIES 8192

so there's an upper limit per mm.

Quoted 8 lines
Quoted 2 lines

Now, please explain what is this accounting for?

Let me try to provide the reasoning at least from my perspective. There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines. The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

It would be better to be explicit

8192 * 8 = 64kB * number_of_tasks

so realistically this is in range of lower megabytes. Is this worth the memcg accounting overhead? Maybe yes but what kind of workloads really care?

Signature · 2 lines
Michal Hocko
SUSE Labs
maintainer
#201 lore raw

Re: [PATCH v2 8/8] memcg: accounting for ldt_struct objects

In reply to Shakeel Butt
Quoted 5 lines · Shakeel Butt

Let me try to provide the reasoning at least from my perspective. There are legitimate workloads with hundreds of processes and there can be hundreds of workloads running on large machines. The unaccounted memory can cause isolation issues between the workloads particularly on highly utilized machines.

Good enough for me, as long as that is part of the commit message.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#202 lore raw
In reply to Vasily Averin
Quoted 43 lines · Vasily Averin
Quoted 24 lines · Michal Hocko
Quoted 15 lines · Vasily Averin
Quoted 9 lines · Shakeel Butt
Quoted 3 lines · Vasily Averin

OpenVZ many years accounted memory of few kernel objects,
this helps us to prevent host memory abuse from inside memcg-limited container.

The text is cryptic but I am assuming you wanted to say that OpenVZ has remained on a kernel which was still on opt-out kmem accounting i.e. <4.5. Now OpenVZ wants to move to a newer kernel and thus these patches are needed, right?

Something like this.
Frankly speaking I badly understand which arguments should I provide to upstream to enable accounting for some new king of objects.

Quoted 7 lines

OpenVZ used own accounting subsystem since 2001 (i.e. since v2.2.x linux kernels) and we have accounted all required kernel objects by using our own patches. When memcg was added to upstream Vladimir Davydov added accounting of some objects to upstream but did not skipped another ones.
Now OpenVZ uses RHEL7-based kernels with cgroup v1 in production, and we still account "skipped" objects by our own patches just because we accounted such objects before. We're working on rebase to new kernels and we prefer to push our old patches to upstream.

Quoted 5 lines

That is certainly an interesting information. But for a changelog it would be more appropriate to provide information about how much memory user can induce and whether there is any way to limit that memory by other means. How practical those other means are and which usecases will benefit from the containment.

Right now I would like to understand how should I argument my requests about accounting of new kind of objects.

Which description it enough to enable object accounting?
Could you please specify some edge rules?
Should I push such patches trough this list?
Is it probably better to send them to mailing lists of according subsystems? Should I notify them somehow at least?

"untrusted netadmin inside memcg-limited container can create unlimited number of routing entries, trigger OOM on host that will be unable to find the reason of memory shortage and kill huge"

Quoted 9 lines

"each mount inside memcg-limited container creates non-accounted mount object,
but new mount namespace creation consumes huge piece of non-accounted memory for cloned mounts"

"unprivileged user inside memcg-limited container can create non-accounted multi-page per-thread kernel objects for LDT"

"non-accounted multi-page tty objects can be created from inside memcg-limited container"

"unprivileged user inside memcg-limited container can trigger creation of huge number of non-accounted fasync_struct objects"

I think the above reasoning is good enough. Just resend your patches with the corresponding details.