Skip to contentSkip to conversation

posix-timers: add clock_compare system call

PATCH Superseded Sagi Maimon 2 participants 5 messages Mar 12, 2024 – Mar 14, 2024

Subsystems (8)

v6 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v6 1/1 posix-timers: add clock_compare system call

Some user space applications need to read a couple of different clocks. Each read requires moving from user space to kernel space. Reading each clock separately (syscall) introduces extra unpredictable/unmeasurable delay. Minimizing this delay contributes to user space actions on these clocks (e.g. synchronization etc).

Introduce a new system call clock_compare, which can be used to measure the offset between two clocks, from variety of types: PHC, virtual PHC and various system clocks (CLOCK_REALTIME, CLOCK_MONOTONIC, etc). The system call returns the clocks timestamps.

When possible, use crosstimespec to sync read values.
Else, read clock A twice (before, and after reading clock B) and average these times – to be as close as possible to the time we read clock B.

---

Addressed comments from:
- Richard Cochran : https://www.spinics.net/lists/netdev/msg964410.html

Changes since version 5:
- take only two clocks time samples
- use crosstimespec if supported

 arch/x86/entry/syscalls/syscall_64.tbl |   1 +
 drivers/ptp/ptp_clock.c                |  34 ++++--
 include/linux/posix-clock.h            |   2 +
 include/linux/syscalls.h               |   4 +
 include/uapi/asm-generic/unistd.h      |   5 +-
 kernel/time/posix-clock.c              |  25 +++++
 kernel/time/posix-timers.c             | 145 +++++++++++++++++++++++++
 kernel/time/posix-timers.h             |   2 +
 8 files changed, 207 insertions(+), 11 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
index 7e8d46f4147f..727930d27e05 100644
@@ -383,6 +383,7 @@
 459	common	lsm_get_self_attr	sys_lsm_get_self_attr
 460	common	lsm_set_self_attr	sys_lsm_set_self_attr
 461	common	lsm_list_modules	sys_lsm_list_modules
+462	common	clock_compare		sys_clock_compare
 
 #
 # Due to a historical design error, certain syscalls are numbered differently
drivers/ptp/ptp_clock.c +24 -10
index 15b804ba4868..37ce66d4159f 100644
@@ -156,17 +156,31 @@ static int ptp_clock_adjtime(struct posix_clock *pc, struct __kernel_timex *tx)
 	return err;
 }
 
+static int ptp_clock_getcrosstime(struct posix_clock *pc, struct system_device_crosststamp *xtstamp)
+{
+	struct ptp_clock *ptp = container_of(pc, struct ptp_clock, clock);
+	int err;
+
+	if (!ptp->info->getcrosststamp)
+		err = -EOPNOTSUPP;
+	else
+		err = ptp->info->getcrosststamp(ptp->info, xtstamp);
+
+	return err;
+}
+
 static struct posix_clock_operations ptp_clock_ops = {
-	.owner		= THIS_MODULE,
-	.clock_adjtime	= ptp_clock_adjtime,
-	.clock_gettime	= ptp_clock_gettime,
-	.clock_getres	= ptp_clock_getres,
-	.clock_settime	= ptp_clock_settime,
-	.ioctl		= ptp_ioctl,
-	.open		= ptp_open,
-	.release	= ptp_release,
-	.poll		= ptp_poll,
-	.read		= ptp_read,
+	.owner			= THIS_MODULE,
+	.clock_adjtime		= ptp_clock_adjtime,
+	.clock_gettime		= ptp_clock_gettime,
+	.clock_getres		= ptp_clock_getres,
+	.clock_settime		= ptp_clock_settime,
+	.clock_getcrosstime	= ptp_clock_getcrosstime,
+	.ioctl			= ptp_ioctl,
+	.open			= ptp_open,
+	.release		= ptp_release,
+	.poll			= ptp_poll,
+	.read			= ptp_read,
 };
 
 static void ptp_clock_release(struct device *dev)
