From: David Vernet <void@manifault.com>
To: Jiri Olsa <jolsa@kernel.org>
Cc: Alexei Starovoitov <ast@kernel.org>,
Daniel Borkmann <daniel@iogearbox.net>,
Andrii Nakryiko <andrii@kernel.org>,
bpf@vger.kernel.org, Martin KaFai Lau <kafai@fb.com>,
Song Liu <songliubraving@fb.com>, Yonghong Song <yhs@fb.com>,
John Fastabend <john.fastabend@gmail.com>,
KP Singh <kpsingh@chromium.org>,
Stanislav Fomichev <sdf@google.com>, Hao Luo <haoluo@google.com>,
Kumar Kartikeya Dwivedi <memxor@gmail.com>,
Artem Savkov <asavkov@redhat.com>
Subject: Re: [PATCHv3 bpf-next 2/9] selftests/bpf: Move test_progs helpers to testing_helpers object
Date: Tue, 7 Feb 2023 08:38:09 -0600 [thread overview]
Message-ID: <Y+JiUQFyalc0aV6M@maniforge.lan> (raw)
In-Reply-To: <20230203162336.608323-3-jolsa@kernel.org>
On Fri, Feb 03, 2023 at 05:23:29PM +0100, Jiri Olsa wrote:
> Moving test_progs helpers to testing_helpers object so they can be
> used from test_verifier in following changes.
>
> Also adding missing ifndef header guard to testing_helpers.h header.
>
> Using stderr instead of env.stderr because un/load_bpf_testmod helpers
> will be used outside test_progs. Also at the point of calling them
> in test_progs the std files are not hijacked yet and stderr is the
> same as env.stderr.
Makes sense. Possibly something to clean up at another time but given
that we were being inconsistent with env.stdout and env.stderr in
load_bpf_testmod() in the first place, this seems totally fine.
Acked-by: David Vernet <void@manifault.com>
Left one question about kern_sync_rcu() below that need not block this
patch series, and can be addressed in a follow-up if it's even relevant.
>
> Signed-off-by: Jiri Olsa <jolsa@kernel.org>
> ---
> tools/testing/selftests/bpf/test_progs.c | 67 +------------------
> tools/testing/selftests/bpf/test_progs.h | 1 -
> tools/testing/selftests/bpf/testing_helpers.c | 63 +++++++++++++++++
> tools/testing/selftests/bpf/testing_helpers.h | 10 +++
> 4 files changed, 75 insertions(+), 66 deletions(-)
>
> diff --git a/tools/testing/selftests/bpf/test_progs.c b/tools/testing/selftests/bpf/test_progs.c
> index 6d5e3022c75f..39ceb6a1bfc6 100644
> --- a/tools/testing/selftests/bpf/test_progs.c
> +++ b/tools/testing/selftests/bpf/test_progs.c
> @@ -11,7 +11,6 @@
> #include <signal.h>
> #include <string.h>
> #include <execinfo.h> /* backtrace */
> -#include <linux/membarrier.h>
> #include <sys/sysinfo.h> /* get_nprocs */
> #include <netinet/in.h>
> #include <sys/select.h>
> @@ -616,68 +615,6 @@ int extract_build_id(char *build_id, size_t size)
> return -1;
> }
>
> -static int finit_module(int fd, const char *param_values, int flags)
> -{
> - return syscall(__NR_finit_module, fd, param_values, flags);
> -}
> -
> -static int delete_module(const char *name, int flags)
> -{
> - return syscall(__NR_delete_module, name, flags);
> -}
> -
> -/*
> - * Trigger synchronize_rcu() in kernel.
> - */
> -int kern_sync_rcu(void)
> -{
> - return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
> -}
> -
> -static void unload_bpf_testmod(void)
> -{
> - if (kern_sync_rcu())
> - fprintf(env.stderr, "Failed to trigger kernel-side RCU sync!\n");
> - if (delete_module("bpf_testmod", 0)) {
> - if (errno == ENOENT) {
> - if (verbose())
> - fprintf(stdout, "bpf_testmod.ko is already unloaded.\n");
> - return;
> - }
> - fprintf(env.stderr, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno);
> - return;
> - }
> - if (verbose())
> - fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n");
> -}
> -
> -static int load_bpf_testmod(void)
> -{
> - int fd;
> -
> - /* ensure previous instance of the module is unloaded */
> - unload_bpf_testmod();
> -
> - if (verbose())
> - fprintf(stdout, "Loading bpf_testmod.ko...\n");
> -
> - fd = open("bpf_testmod.ko", O_RDONLY);
> - if (fd < 0) {
> - fprintf(env.stderr, "Can't find bpf_testmod.ko kernel module: %d\n", -errno);
> - return -ENOENT;
> - }
> - if (finit_module(fd, "", 0)) {
> - fprintf(env.stderr, "Failed to load bpf_testmod.ko into the kernel: %d\n", -errno);
> - close(fd);
> - return -EINVAL;
> - }
> - close(fd);
> -
> - if (verbose())
> - fprintf(stdout, "Successfully loaded bpf_testmod.ko.\n");
> - return 0;
> -}
> -
> /* extern declarations for test funcs */
> #define DEFINE_TEST(name) \
> extern void test_##name(void) __weak; \
> @@ -1655,7 +1592,7 @@ int main(int argc, char **argv)
> env.stderr = stderr;
>
> env.has_testmod = true;
> - if (!env.list_test_names && load_bpf_testmod()) {
> + if (!env.list_test_names && load_bpf_testmod(verbose())) {
> fprintf(env.stderr, "WARNING! Selftests relying on bpf_testmod.ko will be skipped.\n");
> env.has_testmod = false;
> }
> @@ -1754,7 +1691,7 @@ int main(int argc, char **argv)
> close(env.saved_netns_fd);
> out:
> if (!env.list_test_names && env.has_testmod)
> - unload_bpf_testmod();
> + unload_bpf_testmod(verbose());
>
> free_test_selector(&env.test_selector);
> free_test_selector(&env.subtest_selector);
> diff --git a/tools/testing/selftests/bpf/test_progs.h b/tools/testing/selftests/bpf/test_progs.h
> index d5d51ec97ec8..b9dac3c32712 100644
> --- a/tools/testing/selftests/bpf/test_progs.h
> +++ b/tools/testing/selftests/bpf/test_progs.h
> @@ -390,7 +390,6 @@ int bpf_find_map(const char *test, struct bpf_object *obj, const char *name);
> int compare_map_keys(int map1_fd, int map2_fd);
> int compare_stack_ips(int smap_fd, int amap_fd, int stack_trace_len);
> int extract_build_id(char *build_id, size_t size);
> -int kern_sync_rcu(void);
> int trigger_module_test_read(int read_sz);
> int trigger_module_test_write(int write_sz);
> int write_sysctl(const char *sysctl, const char *value);
> diff --git a/tools/testing/selftests/bpf/testing_helpers.c b/tools/testing/selftests/bpf/testing_helpers.c
> index 9695318e8132..3a9e7e8e5b14 100644
> --- a/tools/testing/selftests/bpf/testing_helpers.c
> +++ b/tools/testing/selftests/bpf/testing_helpers.c
> @@ -8,6 +8,7 @@
> #include <bpf/libbpf.h>
> #include "test_progs.h"
> #include "testing_helpers.h"
> +#include <linux/membarrier.h>
>
> int parse_num_list(const char *s, bool **num_set, int *num_set_len)
> {
> @@ -229,3 +230,65 @@ int bpf_test_load_program(enum bpf_prog_type type, const struct bpf_insn *insns,
>
> return bpf_prog_load(type, NULL, license, insns, insns_cnt, &opts);
> }
> +
> +static int finit_module(int fd, const char *param_values, int flags)
> +{
> + return syscall(__NR_finit_module, fd, param_values, flags);
> +}
> +
> +static int delete_module(const char *name, int flags)
> +{
> + return syscall(__NR_delete_module, name, flags);
> +}
> +
> +void unload_bpf_testmod(bool verbose)
> +{
> + if (kern_sync_rcu())
> + fprintf(stderr, "Failed to trigger kernel-side RCU sync!\n");
I realize there's no behavior change here, but out of curiosity, do you
know why we need a synchronize_rcu() here? In general this feels kind of
sketchy, and like something we should just put in bpf_testmod_exit() if
it's really needed for something in the kernel.
> + if (delete_module("bpf_testmod", 0)) {
> + if (errno == ENOENT) {
> + if (verbose)
> + fprintf(stdout, "bpf_testmod.ko is already unloaded.\n");
> + return;
> + }
> + fprintf(stderr, "Failed to unload bpf_testmod.ko from kernel: %d\n", -errno);
> + return;
> + }
> + if (verbose)
> + fprintf(stdout, "Successfully unloaded bpf_testmod.ko.\n");
> +}
> +
> +int load_bpf_testmod(bool verbose)
> +{
> + int fd;
> +
> + /* ensure previous instance of the module is unloaded */
> + unload_bpf_testmod(verbose);
> +
> + if (verbose)
> + fprintf(stdout, "Loading bpf_testmod.ko...\n");
> +
> + fd = open("bpf_testmod.ko", O_RDONLY);
> + if (fd < 0) {
> + fprintf(stderr, "Can't find bpf_testmod.ko kernel module: %d\n", -errno);
> + return -ENOENT;
> + }
> + if (finit_module(fd, "", 0)) {
> + fprintf(stderr, "Failed to load bpf_testmod.ko into the kernel: %d\n", -errno);
> + close(fd);
> + return -EINVAL;
> + }
> + close(fd);
> +
> + if (verbose)
> + fprintf(stdout, "Successfully loaded bpf_testmod.ko.\n");
> + return 0;
> +}
> +
> +/*
> + * Trigger synchronize_rcu() in kernel.
> + */
> +int kern_sync_rcu(void)
> +{
> + return syscall(__NR_membarrier, MEMBARRIER_CMD_SHARED, 0, 0);
> +}
> diff --git a/tools/testing/selftests/bpf/testing_helpers.h b/tools/testing/selftests/bpf/testing_helpers.h
> index 6ec00bf79cb5..7356474def27 100644
> --- a/tools/testing/selftests/bpf/testing_helpers.h
> +++ b/tools/testing/selftests/bpf/testing_helpers.h
> @@ -1,5 +1,9 @@
> /* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
> /* Copyright (C) 2020 Facebook, Inc. */
> +
> +#ifndef __TESTING_HELPERS_H
> +#define __TESTING_HELPERS_H
> +
> #include <stdbool.h>
> #include <bpf/bpf.h>
> #include <bpf/libbpf.h>
> @@ -20,3 +24,9 @@ struct test_filter_set;
> int parse_test_list(const char *s,
> struct test_filter_set *test_set,
> bool is_glob_pattern);
> +
> +int load_bpf_testmod(bool verbose);
> +void unload_bpf_testmod(bool verbose);
> +int kern_sync_rcu(void);
> +
> +#endif /* __TESTING_HELPERS_H */
> --
> 2.39.1
>
next prev parent reply other threads:[~2023-02-07 14:40 UTC|newest]
Thread overview: 28+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-02-03 16:23 [PATCHv3 bpf-next 0/9] bpf: Move kernel test kfuncs into bpf_testmod Jiri Olsa
2023-02-03 16:23 ` [PATCHv3 bpf-next 1/9] selftests/bpf: Move kfunc exports to bpf_testmod/bpf_testmod_kfunc.h Jiri Olsa
2023-02-07 14:28 ` David Vernet
2023-02-09 0:20 ` Andrii Nakryiko
2023-02-09 8:45 ` Jiri Olsa
2023-02-03 16:23 ` [PATCHv3 bpf-next 2/9] selftests/bpf: Move test_progs helpers to testing_helpers object Jiri Olsa
2023-02-07 14:38 ` David Vernet [this message]
2023-02-08 9:35 ` Jiri Olsa
2023-02-03 16:23 ` [PATCHv3 bpf-next 3/9] selftests/bpf: Use only stdout in un/load_bpf_testmod functions Jiri Olsa
2023-02-07 14:41 ` David Vernet
2023-02-08 9:44 ` Jiri Olsa
2023-02-03 16:23 ` [PATCHv3 bpf-next 4/9] selftests/bpf: Do not unload bpf_testmod in load_bpf_testmod Jiri Olsa
2023-02-03 16:23 ` [PATCHv3 bpf-next 5/9] selftests/bpf: Use un/load_bpf_testmod functions in tests Jiri Olsa
2023-02-07 14:45 ` David Vernet
2023-02-03 16:23 ` [PATCHv3 bpf-next 6/9] selftests/bpf: Load bpf_testmod for verifier test Jiri Olsa
2023-02-07 14:46 ` David Vernet
2023-02-03 16:23 ` [PATCHv3 bpf-next 7/9] selftests/bpf: Allow to use kfunc from testmod.ko in test_verifier Jiri Olsa
2023-02-07 15:34 ` David Vernet
2023-02-08 10:09 ` Jiri Olsa
2023-02-03 16:23 ` [PATCHv3 bpf-next 8/9] selftests/bpf: Remove extern from kfuncs declarations Jiri Olsa
2023-02-07 15:35 ` David Vernet
2023-02-03 16:23 ` [PATCHv3 bpf-next 9/9] bpf: Move kernel test kfuncs to bpf_testmod Jiri Olsa
2023-02-04 9:21 ` [PATCHv3 bpf-next 0/9] bpf: Move kernel test kfuncs into bpf_testmod Alexei Starovoitov
2023-02-05 18:17 ` Jiri Olsa
2023-02-05 18:36 ` Ilya Leoshkevich
2023-02-06 9:15 ` Jiri Olsa
2023-02-09 8:47 ` Jiri Olsa
2023-02-09 9:38 ` Ilya Leoshkevich
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=Y+JiUQFyalc0aV6M@maniforge.lan \
--to=void@manifault.com \
--cc=andrii@kernel.org \
--cc=asavkov@redhat.com \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=daniel@iogearbox.net \
--cc=haoluo@google.com \
--cc=john.fastabend@gmail.com \
--cc=jolsa@kernel.org \
--cc=kafai@fb.com \
--cc=kpsingh@chromium.org \
--cc=memxor@gmail.com \
--cc=sdf@google.com \
--cc=songliubraving@fb.com \
--cc=yhs@fb.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox