Skip to contentSkip to conversation

y2038: Adding monitor_wait64()

PATCH Applied Florian Bezdeka 2 participants 6 messages Aug 11, 2021 – Aug 13, 2021

lore
Share & tools
lore
#0 lore raw

v2 cover y2038: Adding monitor_wait64()

Hi!

Just another y2038 related syscall, once again based on v1 sent out by Song and rebased to current next.

Best regards,
Florian

Florian Bezdeka (2):
y2038: cobalt/posix/monitor: Adding monitor_wait64
y2038: testsuite/smokey/y2038: Adding testcases for monitor_wait64()

Song Chen (1):
y2038: lib/cobalt/internal: dispatch monitor_wait

 include/cobalt/uapi/syscall.h          |  1 +
 kernel/cobalt/posix/monitor.c          | 32 ++++++++++-
 kernel/cobalt/posix/monitor.h          | 10 ++++
 kernel/cobalt/posix/syscall32.c        |  8 +++
 kernel/cobalt/posix/syscall32.h        |  6 ++
 kernel/cobalt/trace/cobalt-posix.h     |  3 +-
 lib/cobalt/internal.c                  |  8 ++-
 testsuite/smokey/y2038/syscall-tests.c | 78 +++++++++++++++++++++++++-
 8 files changed, 141 insertions(+), 5 deletions(-)
Signature · 1 line
2.30.2
#1 lore raw

PATCH v2 1/3 y2038: cobalt/posix/monitor: Adding monitor_wait64

Adding monitor_wait64. Patch is based on v1 sent out by Song and was re-worked to apply on top of current next/master.

__cobalt_monitor_wait() is now validating the struct timespec64 it received from userland. That was not the case up to now.

---
 include/cobalt/uapi/syscall.h      |  1 +
 kernel/cobalt/posix/monitor.c      | 32 +++++++++++++++++++++++++++++-
 kernel/cobalt/posix/monitor.h      | 10 ++++++++++
 kernel/cobalt/posix/syscall32.c    |  8 ++++++++
 kernel/cobalt/posix/syscall32.h    |  6 ++++++
 kernel/cobalt/trace/cobalt-posix.h |  3 ++-
 6 files changed, 58 insertions(+), 2 deletions(-)
include/cobalt/uapi/syscall.h +1 -0
index 4de7e59d0..e49110f5d 100644
@@ -132,6 +132,7 @@
 #define sc_cobalt_mq_timedsend64		109
 #define sc_cobalt_mq_timedreceive64		110
 #define sc_cobalt_sigtimedwait64		111
+#define sc_cobalt_monitor_wait64		112
 
 #define __NR_COBALT_SYSCALLS			128 /* Power of 2 */
 
kernel/cobalt/posix/monitor.c +31 -1
index b907e0050..0d0213273 100644
@@ -20,6 +20,7 @@
 #include "clock.h"
 #include "monitor.h"
 #include <trace/events/cobalt-posix.h>
