Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

selftests/bpf: Ensure that next_cpu() returns a valid CPU number

When using option -a without --prod-affinity or --cons-affinity, if the
number of producers and consumers is greater than the number of online
CPUs, the benchmark will fail to run as shown below:

$ getconf _NPROCESSORS_ONLN
8
$ ./bench bpf-loop -a -p9
Setting up benchmark 'bpf-loop'...
setting affinity to CPU #8 failed: -22

Fix it by returning the remainder of next_cpu divided by the number of
online CPUs in next_cpu().

Signed-off-by: Hou Tao <houtao1@huawei.com>
Link: https://lore.kernel.org/r/20230613080921.1623219-4-houtao@huaweicloud.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

authored by

Hou Tao and committed by
Alexei Starovoitov
da77ae2b ea400d13

+3 -1
+2 -1
tools/testing/selftests/bpf/bench.c
··· 469 469 exit(1); 470 470 } 471 471 472 - return cpu_set->next_cpu++; 472 + return cpu_set->next_cpu++ % env.nr_cpus; 473 473 } 474 474 475 475 static struct bench_state { ··· 659 659 660 660 int main(int argc, char **argv) 661 661 { 662 + env.nr_cpus = get_nprocs(); 662 663 parse_cmdline_args_init(argc, argv); 663 664 664 665 if (env.list) {
+1
tools/testing/selftests/bpf/bench.h
··· 27 27 bool quiet; 28 28 int consumer_cnt; 29 29 int producer_cnt; 30 + int nr_cpus; 30 31 struct cpu_set prod_cpus; 31 32 struct cpu_set cons_cpus; 32 33 };