[PATCH 1/2] KVM: selftests: Make rseq compatible with glibc-2.35

From: Gavin Shan
Date: Tue Aug 09 2022 - 00:06:27 EST


The rseq information is registered by TLS, starting from glibc-2.35.
In this case, the test always fails due to syscall(__NR_rseq). For
example, on RHEL9.1 where upstream glibc-2.35 features are enabled
on downstream glibc-2.34, the test fails like below.

# ./rseq_test
==== Test Assertion Failure ====
rseq_test.c:60: !r
pid=112043 tid=112043 errno=22 - Invalid argument
1 0x0000000000401973: main at rseq_test.c:226
2 0x0000ffff84b6c79b: ?? ??:0
3 0x0000ffff84b6c86b: ?? ??:0
4 0x0000000000401b6f: _start at ??:?
rseq failed, errno = 22 (Invalid argument)
# rpm -aq | grep glibc-2
glibc-2.34-39.el9.aarch64

Fix the issue by using the registered rseq information from TLS
if it exists. Otherwise, we're going to register our own rseq
information as before.

Reported-by: Yihuang Yu <yihyu@xxxxxxxxxx>
Suggested-by: Florian Weimer <fweimer@xxxxxxxxxx>
Suggested-by: Mathieu Desnoyers <mathieu.desnoyers@xxxxxxxxxxxx>
Signed-off-by: Gavin Shan <gshan@xxxxxxxxxx>
---
tools/testing/selftests/kvm/rseq_test.c | 30 +++++++++++++++++++++++--
1 file changed, 28 insertions(+), 2 deletions(-)

diff --git a/tools/testing/selftests/kvm/rseq_test.c b/tools/testing/selftests/kvm/rseq_test.c
index a54d4d05a058..acb1bf1f06b3 100644
--- a/tools/testing/selftests/kvm/rseq_test.c
+++ b/tools/testing/selftests/kvm/rseq_test.c
@@ -9,6 +9,7 @@
#include <string.h>
#include <signal.h>
#include <syscall.h>
+#include <dlfcn.h>
#include <sys/ioctl.h>
#include <sys/sysinfo.h>
#include <asm/barrier.h>
@@ -36,6 +37,8 @@ static __thread volatile struct rseq __rseq = {
*/
#define NR_TASK_MIGRATIONS 100000

+static bool __rseq_ownership;
+static volatile struct rseq *__rseq_info;
static pthread_t migration_thread;
static cpu_set_t possible_mask;
static int min_cpu, max_cpu;
@@ -49,11 +52,33 @@ static void guest_code(void)
GUEST_SYNC(0);
}

+static void sys_rseq_ownership(void)
+{
+ long *offset;
+ unsigned int *size, *flags;
+
+ offset = dlsym(RTLD_NEXT, "__rseq_offset");
+ size = dlsym(RTLD_NEXT, "__rseq_size");
+ flags = dlsym(RTLD_NEXT, "__rseq_flags");
+
+ if (offset && size && *size && flags) {
+ __rseq_ownership = false;
+ __rseq_info = (struct rseq *)((uintptr_t)__builtin_thread_pointer() +
+ *offset);
+ } else {
+ __rseq_ownership = true;
+ __rseq_info = &__rseq;
+ }
+}
+
static void sys_rseq(int flags)
{
int r;

- r = syscall(__NR_rseq, &__rseq, sizeof(__rseq), flags, RSEQ_SIG);
+ if (!__rseq_ownership)
+ return;
+
+ r = syscall(__NR_rseq, __rseq_info, sizeof(*__rseq_info), flags, RSEQ_SIG);
TEST_ASSERT(!r, "rseq failed, errno = %d (%s)", errno, strerror(errno));
}

@@ -218,6 +243,7 @@ int main(int argc, char *argv[])

calc_min_max_cpu();

+ sys_rseq_ownership();
sys_rseq(0);

/*
@@ -256,7 +282,7 @@ int main(int argc, char *argv[])
*/
smp_rmb();
cpu = sched_getcpu();
- rseq_cpu = READ_ONCE(__rseq.cpu_id);
+ rseq_cpu = READ_ONCE(__rseq_info->cpu_id);
smp_rmb();
} while (snapshot != atomic_read(&seq_cnt));

--
2.23.0