Re: [PATCH bpf-next] bpf: move rcu lock management out of BPF_PROG_RUN routines

From
Daniel Borkmann <daniel@iogearbox.net>
Date
2022-04-13 23:08:16
Also in
bpf
Reply to
Andrii Nakryiko, 2022-04-13
On 4/13/22 9:52 PM, Andrii Nakryiko wrote:
On Wed, Apr 13, 2022 at 12:39 PM [off-list ref] wrote:
quoted
On 04/13, Andrii Nakryiko wrote:
quoted
On Wed, Apr 13, 2022 at 11:33 AM Stanislav Fomichev [off-list ref]
wrote:
quoted
Commit 7d08c2c91171 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of macros
into functions") switched a bunch of BPF_PROG_RUN macros to inline
routines. This changed the semantic a bit. Due to arguments expansion
of macros, it used to be:

         rcu_read_lock();
         array = rcu_dereference(cgrp->bpf.effective[atype]);
         ...

Now, with with inline routines, we have:
         array_rcu = rcu_dereference(cgrp->bpf.effective[atype]);
         /* array_rcu can be kfree'd here */
         rcu_read_lock();
         array = rcu_dereference(array_rcu);
quoted
So subtle difference, wow...
quoted
But this open-coding of rcu_read_lock() seems very unfortunate as
well. Would making BPF_PROG_RUN_ARRAY back to a macro which only does
rcu lock/unlock and grabs effective array and then calls static inline
function be a viable solution?
quoted
#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog, ret_flags) \
    ({
        int ret;
quoted
        rcu_read_lock();
        ret =
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
        rcu_read_unlock();
        ret;
    })
quoted
where __BPF_PROG_RUN_ARRAY_CG_FLAGS is what
BPF_PROG_RUN_ARRAY_CG_FLAGS is today but with __rcu annotation dropped
(and no internal rcu stuff)?
Yeah, that should work. But why do you think it's better to hide them?
I find those automatic rcu locks deep in the call stack a bit obscure
(when reasoning about sleepable vs non-sleepable contexts/bpf).

I, as the caller, know that the effective array is rcu-managed (it
has __rcu annotation) and it seems natural for me to grab rcu lock
while work with it; I might grab it for some other things like cgroup
anyway.
If you think that having this more explicitly is better, I'm fine with
that as well. I thought a simpler invocation pattern would be good,
given we call bpf_prog_run_array variants in quite a lot of places. So
count me indifferent. I'm curious what others think.
+1 for explicit, might also be easier to review/audit compared to hidden
in macro.
Thread (10 messages) 10 messages, 5 authors, last reply 2022-04-14
Keyboard shortcuts
hback out one level
jnext message in thread / next series-diff position
kprevious message in thread / previous series-diff position
ldrill in
Escclose help / fold thread tree
?toggle this help