include/linux/posix-clock.h +2 -0
index ef8619f48920..3a5b4bb3f56b 100644
@@ -47,6 +47,8 @@ struct posix_clock_operations {
 
 	int  (*clock_settime)(struct posix_clock *pc,
 			      const struct timespec64 *ts);
+	int  (*clock_getcrosstime)(struct posix_clock *pc,
+				   struct system_device_crosststamp *xtstamp);
 
 	/*
 	 * Optional character device methods:
include/linux/syscalls.h +4 -0
index 77eb9b0e7685..ba2ce5b927aa 100644
@@ -1188,6 +1188,10 @@ asmlinkage long sys_ni_syscall(void);
 
 asmlinkage long sys_ni_posix_timers(void);
 
+asmlinkage long clock_compare(const clockid_t clock_a, const clockid_t clock_b,
+			      struct __kernel_timespec __user *tp_a,
+			      struct __kernel_timespec __user *tp_b,
+			      int64_t __user *offs_err);
 /*
  * Kernel code should not call syscalls (i.e., sys_xyzyyz()) directly.
  * Instead, use one of the functions which work equivalently, such as
include/uapi/asm-generic/unistd.h +4 -1
index 75f00965ab15..537a35afd237 100644
@@ -842,8 +842,11 @@ __SYSCALL(__NR_lsm_set_self_attr, sys_lsm_set_self_attr)
 #define __NR_lsm_list_modules 461
 __SYSCALL(__NR_lsm_list_modules, sys_lsm_list_modules)
 
+#define __NR_clock_compare 462
+__SYSCALL(__NR_clock_compare, sys_clock_compare)
+
 #undef __NR_syscalls
-#define __NR_syscalls 462
+#define __NR_syscalls 463
 
 /*
  * 32 bit systems traditionally used different
kernel/time/posix-clock.c +25 -0
index 9de66bbbb3d1..68b2d6741036 100644
@@ -327,9 +327,34 @@ static int pc_clock_settime(clockid_t id, const struct timespec64 *ts)
 	return err;
 }
 
+static int pc_clock_get_crosstime(clockid_t id, struct system_device_crosststamp *xtstamp)
+{
+	struct posix_clock_desc cd;
+	int err;
+
+	err = get_clock_desc(id, &cd);
+	if (err)
+		return err;
+
+	if ((cd.fp->f_mode & FMODE_WRITE) == 0) {
+		err = -EACCES;
+		goto out;
+	}
+
+	if (cd.clk->ops.clock_getcrosstime)
+		err = cd.clk->ops.clock_getcrosstime(cd.clk, xtstamp);
+	else
+		err = -EOPNOTSUPP;
+out:
+	put_clock_desc(&cd);
+
+	return err;
+}
+
 const struct k_clock clock_posix_dynamic = {
 	.clock_getres		= pc_clock_getres,
 	.clock_set		= pc_clock_settime,
 	.clock_get_timespec	= pc_clock_gettime,
 	.clock_adj		= pc_clock_adjtime,
+	.clock_get_crosstimespec	= pc_clock_get_crosstime,
 };
kernel/time/posix-timers.c +145 -0

151 lines not on this page · raw message

kernel/time/posix-timers.h +2 -0
index f32a2ebba9b8..b1f6075f35bb 100644
@@ -11,6 +11,8 @@ struct k_clock {
 				      struct timespec64 *tp);
 	/* Returns the clock value in the root time namespace. */
 	ktime_t	(*clock_get_ktime)(const clockid_t which_clock);
+	int	(*clock_get_crosstimespec)(const clockid_t which_clock,
+					   struct system_device_crosststamp *xtstamp);
 	int	(*clock_adj)(const clockid_t which_clock, struct __kernel_timex *tx);
 	int	(*timer_create)(struct k_itimer *timer);
 	int	(*nsleep)(const clockid_t which_clock, int flags,
Signature · 1 line
2.26.3
maintainer
#1 lore raw
Quoted 11 lines · Sagi Maimon

Some user space applications need to read a couple of different clocks. Each read requires moving from user space to kernel space. Reading each clock separately (syscall) introduces extra unpredictable/unmeasurable delay. Minimizing this delay contributes to user space actions on these clocks (e.g. synchronization etc).

Introduce a new system call clock_compare, which can be used to measure the offset between two clocks, from variety of types: PHC, virtual PHC and various system clocks (CLOCK_REALTIME, CLOCK_MONOTONIC, etc). The system call returns the clocks timestamps.

Quoted 5 lines

When possible, use crosstimespec to sync read values.
Else, read clock A twice (before, and after reading clock B) and average these times – to be as close as possible to the time we read clock B.

I like this a lot better than the previous versions I looked at, so just a few ideas here how this might be improved further.

Quoted 30 lines

+/**
+ * clock_compare - Get couple of clocks time stamps
+ * @clock_a: clock a ID
+ * @clock_b: clock b ID
+ * @tp_a: Pointer to a user space timespec64 for clock a storage
+ * @tp_b: Pointer to a user space timespec64 for clock b storage
+ *
+ * clock_compare gets time sample of two clocks.
+ * Supported clocks IDs: PHC, virtual PHC and various system clocks.
+ *
+ * In case of PHC that supports crosstimespec and the other clock is Monotonic raw
+ * or system time, crosstimespec will be used to synchronously capture
+ * system/device time stamp.
+ *
+ * In other cases: Read clock_a twice (before, and after reading clock_b) and
+ * average these times – to be as close as possible to the time we read clock_b.
+ *
+ * Returns:
+ * 0 Success. @tp_a and @tp_b contains the time stamps
+ * -EINVAL @clock a or b ID is not a valid clock ID
+ * -EFAULT Copying the time stamp to @tp_a or @tp_b faulted
+ * -EOPNOTSUPP Dynamic POSIX clock does not support crosstimespec()
+ **/
+SYSCALL_DEFINE5(clock_compare, const clockid_t, clock_a, const clockid_t, clock_b,

(quoted diff) +1 -0
+		struct __kernel_timespec __user *, tp_a, struct __kernel_timespec 

__user *,

kernel/time/posix-timers.c SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, · line 1454

Quoted 1 line
kernel/time/posix-timers.c +1 -0
+		tp_b, int64_t __user *, offs_err)

The system call is well-formed in the way that the ABI is the same across all supported architectures, good.

A minor issue is the use of int64_t, which in user interfaces can cause namespace problems. Please change that to the kernel side __s64 type.

kernel/time/posix-timers.c SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, · line 1475

Quoted 11 lines
kernel/time/posix-timers.c +11 -0
+	kc_a = clockid_to_kclock(clock_a);
+	if (!kc_a) {
+		error = -EINVAL;
+		return error;
+	}
+
+	kc_b = clockid_to_kclock(clock_b);
+	if (!kc_b) {
+		error = -EINVAL;
+		return error;
+	}

I'm not sure if we really need to have it generic enough to support any combination of clocks here. It complicates the implementation a bit but it also generalizes the user space side of it.

Can you think of cases where you want to compare against something other than CLOCK_MONOTONIC_RAW or CLOCK_REALTIME, or are these going to be the ones that you expect to be used anyway?

kernel/time/posix-timers.c SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, · line 1552

Quoted 13 lines
kernel/time/posix-timers.c +13 -0
+	if (crosstime_support_a) {
+		ktime_a1 = xtstamp_a1.device;
+		ktime_a2 = xtstamp_a2.device;
+	} else {
+		ktime_a1 = timespec64_to_ktime(ts_a1);
+		ktime_a2 = timespec64_to_ktime(ts_a2);
+	}
+
+	ktime_a = ktime_add(ktime_a1, ktime_a2);
+
+	ts_offs = ktime_divns(ktime_a, 2);
+
+	ts_a1 = ns_to_timespec64(ts_offs);

Converting nanoseconds to timespec64 is rather expensive, so I wonder if this could be changed to something cheaper, either by returning nanoseconds in the end and consistently working on those, or by doing the calculation on the timespec64 itself.

Arnd

#2 lore raw

Hi Arnd
Thanks for you comments.

Quoted 57 lines · Arnd Bergmann
Quoted 11 lines · Sagi Maimon

Some user space applications need to read a couple of different clocks. Each read requires moving from user space to kernel space. Reading each clock separately (syscall) introduces extra unpredictable/unmeasurable delay. Minimizing this delay contributes to user space actions on these clocks (e.g. synchronization etc).

Introduce a new system call clock_compare, which can be used to measure the offset between two clocks, from variety of types: PHC, virtual PHC and various system clocks (CLOCK_REALTIME, CLOCK_MONOTONIC, etc). The system call returns the clocks timestamps.

Quoted 5 lines

When possible, use crosstimespec to sync read values.
Else, read clock A twice (before, and after reading clock B) and average these times – to be as close as possible to the time we read clock B.

I like this a lot better than the previous versions I looked at, so just a few ideas here how this might be improved further.

Quoted 25 lines

+/**
+ * clock_compare - Get couple of clocks time stamps
+ * @clock_a: clock a ID
+ * @clock_b: clock b ID
+ * @tp_a: Pointer to a user space timespec64 for clock a storage
+ * @tp_b: Pointer to a user space timespec64 for clock b storage
+ *
+ * clock_compare gets time sample of two clocks.
+ * Supported clocks IDs: PHC, virtual PHC and various system clocks.
+ *
+ * In case of PHC that supports crosstimespec and the other clock is Monotonic raw
+ * or system time, crosstimespec will be used to synchronously capture
+ * system/device time stamp.
+ *
+ * In other cases: Read clock_a twice (before, and after reading clock_b) and
+ * average these times – to be as close as possible to the time we read clock_b.
+ *
+ * Returns:

+ *   0               Success. @tp_a and @tp_b contains the time stamps
+ *   -EINVAL         @clock a or b ID is not a valid clock ID
+ *   -EFAULT         Copying the time stamp to @tp_a or @tp_b faulted
+ *   -EOPNOTSUPP     Dynamic POSIX clock does not support crosstimespec()
Quoted 6 lines

+ **/
+SYSCALL_DEFINE5(clock_compare, const clockid_t, clock_a, const clockid_t, clock_b,
+ struct __kernel_timespec __user *, tp_a, struct __kernel_timespec
__user *,
+ tp_b, int64_t __user *, offs_err)

The system call is well-formed in the way that the ABI is the same across all supported architectures, good.

Quoted 4 lines

A minor issue is the use of int64_t, which in user interfaces can cause namespace problems. Please change that to the kernel side __s64 type.

you are right - it will be fixed on the next patch

Quoted 17 lines

kernel/time/posix-timers.c SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, · line 1475

Quoted 11 lines
kernel/time/posix-timers.c +11 -0
+     kc_a = clockid_to_kclock(clock_a);
+     if (!kc_a) {
+             error = -EINVAL;
+             return error;
+     }
+
+     kc_b = clockid_to_kclock(clock_b);
+     if (!kc_b) {
+             error = -EINVAL;
+             return error;
+     }

I'm not sure if we really need to have it generic enough to support any combination of clocks here. It complicates the implementation a bit but it also generalizes the user space side of it.

Quoted 5 lines

Can you think of cases where you want to compare against something other than CLOCK_MONOTONIC_RAW or CLOCK_REALTIME, or are these going to be the ones that you expect to be used anyway?

sure, one example is syncing two different PHCs (which was originally why we needed this syscall)
I hope that I have understand your note and that answers your question.

Quoted 11 lines
Quoted 11 lines
kernel/time/posix-timers.c +11 -0
+     if (crosstime_support_a) {
+             ktime_a1 = xtstamp_a1.device;
+             ktime_a2 = xtstamp_a2.device;
+     } else {
+             ktime_a1 = timespec64_to_ktime(ts_a1);
+             ktime_a2 = timespec64_to_ktime(ts_a2);
+     }
+
+     ktime_a = ktime_add(ktime_a1, ktime_a2);
+
+     ts_offs = ktime_divns(ktime_a, 2);
Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+     ts_a1 = ns_to_timespec64(ts_offs);

Converting nanoseconds to timespec64 is rather expensive, so I wonder if this could be changed to something cheaper, either by returning nanoseconds in the end and consistently working on those, or by doing the calculation on the timespec64 itself.

I prefer returning timespec64, so this system call aligns with other system calls like clock_gettime for example.
As far as doing the calculation on timespec64 itself, that looks more expansive to me, but I might be wrong.
Sagi

Quoted 1 line

Arnd

maintainer
#3 lore raw
Quoted 19 lines · Sagi Maimon
Quoted 18 lines · Arnd Bergmann

kernel/time/posix-timers.c SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, · line 1475

Quoted 11 lines · Sagi Maimon
kernel/time/posix-timers.c +11 -0
+     kc_a = clockid_to_kclock(clock_a);
+     if (!kc_a) {
+             error = -EINVAL;
+             return error;
+     }
+
+     kc_b = clockid_to_kclock(clock_b);
+     if (!kc_b) {
+             error = -EINVAL;
+             return error;
+     }

I'm not sure if we really need to have it generic enough to support any combination of clocks here. It complicates the implementation a bit but it also generalizes the user space side of it.

Quoted 8 lines
Quoted 5 lines

Can you think of cases where you want to compare against something other than CLOCK_MONOTONIC_RAW or CLOCK_REALTIME, or are these going to be the ones that you expect to be used anyway?

sure, one example is syncing two different PHCs (which was originally why we needed this syscall)
I hope that I have understand your note and that answers your question.

Right, that is clearly a sensible use case.

I'm still trying to understand the implementation for the case where you have two different PHCs and both implement clock_get_crosstimespec(). Rather than averaging between two snapshots here, I would expect this to result in something like

      ktime_a1 += xtstamp_b.sys_monoraw - xtstamp_a1.sys_monoraw;

in order get two device timestamps ktime_a1 and ktime_b that reflect the snapshots as if they were taken simulatenously. Am I missing some finer detail here, or is this something you should do?

Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
kernel/time/posix-timers.c +11 -0
+     if (crosstime_support_a) {
+             ktime_a1 = xtstamp_a1.device;
+             ktime_a2 = xtstamp_a2.device;
+     } else {
+             ktime_a1 = timespec64_to_ktime(ts_a1);
+             ktime_a2 = timespec64_to_ktime(ts_a2);
+     }
+
+     ktime_a = ktime_add(ktime_a1, ktime_a2);
+
+     ts_offs = ktime_divns(ktime_a, 2);
Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+     ts_a1 = ns_to_timespec64(ts_offs);

Converting nanoseconds to timespec64 is rather expensive, so I wonder if this could be changed to something cheaper, either by returning nanoseconds in the end and consistently working on those, or by doing the calculation on the timespec64 itself.

Quoted 4 lines

I prefer returning timespec64, so this system call aligns with other system calls like clock_gettime for example.
As far as doing the calculation on timespec64 itself, that looks more expansive to me, but I might be wrong.

In the general case, dividing a 64-bit variable by some other variable is really expensive and will take hundreds of cycles. This one is a bit cheaper because the division is done using a constant divider of NS_PER_SEC, which can get optimized fairly well on many systems by turning it into an equivalent 128-bit multiplication plus shift.

For the case where you start out with a timespec64, I would expect it to be cheaper to calculate the nanosecond difference between ts_a1 and ts_a2 to add half of that to the timespec than to average two large 64-bit values and convert that back to a timespec afterwards. This should be fairly easy to try out if you can test a 32-bit kernel. We could decide that there is no need to care about anything bug 64-bit kernels here, in which case your current version should be just as good for both the crosstime_support_a and !crosstime_support_a cases.

Arnd

#4 lore raw
Quoted 38 lines · Arnd Bergmann
Quoted 19 lines · Sagi Maimon
Quoted 18 lines · Arnd Bergmann

kernel/time/posix-timers.c SYSCALL_DEFINE4(clock_nanosleep_time32, clockid_t, which_clock, int, flags, · line 1475

Quoted 11 lines · Sagi Maimon
kernel/time/posix-timers.c +11 -0
+     kc_a = clockid_to_kclock(clock_a);
+     if (!kc_a) {
+             error = -EINVAL;
+             return error;
+     }
+
+     kc_b = clockid_to_kclock(clock_b);
+     if (!kc_b) {
+             error = -EINVAL;
+             return error;
+     }

I'm not sure if we really need to have it generic enough to support any combination of clocks here. It complicates the implementation a bit but it also generalizes the user space side of it.

Quoted 8 lines
Quoted 5 lines

Can you think of cases where you want to compare against something other than CLOCK_MONOTONIC_RAW or CLOCK_REALTIME, or are these going to be the ones that you expect to be used anyway?

sure, one example is syncing two different PHCs (which was originally why we needed this syscall)
I hope that I have understand your note and that answers your question.

Right, that is clearly a sensible use case.

I'm still trying to understand the implementation for the case where you have two different PHCs and both implement clock_get_crosstimespec(). Rather than averaging between two snapshots here, I would expect this to result in something like

Quoted 7 lines
      ktime_a1 += xtstamp_b.sys_monoraw - xtstamp_a1.sys_monoraw;

in order get two device timestamps ktime_a1 and ktime_b that reflect the snapshots as if they were taken simulatenously. Am I missing some finer detail here, or is this something you should do?

Since the raw monotonic clock and the PHC are not synthesized, that won't be accurate at all and depends on the frequency delta between them.

Quoted 32 lines
Quoted 20 lines
Quoted 11 lines
Quoted 11 lines
kernel/time/posix-timers.c +11 -0
+     if (crosstime_support_a) {
+             ktime_a1 = xtstamp_a1.device;
+             ktime_a2 = xtstamp_a2.device;
+     } else {
+             ktime_a1 = timespec64_to_ktime(ts_a1);
+             ktime_a2 = timespec64_to_ktime(ts_a2);
+     }
+
+     ktime_a = ktime_add(ktime_a1, ktime_a2);
+
+     ts_offs = ktime_divns(ktime_a, 2);
Quoted 9 lines

Review

Quoted 2 lines
(quoted diff) +2 -0
+
+     ts_a1 = ns_to_timespec64(ts_offs);

Converting nanoseconds to timespec64 is rather expensive, so I wonder if this could be changed to something cheaper, either by returning nanoseconds in the end and consistently working on those, or by doing the calculation on the timespec64 itself.

Quoted 4 lines

I prefer returning timespec64, so this system call aligns with other system calls like clock_gettime for example.
As far as doing the calculation on timespec64 itself, that looks more expansive to me, but I might be wrong.

In the general case, dividing a 64-bit variable by some other variable is really expensive and will take hundreds of cycles. This one is a bit cheaper because the division is done using a constant divider of NS_PER_SEC, which can get optimized fairly well on many systems by turning it into an equivalent 128-bit multiplication plus shift.

Quoted 11 lines

For the case where you start out with a timespec64, I would expect it to be cheaper to calculate the nanosecond difference between ts_a1 and ts_a2 to add half of that to the timespec than to average two large 64-bit values and convert that back to a timespec afterwards. This should be fairly easy to try out if you can test a 32-bit kernel. We could decide that there is no need to care about anything bug 64-bit kernels here, in which case your current version should be just as good for both the crosstime_support_a and !crosstime_support_a cases.

sounds good, it will be done on the next patch.

Quoted 1 line

Arnd