+#include <cobalt/kernel/time.h>
 
 /*
  * The Cobalt monitor is a double-wait condition object, serializing
@@ -218,8 +219,12 @@ int __cobalt_monitor_wait(struct cobalt_monitor_shadow __user *u_mon,
 
 	handle = cobalt_get_handle_from_user(&u_mon->handle);
 
-	if (ts)
+	if (ts) {
+		if (!timespec64_valid(ts))
+			return -EINVAL;
+
 		timeout = ts2ns(ts) + 1;
+	}
 
 	xnlock_get_irqsave(&nklock, s);
 
@@ -289,6 +294,24 @@ out:
 	return ret;
 }
 
+int __cobalt_monitor_wait64(struct cobalt_monitor_shadow __user *u_mon,
+			    int event,
+			    const struct __kernel_timespec __user *u_ts,
+			    int __user *u_ret)
+{
+	struct timespec64 ts, *tsp = NULL;
+	int ret;
+
+	if (u_ts) {
+		tsp = &ts;
+		ret = cobalt_get_timespec64(&ts, u_ts);
+		if (ret)
+			return ret;
+	}
+
+	return __cobalt_monitor_wait(u_mon, event, tsp, u_ret);
+}
+
 COBALT_SYSCALL(monitor_wait, nonrestartable,
 	       (struct cobalt_monitor_shadow __user *u_mon,
 	       int event, const struct __user_old_timespec __user *u_ts,
@@ -307,6 +330,13 @@ COBALT_SYSCALL(monitor_wait, nonrestartable,
 	return __cobalt_monitor_wait(u_mon, event, tsp, u_ret);
 }
 
+COBALT_SYSCALL(monitor_wait64, nonrestartable,
+	       (struct cobalt_monitor_shadow __user *u_mon, int event,
+		const struct __kernel_timespec __user *u_ts, int __user *u_ret))
+{
+	return __cobalt_monitor_wait64(u_mon, event, u_ts, u_ret);
+}
+
 COBALT_SYSCALL(monitor_sync, nonrestartable,
 	       (struct cobalt_monitor_shadow __user *u_mon))
 {
kernel/cobalt/posix/monitor.h +10 -0
index d4a4aa24e..bf8794e36 100644
@@ -42,6 +42,11 @@ int __cobalt_monitor_wait(struct cobalt_monitor_shadow __user *u_mon,
 			  int event, const struct timespec64 *ts,
 			  int __user *u_ret);
 
+int __cobalt_monitor_wait64(struct cobalt_monitor_shadow __user *u_mon,
+			    int event,
+			    const struct __kernel_timespec __user *u_ts,
+			    int __user *u_ret);
+
 COBALT_SYSCALL_DECL(monitor_init,
 		    (struct cobalt_monitor_shadow __user *u_monsh,
 		     clockid_t clk_id,
@@ -61,6 +66,11 @@ COBALT_SYSCALL_DECL(monitor_wait,
 		     int event, const struct __user_old_timespec __user *u_ts,
 		     int __user *u_ret));
 
+COBALT_SYSCALL_DECL(monitor_wait64,
+		    (struct cobalt_monitor_shadow __user *u_monsh, int event,
+		     const struct __kernel_timespec __user *u_ts,
+		     int __user *u_ret));
+
 COBALT_SYSCALL_DECL(monitor_destroy,
 		    (struct cobalt_monitor_shadow __user *u_monsh));
 
kernel/cobalt/posix/syscall32.c +8 -0
index 79fc365fc..e6c9831b9 100644
@@ -768,6 +768,14 @@ COBALT_SYSCALL32emu(monitor_wait, nonrestartable,
 	return __cobalt_monitor_wait(u_mon, event, tsp, u_ret);
 }
 
+COBALT_SYSCALL32emu(monitor_wait64, nonrestartable,
+		    (struct cobalt_monitor_shadow __user *u_mon, int event,
+		     const struct __kernel_timespec __user *u_ts,
+		     int __user *u_ret))
+{
+	return __cobalt_monitor_wait64(u_mon, event, u_ts, u_ret);
+}
+
 COBALT_SYSCALL32emu(event_wait, primary,
 		    (struct cobalt_event_shadow __user *u_event,
 		     unsigned int bits,
kernel/cobalt/posix/syscall32.h +6 -0
index cf295ed5e..a2ef67808 100644
@@ -218,6 +218,12 @@ COBALT_SYSCALL32emu_DECL(monitor_wait,
 			  int event, const struct old_timespec32 __user *u_ts,
 			  int __user *u_ret));
 
+COBALT_SYSCALL32emu_DECL(monitor_wait64,
+			 (struct cobalt_monitor_shadow __user *u_mon,
+			  int event,
+			  const struct __kernel_timespec __user *u_ts,
+			  int __user *u_ret));
+
 COBALT_SYSCALL32emu_DECL(event_wait,
 			 (struct cobalt_event_shadow __user *u_event,
 			  unsigned int bits,
kernel/cobalt/trace/cobalt-posix.h +2 -1
index 5d14d851f..221e25037 100644
@@ -164,7 +164,8 @@
 		__cobalt_symbolic_syscall(mutex_timedlock64),		\
 		__cobalt_symbolic_syscall(mq_timedsend64),  		\
 		__cobalt_symbolic_syscall(mq_timedreceive64),		\
-		__cobalt_symbolic_syscall(sigtimedwait64))
+		__cobalt_symbolic_syscall(sigtimedwait64),		\
+		__cobalt_symbolic_syscall(monitor_wait64))
 
 DECLARE_EVENT_CLASS(cobalt_syscall_entry,
 	TP_PROTO(unsigned int nr),
Signature · 1 line
2.30.2
#2 lore raw

Re: [PATCH v2 1/3] y2038: cobalt/posix/monitor: Adding monitor_wait64

Review

Quoted 8 lines · Florian Bezdeka

kernel/cobalt/posix/monitor.c +4 -1
-	if (ts)
+	if (ts) {
+		if (!timespec64_valid(ts))
+			return -EINVAL;
+

  timeout = ts2ns(ts) + 1;

(quoted diff) +1 -0
+	}

That is mentioned already in the commit message but worth mentioning it
again: We now validate the timespec. I guess that's always a good idea but I don't know if there are applications relying on non-validation. The testsuite did not complain so far...

#3 lore raw

PATCH v2 2/3 y2038: lib/cobalt/internal: dispatch monitor_wait

In reply to Florian Bezdeka

From: Song Chen <[email protected]>

If sizeof time_t bigger than 4, which means glibc supports 64bit timespec, go to monitor_wait64.

otherwise, go to original monitor_wait.

[Florian: Fix warnings reported by checkpatch.pl, coding style]
---
 lib/cobalt/internal.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
lib/cobalt/internal.c +6 -2
index 42b60c35b..75933a830 100644
@@ -258,8 +258,12 @@ int cobalt_monitor_wait(cobalt_monitor_t *mon, int event,
 
 	pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, &oldtype);
 
-	ret = XENOMAI_SYSCALL4(sc_cobalt_monitor_wait,
-			       mon, event, ts, &opret);
+#ifdef __USE_TIME_BITS64
+	ret = XENOMAI_SYSCALL4(sc_cobalt_monitor_wait64, mon, event, ts,
+			       &opret);
+#else
+	ret = XENOMAI_SYSCALL4(sc_cobalt_monitor_wait, mon, event, ts, &opret);
+#endif
 
 	pthread_setcanceltype(oldtype, NULL);
 
Signature · 1 line
2.30.2
#4 lore raw

PATCH v2 3/3 y2038: testsuite/smokey/y2038: Adding testcases for monitor_wait64()

In reply to Florian Bezdeka

The time measurement is a bit different to the other y2038 related measurements we do. Using a monitor with CLOCK_REALTIME requires an absolute timeout.

---
 testsuite/smokey/y2038/syscall-tests.c | 78 +++++++++++++++++++++++++-
 1 file changed, 77 insertions(+), 1 deletion(-)
testsuite/smokey/y2038/syscall-tests.c +77 -1
index 427117038..839a50e07 100644
@@ -19,6 +19,8 @@
 #include <errno.h>
 #include <mqueue.h>
 #include <signal.h>
+#include <cobalt/uapi/monitor.h>
+#include <sys/cobalt.h>
 
 smokey_test_plugin(y2038, SMOKEY_NOARGS, "Validate correct y2038 support");
 
@@ -367,7 +369,8 @@ static int test_sc_cobalt_clock_nanosleep64(void)
 	next.tv_sec  = ts2.tv_sec + interval;
 	next.tv_nsec = ts2.tv_nsec;
 
-	ret = XENOMAI_SYSCALL4(sc_nr, CLOCK_MONOTONIC, TIMER_ABSTIME, &next, &rmt);
+	ret = XENOMAI_SYSCALL4(sc_nr, CLOCK_MONOTONIC, TIMER_ABSTIME, &next,
+			       &rmt);
 	if (!smokey_assert(!ret))
 		return ret;
 
@@ -844,6 +847,75 @@ static int test_sc_cobalt_sigtimedwait64(void)
 	return 0;
 }
 
+static int test_sc_cobalt_monitor_wait64(void)
+{
+	int ret, opret;
+	int sc_nr = sc_cobalt_monitor_wait64;
+	struct xn_timespec64 t1, t2, to;
+	struct timespec ts_nat;
+	struct cobalt_monitor_shadow mon;
+
+	ret = cobalt_monitor_init(&mon, CLOCK_REALTIME, 0);
+	if (ret)
+		return -errno;
+
+	/* Make sure we don't crash because of NULL pointers */
+	ret = XENOMAI_SYSCALL4(sc_nr, NULL, NULL, NULL, NULL);
+	if (ret == -ENOSYS) {
+		smokey_note("monitor_wait64: skipped. (no kernel support)");
+		return 0; // Not implemented, nothing to test, success
+	}
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/* Providing an invalid address has to deliver EFAULT */
+	ret = XENOMAI_SYSCALL4(sc_nr, &mon, COBALT_MONITOR_WAITGRANT,
+			       (void *)0xdeadbeefUL, NULL);
+	if (!smokey_assert(ret == -EFAULT))
+		return ret ? ret : -EINVAL;
+
+	/* providing an invalid timeout has to deliver EINVAL */
+	t1.tv_sec = -1;
+	ret = XENOMAI_SYSCALL4(sc_nr, &mon, COBALT_MONITOR_WAITGRANT, &t1,
+			       NULL);
+	if (!smokey_assert(ret == -EINVAL))
+		return ret ? ret : -EINVAL;
+
+	/*
+	 * Providing a valid timeout, waiting for it to time out and check
+	 * that we didn't come back to early.
+	 */
+	ret = clock_gettime(CLOCK_REALTIME, &ts_nat);
+	if (ret)
+		return -errno;
+
+	t1.tv_sec = ts_nat.tv_sec;
+	t1.tv_nsec = ts_nat.tv_nsec;
+
+	to = t1;
+	ts_add_ns(&to, 50000);
+
+	ret = XENOMAI_SYSCALL4(sc_nr, &mon, COBALT_MONITOR_WAITGRANT, &to,
+			       &opret);
+	if (!smokey_assert(opret == -ETIMEDOUT))
+		return ret;
+
+	ret = clock_gettime(CLOCK_REALTIME, &ts_nat);
+	if (ret)
+		return -errno;
+
+	t2.tv_sec = ts_nat.tv_sec;
+	t2.tv_nsec = ts_nat.tv_nsec;
+
+	if (ts_less(&t2, &to))
+		smokey_warning("monitor_wait64 returned too early!\n"
+			       "Expected wakeup at: %lld sec %lld nsec\n"
+			       "Back at           : %lld sec %lld nsec\n",
+			       to.tv_sec, to.tv_nsec, t2.tv_sec, t2.tv_nsec);
+
+	return 0;
+}
+
 static int run_y2038(struct smokey_test *t, int argc, char *const argv[])
 {
 	int ret;
@@ -888,5 +960,9 @@ static int run_y2038(struct smokey_test *t, int argc, char *const argv[])
 	if (ret)
 		return ret;
 
+	ret = test_sc_cobalt_monitor_wait64();
+	if (ret)
+		return ret;
+
 	return 0;
 }
