Skip to contentSkip to conversation

ia64: fix clock_getre(CLOCK_MONOTONIC) to report ITC frequency

PATCH Superseded Sergei Trofimovich 3 participants 7 messages Aug 15, 2022 – Sep 10, 2022

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ia64: fix clock_getre(CLOCK_MONOTONIC) to report ITC frequency

clock_gettime(CLOCK_MONOTONIC, &tp) is very precise on ia64 as it uses ITC (similar to rdtsc on x86). It's not quite a hrtimer as it is a few times slower than 1ns. Usually 2-3ns.

clock_getres(CLOCK_MONOTONIC, &res) never reflected that fact and reported 0.04s precision (1/HZ value).

In https://bugs.gentoo.org/596382 gstreamer's test suite failed loudly when it noticed precision discrepancy.

Before the change:

clock_getres(CLOCK_MONOTONIC, &res) reported 250Hz precision.

After the change:

clock_getres(CLOCK_MONOTONIC, &res) reports ITC (400Mhz) precision.

The patch is based on matoro's fix. It adds a bit of explanation why we need to special-case arch-specific clock_getres().

---
 arch/ia64/kernel/sys_ia64.c           | 26 ++++++++++++++++++++++++++
 arch/ia64/kernel/syscalls/syscall.tbl |  2 +-
 2 files changed, 27 insertions(+), 1 deletion(-)
arch/ia64/kernel/sys_ia64.c +26 -0
index e14db25146c2..d5d47eb4608e 100644
@@ -166,3 +166,29 @@ ia64_mremap (unsigned long addr, unsigned long old_len, unsigned long new_len, u
 		force_successful_syscall_return();
 	return addr;
 }
+
+asmlinkage long
+ia64_clock_getres(const clockid_t which_clock, struct __kernel_timespec __user *tp)
+{
+	/*
+	 * ia64's clock_gettime() syscall is implemented as a vdso call
+	 * fsys_clock_gettime(). Currently it handles only
+	 * CLOCK_REALTIME and CLOCK_MONOTONIC. Both are based on
+	 * 'ar.itc' counter which gets incremented at a constant
+	 * frequency. It's usually 400MHz, ~2.5x times slower than CPU
+	 * clock frequency. Which is almost a 1ns hrtimer, but not quite.
+	 *
+	 * Let's special-case these timers to report correct precision
+	 * based on ITC frequency and not HZ frequency for supported
+	 * clocks.
+	 */
+	switch (which_clock) {
+		case CLOCK_REALTIME:
+		case CLOCK_MONOTONIC:
+			s64 tick_ns = DIV_ROUND_UP(NSEC_PER_SEC, local_cpu_data->itc_freq);
+			struct timespec64 rtn_tp = ns_to_timespec64(tick_ns);
+			return put_timespec64(&rtn_tp, tp);
+	}
+
+	return sys_clock_getres(which_clock, tp);
+}
arch/ia64/kernel/syscalls/syscall.tbl +1 -1
index 78b1d03e86e1..72c929d9902b 100644
@@ -240,7 +240,7 @@
 228	common	timer_delete			sys_timer_delete
 229	common	clock_settime			sys_clock_settime
 230	common	clock_gettime			sys_clock_gettime
-231	common	clock_getres			sys_clock_getres
+231	common	clock_getres			ia64_clock_getres
 232	common	clock_nanosleep			sys_clock_nanosleep
 233	common	fstatfs64			sys_fstatfs64
 234	common	statfs64			sys_statfs64
Signature · 1 line
2.37.1
#1 lore raw
Quoted 12 lines · On Mon, 15 Aug 2022 06:49:44 +0100 Sergei Trofimovich <[email protected]> wrote:

clock_gettime(CLOCK_MONOTONIC, &tp) is very precise on ia64 as it uses ITC (similar to rdtsc on x86). It's not quite a hrtimer as it is a few times slower than 1ns. Usually 2-3ns.

clock_getres(CLOCK_MONOTONIC, &res) never reflected that fact and reported 0.04s precision (1/HZ value).

In https://bugs.gentoo.org/596382 gstreamer's test suite failed loudly when it noticed precision discrepancy.

