All of lore.kernel.org
 help / color / mirror / Atom feed
From: Tero Kristo <tero.kristo@linux.intel.com>
To: shuah@kernel.org, tglx@linutronix.de, x86@kernel.org,
	bp@alien8.de, dave.hansen@linux.intel.com, mingo@redhat.com
Cc: ast@kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org, andrii@kernel.org,
	daniel@iogearbox.net, bpf@vger.kernel.org
Subject: [PATCH 2/2] selftests/bpf: Add test for bpf_rdtsc
Date: Mon,  3 Jul 2023 13:57:45 +0300	[thread overview]
Message-ID: <20230703105745.1314475-3-tero.kristo@linux.intel.com> (raw)
In-Reply-To: <20230703105745.1314475-1-tero.kristo@linux.intel.com>

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));
+	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;
+}
-- 
2.25.1


  parent reply	other threads:[~2023-07-03 10:58 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-07-03 10:57 [PATCH 0/2] x86/BPF: Add new BPF helper call bpf_rdtsc Tero Kristo
2023-07-03 10:57 ` [PATCH 1/2] x86/tsc: " Tero Kristo
2023-07-04  4:49   ` Yonghong Song
2023-07-06 12:00     ` Tero Kristo
2023-07-06  3:02   ` Alexei Starovoitov
2023-07-06  5:16     ` John Fastabend
2023-07-06 11:59       ` Tero Kristo
2023-07-06 19:51         ` Alexei Starovoitov
2023-07-07  5:41           ` John Fastabend
2023-07-07  8:27             ` Tero Kristo
2023-07-07 14:42               ` Alexei Starovoitov
2023-08-09 11:31                 ` Tero Kristo
2023-07-03 10:57 ` Tero Kristo [this message]
2023-07-03 22:00   ` [PATCH 2/2] selftests/bpf: Add test for bpf_rdtsc John Fastabend
2023-07-04  8:55     ` Tero Kristo
2023-07-06  4:57       ` John Fastabend
2023-07-03 21:55 ` [PATCH 0/2] x86/BPF: Add new BPF helper call bpf_rdtsc John Fastabend

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=20230703105745.1314475-3-tero.kristo@linux.intel.com \
    --to=tero.kristo@linux.intel.com \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bp@alien8.de \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=dave.hansen@linux.intel.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=mingo@redhat.com \
    --cc=shuah@kernel.org \
    --cc=tglx@linutronix.de \
    --cc=x86@kernel.org \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.