Skip to contentSkip to conversation

bpf: move rcu lock management out of BPF_PROG_RUN routines

PATCH Superseded Stanislav Fomichev 5 participants 10 messages Apr 13, 2022 – Apr 14, 2022

Subsystems (9)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 bpf: move rcu lock management out of BPF_PROG_RUN routines

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);

I'm assuming in practice rcu subsystem isn't fast enough to trigger this but let's use rcu API properly: ask callers of BPF_PROG_RUN to manage rcu themselves.

Also, rename to lower caps to not confuse with macros. Additionally, drop and expand BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY.

See [1] for more context.

[1] https://lore.kernel.org/bpf/CAKH8qBs60fOinFdxiiQikK_q0EcVxGvNTQoWvHLEUGbgcj1UYg@mail.gmail.com/T/#u

Cc Martin KaFai Lau <[email protected]>
Fixes 7d08c2c91171 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of macros into functions")
---
 drivers/media/rc/bpf-lirc.c |  8 +++-
 include/linux/bpf.h         | 70 ++++++-------------------------
 kernel/bpf/cgroup.c         | 84 +++++++++++++++++++++++++++++--------
 kernel/trace/bpf_trace.c    |  5 ++-
 4 files changed, 90 insertions(+), 77 deletions(-)
drivers/media/rc/bpf-lirc.c +6 -2
index 3eff08d7b8e5..fe17c7f98e81 100644
@@ -216,8 +216,12 @@ void lirc_bpf_run(struct rc_dev *rcdev, u32 sample)
 
 	raw->bpf_sample = sample;
 
-	if (raw->progs)
-		BPF_PROG_RUN_ARRAY(raw->progs, &raw->bpf_sample, bpf_prog_run);
+	if (raw->progs) {
+		rcu_read_lock();
+		bpf_prog_run_array(rcu_dereference(raw->progs),
+				   &raw->bpf_sample, bpf_prog_run);
+		rcu_read_unlock();
+	}
 }
 
 /*
include/linux/bpf.h +13 -57

144 lines not on this page · raw message

kernel/bpf/cgroup.c +67 -17

155 lines not on this page · raw message

kernel/trace/bpf_trace.c +4 -1
index b26f3da943de..f15b826f9899 100644
@@ -129,7 +129,10 @@ unsigned int trace_call_bpf(struct trace_event_call *call, void *ctx)
 	 * out of events when it was updated in between this and the
 	 * rcu_dereference() which is accepted risk.
 	 */
-	ret = BPF_PROG_RUN_ARRAY(call->prog_array, ctx, bpf_prog_run);
+	rcu_read_lock();
+	ret = bpf_prog_run_array(rcu_dereference(call->prog_array),
+				 ctx, bpf_prog_run);
+	rcu_read_unlock();
 
  out:
 	__this_cpu_dec(bpf_prog_active);
Signature · 1 line
2.36.0.rc0.470.gd361397f0d-goog
#1 lore raw
Quoted 10 lines · Stanislav Fomichev

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]);
        ...
Quoted 6 lines

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);

So subtle difference, wow...

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?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog, ret_flags) \

  ({
      int ret;

      rcu_read_lock();
      ret = __BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
      rcu_read_unlock();
      ret;

})

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)?

Quoted 21 lines

I'm assuming in practice rcu subsystem isn't fast enough to trigger this but let's use rcu API properly: ask callers of BPF_PROG_RUN to manage rcu themselves.

Also, rename to lower caps to not confuse with macros. Additionally, drop and expand BPF_PROG_CGROUP_INET_EGRESS_RUN_ARRAY.

See [1] for more context.

[1] https://lore.kernel.org/bpf/CAKH8qBs60fOinFdxiiQikK_q0EcVxGvNTQoWvHLEUGbgcj1UYg@mail.gmail.com/T/#u

Cc Martin KaFai Lau <[email protected]>
Fixes 7d08c2c91171 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of macros into functions")
---
 drivers/media/rc/bpf-lirc.c |  8 +++-
 include/linux/bpf.h         | 70 ++++++-------------------------
 kernel/bpf/cgroup.c         | 84 +++++++++++++++++++++++++++++--------
 kernel/trace/bpf_trace.c    |  5 ++-
 4 files changed, 90 insertions(+), 77 deletions(-)

[...]

#2 lore raw
Quoted 34 lines · Andrii Nakryiko
Quoted 10 lines · Stanislav Fomichev

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]);
        ...
Quoted 6 lines

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);

So subtle difference, wow...

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?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog, ret_flags) \
   ({
       int ret;

       rcu_read_lock();
       ret =  
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
       rcu_read_unlock();
       ret;
Quoted 6 lines

})

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.

