Skip to contentSkip to conversation

syscalls: avoid time() using __cvdso_gettimeofday in use-level's VDSO

PATCH New Li Wang 5 participants 9 messages Nov 23, 2020 – Nov 26, 2020

lore
Share & tools
lore

Li Wang

author
#0 lore raw

PATCH 1/2 syscalls: avoid time() using __cvdso_gettimeofday in use-level's VDSO

This shmctl01 test detect the time as the number of seconds twice (before and after) the shmget() instruction, then it verifies whether the 'struct shmid_ds ds' gets data correctly. But here it shows 'ds->ctime' out of the seconds range (1604298586, 1604298586),

The reason is that shmget()/msgsnd() always use ktime_get_real_second to get real seconds, but time() on aarch64 via gettimeofday() or (it depends on different kernel versions) clock_gettime() in use-level's VDSO to return tv_sec.

time()
__cvdso_gettimeofday
...

     do_gettimeofday
       ktime_get_real_ts64
        timespc64_add_ns

Situation can be simplify as difference between ktime_get_real_second and ktime_get_real_ts64. As we can see ktime_get_real_second return tk->xtime_sec directly, however

timespc64_add_ns more easily add 1 more second via "a->tv_sec +=..." on a virtual machine, that's why we got occasional errors like:

shmctl01.c:183: TFAIL: SHM_STAT: shm_ctime=1604298585, expected <1604298586,1604298586>
...
msgsnd01.c:59: TFAIL: msg_stime = 1605730573 out of [1605730574, 1605730574]

Here we propose to use '__NR_time' to invoke syscall directly that makes test all get real seconds via ktime_get_real_second.

Cc Chunyu Hu <[email protected]>
Cc Cyril Hrubis <[email protected]>
---
 testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c | 5 +++--
 testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c | 5 +++--
 testcases/kernel/syscalls/ipc/shmctl/shmctl01.c | 5 +++--
 3 files changed, 9 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c +3 -2
index 5c1e317e9..6fdc47dc3 100644
@@ -10,6 +10,7 @@
 #include "tst_test.h"
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
+#include "tst_timer.h"
 
 static key_t msgkey;
 static int queue_id = -1, pid;
@@ -25,13 +26,13 @@ static void verify_msgrcv(void)
 
 	SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
 
-	time(&before_rcv);
+	tst_syscall(__NR_time, &before_rcv);
 	TEST(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "msgrcv failed");
 		return;
 	}
-	time(&after_rcv);
+	tst_syscall(__NR_time, &after_rcv);
 
 	if (strcmp(rcv_buf.mtext, snd_buf.mtext) == 0)
 		tst_res(TPASS, "message received(%s) = message sent(%s)",
testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c +3 -2
index 5f5da52d2..9101f2668 100644
@@ -16,6 +16,7 @@
 #include "tst_test.h"
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
+#include "tst_timer.h"
 
 static key_t msgkey;
 static int queue_id = -1, pid;
@@ -29,13 +30,13 @@ static void verify_msgsnd(void)
 	struct msqid_ds qs_buf;
 	time_t before_snd, after_snd;
 
-	time(&before_snd);
+	tst_syscall(__NR_time, &before_snd);
 	TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, 0));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "msgsnd() failed");
 		return;
 	}
-	time(&after_snd);
+	tst_syscall(__NR_time, &after_snd);
 
 	SAFE_MSGCTL(queue_id, IPC_STAT, &qs_buf);
 
testcases/kernel/syscalls/ipc/shmctl/shmctl01.c +3 -2
index 3a39a4d74..f5b8eaef9 100644
@@ -19,6 +19,7 @@
 #include "tst_test.h"
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
+#include "tst_timer.h"
 
 #define NCHILD 20
 
