RE: [PATCH RFC] net: memcg accounting for veth devices

From: King, Colin
Date: Wed Mar 02 2022 - 08:30:25 EST



Just to note that stress-ng does attempt to set the maximum ulimits across all ulimits before it invokes a stressor to try and stress the system as much as possible. It also changes the per-stressor oom adjust setting to make stressors less OOMable, one can disable this with --no-oom-adjust and one can force stressors from being restarted on an OOM with the --oomable option.


-----Original Message-----
From: Eric W. Biederman <ebiederm@xxxxxxxxxxxx>
Sent: 01 March 2022 20:50
To: Luis Chamberlain <mcgrof@xxxxxxxxxx>
Cc: Shakeel Butt <shakeelb@xxxxxxxxxx>; Colin Ian King <colin.king@xxxxxxxxxxxxx>; NeilBrown <neilb@xxxxxxx>; Vasily Averin <vvs@xxxxxxxxxxxxx>; Vlastimil Babka <vbabka@xxxxxxx>; Hocko, Michal <mhocko@xxxxxxxx>; Roman Gushchin <roman.gushchin@xxxxxxxxx>; Linux MM <linux-mm@xxxxxxxxx>; netdev@xxxxxxxxxxxxxxx; David S. Miller <davem@xxxxxxxxxxxxx>; Jakub Kicinski <kuba@xxxxxxxxxx>; Tejun Heo <tj@xxxxxxxxxx>; Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx>; Eric Dumazet <edumazet@xxxxxxxxxx>; Kees Cook <keescook@xxxxxxxxxxxx>; Hideaki YOSHIFUJI <yoshfuji@xxxxxxxxxxxxxx>; David Ahern <dsahern@xxxxxxxxxx>; linux-kernel@xxxxxxxxxxxxxxx; kernel@xxxxxxxxxx
Subject: Re: [PATCH RFC] net: memcg accounting for veth devices

Luis Chamberlain <mcgrof@xxxxxxxxxx> writes:

> On Tue, Mar 01, 2022 at 10:09:17AM -0800, Shakeel Butt wrote:
>> On Mon, Feb 28, 2022 at 06:36:58AM -0800, Luis Chamberlain wrote:
>> > On Mon, Feb 28, 2022 at 10:17:16AM +0300, Vasily Averin wrote:
>> > > Following one-liner running inside memcg-limited container
>> > > consumes huge number of host memory and can trigger global OOM.
>> > >
>> > > for i in `seq 1 xxx` ; do ip l a v$i type veth peer name vp$i ;
>> > > done
>> > >
>> > > Patch accounts most part of these allocations and can protect host.
>> > > ---[cut]---
>> > > It is not polished, and perhaps should be splitted.
>> > > obviously it affects other kind of netdevices too.
>> > > Unfortunately I'm not sure that I will have enough time to handle
>> > > it
>> > properly
>> > > and decided to publish current patch version as is.
>> > > OpenVz workaround it by using per-container limit for number of
>> > > available netdevices, but upstream does not have any kind of
>> > > per-container configuration.
>> > > ------
>>
>> > Should this just be a new ucount limit on kernel/ucount.c and have
>> > veth use something like inc_ucount(current_user_ns(),
>> > current_euid(), UCOUNT_VETH)?
>>
>> > This might be abusing ucounts though, not sure, Eric?
>>
>>
>> For admins of systems running multiple workloads, there is no easy
>> way to set such limits for each workload.
>
> That's why defaults would exist. Today's ulimits IMHO are insane and
> some are arbitrarily large.

My perspective is that we have two basic kinds of limits.

Limits to catch programs that go out of control hopefully before they bring down the entire system. This is the purpose I see of rlimits and ucounts. Such limits should be set by default so large that no one has to care unless their program is broken.

Limits to contain programs and keep them from having a negative impact on other programs. Generally this is the role I see the cgroups playing. This limits must be much more tightly managed.

The problem with veth that was reported was that the memory cgroup limits fails to contain veth's allocations and veth manages to affect process outside the memory cgroup where the veth ``lives''. The effect is an OOM but the problem is that it is affecting processes out of the memory control group.

Part of the reason for the recent ucount work is so that ordinary users can create user namespaces and root in that user namespace won't be able to exceed the limits that were set when the user namespace was created by creating additional users.

Part of the reason for my ucount work is my frustration that cgroups would up something completely different than what was originally proposed and solve a rather problem set. Originally the proposal was that cgroups would be the user interface for the bean-counter patches.
(Roughly counts like the ucounts are now). Except for maybe the pid controller you mention below cgroups look nothing like that today.
So I went and I solved the original problem because it was still not solved.

The network stack should already have packet limits to prevent a global OOM so I am a bit curious why those limits aren't preventing a global OOM in for the veth device.


I am not saying that the patch is correct (although from 10,000 feet the patch sounds like it is solving the reported problem). I am answering the question of how I understand limits to work.

Luis does this explanation of how limits work help?


>> From admin's perspective it is preferred to have minimal knobs to set
>> and if these objects are charged to memcg then the memcg limits would
>> limit them. There was similar situation for inotify instances where
>> fs sysctl inotify/max_user_instances already limits the inotify
>> instances but we memcg charged them to not worry about setting such
>> limits. See ac7b79fd190b ("inotify, memcg: account inotify instances
>> to kmemcg").
>
> Yes but we want sensible defaults out of the box. What those should be
> IMHO might be work which needs to be figured out well.
>
> IMHO today's ulimits are a bit over the top today. This is off
> slightly off topic but for instance play with:
>
> git clone https://github.com/ColinIanKing/stress-ng
> cd stress-ng
> make -j 8
> echo 0 > /proc/sys/vm/oom_dump_tasks
> i=1; while true; do echo "RUNNING TEST $i"; ./stress-ng --unshare 8192
> --unshare-ops 10000; sleep 1; let i=$i+1; done
>
> If you see:
>
> [ 217.798124] cgroup: fork rejected by pids controller in
> /user.slice/user-1000.slice/session-1.scope
>
> Edit /usr/lib/systemd/system/user-.slice.d/10-defaults.conf to be:
>
> [Slice]
> TasksMax=MAX_TASKS|infinity
>
> Even though we have max_threads set to 61343, things ulimits have a
> different limit set, and what this means is the above can end up
> easily creating over 1048576 (17 times max_threads) threads all
> eagerly doing nothing to just exit, essentially allowing a sort of fork bomb on exit.
> Your system may or not fall to its knees.



What max_threads are you talking about here? The global max_threads exposed in /proc/sys/kernel/threads-max? I don't see how you can get around that. Especially since the count is not decremented until the process is reaped.

Or is this the pids controller having a low limit and /proc/sys/kernel/threads-max having a higher limit?

I really have not looked at this pids controller.

So I am not certain I understand your example here but I hope I have answered your question.

Eric