Commit 7c03e703 authored by Andy Lutomirski's avatar Andy Lutomirski Committed by Thomas Gleixner

selftests/x86: Add clock_gettime() tests to test_vdso

Now that the vDSO implementation of clock_gettime() is getting
reworked, add a selftest for it.  This tests that its output is
consistent with the syscall version.

This is marked for stable to serve as a test for commit

  715bd9d1 ("x86/vdso: Fix asm constraints on vDSO syscall fallbacks")
Signed-off-by: default avatarAndy Lutomirski <luto@kernel.org>
Signed-off-by: default avatarThomas Gleixner <tglx@linutronix.de>
Cc: stable@vger.kernel.org
Link: https://lkml.kernel.org/r/082399674de2619b2befd8c0dde49b260605b126.1538422295.git.luto@kernel.org
parent 715bd9d1
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include <errno.h> #include <errno.h>
#include <sched.h> #include <sched.h>
#include <stdbool.h> #include <stdbool.h>
#include <limits.h>
#ifndef SYS_getcpu #ifndef SYS_getcpu
# ifdef __x86_64__ # ifdef __x86_64__
...@@ -31,6 +32,10 @@ ...@@ -31,6 +32,10 @@
int nerrs = 0; int nerrs = 0;
typedef int (*vgettime_t)(clockid_t, struct timespec *);
vgettime_t vdso_clock_gettime;
typedef long (*getcpu_t)(unsigned *, unsigned *, void *); typedef long (*getcpu_t)(unsigned *, unsigned *, void *);
getcpu_t vgetcpu; getcpu_t vgetcpu;
...@@ -95,6 +100,10 @@ static void fill_function_pointers() ...@@ -95,6 +100,10 @@ static void fill_function_pointers()
printf("Warning: failed to find getcpu in vDSO\n"); printf("Warning: failed to find getcpu in vDSO\n");
vgetcpu = (getcpu_t) vsyscall_getcpu(); vgetcpu = (getcpu_t) vsyscall_getcpu();
vdso_clock_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
if (!vdso_clock_gettime)
printf("Warning: failed to find clock_gettime in vDSO\n");
} }
static long sys_getcpu(unsigned * cpu, unsigned * node, static long sys_getcpu(unsigned * cpu, unsigned * node,
...@@ -103,6 +112,11 @@ static long sys_getcpu(unsigned * cpu, unsigned * node, ...@@ -103,6 +112,11 @@ static long sys_getcpu(unsigned * cpu, unsigned * node,
return syscall(__NR_getcpu, cpu, node, cache); return syscall(__NR_getcpu, cpu, node, cache);
} }
static inline int sys_clock_gettime(clockid_t id, struct timespec *ts)
{
return syscall(__NR_clock_gettime, id, ts);
}
static void test_getcpu(void) static void test_getcpu(void)
{ {
printf("[RUN]\tTesting getcpu...\n"); printf("[RUN]\tTesting getcpu...\n");
...@@ -155,10 +169,95 @@ static void test_getcpu(void) ...@@ -155,10 +169,95 @@ static void test_getcpu(void)
} }
} }
static bool ts_leq(const struct timespec *a, const struct timespec *b)
{
if (a->tv_sec != b->tv_sec)
return a->tv_sec < b->tv_sec;
else
return a->tv_nsec <= b->tv_nsec;
}
static char const * const clocknames[] = {
[0] = "CLOCK_REALTIME",
[1] = "CLOCK_MONOTONIC",
[2] = "CLOCK_PROCESS_CPUTIME_ID",
[3] = "CLOCK_THREAD_CPUTIME_ID",
[4] = "CLOCK_MONOTONIC_RAW",
[5] = "CLOCK_REALTIME_COARSE",
[6] = "CLOCK_MONOTONIC_COARSE",
[7] = "CLOCK_BOOTTIME",
[8] = "CLOCK_REALTIME_ALARM",
[9] = "CLOCK_BOOTTIME_ALARM",
[10] = "CLOCK_SGI_CYCLE",
[11] = "CLOCK_TAI",
};
static void test_one_clock_gettime(int clock, const char *name)
{
struct timespec start, vdso, end;
int vdso_ret, end_ret;
printf("[RUN]\tTesting clock_gettime for clock %s (%d)...\n", name, clock);
if (sys_clock_gettime(clock, &start) < 0) {
if (errno == EINVAL) {
vdso_ret = vdso_clock_gettime(clock, &vdso);
if (vdso_ret == -EINVAL) {
printf("[OK]\tNo such clock.\n");
} else {
printf("[FAIL]\tNo such clock, but __vdso_clock_gettime returned %d\n", vdso_ret);
nerrs++;
}
} else {
printf("[WARN]\t clock_gettime(%d) syscall returned error %d\n", clock, errno);
}
return;
}
vdso_ret = vdso_clock_gettime(clock, &vdso);
end_ret = sys_clock_gettime(clock, &end);
if (vdso_ret != 0 || end_ret != 0) {
printf("[FAIL]\tvDSO returned %d, syscall errno=%d\n",
vdso_ret, errno);
nerrs++;
return;
}
printf("\t%llu.%09ld %llu.%09ld %llu.%09ld\n",
(unsigned long long)start.tv_sec, start.tv_nsec,
(unsigned long long)vdso.tv_sec, vdso.tv_nsec,
(unsigned long long)end.tv_sec, end.tv_nsec);
if (!ts_leq(&start, &vdso) || !ts_leq(&vdso, &end)) {
printf("[FAIL]\tTimes are out of sequence\n");
nerrs++;
}
}
static void test_clock_gettime(void)
{
for (int clock = 0; clock < sizeof(clocknames) / sizeof(clocknames[0]);
clock++) {
test_one_clock_gettime(clock, clocknames[clock]);
}
/* Also test some invalid clock ids */
test_one_clock_gettime(-1, "invalid");
test_one_clock_gettime(INT_MIN, "invalid");
test_one_clock_gettime(INT_MAX, "invalid");
}
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
fill_function_pointers(); fill_function_pointers();
test_clock_gettime();
/*
* Test getcpu() last so that, if something goes wrong setting affinity,
* we still run the other tests.
*/
test_getcpu(); test_getcpu();
return nerrs ? 1 : 0; return nerrs ? 1 : 0;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment