Re: [PATCH v3 bpf-next 12/13] selftests/bpf: Additional test for CO-RE in the kernel.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Nov 19, 2021 at 7:33 PM Alexei Starovoitov
<alexei.starovoitov@xxxxxxxxx> wrote:
>
> From: Alexei Starovoitov <ast@xxxxxxxxxx>
>
> Additional test where randmap() function is appended to three different bpf
> programs. That action checks struct bpf_core_relo replication logic and offset

typo: replication -> relocation?

> adjustment.
>
> Signed-off-by: Alexei Starovoitov <ast@xxxxxxxxxx>
> ---
>  tools/testing/selftests/bpf/Makefile          |  2 +-
>  .../selftests/bpf/prog_tests/core_kern.c      | 14 +++++
>  tools/testing/selftests/bpf/progs/core_kern.c | 60 +++++++++++++++++++
>  3 files changed, 75 insertions(+), 1 deletion(-)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/core_kern.c
>  create mode 100644 tools/testing/selftests/bpf/progs/core_kern.c
>
> diff --git a/tools/testing/selftests/bpf/Makefile b/tools/testing/selftests/bpf/Makefile
> index 4fd040f5944b..139d7e5e0a5f 100644
> --- a/tools/testing/selftests/bpf/Makefile
> +++ b/tools/testing/selftests/bpf/Makefile
> @@ -326,7 +326,7 @@ LINKED_SKELS := test_static_linked.skel.h linked_funcs.skel.h               \
>
>  LSKELS := kfunc_call_test.c fentry_test.c fexit_test.c fexit_sleep.c \
>         test_ringbuf.c atomics.c trace_printk.c trace_vprintk.c \
> -       map_ptr_kern.c
> +       map_ptr_kern.c core_kern.c
>  # Generate both light skeleton and libbpf skeleton for these
>  LSKELS_EXTRA := test_ksyms_module.c test_ksyms_weak.c kfunc_call_test_subprog.c
>  SKEL_BLACKLIST += $$(LSKELS)
> diff --git a/tools/testing/selftests/bpf/prog_tests/core_kern.c b/tools/testing/selftests/bpf/prog_tests/core_kern.c
> new file mode 100644
> index 000000000000..561c5185d886
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/core_kern.c
> @@ -0,0 +1,14 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2021 Facebook */
> +
> +#include "test_progs.h"
> +#include "core_kern.lskel.h"
> +
> +void test_core_kern_lskel(void)
> +{
> +       struct core_kern_lskel *skel;
> +
> +       skel = core_kern_lskel__open_and_load();
> +       ASSERT_OK_PTR(skel, "open_and_load");
> +       core_kern_lskel__destroy(skel);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/core_kern.c b/tools/testing/selftests/bpf/progs/core_kern.c
> new file mode 100644
> index 000000000000..3b4571d68369
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/core_kern.c
> @@ -0,0 +1,60 @@
> +// SPDX-License-Identifier: GPL-2.0
> +/* Copyright (c) 2021 Facebook */
> +#include "vmlinux.h"
> +
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +struct {
> +       __uint(type, BPF_MAP_TYPE_ARRAY);
> +       __type(key, u32);
> +       __type(value, u32);
> +       __uint(max_entries, 256);
> +} array1 SEC(".maps");
> +
> +struct {
> +       __uint(type, BPF_MAP_TYPE_ARRAY);
> +       __type(key, u32);
> +       __type(value, u32);
> +       __uint(max_entries, 256);
> +} array2 SEC(".maps");
> +
> +int randmap(int v)
> +{
> +       struct bpf_map *map = (struct bpf_map *)&array1;
> +       int key = bpf_get_prandom_u32() & 0xff;
> +       int *val;
> +
> +       if (bpf_get_prandom_u32() & 1)
> +               map = (struct bpf_map *)&array2;
> +
> +       val = bpf_map_lookup_elem(map, &key);
> +       if (val)
> +               *val = bpf_get_prandom_u32() + v;
> +
> +       return 0;

If I understand correctly the intent, this function should have had
some CO-RE relocations, no? So that after its code is appended to the
three entry-level progs below CO-RE relocations are performed
correctly, right? But as far as I can see, this function doesn't do
any CO-RE relocations or am I missing something? If it was accessing
map->type or something along those lines (probably through
BPF_CORE_READ() macro), then it would have CO-RE relocs.

> +}
> +
> +SEC("tp_btf/xdp_devmap_xmit")
> +int BPF_PROG(tp_xdp_devmap_xmit_multi, const struct net_device
> +            *from_dev, const struct net_device *to_dev, int sent, int drops,
> +            int err)
> +{
> +       return randmap(from_dev->ifindex);
> +}
> +
> +SEC("fentry/eth_type_trans")
> +int BPF_PROG(fentry_eth_type_trans, struct sk_buff *skb,
> +            struct net_device *dev, unsigned short protocol)
> +{
> +       return randmap(dev->ifindex + skb->len);
> +}
> +
> +SEC("fexit/eth_type_trans")
> +int BPF_PROG(fexit_eth_type_trans, struct sk_buff *skb,
> +            struct net_device *dev, unsigned short protocol)
> +{
> +       return randmap(dev->ifindex + skb->len);
> +}
> +
> +char LICENSE[] SEC("license") = "GPL";
> --
> 2.30.2
>



[Index of Archives]     [Linux Samsung SoC]     [Linux Rockchip SoC]     [Linux Actions SoC]     [Linux for Synopsys ARC Processors]     [Linux NFS]     [Linux NILFS]     [Linux USB Devel]     [Video for Linux]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]


  Powered by Linux