Re: [RFC PATCH bpf-next 00/10] bpf, mm: Recharge pages when reuse bpf map

Roman Gushchin
2022-06-23T04:41:27Z

On Sun, Jun 19, 2022 at 03:50:22PM +0000, Yafang Shao wrote:
> After switching to memcg-based bpf memory accounting, the bpf memory is
> charged to the loader's memcg by default, that causes unexpected issues for
> us. For instance, the container of the loader may be restarted after
> pinning progs and maps, but the bpf memcg will be left and pinned on the
> system. Once the loader's new generation container is started, the leftover
> pages won't be charged to it. That inconsistent behavior will make trouble
> for the memory resource management for this container.
>
> In the past few days, I have proposed two patchsets[1][2] to try to resolve
> this issue, but in both of these two proposals the user code has to be
> changed to adapt to it, that is a pain for us. This patchset relieves the
> pain by triggering the recharge in libbpf. It also addresses Roman's
> critical comments.
>
> The key point we can avoid changing the user code is that there's a resue
> path in libbpf. Once the bpf container is restarted again, it will try
> to re-run the required bpf programs, if the bpf programs are the same with
> the already pinned one, it will reuse them.
>
> To make sure we either recharge all of them successfully or don't recharge
> any of them. The recharge prograss is divided into three steps:
> - Pre charge to the new generation
> To make sure once we uncharge from the old generation, we can always
> charge to the new generation succeesfully. If we can't pre charge to
> the new generation, we won't allow it to be uncharged from the old
> generation.
> - Uncharge from the old generation
> After pre charge to the new generation, we can uncharge from the old
> generation.
> - Post charge to the new generation
> Finnaly we can set pages' memcg_data to the new generation.
> In the pre charge step, we may succeed to charge some addresses, but fail
> to charge a new address, then we should uncharge the already charged
> addresses, so another recharge-err step is instroduced.
>
> This pachset has finished recharging bpf hash map. which is mostly used
> by our bpf services. The other maps hasn't been implemented yet. The bpf
> progs hasn't been implemented neither.

Without going into the implementation details, the overall approach looks
ok to me. But it adds complexity and code into several different subsystems,
and I'm 100% sure it's not worth it if we talking about a partial support
of a single map type. Are you committed to implement the recharging
for all/most map types and progs and support this code in the future?

I'm still feeling you trying to solve a userspace problem in the kernel.
Not saying it can't be solved this way, but it seems like there are
easier options.

Thanks!