bpf.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Problem with BPF_CORE_READ macro function
@ 2020-12-03 12:19 David Marcinkovic
  2020-12-03 20:35 ` Andrii Nakryiko
  0 siblings, 1 reply; 5+ messages in thread
From: David Marcinkovic @ 2020-12-03 12:19 UTC (permalink / raw)
  To: bpf; +Cc: Juraj Vijtiuk, Luka Perkov, Andrii Nakryiko

Hello everyone,

I am trying to run a simple BPF program that hooks onto
`mac80211/drv_sta_state` tracepoint. When I run the program on the arm
32 bit architecture,
the verifier rejects to load the program and outputs the following error
message:

Unrecognized arg#0 type PTR
; int tp__mac80211_drv_sta_state(struct trace_event_raw_drv_sta_state* ctx)
0: (bf) r3 = r1
1: (85) call unknown#195896080
invalid func unknown#195896080

This error does not seem to occur on the amd64 architecture. I am
using clang version 10 for both, compiling on amd64 and
cross-compiling for arm32.


I have prepared a simple program that hooks onto the
`mac80211/drv_sta_state` tracepoint.
In this example, `BPF_CORE_READ` macro function seems to cause the
verifier to reject to load the program.
I've been using this macro in various different programs and it didn't
cause any problems.
Also, I've been using packed structs and bit fields in other programs
and they also didn't cause any problems.

I tried to use BPF_CORE_READ_BITFIELD as stated in this patch [0] and
got a similar error.

Any input is much appreciated,

Best regards,
David Marčinković


[0] https://lore.kernel.org/bpf/20201007202946.3684483-1-andrii@kernel.org/T/#ma08db511daa0b5978f16df9f98f4ef644b83fc96


mac80211.c
-------------------

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>

#include <bpf/libbpf.h>
#include <bpf/bpf.h>
#include "mac80211.skel.h"

void read_trace_pipe(void)
{
int trace_fd;

trace_fd = open("/sys/kernel/debug/tracing/trace_pipe", O_RDONLY, 0);
if (trace_fd < 0)
return;

while (1) {
static char buf[4096];
ssize_t sz;

sz = read(trace_fd, buf, sizeof(buf) - 1);
if (sz > 0) {
buf[sz] = 0;
puts(buf);
}
}
}

int main(int argc, char **argv)
{
struct mac80211_bpf *obj;
int err = 0;

obj = mac80211_bpf__open();
if (!obj) {
fprintf(stderr, "failed to open and/or load BPF object\n");
return 1;
}

err = mac80211_bpf__load(obj);
if (err) {
fprintf(stderr, "failed to load BPF object %d\n", err);
goto cleanup;
}

err = mac80211_bpf__attach(obj);
if (err) {
fprintf(stderr, "failed to attach BPF programs\n");
goto cleanup;
}

read_trace_pipe();

cleanup:
mac80211_bpf__destroy(obj);
return err != 0;
}



mac80211.bpf.c
-------------------

#include "vmlinux.h"

#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>


SEC("tracepoint/mac80211/drv_sta_state")
int tp__mac80211_drv_sta_state(struct trace_event_raw_drv_sta_state* ctx)
{
        u32 old_state = BPF_CORE_READ(ctx, old_state);

        bpf_printk("Old state is %d\n", old_state);

        return 0;
}

char LICENSE[] SEC("license") = "GPL";

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with BPF_CORE_READ macro function
  2020-12-03 12:19 Problem with BPF_CORE_READ macro function David Marcinkovic
@ 2020-12-03 20:35 ` Andrii Nakryiko
  2020-12-04 14:23   ` David Marcinkovic
  0 siblings, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2020-12-03 20:35 UTC (permalink / raw)
  To: David Marcinkovic; +Cc: bpf, Juraj Vijtiuk, Luka Perkov

On Thu, Dec 3, 2020 at 4:20 AM David Marcinkovic
<david.marcinkovic@sartura.hr> wrote:
>
> Hello everyone,
>
> I am trying to run a simple BPF program that hooks onto
> `mac80211/drv_sta_state` tracepoint. When I run the program on the arm
> 32 bit architecture,
> the verifier rejects to load the program and outputs the following error
> message:
>
> Unrecognized arg#0 type PTR
> ; int tp__mac80211_drv_sta_state(struct trace_event_raw_drv_sta_state* ctx)
> 0: (bf) r3 = r1
> 1: (85) call unknown#195896080
> invalid func unknown#195896080
>
> This error does not seem to occur on the amd64 architecture. I am
> using clang version 10 for both, compiling on amd64 and
> cross-compiling for arm32.
>
>
> I have prepared a simple program that hooks onto the
> `mac80211/drv_sta_state` tracepoint.
> In this example, `BPF_CORE_READ` macro function seems to cause the
> verifier to reject to load the program.
> I've been using this macro in various different programs and it didn't
> cause any problems.
> Also, I've been using packed structs and bit fields in other programs
> and they also didn't cause any problems.
>
> I tried to use BPF_CORE_READ_BITFIELD as stated in this patch [0] and
> got a similar error.
>
> Any input is much appreciated,
>

Can you provide libbpf debug output, especially the section about
CO-RE relocations? Could it be that this tracepoint is inside the
kernel module?

> Best regards,
> David Marčinković
>
>
> [0] https://lore.kernel.org/bpf/20201007202946.3684483-1-andrii@kernel.org/T/#ma08db511daa0b5978f16df9f98f4ef644b83fc96
>
>

[...]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with BPF_CORE_READ macro function
  2020-12-03 20:35 ` Andrii Nakryiko