@@ -240,9 +241,9 @@ static int get_shm_idx_from_id(int shm_id)
 
 static void setup(void)
 {
-	ctime_min = time(NULL);
+	ctime_min = tst_syscall(__NR_time, NULL);
 	shm_id = SAFE_SHMGET(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | SHM_RW);
-	ctime_max = time(NULL);
+	ctime_max = tst_syscall(__NR_time, NULL);
 
 	shm_idx = get_shm_idx_from_id(shm_id);
 
Signature · 1 line
2.21.1
author
#1 lore raw

PATCH 2/2 syscalls: shift to time() if __NR_time not support

On some platforms(aarch64) __NR_time is not supported, if that happens, go back to invoke time() and relax 1-second in low bound for comparing.

This also to fix:
TFAIL: msg_stime = 1605730573 out of [1605730574, 1605730574]

Cc Chunyu Hu <[email protected]>
Cc Cyril Hrubis <[email protected]>
---
 testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c | 14 ++++++++++++--
 testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c | 14 ++++++++++++--
 testcases/kernel/syscalls/ipc/shmctl/shmctl01.c | 14 ++++++++++++--
 3 files changed, 36 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c +12 -2
index 6fdc47dc3..9dc778ca7 100644
@@ -26,13 +26,23 @@ static void verify_msgrcv(void)
 
 	SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
 
-	tst_syscall(__NR_time, &before_rcv);
+	before_rcv = syscall(__NR_time, NULL);
+	if (before_rcv == -1 && errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		before_rcv = time(NULL) - 1;
+	}
+
 	TEST(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "msgrcv failed");
 		return;
 	}
-	tst_syscall(__NR_time, &after_rcv);
+
+	after_rcv = syscall(__NR_time, NULL);
+	if (after_rcv == -1 && errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		after_rcv = time(NULL);
+	}
 
 	if (strcmp(rcv_buf.mtext, snd_buf.mtext) == 0)
 		tst_res(TPASS, "message received(%s) = message sent(%s)",
testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c +12 -2
index 9101f2668..27464e79f 100644
@@ -30,13 +30,23 @@ static void verify_msgsnd(void)
 	struct msqid_ds qs_buf;
 	time_t before_snd, after_snd;
 
-	tst_syscall(__NR_time, &before_snd);
+	before_snd = syscall(__NR_time, NULL);
+	if (before_snd == -1 && errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		before_snd = time(NULL) - 1;
+	}
+
 	TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, 0));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "msgsnd() failed");
 		return;
 	}
-	tst_syscall(__NR_time, &after_snd);
+
+	after_snd = syscall(__NR_time, NULL);
+	if (after_snd == -1 && errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		after_snd = time(NULL);
+	}
 
 	SAFE_MSGCTL(queue_id, IPC_STAT, &qs_buf);
 
