* Re: [PATCH bpf-next v3] bpf: Remove redundant checks
2025-04-16 21:55 ` Andrii Nakryiko
@ 2025-04-16 22:44 ` Zvi Effron
2025-04-23 17:05 ` Andrii Nakryiko
2025-04-17 9:52 ` Feng Yang
2025-04-17 13:36 ` Feng Yang
2 siblings, 1 reply; 6+ messages in thread
From: Zvi Effron @ 2025-04-16 22:44 UTC (permalink / raw)
To: Andrii Nakryiko
Cc: Feng Yang, ast, daniel, andrii, martin.lau, eddyz87, song,
yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa,
mattbobrowski, rostedt, mhiramat, mathieu.desnoyers, bpf,
linux-kernel, linux-trace-kernel
On Wed, Apr 16, 2025 at 2:56 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Apr 10, 2025 at 12:03 AM Feng Yang <yangfeng59949@163.com> wrote:
> >
> > From: Feng Yang <yangfeng@kylinos.cn>
> >
> > Many conditional checks in switch-case are redundant
> > with bpf_base_func_proto and should be removed.
> >
> > Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
> > Acked-by: Song Liu <song@kernel.org>
> > ---
> > Changes in v3:
> > - Only modify patch description information.
> > - Link to v2: https://lore.kernel.org/all/20250408071151.229329-1-yangfeng59949@163.com/
> >
> > Changes in v2:
> > - Only modify patch description information.
> > - Link to v1: https://lore.kernel.org/all/20250320032258.116156-1-yangfeng59949@163.com/
> > ---
> > kernel/trace/bpf_trace.c | 72 ----------------------------------------
> > 1 file changed, 72 deletions(-)
> >
>
> All this looks good, I checked that those functions indeed are allowed
> in bpf_base_func_proto. The only (minor) differences are capabilities,
> bpf_base_func_proto() correctly guards some of the helpers with
> CAP_BPF and/or CAP_PERFMON checks, while bpf_tracing_func_proto()
> doesn't seem to bother (which is either a bug or any tracing prog
> implies CAP_BPF and CAP_PERFMON, I'm not sure, didn't check).
>
> But I think we can take it further and remove even more stuff from
> bpf_tracing_func_proto and/or add more stuff into bpf_base_func_proto
> (perhaps as a few patches in a series, so it's easier to review and
> validate).
>
> Basically, except for a few custom implementations that depend on
> tracing program type (like get_stack and others like that), if
> something is OK to call from a tracing program it should be ok to call
> from any program type. And as such it can (should?) be added to
> bpf_base_func_proto, IMO.
Is this true? Does it make sense? (See below.)
> P.S. I'd name the patch/series as "bpf: streamline allowed helpers
> between tracing and base sets" or something like that to make the
> purpose clearer
>
> [...]
>
> > case BPF_FUNC_get_current_uid_gid:
> > return &bpf_get_current_uid_gid_proto;
> > case BPF_FUNC_get_current_comm:
> > return &bpf_get_current_comm_proto;
>
> I'm surprised these two are not part of bpf_base_func_proto, tbh...
> maybe let's move them there while we are cleaning all this up?
Do these make sense in all BPF program types such that they belong in
bpf_base_func_proto? For example, XDP programs don't have a current uid and
gid, do they?
> pw-bot: cr
>
> > - case BPF_FUNC_trace_printk:
> > - return bpf_get_trace_printk_proto();
> > case BPF_FUNC_get_smp_processor_id:
> > return &bpf_get_smp_processor_id_proto;
>
> this one should be cleaned up as well and
> bpf_get_smp_processor_id_proto removed. All BPF programs either
> disable CPU preemption or CPU migration, so bpf_base_func_proto's
> implementation should work just fine (but please do it as a separate
> patch)
>
> > - case BPF_FUNC_get_numa_node_id:
> > - return &bpf_get_numa_node_id_proto;
> > case BPF_FUNC_perf_event_read:
> > return &bpf_perf_event_read_proto;
>
> [...]
>
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3] bpf: Remove redundant checks
2025-04-16 22:44 ` Zvi Effron
@ 2025-04-23 17:05 ` Andrii Nakryiko
0 siblings, 0 replies; 6+ messages in thread
From: Andrii Nakryiko @ 2025-04-23 17:05 UTC (permalink / raw)
To: Zvi Effron
Cc: Feng Yang, ast, daniel, andrii, martin.lau, eddyz87, song,
yonghong.song, john.fastabend, kpsingh, sdf, haoluo, jolsa,
mattbobrowski, rostedt, mhiramat, mathieu.desnoyers, bpf,
linux-kernel, linux-trace-kernel
On Wed, Apr 16, 2025 at 3:44 PM Zvi Effron <zeffron@riotgames.com> wrote:
>
> On Wed, Apr 16, 2025 at 2:56 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Thu, Apr 10, 2025 at 12:03 AM Feng Yang <yangfeng59949@163.com> wrote:
> > >
> > > From: Feng Yang <yangfeng@kylinos.cn>
> > >
> > > Many conditional checks in switch-case are redundant
> > > with bpf_base_func_proto and should be removed.
> > >
> > > Signed-off-by: Feng Yang <yangfeng@kylinos.cn>
> > > Acked-by: Song Liu <song@kernel.org>
> > > ---
> > > Changes in v3:
> > > - Only modify patch description information.
> > > - Link to v2: https://lore.kernel.org/all/20250408071151.229329-1-yangfeng59949@163.com/
> > >
> > > Changes in v2:
> > > - Only modify patch description information.
> > > - Link to v1: https://lore.kernel.org/all/20250320032258.116156-1-yangfeng59949@163.com/
> > > ---
> > > kernel/trace/bpf_trace.c | 72 ----------------------------------------
> > > 1 file changed, 72 deletions(-)
> > >
> >
> > All this looks good, I checked that those functions indeed are allowed
> > in bpf_base_func_proto. The only (minor) differences are capabilities,
> > bpf_base_func_proto() correctly guards some of the helpers with
> > CAP_BPF and/or CAP_PERFMON checks, while bpf_tracing_func_proto()
> > doesn't seem to bother (which is either a bug or any tracing prog
> > implies CAP_BPF and CAP_PERFMON, I'm not sure, didn't check).
> >
> > But I think we can take it further and remove even more stuff from
> > bpf_tracing_func_proto and/or add more stuff into bpf_base_func_proto
> > (perhaps as a few patches in a series, so it's easier to review and
> > validate).
> >
> > Basically, except for a few custom implementations that depend on
> > tracing program type (like get_stack and others like that), if
> > something is OK to call from a tracing program it should be ok to call
> > from any program type. And as such it can (should?) be added to
> > bpf_base_func_proto, IMO.
>
> Is this true? Does it make sense? (See below.)
>
> > P.S. I'd name the patch/series as "bpf: streamline allowed helpers
> > between tracing and base sets" or something like that to make the
> > purpose clearer
> >
> > [...]
> >
> > > case BPF_FUNC_get_current_uid_gid:
> > > return &bpf_get_current_uid_gid_proto;
> > > case BPF_FUNC_get_current_comm:
> > > return &bpf_get_current_comm_proto;
> >
> > I'm surprised these two are not part of bpf_base_func_proto, tbh...
> > maybe let's move them there while we are cleaning all this up?
>
> Do these make sense in all BPF program types such that they belong in
> bpf_base_func_proto? For example, XDP programs don't have a current uid and
> gid, do they?
everything in the kernel, whether NMI handler, hardirq, softirq, or
whatnot runs with *some* current task. So in that sense there is
always pid/tgid and uid/gid. It might not be very relevant for XDP
programs, but it's there, and so if we allow to get current pid/tgid,
why not allow the current comm and uid/gid?
>
> > pw-bot: cr
> >
> > > - case BPF_FUNC_trace_printk:
> > > - return bpf_get_trace_printk_proto();
> > > case BPF_FUNC_get_smp_processor_id:
> > > return &bpf_get_smp_processor_id_proto;
> >
> > this one should be cleaned up as well and
> > bpf_get_smp_processor_id_proto removed. All BPF programs either
> > disable CPU preemption or CPU migration, so bpf_base_func_proto's
> > implementation should work just fine (but please do it as a separate
> > patch)
> >
> > > - case BPF_FUNC_get_numa_node_id:
> > > - return &bpf_get_numa_node_id_proto;
> > > case BPF_FUNC_perf_event_read:
> > > return &bpf_perf_event_read_proto;
> >
> > [...]
> >
^ permalink raw reply [flat|nested] 6+ messages in thread
* Re: [PATCH bpf-next v3] bpf: Remove redundant checks
2025-04-16 21:55 ` Andrii Nakryiko
2025-04-16 22:44 ` Zvi Effron
@ 2025-04-17 9:52 ` Feng Yang
2025-04-17 13:36 ` Feng Yang
2 siblings, 0 replies; 6+ messages in thread
From: Feng Yang @ 2025-04-17 9:52 UTC (permalink / raw)
To: andrii.nakryiko
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, linux-trace-kernel, martin.lau,
mathieu.desnoyers, mattbobrowski, mhiramat, rostedt, sdf, song,
yangfeng59949, yonghong.song
On Wed, 16 Apr 2025 14:55:43 -0700, Andrii Nakryiko <andrii.nakryiko@gmail.com> wrote:
[......]
> I'm surprised these two are not part of bpf_base_func_proto, tbh...
> maybe let's move them there while we are cleaning all this up?
>
> pw-bot: cr
>
> > - case BPF_FUNC_trace_printk:
> > - return bpf_get_trace_printk_proto();
> > case BPF_FUNC_get_smp_processor_id:
> > return &bpf_get_smp_processor_id_proto;
>
> this one should be cleaned up as well and
> bpf_get_smp_processor_id_proto removed. All BPF programs either
> disable CPU preemption or CPU migration, so bpf_base_func_proto's
> implementation should work just fine (but please do it as a separate
> patch)
>
BPF_CALL_0(bpf_get_smp_processor_id)
{
return smp_processor_id();
}
const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
.func = bpf_get_smp_processor_id,
.gpl_only = false,
.ret_type = RET_INTEGER,
.allow_fastcall = true,
};
When attempting to remove bpf_get_smp_processor_id_proto,
it was observed that bpf_get_smp_processor_id is extensively used.
Should we also replace all instances of bpf_get_smp_processor_id with bpf_get_raw_cpu_id in these cases?
For example:
#define ___BPF_FUNC_MAPPER(FN, ctx...) \
......
FN(get_smp_processor_id, 8, ##ctx) \
samples/bpf/sockex3_kern.c:
static struct globals *this_cpu_globals(void)
{
u32 key = bpf_get_smp_processor_id();
return bpf_map_lookup_elem(&percpu_map, &key);
}
and so on......
Thanks.
> > - case BPF_FUNC_get_numa_node_id:
> > - return &bpf_get_numa_node_id_proto;
> > case BPF_FUNC_perf_event_read:
> > return &bpf_perf_event_read_proto;
^ permalink raw reply [flat|nested] 6+ messages in thread* Re: [PATCH bpf-next v3] bpf: Remove redundant checks
2025-04-16 21:55 ` Andrii Nakryiko
2025-04-16 22:44 ` Zvi Effron
2025-04-17 9:52 ` Feng Yang
@ 2025-04-17 13:36 ` Feng Yang
2 siblings, 0 replies; 6+ messages in thread
From: Feng Yang @ 2025-04-17 13:36 UTC (permalink / raw)
To: andrii.nakryiko
Cc: andrii, ast, bpf, daniel, eddyz87, haoluo, john.fastabend, jolsa,
kpsingh, linux-kernel, linux-trace-kernel, martin.lau,
mathieu.desnoyers, mattbobrowski, mhiramat, rostedt, sdf, song,
yangfeng59949, yonghong.song
On Thu, 17 Apr 2025 17:52:45 +0800 Feng Yang wrote:
> > > case BPF_FUNC_get_smp_processor_id:
> > > return &bpf_get_smp_processor_id_proto;
> >
> > this one should be cleaned up as well and
> > bpf_get_smp_processor_id_proto removed. All BPF programs either
> > disable CPU preemption or CPU migration, so bpf_base_func_proto's
> > implementation should work just fine (but please do it as a separate
> > patch)
> >
> BPF_CALL_0(bpf_get_smp_processor_id)
> {
> return smp_processor_id();
> }
> const struct bpf_func_proto bpf_get_smp_processor_id_proto = {
> .func = bpf_get_smp_processor_id,
> .gpl_only = false,
> .ret_type = RET_INTEGER,
> .allow_fastcall = true,
> };
> When attempting to remove bpf_get_smp_processor_id_proto,
> it was observed that bpf_get_smp_processor_id is extensively used.
> Should we also replace all instances of bpf_get_smp_processor_id with bpf_get_raw_cpu_id in these cases?
>
> For example:
> #define ___BPF_FUNC_MAPPER(FN, ctx...) \
> ......
> FN(get_smp_processor_id, 8, ##ctx) \
> samples/bpf/sockex3_kern.c:
> static struct globals *this_cpu_globals(void)
> {
> u32 key = bpf_get_smp_processor_id();
> return bpf_map_lookup_elem(&percpu_map, &key);
> }
> and so on......
> Thanks.
I think I understand the issue now: removing this bpf_get_smp_processor_id has no impact.
For the code:
case BPF_FUNC_get_smp_processor_id:
return &bpf_get_raw_smp_processor_id_proto;
This configuration allows bpf_get_smp_processor_id to actually invoke the bpf_get_raw_smp_processor_id_proto function implementation.
Thanks.
^ permalink raw reply [flat|nested] 6+ messages in thread