mirror of
https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
synced 2026-08-31 02:21:39 -04:00
selftests/bpf: Add test for bpf_get_smp_processor_id
Add a test which checks on each CPU that the bpf_get_smp_processor_id BPF helper is returning the correct CPU number. Signed-off-by: Maxim Khmelevskii <max@linux.ibm.com> Signed-off-by: Daniel Borkmann <daniel@iogearbox.net> Reviewed-by: Ilya Leoshkevich <iii@linux.ibm.com> Link: https://lore.kernel.org/bpf/20260703125648.919196-6-max@linux.ibm.com Signed-off-by: Kumar Kartikeya Dwivedi <memxor@gmail.com>
This commit is contained in:
committed by
Kumar Kartikeya Dwivedi
parent
5f6cc29993
commit
e733303438
@@ -0,0 +1,45 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
#include <test_progs.h>
|
||||
#include "bpf/libbpf_internal.h"
|
||||
#include "get_smp_processor_id.skel.h"
|
||||
|
||||
void test_get_smp_processor_id(void)
|
||||
{
|
||||
LIBBPF_OPTS(bpf_test_run_opts, opts,
|
||||
.flags = BPF_F_TEST_RUN_ON_CPU,
|
||||
.cpu = 0,
|
||||
);
|
||||
struct get_smp_processor_id *skel;
|
||||
int prog_fd, err, online_cpu_nr, i;
|
||||
bool *online = NULL;
|
||||
|
||||
err = parse_cpu_mask_file("/sys/devices/system/cpu/online",
|
||||
&online, &online_cpu_nr);
|
||||
if (!ASSERT_OK(err, "parse_cpu_mask_file"))
|
||||
return;
|
||||
|
||||
skel = get_smp_processor_id__open_and_load();
|
||||
if (!ASSERT_OK_PTR(skel, "get_smp_processor_id__open_and_load"))
|
||||
goto cleanup;
|
||||
|
||||
prog_fd = bpf_program__fd(skel->progs.call_bpf_get_smp_processor_id);
|
||||
|
||||
for (i = 0; i < online_cpu_nr; i++) {
|
||||
if (!online[i])
|
||||
continue;
|
||||
|
||||
opts.cpu = i;
|
||||
skel->bss->cpu_nr_result = -1;
|
||||
|
||||
err = bpf_prog_test_run_opts(prog_fd, &opts);
|
||||
if (!ASSERT_OK(err, "bpf_prog_test_run_opts"))
|
||||
goto cleanup;
|
||||
|
||||
ASSERT_EQ(skel->bss->cpu_nr_result, opts.cpu, "cpu_nr_result");
|
||||
}
|
||||
|
||||
cleanup:
|
||||
free(online);
|
||||
get_smp_processor_id__destroy(skel);
|
||||
}
|
||||
20
tools/testing/selftests/bpf/progs/get_smp_processor_id.c
Normal file
20
tools/testing/selftests/bpf/progs/get_smp_processor_id.c
Normal file
@@ -0,0 +1,20 @@
|
||||
// SPDX-License-Identifier: GPL-2.0
|
||||
|
||||
#include <linux/bpf.h>
|
||||
#include <bpf/bpf_helpers.h>
|
||||
#include "bpf_misc.h"
|
||||
|
||||
__u64 cpu_nr_result;
|
||||
|
||||
SEC("raw_tp")
|
||||
void call_bpf_get_smp_processor_id(void)
|
||||
{
|
||||
register __u64 r0 asm("r0") = -1;
|
||||
asm volatile ("call %[bpf_get_smp_processor_id];"
|
||||
: "+r"(r0)
|
||||
: __imm(bpf_get_smp_processor_id)
|
||||
: "r1", "r2", "r3", "r4", "r5", "memory");
|
||||
cpu_nr_result = r0;
|
||||
}
|
||||
|
||||
char _license[] SEC("license") = "GPL";
|
||||
Reference in New Issue
Block a user