testcases/kernel/syscalls/ipc/shmctl/shmctl01.c +12 -2
index f5b8eaef9..356513726 100644
@@ -241,9 +241,19 @@ static int get_shm_idx_from_id(int shm_id)
 
 static void setup(void)
 {
-	ctime_min = tst_syscall(__NR_time, NULL);
+	ctime_min = syscall(__NR_time, NULL);
+	if (ctime_min == -1 && errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		ctime_min = time(NULL) - 1;
+	}
+
 	shm_id = SAFE_SHMGET(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | SHM_RW);
-	ctime_max = tst_syscall(__NR_time, NULL);
+
+	ctime_max = syscall(__NR_time, NULL);
+	if (ctime_max == -1 && errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		ctime_max = time(NULL);
+	}
 
 	shm_idx = get_shm_idx_from_id(shm_id);
 
Signature · 1 line
2.21.1
#2 lore raw

PATCH 2/2 syscalls: shift to time() if __NR_time not support

Hi Li

I have seen this patchset, Can we use a function to check whether kernel supports time syscall (like time_supported_by_kernel()) in setup and then we use time() - !return value in verify funtion?

Quoted 101 lines

On some platforms(aarch64) __NR_time is not supported, if that happens, go back to invoke time() and relax 1-second in low bound for comparing.

This also to fix:

   TFAIL: msg_stime = 1605730573 out of [1605730574, 1605730574]
Cc Chunyu Hu<[email protected]>
Cc Cyril Hrubis<[email protected]>
---
  testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c | 14 ++++++++++++--
  testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c | 14 ++++++++++++--
  testcases/kernel/syscalls/ipc/shmctl/shmctl01.c | 14 ++++++++++++--
  3 files changed, 36 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c +12 -2
index 6fdc47dc3..9dc778ca7 100644
@@ -26,13 +26,23 @@ static void verify_msgrcv(void)
 
  	SAFE_MSGSND(queue_id,&snd_buf, MSGSIZE, 0);
 
-	tst_syscall(__NR_time,&before_rcv);
+	before_rcv = syscall(__NR_time, NULL);
+	if (before_rcv == -1&&  errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		before_rcv = time(NULL) - 1;
+	}
+
  	TEST(msgrcv(queue_id,&rcv_buf, MSGSIZE, 1, 0));
  	if (TST_RET == -1) {
  		tst_res(TFAIL | TTERRNO, "msgrcv failed");
  		return;
  	}
-	tst_syscall(__NR_time,&after_rcv);
+
+	after_rcv = syscall(__NR_time, NULL);
+	if (after_rcv == -1&&  errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		after_rcv = time(NULL);
+	}
 
  	if (strcmp(rcv_buf.mtext, snd_buf.mtext) == 0)
  		tst_res(TPASS, "message received(%s) = message sent(%s)",
testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c +12 -2
index 9101f2668..27464e79f 100644
@@ -30,13 +30,23 @@ static void verify_msgsnd(void)
  	struct msqid_ds qs_buf;
  	time_t before_snd, after_snd;
 
-	tst_syscall(__NR_time,&before_snd);
+	before_snd = syscall(__NR_time, NULL);
+	if (before_snd == -1&&  errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		before_snd = time(NULL) - 1;
+	}
+
  	TEST(msgsnd(queue_id,&snd_buf, MSGSIZE, 0));
  	if (TST_RET == -1) {
  		tst_res(TFAIL | TTERRNO, "msgsnd() failed");
  		return;
  	}
-	tst_syscall(__NR_time,&after_snd);
+
+	after_snd = syscall(__NR_time, NULL);
+	if (after_snd == -1&&  errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		after_snd = time(NULL);
+	}
 
  	SAFE_MSGCTL(queue_id, IPC_STAT,&qs_buf);
 
testcases/kernel/syscalls/ipc/shmctl/shmctl01.c +12 -2
index f5b8eaef9..356513726 100644
@@ -241,9 +241,19 @@ static int get_shm_idx_from_id(int shm_id)
 
  static void setup(void)
  {
-	ctime_min = tst_syscall(__NR_time, NULL);
+	ctime_min = syscall(__NR_time, NULL);
+	if (ctime_min == -1&&  errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		ctime_min = time(NULL) - 1;
+	}
+
  	shm_id = SAFE_SHMGET(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | SHM_RW);
-	ctime_max = tst_syscall(__NR_time, NULL);
+
+	ctime_max = syscall(__NR_time, NULL);
+	if (ctime_max == -1&&  errno == ENOSYS) {
+		tst_res(TINFO, "__NR_time not supported");
+		ctime_max = time(NULL);
+	}
 
  	shm_idx = get_shm_idx_from_id(shm_id);
 
author
#3 lore raw

PATCH 2/2 syscalls: shift to time() if __NR_time not support

Hi Xu,

Quoted 2 lines · Yang Xu

I have seen this patchset, Can we use a function to check whether kernel

Yes, we can, I was even thinking to define a global MACRO can check any syscall not only __NR_time.
(maybe we can achieve it for other tests)
But for this kind of case, I'd not suggest using that MACRO/function to check __NR_time, because the test will perform twice at the moment for the __NR_time syscall if it supporting(first time for support checking, second time for real invoking).

Considering this is a time comparing test, that makes our seconds more inaccurate to compare.

supports time syscall (like time_supported_by_kernel()) in setup

Quoted 2 lines

and then we use time() - !return value in verify funtion?

Though we check the syscall in setup(), shouldn't we also export a variable to record the result we checked? That does not make things be simple too.

Signature · 5 lines
Regards,
Li Wang
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.linux.it/pipermail/ltp/attachments/20201124/f2bb2de4/attachment.htm>
#4 lore raw

PATCH 2/2 syscalls: shift to time() if __NR_time not support

Hi Li

Quoted 20 lines

Hi Xu,

Yang Xu <[email protected]
<mailto:[email protected]>> wrote:

I have seen this patchset, Can we use a function to check whether kernel

Yes, we can, I was even thinking to define a global MACRO can check any syscall not only __NR_time.
(maybe we can achieve it for other tests)
But for this kind of case, I'd not suggest using that MACRO/function to check __NR_time, because the test will perform twice at the moment for the __NR_time syscall if it supporting(first time for support checking, second time for real invoking).

Considering this is a time comparing test, that makes our seconds more inaccurate to compare.

Quoted 6 lines

supports time syscall (like time_supported_by_kernel()) in setup and then we use time() - !return value in verify funtion?

Though we check the syscall in setup(), shouldn't we also export a variable to record the result we checked? That does not make things be simple too.

Yes. On some platform(aarch64), it is simple because it doesn't need to call tst_syscall two times and time() two times and report non-support info many times especailly when using -i parameters.

IMO, it is a taste perference(I usually detect kernel whether support in setup). Your patchset is also ok. So

Quoted 4 lines

--
Regards,
Li Wang

#5 lore raw

PATCH 1/2 syscalls: avoid time() using __cvdso_gettimeofday in use-level's VDSO

In reply to Li Wang

Hi!
[CC Thomas Gleixner]

Thomas can you please have a look? It looks like we can get the SysV IPC ctime to be one second off compared to what we get from realtime clock.

Do we care to get this fixed in kernel or should we fix the tests?

Quoted 123 lines

This shmctl01 test detect the time as the number of seconds twice (before and after) the shmget() instruction, then it verifies whether the 'struct shmid_ds ds' gets data correctly. But here it shows 'ds->ctime' out of the seconds range (1604298586, 1604298586),

The reason is that shmget()/msgsnd() always use ktime_get_real_second to get real seconds, but time() on aarch64 via gettimeofday() or (it depends on different kernel versions) clock_gettime() in use-level's VDSO to return tv_sec.

time()
__cvdso_gettimeofday
...

     do_gettimeofday
       ktime_get_real_ts64
        timespc64_add_ns

Situation can be simplify as difference between ktime_get_real_second and ktime_get_real_ts64. As we can see ktime_get_real_second return tk->xtime_sec directly, however

timespc64_add_ns more easily add 1 more second via "a->tv_sec +=..." on a virtual machine, that's why we got occasional errors like:

shmctl01.c:183: TFAIL: SHM_STAT: shm_ctime=1604298585, expected <1604298586,1604298586>
...
msgsnd01.c:59: TFAIL: msg_stime = 1605730573 out of [1605730574, 1605730574]

Here we propose to use '__NR_time' to invoke syscall directly that makes test all get real seconds via ktime_get_real_second.

Cc Chunyu Hu <[email protected]>
Cc Cyril Hrubis <[email protected]>
---
 testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c | 5 +++--
 testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c | 5 +++--
 testcases/kernel/syscalls/ipc/shmctl/shmctl01.c | 5 +++--
 3 files changed, 9 insertions(+), 6 deletions(-)
testcases/kernel/syscalls/ipc/msgrcv/msgrcv01.c +3 -2
index 5c1e317e9..6fdc47dc3 100644
@@ -10,6 +10,7 @@
 #include "tst_test.h"
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
+#include "tst_timer.h"
 
 static key_t msgkey;
 static int queue_id = -1, pid;
@@ -25,13 +26,13 @@ static void verify_msgrcv(void)
 
 	SAFE_MSGSND(queue_id, &snd_buf, MSGSIZE, 0);
 
-	time(&before_rcv);
+	tst_syscall(__NR_time, &before_rcv);
 	TEST(msgrcv(queue_id, &rcv_buf, MSGSIZE, 1, 0));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "msgrcv failed");
 		return;
 	}
-	time(&after_rcv);
+	tst_syscall(__NR_time, &after_rcv);
 
 	if (strcmp(rcv_buf.mtext, snd_buf.mtext) == 0)
 		tst_res(TPASS, "message received(%s) = message sent(%s)",
testcases/kernel/syscalls/ipc/msgsnd/msgsnd01.c +3 -2
index 5f5da52d2..9101f2668 100644
@@ -16,6 +16,7 @@
 #include "tst_test.h"
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
+#include "tst_timer.h"
 
 static key_t msgkey;
 static int queue_id = -1, pid;
@@ -29,13 +30,13 @@ static void verify_msgsnd(void)
 	struct msqid_ds qs_buf;
 	time_t before_snd, after_snd;
 
-	time(&before_snd);
+	tst_syscall(__NR_time, &before_snd);
 	TEST(msgsnd(queue_id, &snd_buf, MSGSIZE, 0));
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "msgsnd() failed");
 		return;
 	}