@ 2020-12-04 14:23   ` David Marcinkovic
  2020-12-04 18:01     ` Andrii Nakryiko
  0 siblings, 1 reply; 5+ messages in thread
From: David Marcinkovic @ 2020-12-04 14:23 UTC (permalink / raw)
  To: Andrii Nakryiko; +Cc: bpf, Juraj Vijtiuk, Luka Perkov

On Thu, Dec 3, 2020 at 9:35 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Thu, Dec 3, 2020 at 4:20 AM David Marcinkovic
> <david.marcinkovic@sartura.hr> wrote:
> >
> > Hello everyone,
> >
> > I am trying to run a simple BPF program that hooks onto
> > `mac80211/drv_sta_state` tracepoint. When I run the program on the arm
> > 32 bit architecture,
> > the verifier rejects to load the program and outputs the following error
> > message:
> >
> > Unrecognized arg#0 type PTR
> > ; int tp__mac80211_drv_sta_state(struct trace_event_raw_drv_sta_state* ctx)
> > 0: (bf) r3 = r1
> > 1: (85) call unknown#195896080
> > invalid func unknown#195896080
> >
> > This error does not seem to occur on the amd64 architecture. I am
> > using clang version 10 for both, compiling on amd64 and
> > cross-compiling for arm32.
> >
> >
> > I have prepared a simple program that hooks onto the
> > `mac80211/drv_sta_state` tracepoint.
> > In this example, `BPF_CORE_READ` macro function seems to cause the
> > verifier to reject to load the program.
> > I've been using this macro in various different programs and it didn't
> > cause any problems.
> > Also, I've been using packed structs and bit fields in other programs
> > and they also didn't cause any problems.
> >
> > I tried to use BPF_CORE_READ_BITFIELD as stated in this patch [0] and
> > got a similar error.
> >
> > Any input is much appreciated,
> >
>
> Can you provide libbpf debug output, especially the section about
> CO-RE relocations? Could it be that this tracepoint is inside the
> kernel module?

You're right. The problem is that this tracepoint is inside the kernel module.
I recompiled the kernel with CONFIG_MAC80211 flag set to 'y' and the
program loads
successfully.

