Message ID | 20230703105745.1314475-3-tero.kristo@linux.intel.com (mailing list archive) |
---|---|
State | Changes Requested |
Delegated to: | BPF |
Headers | show |
Series | x86/BPF: Add new BPF helper call bpf_rdtsc | expand |
Context | Check | Description |
---|---|---|
netdev/tree_selection | success | Not a local patch, async |
bpf/vmtest-bpf-next-VM_Test-1 | success | Logs for ShellCheck |
bpf/vmtest-bpf-next-VM_Test-2 | success | Logs for build for aarch64 with gcc |
bpf/vmtest-bpf-next-VM_Test-5 | success | Logs for build for x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-6 | success | Logs for set-matrix |
bpf/vmtest-bpf-next-VM_Test-3 | success | Logs for build for s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-4 | success | Logs for build for x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-7 | success | Logs for test_maps on aarch64 with gcc |
bpf/vmtest-bpf-next-VM_Test-9 | success | Logs for test_maps on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-10 | success | Logs for test_maps on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-11 | success | Logs for test_progs on aarch64 with gcc |
bpf/vmtest-bpf-next-VM_Test-13 | fail | Logs for test_progs on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-14 | fail | Logs for test_progs on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-15 | success | Logs for test_progs_no_alu32 on aarch64 with gcc |
bpf/vmtest-bpf-next-VM_Test-17 | fail | Logs for test_progs_no_alu32 on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-18 | fail | Logs for test_progs_no_alu32 on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-19 | success | Logs for test_progs_no_alu32_parallel on aarch64 with gcc |
bpf/vmtest-bpf-next-VM_Test-20 | success | Logs for test_progs_no_alu32_parallel on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-21 | success | Logs for test_progs_no_alu32_parallel on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-22 | success | Logs for test_progs_parallel on aarch64 with gcc |
bpf/vmtest-bpf-next-VM_Test-23 | success | Logs for test_progs_parallel on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-24 | success | Logs for test_progs_parallel on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-25 | success | Logs for test_verifier on aarch64 with gcc |
bpf/vmtest-bpf-next-VM_Test-26 | success | Logs for test_verifier on s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-27 | success | Logs for test_verifier on x86_64 with gcc |
bpf/vmtest-bpf-next-VM_Test-28 | success | Logs for test_verifier on x86_64 with llvm-16 |
bpf/vmtest-bpf-next-VM_Test-29 | success | Logs for veristat |
bpf/vmtest-bpf-next-VM_Test-16 | success | Logs for test_progs_no_alu32 on s390x with gcc |
bpf/vmtest-bpf-next-VM_Test-12 | success | Logs for test_progs on s390x with gcc |
bpf/vmtest-bpf-next-PR | fail | PR summary |
bpf/vmtest-bpf-next-VM_Test-8 | success | Logs for test_maps on s390x with gcc |
Tero Kristo wrote: > Add selftest for bpf_rdtsc() which reads the TSC (Time Stamp Counter) on > x86_64 architectures. The test reads the TSC from both userspace and the > BPF program, and verifies the TSC values are in incremental order as > expected. The test is automatically skipped on architectures that do not > support the feature. > > Signed-off-by: Tero Kristo <tero.kristo@linux.intel.com> > --- > .../selftests/bpf/prog_tests/test_rdtsc.c | 67 +++++++++++++++++++ > .../testing/selftests/bpf/progs/test_rdtsc.c | 21 ++++++ > 2 files changed, 88 insertions(+) > create mode 100644 tools/testing/selftests/bpf/prog_tests/test_rdtsc.c > create mode 100644 tools/testing/selftests/bpf/progs/test_rdtsc.c > > diff --git a/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c > new file mode 100644 > index 000000000000..2b26deb5b35a > --- /dev/null > +++ b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c > @@ -0,0 +1,67 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* Copyright(c) 2023 Intel Corporation */ > + > +#include "test_progs.h" > +#include "test_rdtsc.skel.h" > + > +#ifdef __x86_64__ > + > +static inline u64 _rdtsc(void) > +{ > + u32 low, high; > + > + __asm__ __volatile__("rdtscp" : "=a" (low), "=d" (high)); I think its ok but note this could fail if user doesn't have access to rdtscp and iirc that can be restricted? > + return ((u64)high << 32) | low; > +}
On 04/07/2023 01:00, John Fastabend wrote: > Tero Kristo wrote: >> Add selftest for bpf_rdtsc() which reads the TSC (Time Stamp Counter) on >> x86_64 architectures. The test reads the TSC from both userspace and the >> BPF program, and verifies the TSC values are in incremental order as >> expected. The test is automatically skipped on architectures that do not >> support the feature. >> >> Signed-off-by: Tero Kristo <tero.kristo@linux.intel.com> >> --- >> .../selftests/bpf/prog_tests/test_rdtsc.c | 67 +++++++++++++++++++ >> .../testing/selftests/bpf/progs/test_rdtsc.c | 21 ++++++ >> 2 files changed, 88 insertions(+) >> create mode 100644 tools/testing/selftests/bpf/prog_tests/test_rdtsc.c >> create mode 100644 tools/testing/selftests/bpf/progs/test_rdtsc.c >> >> diff --git a/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c >> new file mode 100644 >> index 000000000000..2b26deb5b35a >> --- /dev/null >> +++ b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c >> @@ -0,0 +1,67 @@ >> +// SPDX-License-Identifier: GPL-2.0 >> +/* Copyright(c) 2023 Intel Corporation */ >> + >> +#include "test_progs.h" >> +#include "test_rdtsc.skel.h" >> + >> +#ifdef __x86_64__ >> + >> +static inline u64 _rdtsc(void) >> +{ >> + u32 low, high; >> + >> + __asm__ __volatile__("rdtscp" : "=a" (low), "=d" (high)); > I think its ok but note this could fail if user doesn't have > access to rdtscp and iirc that can be restricted? It is possible to restrict RDTSC access from userspace by enabling the TSD bit in CR4 register, and it will cause the userspace process to trap with general protection fault. However, the usage of RDTSC appears to be built-in to C standard libraries (probably some timer routines) and enabling the CR4 TSD makes the system near unusable. Things like sshd + systemd also start generating the same general protection faults if RDTSC is blocked. Also, attempting to run anything at all with the BPF selftest suite causes the same general protection fault; not only the rdtsc test. I tried this with couple of setups, one system running a minimalistic buildroot and another one running a fedora37 installation and the results were similar. -Tero > >> + return ((u64)high << 32) | low; >> +}
Tero Kristo wrote: > > On 04/07/2023 01:00, John Fastabend wrote: > > Tero Kristo wrote: > >> Add selftest for bpf_rdtsc() which reads the TSC (Time Stamp Counter) on > >> x86_64 architectures. The test reads the TSC from both userspace and the > >> BPF program, and verifies the TSC values are in incremental order as > >> expected. The test is automatically skipped on architectures that do not > >> support the feature. > >> > >> Signed-off-by: Tero Kristo <tero.kristo@linux.intel.com> > >> --- > >> .../selftests/bpf/prog_tests/test_rdtsc.c | 67 +++++++++++++++++++ > >> .../testing/selftests/bpf/progs/test_rdtsc.c | 21 ++++++ > >> 2 files changed, 88 insertions(+) > >> create mode 100644 tools/testing/selftests/bpf/prog_tests/test_rdtsc.c > >> create mode 100644 tools/testing/selftests/bpf/progs/test_rdtsc.c > >> > >> diff --git a/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c > >> new file mode 100644 > >> index 000000000000..2b26deb5b35a > >> --- /dev/null > >> +++ b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c > >> @@ -0,0 +1,67 @@ > >> +// SPDX-License-Identifier: GPL-2.0 > >> +/* Copyright(c) 2023 Intel Corporation */ > >> + > >> +#include "test_progs.h" > >> +#include "test_rdtsc.skel.h" > >> + > >> +#ifdef __x86_64__ > >> + > >> +static inline u64 _rdtsc(void) > >> +{ > >> + u32 low, high; > >> + > >> + __asm__ __volatile__("rdtscp" : "=a" (low), "=d" (high)); > > I think its ok but note this could fail if user doesn't have > > access to rdtscp and iirc that can be restricted? > > It is possible to restrict RDTSC access from userspace by enabling the > TSD bit in CR4 register, and it will cause the userspace process to trap > with general protection fault. > > However, the usage of RDTSC appears to be built-in to C standard > libraries (probably some timer routines) and enabling the CR4 TSD makes > the system near unusable. Things like sshd + systemd also start > generating the same general protection faults if RDTSC is blocked. Also, > attempting to run anything at all with the BPF selftest suite causes the > same general protection fault; not only the rdtsc test. > > I tried this with couple of setups, one system running a minimalistic > buildroot and another one running a fedora37 installation and the > results were similar. Thanks. Good enough for me. > > -Tero
diff --git a/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c new file mode 100644 index 000000000000..2b26deb5b35a --- /dev/null +++ b/tools/testing/selftests/bpf/prog_tests/test_rdtsc.c @@ -0,0 +1,67 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright(c) 2023 Intel Corporation */ + +#include "test_progs.h" +#include "test_rdtsc.skel.h" + +#ifdef __x86_64__ + +static inline u64 _rdtsc(void) +{ + u32 low, high; + + __asm__ __volatile__("rdtscp" : "=a" (low), "=d" (high)); + return ((u64)high << 32) | low; +} + +static int rdtsc(struct test_rdtsc *skel) +{ + int err, prog_fd; + u64 user_c1, user_c2; + + LIBBPF_OPTS(bpf_test_run_opts, topts); + + err = test_rdtsc__attach(skel); + if (!ASSERT_OK(err, "test_rdtsc_attach")) + return err; + + user_c1 = _rdtsc(); + + prog_fd = bpf_program__fd(skel->progs.test1); + err = bpf_prog_test_run_opts(prog_fd, &topts); + + user_c2 = _rdtsc(); + + ASSERT_OK(err, "test_run"); + ASSERT_EQ(topts.retval, 0, "test_run"); + + test_rdtsc__detach(skel); + + ASSERT_GE(skel->bss->c1, user_c1, "bpf c1 > user c1"); + ASSERT_GE(user_c2, skel->bss->c2, "user c2 > bpf c2"); + ASSERT_GE(skel->bss->c2, user_c1, "bpf c2 > bpf c1"); + ASSERT_GE(user_c2, user_c1, "user c2 > user c1"); + + return 0; +} +#endif + +void test_rdtsc(void) +{ +#ifdef __x86_64__ + struct test_rdtsc *skel; + int err; + + skel = test_rdtsc__open_and_load(); + if (!ASSERT_OK_PTR(skel, "test_rdtsc_skel_load")) + goto cleanup; + err = rdtsc(skel); + ASSERT_OK(err, "rdtsc"); + +cleanup: + test_rdtsc__destroy(skel); +#else + printf("%s:SKIP:bpf_rdtsc() kfunc not supported\n", __func__); + test__skip(); +#endif +} diff --git a/tools/testing/selftests/bpf/progs/test_rdtsc.c b/tools/testing/selftests/bpf/progs/test_rdtsc.c new file mode 100644 index 000000000000..14776b83bd3e --- /dev/null +++ b/tools/testing/selftests/bpf/progs/test_rdtsc.c @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright(c) 2023 Intel Corporation */ +#include <linux/bpf.h> +#include <bpf/bpf_tracing.h> +#include <bpf/bpf_helpers.h> + +char _license[] SEC("license") = "GPL"; + +__u64 c1; +__u64 c2; + +extern __u64 bpf_rdtsc(void) __ksym; + +SEC("fentry/bpf_fentry_test1") +int BPF_PROG2(test1, int, a) +{ + c1 = bpf_rdtsc(); + c2 = bpf_rdtsc(); + + return 0; +}
Add selftest for bpf_rdtsc() which reads the TSC (Time Stamp Counter) on x86_64 architectures. The test reads the TSC from both userspace and the BPF program, and verifies the TSC values are in incremental order as expected. The test is automatically skipped on architectures that do not support the feature. Signed-off-by: Tero Kristo <tero.kristo@linux.intel.com> --- .../selftests/bpf/prog_tests/test_rdtsc.c | 67 +++++++++++++++++++ .../testing/selftests/bpf/progs/test_rdtsc.c | 21 ++++++ 2 files changed, 88 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/test_rdtsc.c create mode 100644 tools/testing/selftests/bpf/progs/test_rdtsc.c