linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
From: Roman Gushchin <guro@fb.com>
To: Daniel Borkmann <daniel@iogearbox.net>
Cc: <bpf@vger.kernel.org>, <ast@kernel.org>, <netdev@vger.kernel.org>,
	<andrii@kernel.org>, <akpm@linux-foundation.org>,
	<linux-mm@kvack.org>, <linux-kernel@vger.kernel.org>,
	<kernel-team@fb.com>
Subject: Re: [PATCH bpf-next v7 00/34] bpf: switch to memcg-based memory accounting
Date: Mon, 23 Nov 2020 16:05:48 -0800	[thread overview]
Message-ID: <20201124000548.GE186396@carbon.dhcp.thefacebook.com> (raw)
In-Reply-To: <9134a408-e26c-a7f2-23a7-5fc221bafdde@iogearbox.net>

On Mon, Nov 23, 2020 at 02:30:09PM +0100, Daniel Borkmann wrote:
> On 11/19/20 6:37 PM, Roman Gushchin wrote:
> > Currently bpf is using the memlock rlimit for the memory accounting.
> > This approach has its downsides and over time has created a significant
> > amount of problems:
> > 
> > 1) The limit is per-user, but because most bpf operations are performed
> >     as root, the limit has a little value.
> > 
> > 2) It's hard to come up with a specific maximum value. Especially because
> >     the counter is shared with non-bpf users (e.g. memlock() users).
> >     Any specific value is either too low and creates false failures
> >     or too high and useless.
> > 
> > 3) Charging is not connected to the actual memory allocation. Bpf code
> >     should manually calculate the estimated cost and precharge the counter,
> >     and then take care of uncharging, including all fail paths.
> >     It adds to the code complexity and makes it easy to leak a charge.
> > 
> > 4) There is no simple way of getting the current value of the counter.
> >     We've used drgn for it, but it's far from being convenient.
> > 
> > 5) Cryptic -EPERM is returned on exceeding the limit. Libbpf even had
> >     a function to "explain" this case for users.
> > 
> > In order to overcome these problems let's switch to the memcg-based
> > memory accounting of bpf objects. With the recent addition of the percpu
> > memory accounting, now it's possible to provide a comprehensive accounting
> > of the memory used by bpf programs and maps.
> > 
> > This approach has the following advantages:
> > 1) The limit is per-cgroup and hierarchical. It's way more flexible and allows
> >     a better control over memory usage by different workloads. Of course, it
> >     requires enabled cgroups and kernel memory accounting and properly configured
> >     cgroup tree, but it's a default configuration for a modern Linux system.
> > 
> > 2) The actual memory consumption is taken into account. It happens automatically
> >     on the allocation time if __GFP_ACCOUNT flags is passed. Uncharging is also
> >     performed automatically on releasing the memory. So the code on the bpf side
> >     becomes simpler and safer.
> > 
> > 3) There is a simple way to get the current value and statistics.
> > 
> > In general, if a process performs a bpf operation (e.g. creates or updates
> > a map), it's memory cgroup is charged. However map updates performed from
> > an interrupt context are charged to the memory cgroup which contained
> > the process, which created the map.
> > 
> > Providing a 1:1 replacement for the rlimit-based memory accounting is
> > a non-goal of this patchset. Users and memory cgroups are completely
> > orthogonal, so it's not possible even in theory.
> > Memcg-based memory accounting requires a properly configured cgroup tree
> > to be actually useful. However, it's the way how the memory is managed
> > on a modern Linux system.

Hi Daniel!

> 
> The cover letter here only describes the advantages of this series, but leaves
> out discussion of the disadvantages. They definitely must be part of the series
> to provide a clear description of the semantic changes to readers.

Honestly, I don't see them as disadvantages. Cgroups are basic units in which
resource control limits/guarantees/accounting are expressed. If there are
no cgroups created and configured in the system, it's obvious (maybe only to me)
that no limits are applied.

Users (rlimits) are to some extent similar units, but they do not provide
a comprehensive resource control system. Some parts are deprecated (like rss limits),
some parts are just missing. Aside from bpf nobody uses users to control
the memory as a physical resource. It simple doesn't work (and never did).
If somebody expects that a non-privileged user can't harm the system by depleting
it's memory (and other resources), it's simple not correct. There are multiple ways
for doing it. Accounting or not accounting bpf maps doesn't really change anything.
If we see them not as a real security mechanism, but as a way to prevent "mistakes",
which can harm the system, it's to some extent legit. The question is only if
it justifies the amount of problems we had with these limits.

Switching to memory cgroups, which are the way how the memory control is expressed,
IMO doesn't need an additional justification. During the last year I remember 2 or 3
times when various people (internally in Fb and in public mailing lists) were asking
why bpf memory is not accounted to memory cgroups. I think it's basically expected
these days.

I'll try to make more obvious that we're switching from users to cgroups and
describe the consequences of it on an unconfigured system. I'll update the cover.

> Last time we
> discussed them, they were i) no mem limits in general on unprivileged users when
> memory cgroups was not configured in the kernel, and ii) no mem limits by default
> if not configured in the cgroup specifically.
> Did we made any progress on these
> in the meantime? How do we want to address them? What is the concrete justification
> to not address them?