#3 lore raw
Quoted 44 lines · On Wed, Apr 13, 2022 at 12:39 PM <[email protected]> wrote:
Quoted 35 lines · Andrii Nakryiko
Quoted 10 lines · Stanislav Fomichev

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]);
        ...
Quoted 6 lines

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);

So subtle difference, wow...

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?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog, ret_flags) \
   ({
       int ret;

       rcu_read_lock();
       ret =
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
       rcu_read_unlock();
       ret;
Quoted 6 lines

})

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)?

Quoted 8 lines

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.

#4 lore raw
Quoted 54 lines · Andrii Nakryiko
Quoted 44 lines · On Wed, Apr 13, 2022 at 12:39 PM <[email protected]> wrote:
Quoted 35 lines · Andrii Nakryiko
Quoted 10 lines · Stanislav Fomichev

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]);
        ...
Quoted 6 lines

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);

So subtle difference, wow...

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?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog, ret_flags) \
   ({
       int ret;

       rcu_read_lock();
       ret =
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
       rcu_read_unlock();
       ret;
Quoted 6 lines

})

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)?

Quoted 8 lines

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.

Quoted 4 lines

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.

Would it work if the bpf_prog_run_array_cg() directly takes the 'struct cgroup *cgrp' argument instead of the array ? bpf_prog_run_array_cg() should know what protection is needed to get member from the cgrp ptr. The sk call path should be able to provide a cgrp ptr. For current cgrp, pass NULL as the cgrp pointer and then current will be used in bpf_prog_run_array_cg(). A rcu_read_lock() is needed anyway to get the current's cgrp and can be done together in bpf_prog_run_array_cg().

That there are only two remaining bpf_prog_run_array() usages from lirc and bpf_trace which are not too bad to have them directly do rcu_read_lock on their own struct ?

#5 lore raw
Quoted 75 lines · Martin KaFai Lau
Quoted 7 lines · Andrii Nakryiko
Quoted 6 lines · On Wed, Apr 13, 2022 at 12:39 PM <[email protected]> wrote:
Quoted 4 lines · Andrii Nakryiko
Quoted 2 lines · Stanislav Fomichev

Commit 7d08c2c91171 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of

macros

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

into functions") switched a bunch of BPF_PROG_RUN macros to inline routines. This changed the semantic a bit. Due to arguments

expansion

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 12 lines

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 4 lines
Quoted 4 lines
Quoted 4 lines

So subtle difference, wow...

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

Quoted 1 line
Quoted 1 line
Quoted 1 line

rcu lock/unlock and grabs effective array and then calls static

inline

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

function be a viable solution?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog,

ret_flags) \

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
   ({
       int ret;
       rcu_read_lock();
       ret =
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
       rcu_read_unlock();
       ret;

})

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

where __BPF_PROG_RUN_ARRAY_CG_FLAGS is what BPF_PROG_RUN_ARRAY_CG_FLAGS is today but with __rcu annotation

dropped

Quoted 11 lines
Quoted 10 lines
Quoted 1 line

(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.

Quoted 4 lines

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.

Would it work if the bpf_prog_run_array_cg() directly takes the 'struct cgroup *cgrp' argument instead of the array ? bpf_prog_run_array_cg() should know what protection is needed to get member from the cgrp ptr. The sk call path should be able to provide a cgrp ptr. For current cgrp, pass NULL as the cgrp pointer and then current will be used in bpf_prog_run_array_cg(). A rcu_read_lock() is needed anyway to get the current's cgrp and can be done together in bpf_prog_run_array_cg().

Quoted 3 lines

That there are only two remaining bpf_prog_run_array() usages from lirc and bpf_trace which are not too bad to have them directly do rcu_read_lock on their own struct ?

From Andrii's original commit message:

I think BPF_PROG_RUN_ARRAY_CG would benefit from further refactoring to
accept
struct cgroup and enum bpf_attach_type instead of bpf_prog_array,
fetching
cgrp->bpf.effective[type] and RCU-dereferencing it internally. But that required including include/linux/cgroup-defs.h, which I wasn't sure is
ok with
everyone.

I guess including cgroup-defs.h/bpf-cgroup-defs.h into bpf.h might still be somewhat problematic?

But even if we pass the cgroup pointer, I'm assuming that this cgroup pointer is still rcu-managed, right? So the callers still have to rcu-lock. However, in most places we don't care and do "cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);" but seems like it depends on the fact that sockets can't (yet?)
change their cgroup association and it's fine to not rcu-lock that
cgroup. Seems fragile, but ok. It always stumbles me when I see:

cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); bpf_prog_run_array_cg_flags(cgrp.bpf->effective[atype], ...)

But then, with current, it becomes:

