]> www.infradead.org Git - users/dwmw2/linux.git/commitdiff
selftests/bpf: Fix bench runner SIGSEGV
authorAndrii Nakryiko <andrii@kernel.org>
Thu, 1 Feb 2024 17:20:27 +0000 (09:20 -0800)
committerDaniel Borkmann <daniel@iogearbox.net>
Thu, 1 Feb 2024 21:16:12 +0000 (22:16 +0100)
Some benchmarks don't have either "consumer" or "producer" sides. For
example, trig-tp and other BPF triggering benchmarks don't have
consumers, as they only do "producing" by calling into syscall or
predefined uproes. As such it's valid for some benchmarks to have zero
consumers or producers. So allows to specify `-c0` explicitly.

This triggers another problem. If benchmark doesn't support either
consumer or producer side, consumer_thread/producer_thread callback will
be NULL, but benchmark runner will attempt to use those NULL callback to
create threads anyways. So instead of crashing with SIGSEGV in case of
misconfigured benchmark, detect the condition and report error.

Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Signed-off-by: Daniel Borkmann <daniel@iogearbox.net>
Acked-by: Eduard Zingerman <eddyz87@gmail.com>
Link: https://lore.kernel.org/bpf/20240201172027.604869-6-andrii@kernel.org
tools/testing/selftests/bpf/bench.c

index 73ce11b0547da7c72237a9536740ff26f3c8e6ba..1724d50ba9421a4d5408c178c475573e85d2f4fc 100644 (file)
@@ -323,14 +323,14 @@ static error_t parse_arg(int key, char *arg, struct argp_state *state)
                break;
        case 'p':
                env.producer_cnt = strtol(arg, NULL, 10);
-               if (env.producer_cnt <= 0) {
+               if (env.producer_cnt < 0) {
                        fprintf(stderr, "Invalid producer count: %s\n", arg);
                        argp_usage(state);
                }
                break;
        case 'c':
                env.consumer_cnt = strtol(arg, NULL, 10);
-               if (env.consumer_cnt <= 0) {
+               if (env.consumer_cnt < 0) {
                        fprintf(stderr, "Invalid consumer count: %s\n", arg);
                        argp_usage(state);
                }
@@ -607,6 +607,10 @@ static void setup_benchmark(void)
                bench->setup();
 
        for (i = 0; i < env.consumer_cnt; i++) {
+               if (!bench->consumer_thread) {
+                       fprintf(stderr, "benchmark doesn't support consumers!\n");
+                       exit(1);
+               }
                err = pthread_create(&state.consumers[i], NULL,
                                     bench->consumer_thread, (void *)(long)i);
                if (err) {
@@ -626,6 +630,10 @@ static void setup_benchmark(void)
                env.prod_cpus.next_cpu = env.cons_cpus.next_cpu;
 
        for (i = 0; i < env.producer_cnt; i++) {
+               if (!bench->producer_thread) {
+                       fprintf(stderr, "benchmark doesn't support producers!\n");
+                       exit(1);
+               }
                err = pthread_create(&state.producers[i], NULL,
                                     bench->producer_thread, (void *)(long)i);
                if (err) {