-	time(&after_snd);
+	tst_syscall(__NR_time, &after_snd);
 
 	SAFE_MSGCTL(queue_id, IPC_STAT, &qs_buf);
 
testcases/kernel/syscalls/ipc/shmctl/shmctl01.c +3 -2
index 3a39a4d74..f5b8eaef9 100644
@@ -19,6 +19,7 @@
 #include "tst_test.h"
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
+#include "tst_timer.h"
 
 #define NCHILD 20
 
@@ -240,9 +241,9 @@ static int get_shm_idx_from_id(int shm_id)
 
 static void setup(void)
 {
-	ctime_min = time(NULL);
+	ctime_min = tst_syscall(__NR_time, NULL);
 	shm_id = SAFE_SHMGET(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | SHM_RW);
-	ctime_max = time(NULL);
+	ctime_max = tst_syscall(__NR_time, NULL);
 
 	shm_idx = get_shm_idx_from_id(shm_id);
 
Signature · 1 line
2.21.1
Signature · 2 lines
Cyril Hrubis
[email protected]
#6 lore raw

Cyril,

Quoted 4 lines · Cyril Hrubis

Thomas can you please have a look? It looks like we can get the SysV IPC ctime to be one second off compared to what we get from realtime clock.

Do we care to get this fixed in kernel or should we fix the tests?

See below.

Quoted 24 lines
Quoted 24 lines

This shmctl01 test detect the time as the number of seconds twice (before and after) the shmget() instruction, then it verifies whether the 'struct shmid_ds ds' gets data correctly. But here it shows 'ds->ctime' out of the seconds range (1604298586, 1604298586),

The reason is that shmget()/msgsnd() always use ktime_get_real_second to get real seconds, but time() on aarch64 via gettimeofday() or (it depends on different kernel versions) clock_gettime() in use-level's VDSO to return tv_sec.

time()
__cvdso_gettimeofday
...

     do_gettimeofday
       ktime_get_real_ts64
        timespc64_add_ns

Situation can be simplify as difference between ktime_get_real_second and ktime_get_real_ts64. As we can see ktime_get_real_second return tk->xtime_sec directly, however

timespc64_add_ns more easily add 1 more second via "a->tv_sec +=..." on a virtual machine, that's why we got occasional errors like:

Quoted 6 lines
Quoted 6 lines

shmctl01.c:183: TFAIL: SHM_STAT: shm_ctime=1604298585, expected <1604298586,1604298586>
...
msgsnd01.c:59: TFAIL: msg_stime = 1605730573 out of [1605730574, 1605730574]

Here we propose to use '__NR_time' to invoke syscall directly that makes test all get real seconds via ktime_get_real_second.

This is a general problem and not really just for this particular test case.

Due to the internal implementation of ktime_get_real_seconds(), which is a 2038 safe replacement for the former get_seconds() function, this accumulation issue can be observed. (time(2) via syscall and newer versions of VDSO use the same mechanism).

     clock_gettime(CLOCK_REALTIME, &ts);
     sec = time();
     assert(sec >= ts.tv_sec);

That assert can trigger for two reasons:

1) Clock was set between the clock_gettime() and time().

2) The clock has advanced far enough that:

timekeeper.tv_nsec + (clock_now_ns() - last_update_ns) > NSEC_PER_SEC

#1 is just a property of clock REALTIME. There is nothing we can do
about that.

#2 is due to the optimized get_seconds()/time() access which avoids to
read the clock. This can happen on bare metal as well, but is far more likely to be exposed on virt.

The same problem exists for CLOCK_XXX vs. CLOCK_XXX_COARSE

     clock_gettime(CLOCK_XXX, &ts);
     clock_gettime(CLOCK_XXX_COARSE, &tc);
     assert(tc.tv_sec >= ts.tv_sec);

The _COARSE variants return their associated timekeeper.tv_sec,tv_nsec pair without reading the clock. Same as #2 above just extended to clock MONOTONIC.

There is no way to fix this except giving up on the fast accessors and make everything take the slow path and read the clock, which might make a lot of people unhappy.

For clock REALTIME #1 is anyway an issue, so I think documenting this proper is the right thing to do.

Thoughts?

Thanks,

tglx

#7 lore raw

Hi!

Quoted 29 lines

This is a general problem and not really just for this particular test case.

Due to the internal implementation of ktime_get_real_seconds(), which is a 2038 safe replacement for the former get_seconds() function, this accumulation issue can be observed. (time(2) via syscall and newer versions of VDSO use the same mechanism).

     clock_gettime(CLOCK_REALTIME, &ts);
     sec = time();
     assert(sec >= ts.tv_sec);

