Skip to contentSkip to conversation

vdso/gettimeofday: Some cleanups and refactorings

PATCH Applied Thomas Weißschuh 3 participants 15 messages Feb 27 – Mar 11 Generic VDSO library

lore
Share & tools
lore
#0 lore raw

cover vdso/gettimeofday: Some cleanups and refactorings

Refactor some of the generic vDSO code to be less repetitive.

---
Thomas Weißschuh (4):
vdso/gettimeofday: Drop a few usages of __maybe_unused
vdso/gettimeofday: Add a helper to read the sequence lock of a time namespace aware clock vdso/gettimeofday: Add a helper to test if a clock is namespaced vdso/gettimeofday: Move the unlikely() into vdso_read_retry()

 include/vdso/helpers.h  | 30 +++++++++++++++++-
 lib/vdso/gettimeofday.c | 82 ++++++++++++++-----------------------------------
 2 files changed, 52 insertions(+), 60 deletions(-)

---
base-commit: a4bce5f2dd9d89ebfc644082550f27e5068a4f0a
change-id: 20260213-vdso-cleanups-a342c4ffa81d

Best regards,

Signature · 1 line
Thomas Weißschuh <[email protected]>
#1 lore raw

PATCH 1/4 vdso/gettimeofday: Drop a few usages of __maybe_unused

These functions are used from the very same file,
so this annotation is unnecessary.

---
 lib/vdso/gettimeofday.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/vdso/gettimeofday.c +6 -6
index 4399e143d43a..4939ee86af65 100644
@@ -313,7 +313,7 @@ __cvdso_clock_gettime_common(const struct vdso_time_data *vd, clockid_t clock,
 	return do_hres(vd, vc, clock, ts);
 }
 
-static __maybe_unused int
+static int
 __cvdso_clock_gettime_data(const struct vdso_time_data *vd, clockid_t clock,
 			   struct __kernel_timespec *ts)
 {
@@ -333,7 +333,7 @@ __cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
 }
 
 #ifdef BUILD_VDSO32
-static __maybe_unused int
+static int
 __cvdso_clock_gettime32_data(const struct vdso_time_data *vd, clockid_t clock,
 			     struct old_timespec32 *res)
 {
@@ -359,7 +359,7 @@ __cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res)
 }
 #endif /* BUILD_VDSO32 */
 
-static __maybe_unused int
+static int
 __cvdso_gettimeofday_data(const struct vdso_time_data *vd,
 			  struct __kernel_old_timeval *tv, struct timezone *tz)
 {
@@ -394,7 +394,7 @@ __cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
 }
 
 #ifdef VDSO_HAS_TIME
-static __maybe_unused __kernel_old_time_t
+static __kernel_old_time_t
 __cvdso_time_data(const struct vdso_time_data *vd, __kernel_old_time_t *time)
 {
 	const struct vdso_clock *vc = vd->clock_data;
@@ -464,7 +464,7 @@ bool __cvdso_clock_getres_common(const struct vdso_time_data *vd, clockid_t cloc
 	return true;
 }
 
-static __maybe_unused
+static
 int __cvdso_clock_getres_data(const struct vdso_time_data *vd, clockid_t clock,
 			      struct __kernel_timespec *res)
 {
@@ -484,7 +484,7 @@ int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res)
 }
 
 #ifdef BUILD_VDSO32
