[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-Id: <20210429184101.RFC.v2.2.I0d44ed3cc06ff2924c1b5e418e0599e1c1731c3c@changeid>
Date: Thu, 29 Apr 2021 18:44:10 +1000
From: Anand K Mistry <amistry@...gle.com>
To: x86@...nel.org
Cc: joelaf@...gle.com, asteinhauser@...gle.com, bp@...en8.de,
tglx@...utronix.de, Anand K Mistry <amistry@...gle.com>,
Shuah Khan <shuah@...nel.org>, linux-kernel@...r.kernel.org,
linux-kselftest@...r.kernel.org
Subject: [RFC PATCH v2 2/2] selftests: Benchmark for the cost of disabling IB speculation
This is a simple benchmark for determining the cost of disabling IB
speculation. It forks a child process and does a simple ping-pong
using pipes between the parent and child. The child process can have IB
speculation disabled by running with 'd' as the first argument.
The test increases the number of iterations until the iterations take at
least 1 second, to minimise noise.
This file is NOT intended for inclusion in the kernel source. It is
presented here as a patch for reference and for others to replicate
results.
The binary should be run with 'taskset' and pinned to a single core,
since the goal is to benchmark process switching on a single core.
Signed-off-by: Anand K Mistry <amistry@...gle.com>
---
(no changes since v1)
.../testing/selftests/ib_spec/ib_spec_bench.c | 109 ++++++++++++++++++
1 file changed, 109 insertions(+)
create mode 100644 tools/testing/selftests/ib_spec/ib_spec_bench.c
diff --git a/tools/testing/selftests/ib_spec/ib_spec_bench.c b/tools/testing/selftests/ib_spec/ib_spec_bench.c
new file mode 100644
index 000000000000..e8eab910a9d0
--- /dev/null
+++ b/tools/testing/selftests/ib_spec/ib_spec_bench.c
@@ -0,0 +1,109 @@
+#include <stdio.h>
+#include <time.h>
+#include <stdint.h>
+#include <assert.h>
+#include <unistd.h>
+#include <sys/prctl.h>
+
+#define PR_SPEC_IBPB_MODE 2
+#define PR_SPEC_IBPB_MODE_DEFAULT 0
+#define PR_SPEC_IBPB_MODE_SANDBOX 1
+#define PR_SPEC_IBPB_MODE_PROTECT 2
+
+int64_t get_time_us() {
+ struct timespec ts = {0};
+ assert(clock_gettime(CLOCK_MONOTONIC, &ts) == 0);
+ return (ts.tv_sec * 1000000) + (ts.tv_nsec/1000);
+}
+
+void pong(int read_fd, int write_fd) {
+ int ret;
+ char buf;
+
+ while (1) {
+ ret = read(read_fd, &buf, 1);
+ if (ret == 0)
+ return;
+ assert(ret == 1);
+
+ assert(write(write_fd, &buf, 1) == 1);
+ }
+}
+
+void ping_once(int write_fd, int read_fd) {
+ char buf = 42;
+ assert(write(write_fd, &buf, 1) == 1);
+ assert(read(read_fd, &buf, 1) == 1);
+}
+
+int64_t ping_multi(int iters, int write_fd, int read_fd) {
+ int64_t start_time = get_time_us();
+ for (int i = 0; i < iters; i++)
+ ping_once(write_fd, read_fd);
+ return get_time_us() - start_time;
+}
+
+void run_test(int write_fd, int read_fd) {
+ int64_t iters = 1;
+ int64_t t;
+ for (int i = 0; i < 60; i++) {
+ t = ping_multi(iters, write_fd, read_fd);
+ printf("iters: %d, t: %dus, iter/sec: %d, us/iter: %d\n",
+ iters, t, (iters * 1000000LL) / t, t/iters);
+
+ if (t > 1000000)
+ break;
+ iters <<= 1;
+ }
+}
+
+int main(int argc, char* argv[]) {
+ int fds_ping[2], fds_pong[2];
+ assert(pipe(fds_ping) == 0);
+ assert(pipe(fds_pong) == 0);
+
+ int disable_ib = 0;
+ int spec_ibpb_mode = 0;
+
+ if (argc > 1) {
+ int done = 0;
+ for (int i = 0; !done; i++) {
+ switch (argv[1][i]) {
+ case 0:
+ done = 1;
+ break;
+ case 'd':
+ disable_ib = 1;
+ break;
+ case 's':
+ spec_ibpb_mode = PR_SPEC_IBPB_MODE_SANDBOX;
+ break;
+ case 'p':
+ spec_ibpb_mode = PR_SPEC_IBPB_MODE_PROTECT;
+ break;
+ }
+ }
+ }
+
+ pid_t pid = fork();
+ assert(pid >= 0);
+ if (!pid) {
+ if (prctl(PR_SET_SPECULATION_CTRL,
+ PR_SPEC_IBPB_MODE, spec_ibpb_mode, 0, 0)) {
+ perror("Unable to set IBPB mode");
+ }
+
+ if (disable_ib)
+ assert(prctl(PR_SET_SPECULATION_CTRL,
+ PR_SPEC_INDIRECT_BRANCH,
+ PR_SPEC_DISABLE, 0, 0) == 0);
+
+ close(fds_ping[1]);
+ pong(fds_ping[0], fds_pong[1]);
+ } else {
+ run_test(fds_ping[1], fds_pong[0]);
+ close(fds_ping[1]);
+ }
+
+ return 0;
+}
--
2.31.1.498.g6c1eba8ee3d-goog
Powered by blists - more mailing lists