I don't see how they can and should be addressed.
Cgroups are the way how the resource consumption of a group of processes can be
limited. If there are no cgroups configured, it means all resources are available
to everyone. Maybe a user wants to use the whole memory for a bpf map? Why not?

Do you have any specific use case in your mind?
If you see a real value in the old system (I don't), which can justify an additional
complexity of keeping them both in a working state, we can discuss this option too.
We can make a switch in few steps, if you think it's too risky.

> 
> Also I wonder what are the risk of regressions here, for example, if an existing
> orchestrator has configured memory cgroup limits that are tailored to the application's
> needs.. now, with kernel upgrade BPF will start to interfere, e.g. if a BPF program
> attached to cgroups (e.g. connect/sendmsg/recvmsg or general cgroup skb egress hook)
> starts charging to the process' memcg due to map updates?

Well, if somebody has a tight memory limit and large bpf map(s), they can see a "regression".
However the kernel memory usage and accounting implementation details vary from a version
to a version, so nobody should expect that limits once set will work forever.
If for some strange reason it'll create a critical problem, as a workaround it's possible
to disable the kernel memory accounting as a whole (via a boot option).

Actually, it seems that the usefulness of strict limits is generally limited, because
it's hard to get and assign any specific value. They are always either too relaxed
(and have no value), either too strict (and causing production issues). Memory cgroups
are generally moving towards soft limits and protections. But it's a separate theme...

Thanks!


      parent reply	other threads:[~2020-11-24  0:06 UTC|newest]

Thread overview: 47+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-19 17:37 [PATCH bpf-next v7 00/34] bpf: switch to memcg-based memory accounting Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 01/34] mm: memcontrol: use helpers to read page's memcg data Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 02/34] mm: memcontrol/slab: use helpers to access slab page's memcg_data Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 03/34] mm: introduce page memcg flags Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 04/34] mm: convert page kmemcg type to a page memcg flag Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 05/34] bpf: memcg-based memory accounting for bpf progs Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 06/34] bpf: prepare for memcg-based memory accounting for bpf maps Roman Gushchin
2020-11-20  1:05   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 07/34] bpf: " Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 08/34] bpf: refine memcg-based memory accounting for arraymap maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 09/34] bpf: refine memcg-based memory accounting for cpumap maps Roman Gushchin
2020-11-20  1:33   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 10/34] bpf: memcg-based memory accounting for cgroup storage maps Roman Gushchin
2020-11-20  1:37   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 11/34] bpf: refine memcg-based memory accounting for devmap maps Roman Gushchin
2020-11-20  1:38   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 12/34] bpf: refine memcg-based memory accounting for hashtab maps Roman Gushchin
2020-11-20  1:39   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 13/34] bpf: memcg-based memory accounting for lpm_trie maps Roman Gushchin
2020-11-20  1:39   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 14/34] bpf: memcg-based memory accounting for bpf ringbuffer Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 15/34] bpf: memcg-based memory accounting for bpf local storage maps Roman Gushchin
2020-11-20  1:44   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 16/34] bpf: refine memcg-based memory accounting for sockmap and sockhash maps Roman Gushchin
2020-11-20  1:45   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 17/34] bpf: refine memcg-based memory accounting for xskmap maps Roman Gushchin
2020-11-20  1:45   ` Song Liu
2020-11-19 17:37 ` [PATCH bpf-next v7 18/34] bpf: eliminate rlimit-based memory accounting for arraymap maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 19/34] bpf: eliminate rlimit-based memory accounting for bpf_struct_ops maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 20/34] bpf: eliminate rlimit-based memory accounting for cpumap maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 21/34] bpf: eliminate rlimit-based memory accounting for cgroup storage maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 22/34] bpf: eliminate rlimit-based memory accounting for devmap maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 23/34] bpf: eliminate rlimit-based memory accounting for hashtab maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 24/34] bpf: eliminate rlimit-based memory accounting for lpm_trie maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 25/34] bpf: eliminate rlimit-based memory accounting for queue_stack_maps maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 26/34] bpf: eliminate rlimit-based memory accounting for reuseport_array maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 27/34] bpf: eliminate rlimit-based memory accounting for bpf ringbuffer Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 28/34] bpf: eliminate rlimit-based memory accounting for sockmap and sockhash maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 29/34] bpf: eliminate rlimit-based memory accounting for stackmap maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 30/34] bpf: eliminate rlimit-based memory accounting for xskmap maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 31/34] bpf: eliminate rlimit-based memory accounting for bpf local storage maps Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 32/34] bpf: eliminate rlimit-based memory accounting infra for bpf maps Roman Gushchin
2020-11-21  2:52   ` Alexei Starovoitov
2020-11-21  2:59     ` Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 33/34] bpf: eliminate rlimit-based memory accounting for bpf progs Roman Gushchin
2020-11-19 17:37 ` [PATCH bpf-next v7 34/34] bpf: samples: do not touch RLIMIT_MEMLOCK Roman Gushchin
     [not found] ` <9134a408-e26c-a7f2-23a7-5fc221bafdde@iogearbox.net>
2020-11-24  0:05   ` Roman Gushchin [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201124000548.GE186396@carbon.dhcp.thefacebook.com \
    --to=guro@fb.com \
    --cc=akpm@linux-foundation.org \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=kernel-team@fb.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).