rcu_read_lock();
cgrp = task_dfl_cgroup(current); bpf_prog_run_array_cg_flags(cgrp.bpf->effective[atype], ...) rcu_read_unlock();

Idk, I might be overthinking it. I'll try to see if including bpf-cgroup-defs.h and passing cgroup_bpf is workable.

#6 lore raw
Quoted 94 lines · On Wed, Apr 13, 2022 at 03:52:18PM -0700, [email protected] wrote:
Quoted 75 lines · Martin KaFai Lau
Quoted 7 lines · Andrii Nakryiko
Quoted 6 lines · On Wed, Apr 13, 2022 at 12:39 PM <[email protected]> wrote:
Quoted 4 lines · Andrii Nakryiko
Quoted 2 lines · Stanislav Fomichev

Commit 7d08c2c91171 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of

macros

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

into functions") switched a bunch of BPF_PROG_RUN macros to inline routines. This changed the semantic a bit. Due to arguments

expansion

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 12 lines

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 4 lines
Quoted 4 lines
Quoted 4 lines

So subtle difference, wow...

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

Quoted 1 line
Quoted 1 line
Quoted 1 line

rcu lock/unlock and grabs effective array and then calls static

inline

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

function be a viable solution?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog,

ret_flags) \

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
   ({
       int ret;
       rcu_read_lock();
       ret =
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
       rcu_read_unlock();
       ret;

})

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

where __BPF_PROG_RUN_ARRAY_CG_FLAGS is what BPF_PROG_RUN_ARRAY_CG_FLAGS is today but with __rcu annotation

dropped

Quoted 11 lines
Quoted 10 lines
Quoted 1 line

(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.

Quoted 4 lines

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.

Would it work if the bpf_prog_run_array_cg() directly takes the 'struct cgroup *cgrp' argument instead of the array ? bpf_prog_run_array_cg() should know what protection is needed to get member from the cgrp ptr. The sk call path should be able to provide a cgrp ptr. For current cgrp, pass NULL as the cgrp pointer and then current will be used in bpf_prog_run_array_cg(). A rcu_read_lock() is needed anyway to get the current's cgrp and can be done together in bpf_prog_run_array_cg().

Quoted 3 lines

That there are only two remaining bpf_prog_run_array() usages from lirc and bpf_trace which are not too bad to have them directly do rcu_read_lock on their own struct ?

From Andrii's original commit message:

I think BPF_PROG_RUN_ARRAY_CG would benefit from further refactoring to
accept
struct cgroup and enum bpf_attach_type instead of bpf_prog_array,
fetching
cgrp->bpf.effective[type] and RCU-dereferencing it internally. But that required including include/linux/cgroup-defs.h, which I wasn't sure is
ok with
everyone.

I guess including cgroup-defs.h/bpf-cgroup-defs.h into bpf.h might still be somewhat problematic?

Quoted 8 lines

But even if we pass the cgroup pointer, I'm assuming that this cgroup pointer
is still rcu-managed, right? So the callers still have to rcu-lock. However, in most places we don't care and do "cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);"
but seems like it depends on the fact that sockets can't (yet?) change their cgroup association and it's fine to not rcu-lock that cgroup. Seems fragile, but ok.

There is no __rcu tag in struct sock_cgroup_data, so presumably it won't change or a lock is needed ? seems to be the former.

Quoted 7 lines

It always stumbles me when I see:

cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); bpf_prog_run_array_cg_flags(cgrp.bpf->effective[atype], ...)

But then, with current, it becomes:

Quoted 7 lines

rcu_read_lock();
cgrp = task_dfl_cgroup(current); bpf_prog_run_array_cg_flags(cgrp.bpf->effective[atype], ...) rcu_read_unlock();

Idk, I might be overthinking it. I'll try to see if including bpf-cgroup-defs.h and passing cgroup_bpf is workable.

yeah, passing cgroup_bpf and bpf-cgroup-defs.h is a better option.

#7 lore raw
Quoted 114 lines · Martin KaFai Lau
Quoted 94 lines · On Wed, Apr 13, 2022 at 03:52:18PM -0700, [email protected] wrote:
Quoted 75 lines · Martin KaFai Lau
Quoted 7 lines · Andrii Nakryiko
Quoted 6 lines · On Wed, Apr 13, 2022 at 12:39 PM <[email protected]> wrote:
Quoted 4 lines · Andrii Nakryiko
Quoted 2 lines · Stanislav Fomichev