That assert can trigger for two reasons:

1) Clock was set between the clock_gettime() and time().

2) The clock has advanced far enough that:

timekeeper.tv_nsec + (clock_now_ns() - last_update_ns) > NSEC_PER_SEC

#1 is just a property of clock REALTIME. There is nothing we can do
about that.

#2 is due to the optimized get_seconds()/time() access which avoids to
read the clock. This can happen on bare metal as well, but is far more likely to be exposed on virt.

The same problem exists for CLOCK_XXX vs. CLOCK_XXX_COARSE

Quoted 7 lines
     clock_gettime(CLOCK_XXX, &ts);
     clock_gettime(CLOCK_XXX_COARSE, &tc);
     assert(tc.tv_sec >= ts.tv_sec);

The _COARSE variants return their associated timekeeper.tv_sec,tv_nsec pair without reading the clock. Same as #2 above just extended to clock MONOTONIC.

Good hint, I guess that easiest fix would be to switch to coarse timers for these tests.

Quoted 3 lines

There is no way to fix this except giving up on the fast accessors and make everything take the slow path and read the clock, which might make a lot of people unhappy.

That's understandable and reasonable. Thanks a lot for the confirmation.

Quoted 4 lines

For clock REALTIME #1 is anyway an issue, so I think documenting this proper is the right thing to do.

Thoughts?

I guess that ideally BUGS section for time(2) and clock_gettime(2) should be updated with this explanation.

Signature · 2 lines
Cyril Hrubis
[email protected]
#8 lore raw
In reply to Thomas Gleixner

Hi Thomas.

On 11/25/20 11:32 AM, Thomas Gleixner wrote:
[...]

Quoted 40 lines
Quoted 2 lines
Quoted 2 lines

Here we propose to use '__NR_time' to invoke syscall directly that makes test all get real seconds via ktime_get_real_second.

This is a general problem and not really just for this particular test case.

Due to the internal implementation of ktime_get_real_seconds(), which is a 2038 safe replacement for the former get_seconds() function, this accumulation issue can be observed. (time(2) via syscall and newer versions of VDSO use the same mechanism).

     clock_gettime(CLOCK_REALTIME, &ts);
     sec = time();
     assert(sec >= ts.tv_sec);

That assert can trigger for two reasons:

1) Clock was set between the clock_gettime() and time().

2) The clock has advanced far enough that:

timekeeper.tv_nsec + (clock_now_ns() - last_update_ns) > NSEC_PER_SEC

#1 is just a property of clock REALTIME. There is nothing we can do
about that.

#2 is due to the optimized get_seconds()/time() access which avoids to
read the clock. This can happen on bare metal as well, but is far more likely to be exposed on virt.

The same problem exists for CLOCK_XXX vs. CLOCK_XXX_COARSE

     clock_gettime(CLOCK_XXX, &ts);
     clock_gettime(CLOCK_XXX_COARSE, &tc);
     assert(tc.tv_sec >= ts.tv_sec);

The _COARSE variants return their associated timekeeper.tv_sec,tv_nsec pair without reading the clock. Same as #2 above just extended to clock MONOTONIC.

Quoted 9 lines

There is no way to fix this except giving up on the fast accessors and make everything take the slow path and read the clock, which might make a lot of people unhappy.

For clock REALTIME #1 is anyway an issue, so I think documenting this proper is the right thing to do.

Thoughts?

I completely agree with your analysis and the fact that we should document this information.

My proposal would be to use either the vDSO document present in the kernel [1] or the man pages for time(2) and clock_gettime(2). Probably the second would be more accessible to user space developers.

[1] Documentation/ABI/stable/vdso

Quoted 4 lines

Thanks,

tglx

Signature · 2 lines
Regards,
Vincenzo