Before the change:

Quoted 9 lines

clock_getres(CLOCK_MONOTONIC, &res) reported 250Hz precision.

After the change:

clock_getres(CLOCK_MONOTONIC, &res) reports ITC (400Mhz) precision.

The patch is based on matoro's fix. It adds a bit of explanation why we need to special-case arch-specific clock_getres().

It would be best (and nice) to include the original developer's
Signed-off-by: and to Cc Émeric Maschino if possible?

Could you please take care of these paperwork issues?

#2 lore raw

Hi Andrew, I came up with the prototype for this patch, but it was based entirely on Sergei's investigation which was documented in
https://bugs.gentoo.org/596382. I asked him to send it upstream because I'm unable to attach my realname to it due to my job. I can place a signed-off-by with my handle but I understand that's normally against kernel policy which is why I didn't. Either way the bulk of the work belongs to Sergei, I just scribbled it down, and he cleaned it up for this submission.

-------- Original Message --------
Subject: Re: [PATCH] ia64: fix clock_getre(CLOCK_MONOTONIC) to report ITC frequency
Date: 2022-08-17 15:21
From: Andrew Morton <[email protected]>
To: Sergei Trofimovich <[email protected]>

Quoted 12 lines · On Mon, 15 Aug 2022 06:49:44 +0100 Sergei Trofimovich <[email protected]> wrote:

clock_gettime(CLOCK_MONOTONIC, &tp) is very precise on ia64 as it uses ITC (similar to rdtsc on x86). It's not quite a hrtimer as it is a few times slower than 1ns. Usually 2-3ns.

clock_getres(CLOCK_MONOTONIC, &res) never reflected that fact and reported 0.04s precision (1/HZ value).

In https://bugs.gentoo.org/596382 gstreamer's test suite failed loudly when it noticed precision discrepancy.

Before the change:

Quoted 9 lines

clock_getres(CLOCK_MONOTONIC, &res) reported 250Hz precision.

After the change:

clock_getres(CLOCK_MONOTONIC, &res) reports ITC (400Mhz) precision.

The patch is based on matoro's fix. It adds a bit of explanation why we need to special-case arch-specific clock_getres().

It would be best (and nice) to include the original developer's
Signed-off-by: and to Cc Émeric Maschino if possible?

Could you please take care of these paperwork issues?

#3 lore raw
Quoted 4 lines · On Wed, 17 Aug 2022 17:57:42 -0400 matoro…

Hi Andrew, I came up with the prototype for this patch, but it was based entirely on Sergei's investigation which was documented in
https://bugs.gentoo.org/596382. I asked him to send it upstream because I'm unable to attach my realname to it due to my job.

In that case it's unclear that your Signed-off-by: is appropriate?

: Developer's Certificate of Origin 1.1
: ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
:
: By making a contribution to this project, I certify that:
:
: (a) The contribution was created in whole or in part by me and I
: have the right to submit it under the open source license
: indicated in the file; or
:
: (b) The contribution is based upon previous work that, to the best

:             of my knowledge, is covered under an appropriate open source
:             license and I have the right under that license to submit that
:             work with modifications, whether created in whole or in part
:             by me, under the same open source license (unless I am
:             permitted to submit under a different license), as indicated
:             in the file; or

:
: (c) The contribution was provided directly to me by some other
: person who certified (a), (b) or (c) and I have not modified
: it.
:
: (d) I understand and agree that this project and the contribution

:             are public and that a record of the contribution (including all
:             personal information I submit with it, including my sign-off) is
:             maintained indefinitely and may be redistributed consistent with
:             this project or the open source license(s) involved.
Quoted 5 lines

I can place a
signed-off-by with my handle but I understand that's normally against kernel policy which is why I didn't. Either way the bulk of the work belongs to Sergei, I just scribbled it down, and he cleaned it up for this submission.

I think I'll switch it to a Cc: :)

#4 lore raw

PATCH v3 1/1 ia64: fix clock_getres(CLOCK_MONOTONIC) to report ITC frequency

clock_gettime(CLOCK_MONOTONIC, &tp) is very precise on ia64 as it uses ITC (similar to rdtsc on x86). It's not quite a hrtimer as it is a few times slower than 1ns. Usually 2-3ns.