-static __maybe_unused int
+static int
 __cvdso_clock_getres_time32_data(const struct vdso_time_data *vd, clockid_t clock,
 				 struct old_timespec32 *res)
 {
Signature · 1 line
2.53.0
#2 lore raw

[tip: timers/vdso] vdso/gettimeofday: Drop a few usages of __maybe_unused

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     08cd5e1de815842089ca3938c3ad7ac511097d70
Gitweb:        https://git.kernel.org/tip/08cd5e1de815842089ca3938c3ad7ac511097d70
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Fri, 27 Feb 2026 07:43:20 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 11 Mar 2026 10:27:35 +01:00

vdso/gettimeofday: Drop a few usages of __maybe_unused

These functions are used from the very same file,
so this annotation is unnecessary.

---
 lib/vdso/gettimeofday.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
lib/vdso/gettimeofday.c +6 -6
index 4399e14..4939ee8 100644
@@ -313,7 +313,7 @@ __cvdso_clock_gettime_common(const struct vdso_time_data *vd, clockid_t clock,
 	return do_hres(vd, vc, clock, ts);
 }
 
-static __maybe_unused int
+static int
 __cvdso_clock_gettime_data(const struct vdso_time_data *vd, clockid_t clock,
 			   struct __kernel_timespec *ts)
 {
@@ -333,7 +333,7 @@ __cvdso_clock_gettime(clockid_t clock, struct __kernel_timespec *ts)
 }
 
 #ifdef BUILD_VDSO32
-static __maybe_unused int
+static int
 __cvdso_clock_gettime32_data(const struct vdso_time_data *vd, clockid_t clock,
 			     struct old_timespec32 *res)
 {
@@ -359,7 +359,7 @@ __cvdso_clock_gettime32(clockid_t clock, struct old_timespec32 *res)
 }
 #endif /* BUILD_VDSO32 */
 
-static __maybe_unused int
+static int
 __cvdso_gettimeofday_data(const struct vdso_time_data *vd,
 			  struct __kernel_old_timeval *tv, struct timezone *tz)
 {
@@ -394,7 +394,7 @@ __cvdso_gettimeofday(struct __kernel_old_timeval *tv, struct timezone *tz)
 }
 
 #ifdef VDSO_HAS_TIME
-static __maybe_unused __kernel_old_time_t
+static __kernel_old_time_t
 __cvdso_time_data(const struct vdso_time_data *vd, __kernel_old_time_t *time)
 {
 	const struct vdso_clock *vc = vd->clock_data;
@@ -464,7 +464,7 @@ bool __cvdso_clock_getres_common(const struct vdso_time_data *vd, clockid_t cloc
 	return true;
 }
 
-static __maybe_unused
+static
 int __cvdso_clock_getres_data(const struct vdso_time_data *vd, clockid_t clock,
 			      struct __kernel_timespec *res)
 {
@@ -484,7 +484,7 @@ int __cvdso_clock_getres(clockid_t clock, struct __kernel_timespec *res)
 }
 
 #ifdef BUILD_VDSO32
-static __maybe_unused int
+static int
 __cvdso_clock_getres_time32_data(const struct vdso_time_data *vd, clockid_t clock,
 				 struct old_timespec32 *res)
 {
#3 lore raw

PATCH 2/4 vdso/gettimeofday: Add a helper to read the sequence lock of a time namespace aware clock

In reply to Thomas Weißschuh

Currently there are three different open-coded variants of a time namespace aware variant of vdso_read_begin(). They make the code hard to read and introduce an inconsistency, as only the first copy uses unlikely().

Split the code into a shared helper function.
Move that next to the definition of the regular vdso_read_begin(), so that any future changes can be kept in sync easily.

---
 include/vdso/helpers.h  | 23 ++++++++++++++++++++++
 lib/vdso/gettimeofday.c | 51 +++++++++----------------------------------------
 2 files changed, 32 insertions(+), 42 deletions(-)
include/vdso/helpers.h +23 -0
index 1a5ee9d9052c..0064c30cc40c 100644
@@ -18,6 +18,29 @@ static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 	return seq;
 }
 
+/*
+ * Variant of vdso_read_begin() to handle
+ * VDSO_CLOCKMODE_TIMENS. Time namespace enabled tasks have a
+ * special VVAR page installed which has vc->seq set to 1 and
+ * vc->clock_mode set to VDSO_CLOCKMODE_TIMENS. For non time
+ * namespace affected tasks this does not affect performance
+ * because if vc->seq is odd, i.e. a concurrent update is in
+ * progress the extra check for vc->clock_mode is just a few
+ * extra instructions while spin waiting for vc->seq to become
+ * even again.
+ */
+static __always_inline bool vdso_read_begin_timens(const struct vdso_clock *vc, u32 *seq)
+{
+	while (unlikely((*seq = READ_ONCE(vc->seq)) & 1)) {
+		if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+			return true;
+		cpu_relax();
+	}
+	smp_rmb();
+
+	return false;
+}
+
 static __always_inline u32 vdso_read_retry(const struct vdso_clock *vc,
 					   u32 start)
 {
lib/vdso/gettimeofday.c +9 -42

70 lines not on this page · raw message

Signature · 1 line
2.53.0
#4 lore raw

[tip: timers/vdso] vdso/gettimeofday: Add a helper to read the sequence lock of a time namespace aware clock

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     ed78b7b2c5ae679960469c0f679539c427e051ab
Gitweb:        https://git.kernel.org/tip/ed78b7b2c5ae679960469c0f679539c427e051ab
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Fri, 27 Feb 2026 07:43:21 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 11 Mar 2026 10:27:35 +01:00

vdso/gettimeofday: Add a helper to read the sequence lock of a time namespace aware clock

Currently there are three different open-coded variants of a time namespace aware variant of vdso_read_begin(). They make the code hard to read and introduce an inconsistency, as only the first copy uses unlikely().

Split the code into a shared helper function.
Move that next to the definition of the regular vdso_read_begin(), so that any future changes can be kept in sync easily.

---
 include/vdso/helpers.h  | 22 +++++++++++++++++-
 lib/vdso/gettimeofday.c | 51 +++++++---------------------------------
 2 files changed, 31 insertions(+), 42 deletions(-)
include/vdso/helpers.h +22 -0
index 1a5ee9d..9ccf6b5 100644
@@ -18,6 +18,28 @@ static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 	return seq;
 }
 
+/*
+ * Variant of vdso_read_begin() to handle VDSO_CLOCKMODE_TIMENS.
+ *
+ * Time namespace enabled tasks have a special VVAR page installed which has
+ * vc->seq set to 1 and vc->clock_mode set to VDSO_CLOCKMODE_TIMENS. For non
+ * time namespace affected tasks this does not affect performance because if
+ * vc->seq is odd, i.e. a concurrent update is in progress the extra check for
+ * vc->clock_mode is just a few extra instructions while spin waiting for
+ * vc->seq to become even again.
+ */
+static __always_inline bool vdso_read_begin_timens(const struct vdso_clock *vc, u32 *seq)
+{
+	while (unlikely((*seq = READ_ONCE(vc->seq)) & 1)) {
+		if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+			return true;
+		cpu_relax();
+	}
+	smp_rmb();
+
+	return false;
+}
+
 static __always_inline u32 vdso_read_retry(const struct vdso_clock *vc,
 					   u32 start)
 {
lib/vdso/gettimeofday.c +9 -42

70 lines not on this page · raw message

#5 lore raw

PATCH 3/4 vdso/gettimeofday: Add a helper to test if a clock is namespaced

In reply to Thomas Weißschuh

Currently this logic is duplicate multiple times.

Add a helper for it to make the code more readable.

---
 include/vdso/helpers.h  | 7 ++++++-
 lib/vdso/gettimeofday.c | 9 +++------
 2 files changed, 9 insertions(+), 7 deletions(-)
include/vdso/helpers.h +6 -1
index 0064c30cc40c..3dd67ed85c2e 100644
@@ -7,6 +7,11 @@
 #include <asm/barrier.h>
 #include <vdso/datapage.h>
 
+static __always_inline bool vdso_is_timens_clock(const struct vdso_clock *vc)
+{
+	return IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS;
+}
+
 static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 {
 	u32 seq;
@@ -32,7 +37,7 @@ static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 static __always_inline bool vdso_read_begin_timens(const struct vdso_clock *vc, u32 *seq)
 {
 	while (unlikely((*seq = READ_ONCE(vc->seq)) & 1)) {
-		if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+		if (vdso_is_timens_clock(vc))
 			return true;
 		cpu_relax();
 	}
lib/vdso/gettimeofday.c +3 -6
index e49369676928..2faed7851635 100644
@@ -343,8 +343,7 @@ __cvdso_gettimeofday_data(const struct vdso_time_data *vd,
 	}
 
 	if (unlikely(tz != NULL)) {
-		if (IS_ENABLED(CONFIG_TIME_NS) &&
-		    vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+		if (vdso_is_timens_clock(vc))
 			vd = __arch_get_vdso_u_timens_data(vd);
 
 		tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
@@ -367,8 +366,7 @@ __cvdso_time_data(const struct vdso_time_data *vd, __kernel_old_time_t *time)
 	const struct vdso_clock *vc = vd->clock_data;
 	__kernel_old_time_t t;
 
-	if (IS_ENABLED(CONFIG_TIME_NS) &&
-	    vc->clock_mode == VDSO_CLOCKMODE_TIMENS) {
+	if (vdso_is_timens_clock(vc)) {
 		vd = __arch_get_vdso_u_timens_data(vd);
 		vc = vd->clock_data;
 	}
@@ -399,8 +397,7 @@ bool __cvdso_clock_getres_common(const struct vdso_time_data *vd, clockid_t cloc
 	if (!vdso_clockid_valid(clock))
 		return false;
 
-	if (IS_ENABLED(CONFIG_TIME_NS) &&
-	    vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+	if (vdso_is_timens_clock(vc))
 		vd = __arch_get_vdso_u_timens_data(vd);
 
 	/*
Signature · 1 line
2.53.0
#6 lore raw

[tip: timers/vdso] vdso/gettimeofday: Add a helper to test if a clock is namespaced

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     daa2b92d33a4038123598aff55240ae3c54f870c
Gitweb:        https://git.kernel.org/tip/daa2b92d33a4038123598aff55240ae3c54f870c
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Fri, 27 Feb 2026 07:43:22 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 11 Mar 2026 10:27:35 +01:00

vdso/gettimeofday: Add a helper to test if a clock is namespaced

Currently this logic is duplicate multiple times.

Add a helper for it to make the code more readable.

---
 include/vdso/helpers.h  |  7 ++++++-
 lib/vdso/gettimeofday.c |  9 +++------
 2 files changed, 9 insertions(+), 7 deletions(-)
include/vdso/helpers.h +6 -1
index 9ccf6b5..a776554 100644
@@ -7,6 +7,11 @@
 #include <asm/barrier.h>
 #include <vdso/datapage.h>
 
+static __always_inline bool vdso_is_timens_clock(const struct vdso_clock *vc)
+{
+	return IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS;
+}
+
 static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 {
 	u32 seq;
@@ -31,7 +36,7 @@ static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 static __always_inline bool vdso_read_begin_timens(const struct vdso_clock *vc, u32 *seq)
 {
 	while (unlikely((*seq = READ_ONCE(vc->seq)) & 1)) {
-		if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+		if (vdso_is_timens_clock(vc))
 			return true;
 		cpu_relax();
 	}
lib/vdso/gettimeofday.c +3 -6
index e493696..2faed78 100644
@@ -343,8 +343,7 @@ __cvdso_gettimeofday_data(const struct vdso_time_data *vd,
 	}
 
 	if (unlikely(tz != NULL)) {
-		if (IS_ENABLED(CONFIG_TIME_NS) &&
-		    vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+		if (vdso_is_timens_clock(vc))
 			vd = __arch_get_vdso_u_timens_data(vd);
 
 		tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
@@ -367,8 +366,7 @@ __cvdso_time_data(const struct vdso_time_data *vd, __kernel_old_time_t *time)
 	const struct vdso_clock *vc = vd->clock_data;
 	__kernel_old_time_t t;
 
-	if (IS_ENABLED(CONFIG_TIME_NS) &&
-	    vc->clock_mode == VDSO_CLOCKMODE_TIMENS) {
+	if (vdso_is_timens_clock(vc)) {
 		vd = __arch_get_vdso_u_timens_data(vd);
 		vc = vd->clock_data;
 	}
@@ -399,8 +397,7 @@ bool __cvdso_clock_getres_common(const struct vdso_time_data *vd, clockid_t cloc
 	if (!vdso_clockid_valid(clock))
 		return false;
 
-	if (IS_ENABLED(CONFIG_TIME_NS) &&
-	    vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+	if (vdso_is_timens_clock(vc))
 		vd = __arch_get_vdso_u_timens_data(vd);
 
 	/*
#7 lore raw

Re: [tip: timers/vdso] vdso/gettimeofday: Add a helper to test if a clock is namespaced

Quoted 28 lines · On Wed, Mar 11, 2026 at 09:31:30AM +0000, tip-bot2 for Thomas Weißschuh wrote:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     daa2b92d33a4038123598aff55240ae3c54f870c
Gitweb:        https://git.kernel.org/tip/daa2b92d33a4038123598aff55240ae3c54f870c
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Fri, 27 Feb 2026 07:43:22 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 11 Mar 2026 10:27:35 +01:00

vdso/gettimeofday: Add a helper to test if a clock is namespaced

Currently this logic is duplicate multiple times.

Add a helper for it to make the code more readable.

---
 include/vdso/helpers.h  |  7 ++++++-
 lib/vdso/gettimeofday.c |  9 +++------
 2 files changed, 9 insertions(+), 7 deletions(-)
include/vdso/helpers.h +0 -0
index 9ccf6b5..a776554 100644

include/vdso/helpers.h line 13

Quoted 7 lines
include/vdso/helpers.h +4 -0
@@ -7,6 +7,11 @@
 #include <asm/barrier.h>
 #include <vdso/datapage.h>
 
+static __always_inline bool vdso_is_timens_clock(const struct vdso_clock *vc)
+{
+	return IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS;
+}

Welp, I never seem to have tested this together with my own vDSO header cleanup series. This fails to build because VDSO_CLOCKMODE_TIMENS is not visible. There should be an include of <vdso/clocksource.h> above.

Could you fix this up, or drop the series so I can resubmit it?

Quoted 4 lines
include/vdso/helpers.h +1 -0
+
 static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 {
 	u32 seq;

(...)

#8 lore raw

Re: [tip: timers/vdso] vdso/gettimeofday: Add a helper to test if a clock is namespaced

Quoted 10 lines · Thomas Weißschuh

include/vdso/helpers.h line 13

Quoted 4 lines
include/vdso/helpers.h +4 -0
+static __always_inline bool vdso_is_timens_clock(const struct vdso_clock *vc)
+{
+	return IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS;
+}

Welp, I never seem to have tested this together with my own vDSO header cleanup series. This fails to build because VDSO_CLOCKMODE_TIMENS is not visible. There should be an include of <vdso/clocksource.h> above.

Could you fix this up, or drop the series so I can resubmit it?

Lemme fix that up. I just saw it here too.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#9 lore raw

Re: [tip: timers/vdso] vdso/gettimeofday: Add a helper to test if a clock is namespaced

Quoted 10 lines · Borislav Petkov
Quoted 9 lines · Thomas Weißschuh

include/vdso/helpers.h line 13

Quoted 4 lines
include/vdso/helpers.h +4 -0
+static __always_inline bool vdso_is_timens_clock(const struct vdso_clock *vc)
+{
+	return IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS;
+}

Welp, I never seem to have tested this together with my own vDSO header cleanup series. This fails to build because VDSO_CLOCKMODE_TIMENS is not visible. There should be an include of <vdso/clocksource.h> above.

Quoted 3 lines
Quoted 1 line

Could you fix this up, or drop the series so I can resubmit it?

Lemme fix that up. I just saw it here too.

Thanks! And sorry for the extra work.

Thomas

#10 lore raw

Re: [tip: timers/vdso] vdso/gettimeofday: Add a helper to test if a clock is namespaced

Quoted 11 lines · Thomas Weißschuh
Quoted 10 lines · Borislav Petkov
Quoted 9 lines · Thomas Weißschuh

include/vdso/helpers.h line 13

Quoted 4 lines
include/vdso/helpers.h +4 -0
+static __always_inline bool vdso_is_timens_clock(const struct vdso_clock *vc)
+{
+	return IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS;
+}

Welp, I never seem to have tested this together with my own vDSO header cleanup series. This fails to build because VDSO_CLOCKMODE_TIMENS is not visible. There should be an include of <vdso/clocksource.h> above.

Quoted 5 lines
Quoted 3 lines
Quoted 1 line

Could you fix this up, or drop the series so I can resubmit it?

Lemme fix that up. I just saw it here too.

Thanks! And sorry for the extra work.

No worries.

Btw, this patch itself builds still, I guess the breakage is caused by one of the following ones.

I won't chase that though because this one is using VDSO_CLOCKMODE_TIMENS and I will simply add the clocksource.h include here.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#11 lore raw

[tip: timers/vdso] vdso/gettimeofday: Add a helper to test if a clock is namespaced

In reply to Thomas Weißschuh
The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     a657bebd7f02d0ec1ddb08c1d8c572fe1e187f9c
Gitweb:        https://git.kernel.org/tip/a657bebd7f02d0ec1ddb08c1d8c572fe1e187f9c
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Fri, 27 Feb 2026 07:43:22 +01:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Wed, 11 Mar 2026 15:09:59 +01:00

vdso/gettimeofday: Add a helper to test if a clock is namespaced

Currently this logic is duplicate multiple times.

Add a helper for it to make the code more readable.

[ bp: Add a missing clocksource.h include, see

    https://lore.kernel.org/r/20260311113435-f72f81d8-33a6-4a0f-bd80-4997aad068cc@linutronix.de ]
---
 include/vdso/helpers.h  |  8 +++++++-
 lib/vdso/gettimeofday.c |  9 +++------
 2 files changed, 10 insertions(+), 7 deletions(-)
include/vdso/helpers.h +7 -1
index 9ccf6b5..4d35877 100644
@@ -6,6 +6,12 @@
 
 #include <asm/barrier.h>
 #include <vdso/datapage.h>
+#include <vdso/clocksource.h>
+
+static __always_inline bool vdso_is_timens_clock(const struct vdso_clock *vc)
+{
+	return IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS;
+}
 
 static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 {
@@ -31,7 +37,7 @@ static __always_inline u32 vdso_read_begin(const struct vdso_clock *vc)
 static __always_inline bool vdso_read_begin_timens(const struct vdso_clock *vc, u32 *seq)
 {
 	while (unlikely((*seq = READ_ONCE(vc->seq)) & 1)) {
-		if (IS_ENABLED(CONFIG_TIME_NS) && vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+		if (vdso_is_timens_clock(vc))
 			return true;
 		cpu_relax();
 	}
lib/vdso/gettimeofday.c +3 -6
index e493696..2faed78 100644
@@ -343,8 +343,7 @@ __cvdso_gettimeofday_data(const struct vdso_time_data *vd,
 	}
 
 	if (unlikely(tz != NULL)) {
-		if (IS_ENABLED(CONFIG_TIME_NS) &&
-		    vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+		if (vdso_is_timens_clock(vc))
 			vd = __arch_get_vdso_u_timens_data(vd);
 
 		tz->tz_minuteswest = vd[CS_HRES_COARSE].tz_minuteswest;
@@ -367,8 +366,7 @@ __cvdso_time_data(const struct vdso_time_data *vd, __kernel_old_time_t *time)
 	const struct vdso_clock *vc = vd->clock_data;
 	__kernel_old_time_t t;
 
-	if (IS_ENABLED(CONFIG_TIME_NS) &&
-	    vc->clock_mode == VDSO_CLOCKMODE_TIMENS) {
+	if (vdso_is_timens_clock(vc)) {
 		vd = __arch_get_vdso_u_timens_data(vd);
 		vc = vd->clock_data;
 	}
@@ -399,8 +397,7 @@ bool __cvdso_clock_getres_common(const struct vdso_time_data *vd, clockid_t cloc
 	if (!vdso_clockid_valid(clock))
 		return false;
 
-	if (IS_ENABLED(CONFIG_TIME_NS) &&
-	    vc->clock_mode == VDSO_CLOCKMODE_TIMENS)
+	if (vdso_is_timens_clock(vc))
 		vd = __arch_get_vdso_u_timens_data(vd);
 
 	/*
#12 lore raw

PATCH 4/4 vdso/gettimeofday: Move the unlikely() into vdso_read_retry()

In reply to Thomas Weißschuh

All callers of vdso_read_retry() test its return value with unlikely(). Move the unlikely into the helper to make the code easier to read. This is equivalent to the retry function of non-vDSO seqlocks.

---
 include/vdso/helpers.h  |  2 +-
 lib/vdso/gettimeofday.c | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)
include/vdso/helpers.h +1 -1
index 3dd67ed85c2e..9e967c2c6d20 100644
@@ -53,7 +53,7 @@ static __always_inline u32 vdso_read_retry(const struct vdso_clock *vc,
 
 	smp_rmb();
 	seq = READ_ONCE(vc->seq);
-	return seq != start;
+	return unlikely(seq != start);
 }
 
 static __always_inline void vdso_write_seq_begin(struct vdso_clock *vc)
lib/vdso/gettimeofday.c +5 -5
index 2faed7851635..efd1b82af614 100644
@@ -135,7 +135,7 @@ bool do_hres_timens(const struct vdso_time_data *vdns, const struct vdso_clock *
 
 		if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	/* Add the namespace offset */
 	sec += offs->sec;
@@ -163,7 +163,7 @@ bool do_hres(const struct vdso_time_data *vd, const struct vdso_clock *vc,
 
 		if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	vdso_set_timespec(ts, sec, ns);
 
@@ -188,7 +188,7 @@ bool do_coarse_timens(const struct vdso_time_data *vdns, const struct vdso_clock
 		seq = vdso_read_begin(vc);
 		sec = vdso_ts->sec;
 		nsec = vdso_ts->nsec;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	/* Add the namespace offset */
 	sec += offs->sec;
@@ -212,7 +212,7 @@ bool do_coarse(const struct vdso_time_data *vd, const struct vdso_clock *vc,
 
 		ts->tv_sec = vdso_ts->sec;
 		ts->tv_nsec = vdso_ts->nsec;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	return true;
 }
@@ -244,7 +244,7 @@ bool do_aux(const struct vdso_time_data *vd, clockid_t clock, struct __kernel_ti
 
 		if (!vdso_get_timestamp(vd, vc, VDSO_BASE_AUX, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	vdso_set_timespec(ts, sec, ns);
 
Signature · 1 line
2.53.0
#13 lore raw

[tip: timers/vdso] vdso/gettimeofday: Move the unlikely() into vdso_read_retry()

The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     76970d39a0600e7432b4e4fb538ba0a81e8b4add
Gitweb:        https://git.kernel.org/tip/76970d39a0600e7432b4e4fb538ba0a81e8b4add
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Fri, 27 Feb 2026 07:43:23 +01:00
Committer:     Thomas Gleixner <[email protected]>
CommitterDate: Wed, 11 Mar 2026 10:27:35 +01:00

vdso/gettimeofday: Move the unlikely() into vdso_read_retry()

All callers of vdso_read_retry() test its return value with unlikely(). Move the unlikely into the helper to make the code easier to read. This is equivalent to the retry function of non-vDSO seqlocks.

---
 include/vdso/helpers.h  |  2 +-
 lib/vdso/gettimeofday.c | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)
include/vdso/helpers.h +1 -1
index a776554..403bb45 100644
@@ -52,7 +52,7 @@ static __always_inline u32 vdso_read_retry(const struct vdso_clock *vc,
 
 	smp_rmb();
 	seq = READ_ONCE(vc->seq);
-	return seq != start;
+	return unlikely(seq != start);
 }
 
 static __always_inline void vdso_write_seq_begin(struct vdso_clock *vc)
lib/vdso/gettimeofday.c +5 -5
index 2faed78..efd1b82 100644
@@ -135,7 +135,7 @@ bool do_hres_timens(const struct vdso_time_data *vdns, const struct vdso_clock *
 
 		if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	/* Add the namespace offset */
 	sec += offs->sec;
@@ -163,7 +163,7 @@ bool do_hres(const struct vdso_time_data *vd, const struct vdso_clock *vc,
 
 		if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	vdso_set_timespec(ts, sec, ns);
 
@@ -188,7 +188,7 @@ bool do_coarse_timens(const struct vdso_time_data *vdns, const struct vdso_clock
 		seq = vdso_read_begin(vc);
 		sec = vdso_ts->sec;
 		nsec = vdso_ts->nsec;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	/* Add the namespace offset */
 	sec += offs->sec;
@@ -212,7 +212,7 @@ bool do_coarse(const struct vdso_time_data *vd, const struct vdso_clock *vc,
 
 		ts->tv_sec = vdso_ts->sec;
 		ts->tv_nsec = vdso_ts->nsec;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	return true;
 }
@@ -244,7 +244,7 @@ bool do_aux(const struct vdso_time_data *vd, clockid_t clock, struct __kernel_ti
 
 		if (!vdso_get_timestamp(vd, vc, VDSO_BASE_AUX, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	vdso_set_timespec(ts, sec, ns);
 
#14 lore raw

[tip: timers/vdso] vdso/gettimeofday: Move the unlikely() into vdso_read_retry()

In reply to Thomas Weißschuh
The following commit has been merged into the timers/vdso branch of tip:

The following commit has been merged into the timers/vdso branch of tip:

Commit-ID:     0c02d6df15d4bf7376a965b66d92ad31b0e458fd
Gitweb:        https://git.kernel.org/tip/0c02d6df15d4bf7376a965b66d92ad31b0e458fd
Author:        Thomas Weißschuh <[email protected]>
AuthorDate:    Fri, 27 Feb 2026 07:43:23 +01:00
Committer:     Borislav Petkov (AMD) <[email protected]>
CommitterDate: Wed, 11 Mar 2026 15:12:05 +01:00

vdso/gettimeofday: Move the unlikely() into vdso_read_retry()

All callers of vdso_read_retry() test its return value with unlikely(). Move the unlikely into the helper to make the code easier to read. This is equivalent to the retry function of non-vDSO seqlocks.

---
 include/vdso/helpers.h  |  2 +-
 lib/vdso/gettimeofday.c | 10 +++++-----
 2 files changed, 6 insertions(+), 6 deletions(-)
include/vdso/helpers.h +1 -1
index 4d35877..197d233 100644
@@ -53,7 +53,7 @@ static __always_inline u32 vdso_read_retry(const struct vdso_clock *vc,
 
 	smp_rmb();
 	seq = READ_ONCE(vc->seq);
-	return seq != start;
+	return unlikely(seq != start);
 }
 
 static __always_inline void vdso_write_seq_begin(struct vdso_clock *vc)
lib/vdso/gettimeofday.c +5 -5
index 2faed78..efd1b82 100644
@@ -135,7 +135,7 @@ bool do_hres_timens(const struct vdso_time_data *vdns, const struct vdso_clock *
 
 		if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	/* Add the namespace offset */
 	sec += offs->sec;
@@ -163,7 +163,7 @@ bool do_hres(const struct vdso_time_data *vd, const struct vdso_clock *vc,
 
 		if (!vdso_get_timestamp(vd, vc, clk, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	vdso_set_timespec(ts, sec, ns);
 
@@ -188,7 +188,7 @@ bool do_coarse_timens(const struct vdso_time_data *vdns, const struct vdso_clock
 		seq = vdso_read_begin(vc);
 		sec = vdso_ts->sec;
 		nsec = vdso_ts->nsec;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	/* Add the namespace offset */
 	sec += offs->sec;
@@ -212,7 +212,7 @@ bool do_coarse(const struct vdso_time_data *vd, const struct vdso_clock *vc,
 
 		ts->tv_sec = vdso_ts->sec;
 		ts->tv_nsec = vdso_ts->nsec;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	return true;
 }
@@ -244,7 +244,7 @@ bool do_aux(const struct vdso_time_data *vd, clockid_t clock, struct __kernel_ti
 
 		if (!vdso_get_timestamp(vd, vc, VDSO_BASE_AUX, &sec, &ns))
 			return false;
-	} while (unlikely(vdso_read_retry(vc, seq)));
+	} while (vdso_read_retry(vc, seq));
 
 	vdso_set_timespec(ts, sec, ns);