Signature · 1 line
2.30.2
Applied
#5 lore raw
In reply to Florian Bezdeka
Quoted 15 lines · Florian Bezdeka

Hi!

Just another y2038 related syscall, once again based on v1 sent out by Song and rebased to current next.

Best regards,
Florian

Florian Bezdeka (2):
y2038: cobalt/posix/monitor: Adding monitor_wait64
y2038: testsuite/smokey/y2038: Adding testcases for monitor_wait64()

Song Chen (1):
y2038: lib/cobalt/internal: dispatch monitor_wait

Quoted 10 lines
 include/cobalt/uapi/syscall.h          |  1 +
 kernel/cobalt/posix/monitor.c          | 32 ++++++++++-
 kernel/cobalt/posix/monitor.h          | 10 ++++
 kernel/cobalt/posix/syscall32.c        |  8 +++
 kernel/cobalt/posix/syscall32.h        |  6 ++
 kernel/cobalt/trace/cobalt-posix.h     |  3 +-
 lib/cobalt/internal.c                  |  8 ++-
 testsuite/smokey/y2038/syscall-tests.c | 78 +++++++++++++++++++++++++-
 8 files changed, 141 insertions(+), 5 deletions(-)

Thanks, applied.

Jan

Signature · 2 lines
Siemens AG, T RDA IOT
Corporate Competence Center Embedded Linux