clock_getres(CLOCK_MONOTONIC, &res) never reflected that fact and reported 0.04s precision (1/HZ value).

In https://bugs.gentoo.org/596382 gstreamer's test suite failed loudly when it noticed precision discrepancy.

Before the change:

clock_getres(CLOCK_MONOTONIC, &res) reported 250Hz precision.

After the change:

clock_getres(CLOCK_MONOTONIC, &res) reports ITC (400Mhz) precision.

The patch is based on matoro's fix. I added a bit of explanation why we need to special-case arch-specific clock_getres().

---
Change since v2:
- Moved matoro to CC
Change since v1:
- No code or in-code comment change
- CCed Émeric
- Added matoro's S-O-B the way matoro comfortable with
- Fixed Subject typo s/clock_getre/clock_getres/

 arch/ia64/kernel/sys_ia64.c           | 26 ++++++++++++++++++++++++++
 arch/ia64/kernel/syscalls/syscall.tbl |  2 +-
 2 files changed, 27 insertions(+), 1 deletion(-)
arch/ia64/kernel/sys_ia64.c +26 -0
index e14db25146c2..d5d47eb4608e 100644
@@ -166,3 +166,29 @@ ia64_mremap (unsigned long addr, unsigned long old_len, unsigned long new_len, u
 		force_successful_syscall_return();
 	return addr;
 }
+
+asmlinkage long
+ia64_clock_getres(const clockid_t which_clock, struct __kernel_timespec __user *tp)
+{
+	/*
+	 * ia64's clock_gettime() syscall is implemented as a vdso call
+	 * fsys_clock_gettime(). Currently it handles only
+	 * CLOCK_REALTIME and CLOCK_MONOTONIC. Both are based on
+	 * 'ar.itc' counter which gets incremented at a constant
+	 * frequency. It's usually 400MHz, ~2.5x times slower than CPU
+	 * clock frequency. Which is almost a 1ns hrtimer, but not quite.
+	 *
+	 * Let's special-case these timers to report correct precision
+	 * based on ITC frequency and not HZ frequency for supported
+	 * clocks.
+	 */
+	switch (which_clock) {
+		case CLOCK_REALTIME:
+		case CLOCK_MONOTONIC:
+			s64 tick_ns = DIV_ROUND_UP(NSEC_PER_SEC, local_cpu_data->itc_freq);
+			struct timespec64 rtn_tp = ns_to_timespec64(tick_ns);
+			return put_timespec64(&rtn_tp, tp);
+	}
+
+	return sys_clock_getres(which_clock, tp);
+}
arch/ia64/kernel/syscalls/syscall.tbl +1 -1
index 78b1d03e86e1..72c929d9902b 100644
@@ -240,7 +240,7 @@
 228	common	timer_delete			sys_timer_delete
 229	common	clock_settime			sys_clock_settime
 230	common	clock_gettime			sys_clock_gettime
-231	common	clock_getres			sys_clock_getres
+231	common	clock_getres			ia64_clock_getres
 232	common	clock_nanosleep			sys_clock_nanosleep
 233	common	fstatfs64			sys_fstatfs64
 234	common	statfs64			sys_statfs64
Signature · 1 line
2.37.2
#5 lore raw
In reply to Andrew Morton
Quoted 20 lines · On Wed, 17 Aug 2022 12:21:03 -0700 Andrew Morton <[email protected]>…
Quoted 18 lines · On Mon, 15 Aug 2022 06:49:44 +0100 Sergei Trofimovich <[email protected]> wrote:

clock_gettime(CLOCK_MONOTONIC, &tp) is very precise on ia64 as it uses ITC (similar to rdtsc on x86). It's not quite a hrtimer as it is a few times slower than 1ns. Usually 2-3ns.

clock_getres(CLOCK_MONOTONIC, &res) never reflected that fact and reported 0.04s precision (1/HZ value).

In https://bugs.gentoo.org/596382 gstreamer's test suite failed loudly when it noticed precision discrepancy.

Before the change:

clock_getres(CLOCK_MONOTONIC, &res) reported 250Hz precision.