Commit 7d08c2c91171 ("bpf: Refactor BPF_PROG_RUN_ARRAY family of

macros

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

into functions") switched a bunch of BPF_PROG_RUN macros to inline routines. This changed the semantic a bit. Due to arguments

expansion

Quoted 13 lines
Quoted 13 lines
Quoted 12 lines
Quoted 12 lines

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 4 lines
Quoted 4 lines
Quoted 4 lines

So subtle difference, wow...

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

Quoted 1 line
Quoted 1 line
Quoted 1 line

rcu lock/unlock and grabs effective array and then calls static

inline

Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

function be a viable solution?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog,

ret_flags) \

Quoted 11 lines
Quoted 11 lines
Quoted 9 lines
   ({
       int ret;
       rcu_read_lock();
       ret =
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
       rcu_read_unlock();
       ret;

})

Quoted 2 lines
Quoted 2 lines
Quoted 2 lines

where __BPF_PROG_RUN_ARRAY_CG_FLAGS is what BPF_PROG_RUN_ARRAY_CG_FLAGS is today but with __rcu annotation

dropped

Quoted 11 lines
Quoted 10 lines
Quoted 1 line

(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.

Quoted 4 lines

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.

Would it work if the bpf_prog_run_array_cg() directly takes the 'struct cgroup *cgrp' argument instead of the array ? bpf_prog_run_array_cg() should know what protection is needed to get member from the cgrp ptr. The sk call path should be able to provide a cgrp ptr. For current cgrp, pass NULL as the cgrp pointer and then current will be used in bpf_prog_run_array_cg(). A rcu_read_lock() is needed anyway to get the current's cgrp and can be done together in bpf_prog_run_array_cg().

Quoted 3 lines

That there are only two remaining bpf_prog_run_array() usages from lirc and bpf_trace which are not too bad to have them directly do rcu_read_lock on their own struct ?

From Andrii's original commit message:

I think BPF_PROG_RUN_ARRAY_CG would benefit from further refactoring to
accept
struct cgroup and enum bpf_attach_type instead of bpf_prog_array,
fetching
cgrp->bpf.effective[type] and RCU-dereferencing it internally. But that required including include/linux/cgroup-defs.h, which I wasn't sure is
ok with
everyone.

I guess including cgroup-defs.h/bpf-cgroup-defs.h into bpf.h might still be somewhat problematic?

Quoted 8 lines

But even if we pass the cgroup pointer, I'm assuming that this cgroup pointer
is still rcu-managed, right? So the callers still have to rcu-lock. However, in most places we don't care and do "cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data);"
but seems like it depends on the fact that sockets can't (yet?) change their cgroup association and it's fine to not rcu-lock that cgroup. Seems fragile, but ok.

There is no __rcu tag in struct sock_cgroup_data, so presumably it won't change or a lock is needed ? seems to be the former.

Quoted 7 lines

It always stumbles me when I see:

cgrp = sock_cgroup_ptr(&sk->sk_cgrp_data); bpf_prog_run_array_cg_flags(cgrp.bpf->effective[atype], ...)

But then, with current, it becomes:

Quoted 8 lines
Quoted 7 lines

rcu_read_lock();
cgrp = task_dfl_cgroup(current); bpf_prog_run_array_cg_flags(cgrp.bpf->effective[atype], ...) rcu_read_unlock();

Idk, I might be overthinking it. I'll try to see if including bpf-cgroup-defs.h and passing cgroup_bpf is workable.

yeah, passing cgroup_bpf and bpf-cgroup-defs.h is a better option.

+1. Daniel, would you be fine with this instead of explicit rcu_read_lock()/rcu_read_unlock() everywhere?

maintainer
#8 lore raw
In reply to [email protected]
Quoted 2 lines · On Wed, 13 Apr 2022 15:52:18 -0700 [email protected] wrote:

I guess including cgroup-defs.h/bpf-cgroup-defs.h into bpf.h might still be somewhat problematic?

FWIW including -defs.h into bpf.h should be fine. Obviously fewer cross-header deps the better. But the main point is that we don't want bpf.h to be included in too many places, so opposite direction to what you're asking IIUC.

maintainer
#9 lore raw
In reply to Andrii Nakryiko
Quoted 53 lines · Andrii Nakryiko
Quoted 43 lines · On Wed, Apr 13, 2022 at 12:39 PM <[email protected]> wrote:
Quoted 38 lines · Andrii Nakryiko
Quoted 10 lines · Stanislav Fomichev

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]);
         ...
Quoted 6 lines

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);

So subtle difference, wow...

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?

#define BPF_PROG_RUN_ARRAY_CG_FLAGS(array_rcu, ctx, run_prog, ret_flags) \
    ({
        int ret;

        rcu_read_lock();
        ret =
__BPF_PROG_RUN_ARRAY_CG_FLAGS(rcu_dereference(array_rcu), ....);
        rcu_read_unlock();
        ret;
    })
Quoted 3 lines

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)?

Quoted 8 lines

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.

Quoted 4 lines

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.