Thank you very much for your fast reply.

>
> > Best regards,
> > David Marčinković
> >
> >
> > [0] https://lore.kernel.org/bpf/20201007202946.3684483-1-andrii@kernel.org/T/#ma08db511daa0b5978f16df9f98f4ef644b83fc96
> >
> >
>
> [...]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with BPF_CORE_READ macro function
  2020-12-04 14:23   ` David Marcinkovic
@ 2020-12-04 18:01     ` Andrii Nakryiko
  2020-12-06 20:56       ` David Marcinkovic
  0 siblings, 1 reply; 5+ messages in thread
From: Andrii Nakryiko @ 2020-12-04 18:01 UTC (permalink / raw)
  To: David Marcinkovic; +Cc: bpf, Juraj Vijtiuk, Luka Perkov

On Fri, Dec 4, 2020 at 6:23 AM David Marcinkovic
<david.marcinkovic@sartura.hr> wrote:
>
> On Thu, Dec 3, 2020 at 9:35 PM Andrii Nakryiko
> <andrii.nakryiko@gmail.com> wrote:
> >
> > On Thu, Dec 3, 2020 at 4:20 AM David Marcinkovic
> > <david.marcinkovic@sartura.hr> wrote:
> > >
> > > Hello everyone,
> > >
> > > I am trying to run a simple BPF program that hooks onto
> > > `mac80211/drv_sta_state` tracepoint. When I run the program on the arm
> > > 32 bit architecture,
> > > the verifier rejects to load the program and outputs the following error
> > > message:
> > >
> > > Unrecognized arg#0 type PTR
> > > ; int tp__mac80211_drv_sta_state(struct trace_event_raw_drv_sta_state* ctx)
> > > 0: (bf) r3 = r1
> > > 1: (85) call unknown#195896080
> > > invalid func unknown#195896080
> > >
> > > This error does not seem to occur on the amd64 architecture. I am
> > > using clang version 10 for both, compiling on amd64 and
> > > cross-compiling for arm32.
> > >
> > >
> > > I have prepared a simple program that hooks onto the
> > > `mac80211/drv_sta_state` tracepoint.
> > > In this example, `BPF_CORE_READ` macro function seems to cause the
> > > verifier to reject to load the program.
> > > I've been using this macro in various different programs and it didn't
> > > cause any problems.
> > > Also, I've been using packed structs and bit fields in other programs
> > > and they also didn't cause any problems.
> > >
> > > I tried to use BPF_CORE_READ_BITFIELD as stated in this patch [0] and
> > > got a similar error.
> > >
> > > Any input is much appreciated,
> > >
> >
> > Can you provide libbpf debug output, especially the section about
> > CO-RE relocations? Could it be that this tracepoint is inside the
> > kernel module?
>
> You're right. The problem is that this tracepoint is inside the kernel module.
> I recompiled the kernel with CONFIG_MAC80211 flag set to 'y' and the
> program loads
> successfully.
>

Ok, just as I suspected. With [0] and [1] (and using pahole 1.19+ to
build the kernel and modules), it should work even for modules now.

[0] https://patchwork.kernel.org/project/netdevbpf/list/?series=395715&state=*
[1] https://patchwork.kernel.org/project/netdevbpf/list/?series=380759&state=*

> Thank you very much for your fast reply.
>
> >
> > > Best regards,
> > > David Marčinković
> > >
> > >
> > > [0] https://lore.kernel.org/bpf/20201007202946.3684483-1-andrii@kernel.org/T/#ma08db511daa0b5978f16df9f98f4ef644b83fc96
> > >
> > >
> >
> > [...]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: Problem with BPF_CORE_READ macro function
  2020-12-04 18:01     ` Andrii Nakryiko
