lists.openwall.net   lists  /  announce  owl-users  owl-dev  john-users  john-dev  passwdqc-users  yescrypt  popa3d-users  /  oss-security  kernel-hardening  musl  sabotage  tlsify  passwords  /  crypt-dev  xvendor  /  Bugtraq  Full-Disclosure  linux-kernel  linux-netdev  linux-ext4  linux-hardening  linux-cve-announce  PHC 
Open Source and information security mailing list archives
 
Hash Suite: Windows password security audit tool. GUI, reports in PDF.
[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Date:   Mon, 4 Dec 2023 14:14:46 -0800
From:   Andrii Nakryiko <andrii.nakryiko@...il.com>
To:     Kyle Huey <me@...ehuey.com>
Cc:     Kyle Huey <khuey@...ehuey.com>, linux-kernel@...r.kernel.org,
        "Robert O'Callahan" <robert@...llahan.org>,
        Andrii Nakryiko <andrii@...nel.org>,
        Mykola Lysenko <mykolal@...com>,
        Alexei Starovoitov <ast@...nel.org>,
        Daniel Borkmann <daniel@...earbox.net>,
        Martin KaFai Lau <martin.lau@...ux.dev>,
        Song Liu <song@...nel.org>,
        Yonghong Song <yonghong.song@...ux.dev>,
        John Fastabend <john.fastabend@...il.com>,
        KP Singh <kpsingh@...nel.org>,
        Stanislav Fomichev <sdf@...gle.com>,
        Hao Luo <haoluo@...gle.com>, Jiri Olsa <jolsa@...nel.org>,
        Shuah Khan <shuah@...nel.org>, bpf@...r.kernel.org,
        linux-kselftest@...r.kernel.org
Subject: Re: [PATCH 2/2] selftest/bpf: Test returning zero from a perf bpf
 program suppresses SIGIO.

On Mon, Dec 4, 2023 at 12:14 PM Kyle Huey <me@...ehuey.com> wrote:
>
> The test sets a hardware breakpoint and uses a bpf program to suppress the
> I/O availability signal if the ip matches the expected value.
>
> Signed-off-by: Kyle Huey <khuey@...ehuey.com>
> ---
>  .../selftests/bpf/prog_tests/perf_skip.c      | 95 +++++++++++++++++++
>  .../selftests/bpf/progs/test_perf_skip.c      | 23 +++++
>  2 files changed, 118 insertions(+)
>  create mode 100644 tools/testing/selftests/bpf/prog_tests/perf_skip.c
>  create mode 100644 tools/testing/selftests/bpf/progs/test_perf_skip.c
>
> diff --git a/tools/testing/selftests/bpf/prog_tests/perf_skip.c b/tools/testing/selftests/bpf/prog_tests/perf_skip.c
> new file mode 100644
> index 000000000000..b269a31669b7
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/prog_tests/perf_skip.c
> @@ -0,0 +1,95 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#define _GNU_SOURCE
> +#include <test_progs.h>
> +#include "test_perf_skip.skel.h"
> +#include <linux/hw_breakpoint.h>
> +#include <sys/mman.h>
> +
> +#define BPF_OBJECT            "test_perf_skip.bpf.o"

leftover?

> +
> +static void handle_sig(int)
> +{
> +       ASSERT_OK(1, "perf event not skipped");
> +}
> +
> +static noinline int test_function(void)
> +{

please add

asm volatile ("");

here to prevent compiler from actually inlining at the call site

> +       return 0;
> +}
> +
> +void serial_test_perf_skip(void)
> +{
> +       sighandler_t previous;
> +       int duration = 0;
> +       struct test_perf_skip *skel = NULL;
> +       int map_fd = -1;
> +       long page_size = sysconf(_SC_PAGE_SIZE);
> +       uintptr_t *ip = NULL;
> +       int prog_fd = -1;
> +       struct perf_event_attr attr = {0};
> +       int perf_fd = -1;
> +       struct f_owner_ex owner;
> +       int err;
> +
> +       previous = signal(SIGIO, handle_sig);
> +
> +       skel = test_perf_skip__open_and_load();
> +       if (!ASSERT_OK_PTR(skel, "skel_load"))
> +               goto cleanup;
> +
> +       prog_fd = bpf_program__fd(skel->progs.handler);
> +       if (!ASSERT_OK(prog_fd < 0, "bpf_program__fd"))
> +               goto cleanup;
> +
> +       map_fd = bpf_map__fd(skel->maps.ip);
> +       if (!ASSERT_OK(map_fd < 0, "bpf_map__fd"))
> +               goto cleanup;
> +
> +       ip = mmap(NULL, page_size, PROT_READ | PROT_WRITE, MAP_SHARED, map_fd, 0);
> +       if (!ASSERT_OK_PTR(ip, "mmap bpf map"))
> +               goto cleanup;
> +
> +       *ip = (uintptr_t)test_function;
> +
> +       attr.type = PERF_TYPE_BREAKPOINT;
> +       attr.size = sizeof(attr);
> +       attr.bp_type = HW_BREAKPOINT_X;
> +       attr.bp_addr = (uintptr_t)test_function;
> +       attr.bp_len = sizeof(long);
> +       attr.sample_period = 1;
> +       attr.sample_type = PERF_SAMPLE_IP;
> +       attr.pinned = 1;
> +       attr.exclude_kernel = 1;
> +       attr.exclude_hv = 1;
> +       attr.precise_ip = 3;
> +
> +       perf_fd = syscall(__NR_perf_event_open, &attr, 0, -1, -1, 0);
> +       if (CHECK(perf_fd < 0, "perf_event_open", "err %d\n", perf_fd))

please don't use CHECK() macro, stick to ASSERT_xxx()

also, we are going to run all this on different hardware and VMs, see
how we skip tests if hardware support is not there. See test__skip
usage in prog_tests/perf_branches.c, as one example

> +               goto cleanup;
> +
> +       err = fcntl(perf_fd, F_SETFL, O_ASYNC);

I assume this is what will send SIGIO, right? Can you add a small
comment explicitly saying this?

> +       if (!ASSERT_OK(err, "fcntl(F_SETFL, O_ASYNC)"))
> +               goto cleanup;
> +
> +       owner.type = F_OWNER_TID;
> +       owner.pid = gettid();
> +       err = fcntl(perf_fd, F_SETOWN_EX, &owner);
> +       if (!ASSERT_OK(err, "fcntl(F_SETOWN_EX)"))
> +               goto cleanup;
> +
> +       err = ioctl(perf_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
> +       if (!ASSERT_OK(err, "ioctl(PERF_EVENT_IOC_SET_BPF)"))
> +               goto cleanup;

we have a better way to do this, please use
bpf_program__attach_perf_event() instead

> +
> +       test_function();
> +
> +cleanup:
> +       if (perf_fd >= 0)
> +               close(perf_fd);
> +       if (ip)
> +               munmap(ip, page_size);
> +       if (skel)
> +               test_perf_skip__destroy(skel);
> +
> +       signal(SIGIO, previous);
> +}
> diff --git a/tools/testing/selftests/bpf/progs/test_perf_skip.c b/tools/testing/selftests/bpf/progs/test_perf_skip.c
> new file mode 100644
> index 000000000000..ef01a9161afe
> --- /dev/null
> +++ b/tools/testing/selftests/bpf/progs/test_perf_skip.c
> @@ -0,0 +1,23 @@
> +// SPDX-License-Identifier: GPL-2.0
> +#include "vmlinux.h"
> +#include <bpf/bpf_helpers.h>
> +#include <bpf/bpf_tracing.h>
> +
> +struct {
> +       __uint(type, BPF_MAP_TYPE_ARRAY);
> +       __uint(max_entries, 1);
> +       __uint(map_flags, BPF_F_MMAPABLE);
> +       __type(key, uint32_t);
> +       __type(value, uintptr_t);
> +} ip SEC(".maps");

please use global variable:

__u64 ip;

and then access it from user-space side through skeleton

skel->bss.ip = &test_function;

> +
> +SEC("perf_event")
> +int handler(struct bpf_perf_event_data *data)
> +{
> +       const uint32_t index = 0;
> +       uintptr_t *v = bpf_map_lookup_elem(&ip, &index);
> +
> +       return !(v && *v == PT_REGS_IP(&data->regs));

and so we the above global var suggestion this will be just:

return ip == PT_REGS_IP(&data->regs);

> +}
> +
> +char _license[] SEC("license") = "GPL";
> --
> 2.34.1
>

Powered by blists - more mailing lists

Powered by Openwall GNU/*/Linux Powered by OpenVZ