After the change:

clock_getres(CLOCK_MONOTONIC, &res) reports ITC (400Mhz) precision.

Quoted 8 lines
Quoted 3 lines

The patch is based on matoro's fix. It adds a bit of explanation why we need to special-case arch-specific clock_getres().

It would be best (and nice) to include the original developer's
Signed-off-by: and to Cc Émeric Maschino if possible?

Could you please take care of these paperwork issues?

Sounds good!

I'll use matoro's nickname as is for S-o-B the way matoro is comfortable with to share it. Will add Émeric in v2 and send out in a few minutes.

Signature · 1 line
  Sergei
#6 lore raw

PATCH v2 1/1 ia64: fix clock_getres(CLOCK_MONOTONIC) to report ITC frequency

clock_gettime(CLOCK_MONOTONIC, &tp) is very precise on ia64 as it uses ITC (similar to rdtsc on x86). It's not quite a hrtimer as it is a few times slower than 1ns. Usually 2-3ns.

clock_getres(CLOCK_MONOTONIC, &res) never reflected that fact and reported 0.04s precision (1/HZ value).

In https://bugs.gentoo.org/596382 gstreamer's test suite failed loudly when it noticed precision discrepancy.

Before the change:

clock_getres(CLOCK_MONOTONIC, &res) reported 250Hz precision.

After the change:

clock_getres(CLOCK_MONOTONIC, &res) reports ITC (400Mhz) precision.

The patch is based on matoro's fix. I added a bit of explanation why we need to special-case arch-specific clock_getres().

---
Change since v1:
- No code or in-code comment change
- CCed Émeric
- Added matoro's S-O-B the way matoro comfortable with
- Fixed Subject typo s/clock_getre/clock_getres/

 arch/ia64/kernel/sys_ia64.c           | 26 ++++++++++++++++++++++++++
 arch/ia64/kernel/syscalls/syscall.tbl |  2 +-
 2 files changed, 27 insertions(+), 1 deletion(-)
arch/ia64/kernel/sys_ia64.c +26 -0
index e14db25146c2..d5d47eb4608e 100644
@@ -166,3 +166,29 @@ ia64_mremap (unsigned long addr, unsigned long old_len, unsigned long new_len, u
 		force_successful_syscall_return();
 	return addr;
 }
+
+asmlinkage long
+ia64_clock_getres(const clockid_t which_clock, struct __kernel_timespec __user *tp)
+{
+	/*
+	 * ia64's clock_gettime() syscall is implemented as a vdso call
+	 * fsys_clock_gettime(). Currently it handles only
+	 * CLOCK_REALTIME and CLOCK_MONOTONIC. Both are based on
+	 * 'ar.itc' counter which gets incremented at a constant
+	 * frequency. It's usually 400MHz, ~2.5x times slower than CPU
+	 * clock frequency. Which is almost a 1ns hrtimer, but not quite.
+	 *
+	 * Let's special-case these timers to report correct precision
+	 * based on ITC frequency and not HZ frequency for supported
+	 * clocks.
+	 */
+	switch (which_clock) {
+		case CLOCK_REALTIME:
+		case CLOCK_MONOTONIC:
+			s64 tick_ns = DIV_ROUND_UP(NSEC_PER_SEC, local_cpu_data->itc_freq);
+			struct timespec64 rtn_tp = ns_to_timespec64(tick_ns);
+			return put_timespec64(&rtn_tp, tp);
+	}
+
+	return sys_clock_getres(which_clock, tp);
+}
arch/ia64/kernel/syscalls/syscall.tbl +1 -1
index 78b1d03e86e1..72c929d9902b 100644
@@ -240,7 +240,7 @@
 228	common	timer_delete			sys_timer_delete
 229	common	clock_settime			sys_clock_settime
 230	common	clock_gettime			sys_clock_gettime
-231	common	clock_getres			sys_clock_getres
+231	common	clock_getres			ia64_clock_getres
 232	common	clock_nanosleep			sys_clock_nanosleep
 233	common	fstatfs64			sys_fstatfs64
 234	common	statfs64			sys_statfs64
Signature · 1 line
2.37.1