@ 2020-12-06 20:56       ` David Marcinkovic
  0 siblings, 0 replies; 5+ messages in thread
From: David Marcinkovic @ 2020-12-06 20:56 UTC (permalink / raw)
  To: Andrii Nakryiko; +Cc: bpf, Juraj Vijtiuk, Luka Perkov

On Fri, Dec 4, 2020 at 7:01 PM Andrii Nakryiko
<andrii.nakryiko@gmail.com> wrote:
>
> On Fri, Dec 4, 2020 at 6:23 AM David Marcinkovic
> <david.marcinkovic@sartura.hr> wrote:
> >
> > On Thu, Dec 3, 2020 at 9:35 PM Andrii Nakryiko
> > <andrii.nakryiko@gmail.com> wrote:
> > >
> > > On Thu, Dec 3, 2020 at 4:20 AM David Marcinkovic
> > > <david.marcinkovic@sartura.hr> wrote:
> > > >
> > > > Hello everyone,
> > > >
> > > > I am trying to run a simple BPF program that hooks onto
> > > > `mac80211/drv_sta_state` tracepoint. When I run the program on the arm
> > > > 32 bit architecture,
> > > > the verifier rejects to load the program and outputs the following error
> > > > message:
> > > >
> > > > Unrecognized arg#0 type PTR
> > > > ; int tp__mac80211_drv_sta_state(struct trace_event_raw_drv_sta_state* ctx)
> > > > 0: (bf) r3 = r1
> > > > 1: (85) call unknown#195896080
> > > > invalid func unknown#195896080
> > > >
> > > > This error does not seem to occur on the amd64 architecture. I am
> > > > using clang version 10 for both, compiling on amd64 and
> > > > cross-compiling for arm32.
> > > >
> > > >
> > > > I have prepared a simple program that hooks onto the
> > > > `mac80211/drv_sta_state` tracepoint.
> > > > In this example, `BPF_CORE_READ` macro function seems to cause the
> > > > verifier to reject to load the program.
> > > > I've been using this macro in various different programs and it didn't
> > > > cause any problems.
> > > > Also, I've been using packed structs and bit fields in other programs
> > > > and they also didn't cause any problems.
> > > >
> > > > I tried to use BPF_CORE_READ_BITFIELD as stated in this patch [0] and
> > > > got a similar error.
> > > >
> > > > Any input is much appreciated,
> > > >
> > >
> > > Can you provide libbpf debug output, especially the section about
> > > CO-RE relocations? Could it be that this tracepoint is inside the
> > > kernel module?
> >
> > You're right. The problem is that this tracepoint is inside the kernel module.
> > I recompiled the kernel with CONFIG_MAC80211 flag set to 'y' and the
> > program loads
> > successfully.
> >
>
> Ok, just as I suspected. With [0] and [1] (and using pahole 1.19+ to
> build the kernel and modules), it should work even for modules now.
>
> [0] https://patchwork.kernel.org/project/netdevbpf/list/?series=395715&state=*
> [1] https://patchwork.kernel.org/project/netdevbpf/list/?series=380759&state=*

Thank you for your help. We really appreciate your quick responses.

Unfortunately, we are not able to test those patches right now as we
have quite a few out of tree patches for our board.
We will definitely test them once they make their way into a stable
release or release candidate.

King regards,
David Marčinković

>
> > Thank you very much for your fast reply.
> >
> > >
> > > > Best regards,
> > > > David Marčinković
> > > >
> > > >
> > > > [0] https://lore.kernel.org/bpf/20201007202946.3684483-1-andrii@kernel.org/T/#ma08db511daa0b5978f16df9f98f4ef644b83fc96
> > > >
> > > >
> > >
> > > [...]

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-12-06 20:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-03 12:19 Problem with BPF_CORE_READ macro function David Marcinkovic
2020-12-03 20:35 ` Andrii Nakryiko
2020-12-04 14:23   ` David Marcinkovic
2020-12-04 18:01     ` Andrii Nakryiko
2020-12-06 20:56       ` David Marcinkovic

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