Skip to contentSkip to conversation

arm64: Enforce gettimeofday vdso structure read ordering

RFC In review Christopher Covington 4 participants 10 messages Aug 11, 2016 – Aug 22, 2016 arm64

lore
Share & tools
lore
#0 lore raw

RFC 1/1 arm64: Enforce gettimeofday vdso structure read ordering

From: Brent DeGraaf <[email protected]>

Prior gettimeofday code register read code is not architecturally correct as there is no control flow gating logic enforced immediately prior to the required isb. Introduce explicit control-flow logic prior to register read in all cases so that the mrs read will always be done after all vdso data elements are read, and read all data elements within the protection logic provided by the sequence counter.

---
 arch/arm64/include/asm/vdso_datapage.h |   4 +-
 arch/arm64/kernel/vdso.c               |  11 ++--
 arch/arm64/kernel/vdso/gettimeofday.S  | 106 +++++++++++++++------------------
 3 files changed, 56 insertions(+), 65 deletions(-)
arch/arm64/include/asm/vdso_datapage.h +2 -2
index 2b9a637..49a0a51 100644
@@ -21,6 +21,8 @@
 #ifndef __ASSEMBLY__
 
 struct vdso_data {
+	__u32 tb_seq_count;	/* Timebase sequence counter */
+	__u32 use_syscall;
 	__u64 cs_cycle_last;	/* Timebase at clocksource init */
 	__u64 raw_time_sec;	/* Raw time */
 	__u64 raw_time_nsec;
@@ -30,14 +32,12 @@ struct vdso_data {
 	__u64 xtime_coarse_nsec;
 	__u64 wtm_clock_sec;	/* Wall to monotonic time */
 	__u64 wtm_clock_nsec;
-	__u32 tb_seq_count;	/* Timebase sequence counter */
 	/* cs_* members must be adjacent and in this order (ldp accesses) */
 	__u32 cs_mono_mult;	/* NTP-adjusted clocksource multiplier */
 	__u32 cs_shift;		/* Clocksource shift (mono = raw) */
 	__u32 cs_raw_mult;	/* Raw clocksource multiplier */
 	__u32 tz_minuteswest;	/* Whacky timezone stuff */
 	__u32 tz_dsttime;
-	__u32 use_syscall;
 };
 
 #endif /* !__ASSEMBLY__ */
arch/arm64/kernel/vdso.c +7 -4
index 076312b..7751667 100644
@@ -201,10 +201,12 @@ up_fail:
  */
 void update_vsyscall(struct timekeeper *tk)
 {
+	register u32 tmp;
 	u32 use_syscall = strcmp(tk->tkr_mono.clock->name, "arch_sys_counter");
 
-	++vdso_data->tb_seq_count;
-	smp_wmb();
+	tmp = smp_load_acquire(&vdso_data->tb_seq_count);
+	++tmp;
+	smp_store_release(&vdso_data->tb_seq_count, tmp);
 
 	vdso_data->use_syscall			= use_syscall;
 	vdso_data->xtime_coarse_sec		= tk->xtime_sec;
@@ -227,8 +229,9 @@ void update_vsyscall(struct timekeeper *tk)
 		vdso_data->cs_shift		= tk->tkr_mono.shift;
 	}
 
-	smp_wmb();
-	++vdso_data->tb_seq_count;
+	tmp = smp_load_acquire(&vdso_data->tb_seq_count);
+	++tmp;
+	smp_store_release(&vdso_data->tb_seq_count, tmp);
 }
 
 void update_vsyscall_tz(void)
arch/arm64/kernel/vdso/gettimeofday.S +47 -59

165 lines not on this page · raw message

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Project.
#1 lore raw
Quoted 9 lines · Christopher Covington

From: Brent DeGraaf <[email protected]>

Prior gettimeofday code register read code is not architecturally correct as there is no control flow gating logic enforced immediately prior to the required isb. Introduce explicit control-flow logic prior to register read in all cases so that the mrs read will always be done after all vdso data elements are read, and read all data elements within the protection logic provided by the sequence counter.

-ENOPARSE

Can you explain the problem that you're fixing here, please?

Quoted 41 lines
---
 arch/arm64/include/asm/vdso_datapage.h |   4 +-
 arch/arm64/kernel/vdso.c               |  11 ++--
 arch/arm64/kernel/vdso/gettimeofday.S  | 106 +++++++++++++++------------------
 3 files changed, 56 insertions(+), 65 deletions(-)
arch/arm64/include/asm/vdso_datapage.h +2 -2
index 2b9a637..49a0a51 100644
@@ -21,6 +21,8 @@
 #ifndef __ASSEMBLY__
 
 struct vdso_data {
+	__u32 tb_seq_count;	/* Timebase sequence counter */
+	__u32 use_syscall;
 	__u64 cs_cycle_last;	/* Timebase at clocksource init */
 	__u64 raw_time_sec;	/* Raw time */
 	__u64 raw_time_nsec;
@@ -30,14 +32,12 @@ struct vdso_data {
 	__u64 xtime_coarse_nsec;
 	__u64 wtm_clock_sec;	/* Wall to monotonic time */
 	__u64 wtm_clock_nsec;
-	__u32 tb_seq_count;	/* Timebase sequence counter */
 	/* cs_* members must be adjacent and in this order (ldp accesses) */
 	__u32 cs_mono_mult;	/* NTP-adjusted clocksource multiplier */
 	__u32 cs_shift;		/* Clocksource shift (mono = raw) */
 	__u32 cs_raw_mult;	/* Raw clocksource multiplier */
 	__u32 tz_minuteswest;	/* Whacky timezone stuff */
 	__u32 tz_dsttime;
-	__u32 use_syscall;
 };
 
 #endif /* !__ASSEMBLY__ */
arch/arm64/kernel/vdso.c +0 -0
index 076312b..7751667 100644
@@ -201,10 +201,12 @@ up_fail:
  */

arch/arm64/kernel/vdso.c up_fail: · line 209

Quoted 10 lines
arch/arm64/kernel/vdso.c +4 -2
 void update_vsyscall(struct timekeeper *tk)
 {
+	register u32 tmp;
 	u32 use_syscall = strcmp(tk->tkr_mono.clock->name, "arch_sys_counter");
 
-	++vdso_data->tb_seq_count;
-	smp_wmb();
+	tmp = smp_load_acquire(&vdso_data->tb_seq_count);
+	++tmp;
+	smp_store_release(&vdso_data->tb_seq_count, tmp);

This looks busted -- the writes to vdso_data can be reordered before the update of tb_seq_count.

/confused

Will

#2 lore raw
Quoted 74 lines · Will Deacon
Quoted 9 lines · Christopher Covington

From: Brent DeGraaf <[email protected]>

Prior gettimeofday code register read code is not architecturally correct as there is no control flow gating logic enforced immediately prior to the required isb. Introduce explicit control-flow logic prior to register read in all cases so that the mrs read will always be done after all vdso data elements are read, and read all data elements within the protection logic provided by the sequence counter.

-ENOPARSE

Can you explain the problem that you're fixing here, please?

Quoted 44 lines
---
 arch/arm64/include/asm/vdso_datapage.h |   4 +-
 arch/arm64/kernel/vdso.c               |  11 ++--
 arch/arm64/kernel/vdso/gettimeofday.S  | 106 
+++++++++++++++------------------
 3 files changed, 56 insertions(+), 65 deletions(-)
arch/arm64/include/asm/vdso_datapage.h +2 -2
index 2b9a637..49a0a51 100644
@@ -21,6 +21,8 @@
 #ifndef __ASSEMBLY__
 
 struct vdso_data {
+	__u32 tb_seq_count;	/* Timebase sequence counter */
+	__u32 use_syscall;
 	__u64 cs_cycle_last;	/* Timebase at clocksource init */
 	__u64 raw_time_sec;	/* Raw time */
 	__u64 raw_time_nsec;
@@ -30,14 +32,12 @@ struct vdso_data {
 	__u64 xtime_coarse_nsec;
 	__u64 wtm_clock_sec;	/* Wall to monotonic time */
 	__u64 wtm_clock_nsec;
-	__u32 tb_seq_count;	/* Timebase sequence counter */
 	/* cs_* members must be adjacent and in this order (ldp accesses) */
 	__u32 cs_mono_mult;	/* NTP-adjusted clocksource multiplier */
 	__u32 cs_shift;		/* Clocksource shift (mono = raw) */
 	__u32 cs_raw_mult;	/* Raw clocksource multiplier */
 	__u32 tz_minuteswest;	/* Whacky timezone stuff */
 	__u32 tz_dsttime;
-	__u32 use_syscall;
 };
 
 #endif /* !__ASSEMBLY__ */
arch/arm64/kernel/vdso.c +0 -0
index 076312b..7751667 100644
@@ -201,10 +201,12 @@ up_fail:
  */
 void update_vsyscall(struct timekeeper *tk)

arch/arm64/kernel/vdso.c up_fail: · line 210

Quoted 10 lines
arch/arm64/kernel/vdso.c +4 -2
 {
+	register u32 tmp;
 	u32 use_syscall = strcmp(tk->tkr_mono.clock->name, 
 "arch_sys_counter");
 
-	++vdso_data->tb_seq_count;
-	smp_wmb();
+	tmp = smp_load_acquire(&vdso_data->tb_seq_count);
+	++tmp;
+	smp_store_release(&vdso_data->tb_seq_count, tmp);

This looks busted -- the writes to vdso_data can be reordered before the
update of tb_seq_count.

Quoted 3 lines

/confused

Will

The ldar/strl contain implicit barriers that will prevent the reorder, similar to the way they allowed removal of the explicit barriers in the spinlock code.

Thank you,
Brent

#3 lore raw
In reply to Will Deacon

Sorry, this is my first go at proper email etiquette. I think I've got it now.

------------------------------------------------------------------------------
"Can you explain the problem that you're fixing here, please?"
------------------------------------------------------------------------------
Since your question included only the vdso_data structure itself and the vdso.c file changes, I'm assuming that's what you meant by "here." I moved tb_seq_count to the start of the vdso_data structure because of the
nature of the ldar/strl instructions, which do not support offsets. This avoided the extra math to locate the structure offset, while at the same
time using the same base pointer for the structure. ia64 does something similar and also puts this as the first field. I moved the use_syscall field up next to it just to keep most elements aligned according to their
width. That "use_syscall" is the first element used in the logic was an additional reason I decided to move that one in particular.

arch/arm64/include/asm/vdso_datapage.h +2 -1
@@ -21,6 +21,8 @@
  #ifndef __ASSEMBLY__
 
  struct vdso_data {
+    __u32 tb_seq_count;    /* Timebase sequence counter */
+    __u32 use_syscall;
      __u64 cs_cycle_last;    /* Timebase at clocksource init */
      __u64 raw_time_sec;    /* Raw time */
      __u64 raw_time_nsec;
@@ -30,14 +32,12 @@ struct vdso_data {
      __u64 xtime_coarse_nsec;
      __u64 wtm_clock_sec;    /* Wall to monotonic time */
      __u64 wtm_clock_nsec;
-    __u32 tb_seq_count;    /* Timebase sequence counter */
      /* cs_* members must be adjacent and in this order (ldp accesses) 

*/
__u32 cs_mono_mult; /* NTP-adjusted clocksource multiplier */
__u32 cs_shift; /* Clocksource shift (mono = raw) */
__u32 cs_raw_mult; /* Raw clocksource multiplier */
__u32 tz_minuteswest; /* Whacky timezone stuff */
__u32 tz_dsttime;
- __u32 use_syscall;
};

----------------------------------------------------------------------------
As to vdso.c, I changed the instructions used to access tb_seq_count (both
in vdso.c and the gettimeofday.S file) to the load-acquire/store-release instructions because those instructions seem to be designed for this particular scenario: their "one-way" barriers enforce the order we need, on top of eliminating the explicit barriers.
----------------------------------------------------------------------------

arch/arm64/kernel/vdso.c +1 -0
@@ -201,10 +201,12 @@ up_fail:
   */
  void update_vsyscall(struct timekeeper *tk)
  {
+    register u32 tmp;
      u32 use_syscall = strcmp(tk->tkr_mono.clock->name, 

"arch_sys_counter");

-    ++vdso_data->tb_seq_count;
-    smp_wmb();
+    tmp = smp_load_acquire(&vdso_data->tb_seq_count);
+    ++tmp;
+    smp_store_release(&vdso_data->tb_seq_count, tmp);

"This looks busted -- the writes to vdso_data can be reordered before the
update of tb_seq_count.

/confused

Will"

--------------------------------------------------------------------------

Hopefully, this more detailed explanation helps make things clearer.

Thank you,
Brent

reviewer
#4 lore raw

Hi,

Quoted 2 lines · bdegraaf at codeaurora.org

Sorry, this is my first go at proper email etiquette. I think I've got it now.

Please just reply inline, with the previous email '>' prefixed, as with this reply. Most mail clients do this for plain text email, and there are plenty of examples in the archive.

This style makes it clear who said what, even when arbitrary liens (of unnecessary context) are removed.

Quoted 8 lines

------------------------------------------------------------------------------
"Can you explain the problem that you're fixing here, please?"
------------------------------------------------------------------------------
Since your question included only the vdso_data structure itself and the vdso.c file changes, I'm assuming that's what you meant by "here." I moved tb_seq_count to the start of the vdso_data structure because of the
nature of the ldar/strl instructions, which do not support offsets.

Quoted 8 lines

This avoided the extra math to locate the structure offset, while at the same
time using the same base pointer for the structure. ia64 does something similar and also puts this as the first field. I moved the use_syscall field up next to it just to keep most elements aligned according to their
width. That "use_syscall" is the first element used in the logic was an additional reason I decided to move that one in particular.

This explains what has *changed*, but not *why*.

The title of this commit implies that this is enforcing something that wasn't previously enforced. That makes is sound like a bug fix of some sort, yet the bug is not described.

Is this a bug fix? If so, can you describe what goes wrong with the current code?

Or is this an optimisation? Or a rework for other reasons?

Quoted 26 lines
arch/arm64/include/asm/vdso_datapage.h +2 -1
@@ -21,6 +21,8 @@
 #ifndef __ASSEMBLY__
 
 struct vdso_data {
+    __u32 tb_seq_count;    /* Timebase sequence counter */
+    __u32 use_syscall;
     __u64 cs_cycle_last;    /* Timebase at clocksource init */
     __u64 raw_time_sec;    /* Raw time */
     __u64 raw_time_nsec;
@@ -30,14 +32,12 @@ struct vdso_data {
     __u64 xtime_coarse_nsec;
     __u64 wtm_clock_sec;    /* Wall to monotonic time */
     __u64 wtm_clock_nsec;
-    __u32 tb_seq_count;    /* Timebase sequence counter */
     /* cs_* members must be adjacent and in this order (ldp

accesses) */
__u32 cs_mono_mult; /* NTP-adjusted clocksource multiplier */
__u32 cs_shift; /* Clocksource shift (mono = raw) */
__u32 cs_raw_mult; /* Raw clocksource multiplier */
__u32 tz_minuteswest; /* Whacky timezone stuff */
__u32 tz_dsttime;
- __u32 use_syscall;
};

Quoted 8 lines

----------------------------------------------------------------------------
As to vdso.c, I changed the instructions used to access tb_seq_count (both
in vdso.c and the gettimeofday.S file) to the load-acquire/store-release instructions because those instructions seem to be designed for this particular scenario: their "one-way" barriers enforce the order we need, on top of eliminating the explicit barriers.
----------------------------------------------------------------------------

As Will pointed out, the use of acquire/release one-way barriers doesn't provide the guarantee you need. Two half barriers do not always combine to form a full barrier.

For example, see commit 8e86f0b409a44193 ("arm64: atomics: fix use of acquire + release for full barrier semantics), which fixes a similar misuse of acquire/release to the example below.

Quoted 10 lines
arch/arm64/include/asm/vdso_datapage.h +3 -2
-    ++vdso_data->tb_seq_count;
-    smp_wmb();
+    tmp = smp_load_acquire(&vdso_data->tb_seq_count);
+    ++tmp;
+    smp_store_release(&vdso_data->tb_seq_count, tmp);

"This looks busted -- the writes to vdso_data can be reordered before the
update of tb_seq_count.

Quoted 7 lines

/confused

Will"

--------------------------------------------------------------------------

Hopefully, this more detailed explanation helps make things clearer.

As will points out (and as covered in the description of commit 8e86f0b409a44193), the use of half-barriers allows accesses to be pulled into the critical section, where they can be reordered w.r.t. each other.

The data update after the release can be reordered before the release (but not the acquire). As it can be moved before the release, the new data can be seen with the old seq count. Similar applies to the other end, after the update, with acquire/release swapped - the updated seq count can be seen before the data update.

So to another observer, the updates of the data can be observed in an arbitrary order w.r.t. the seq count.

Thanks,
Mark.

#5 lore raw

RFC 1/1 arm64: Enforce gettimeofday vdso structure read ordering

Introduce explicit control-flow logic immediately prior to virtual counter register read in all cases so that the mrs read will always be accessed after all vdso data elements are read and sequence count is verified. Ensure data elements under the protection provided by the sequence counter are read only within the protection logic loop. Read the virtual counter as soon as possible after the data elements are confirmed correctly read, rather than after several other operations which can affect time. Reduce full barriers required in register read code in favor of the lighter-weight one-way barrier supplied by a load-acquire wherever possible.

---
 arch/arm64/include/asm/vdso_datapage.h |   4 +-
 arch/arm64/kernel/vdso/gettimeofday.S  | 107 +++++++++++++++------------------
 2 files changed, 50 insertions(+), 61 deletions(-)
arch/arm64/include/asm/vdso_datapage.h +2 -2
index 2b9a637..49a0a51 100644
@@ -21,6 +21,8 @@
 #ifndef __ASSEMBLY__
 
 struct vdso_data {
+	__u32 tb_seq_count;	/* Timebase sequence counter */
+	__u32 use_syscall;
 	__u64 cs_cycle_last;	/* Timebase at clocksource init */
 	__u64 raw_time_sec;	/* Raw time */
 	__u64 raw_time_nsec;
@@ -30,14 +32,12 @@ struct vdso_data {
 	__u64 xtime_coarse_nsec;
 	__u64 wtm_clock_sec;	/* Wall to monotonic time */
 	__u64 wtm_clock_nsec;
-	__u32 tb_seq_count;	/* Timebase sequence counter */
 	/* cs_* members must be adjacent and in this order (ldp accesses) */
 	__u32 cs_mono_mult;	/* NTP-adjusted clocksource multiplier */
 	__u32 cs_shift;		/* Clocksource shift (mono = raw) */
 	__u32 cs_raw_mult;	/* Raw clocksource multiplier */
 	__u32 tz_minuteswest;	/* Whacky timezone stuff */
 	__u32 tz_dsttime;
-	__u32 use_syscall;
 };
 
 #endif /* !__ASSEMBLY__ */
arch/arm64/kernel/vdso/gettimeofday.S +48 -59

166 lines not on this page · raw message

Signature · 3 lines
Qualcomm Datacenter Technologies, Inc. as an affiliate of Qualcomm
Technologies, Inc. Qualcomm Technologies, Inc. is a member of the Code Aurora
Forum, a Linux Foundation Collaborative Project.
reviewer
#6 lore raw

Hi,

Quoted 11 lines · Brent DeGraaf

Introduce explicit control-flow logic immediately prior to virtual counter register read in all cases so that the mrs read will always be accessed after all vdso data elements are read and sequence count is verified. Ensure data elements under the protection provided by the sequence counter are read only within the protection logic loop. Read the virtual counter as soon as possible after the data elements are confirmed correctly read, rather than after several other operations which can affect time. Reduce full barriers required in register read code in favor of the lighter-weight one-way barrier supplied by a load-acquire wherever possible.

As I commented previously, can you please explain *why* these chagnes should be made?

* What problem does this patch address?

* Is this a bug fix? If so, what problem can be seen currently?

* Is this an optimisation? If so, how much of an improvement can be seen?

In the absence of answers to the above, this patch is impossible to review, and will not get applied. It makes invasive changes, ans we need a rationale for those.

I've made some comments below, but the above is key.

[...]

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 49

Quoted 8 lines
arch/arm64/kernel/vdso/gettimeofday.S +6 -1
+	.macro	seqdata_acquire fallback, tzonly=NO_TZ, skipvcnt=0, getdata
+9999:	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
+8888:	tbnz	seqcnt, #0, 9999b
 	ldr	w_tmp, [vdso_data, #VDSO_USE_SYSCALL]
-	cbnz	w_tmp, \fail
+	cbnz	w_tmp, \fallback
+	\getdata
+	dmb	ishld	/* No loads from vdso_data after this point */

What ordering guarantee is the DMB attempting to provide? Given we have the acquire, I assume some prior load, but I couldn't figure out what specifically.

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 51

Quoted 2 lines
arch/arm64/kernel/vdso/gettimeofday.S +2 -0
+	mov	w9, seqcnt
+	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]

Usually, acquire operations pair with a release operation elsewhere. What does this pair with?

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 61

Quoted 10 lines
arch/arm64/kernel/vdso/gettimeofday.S +9 -0
+	cmp	w9, seqcnt
+	bne	8888b	/* Do not needlessly repeat ldar and its implicit barrier */
+	.if (\tzonly) != NO_TZ
+		cbz	x0, \tzonly
+	.endif
+	.if (\skipvcnt) == 0
+		isb
+		mrs	x_tmp, cntvct_el0
+	.endif
 	.endm

All this conitional code makes the callers somehwat painful to read.

It might be nicer to have this explicit in the calelrs that require it rather than conditional in the macro.

Quoted 23 lines
arch/arm64/kernel/vdso/gettimeofday.S +0 -10
 	.macro get_nsec_per_sec res
@@ -64,9 +70,6 @@ x_tmp		.req	x8
 	 * shift.
 	 */
 	.macro	get_clock_shifted_nsec res, cycle_last, mult
-	/* Read the virtual counter. */
-	isb
-	mrs	x_tmp, cntvct_el0
 	/* Calculate cycle delta and convert to ns. */
 	sub	\res, x_tmp, \cycle_last
 	/* We can only guarantee 56 bits of precision. */
@@ -137,17 +140,12 @@ x_tmp		.req	x8
 ENTRY(__kernel_gettimeofday)
 	.cfi_startproc
 	adr	vdso_data, _vdso_data
-	/* If tv is NULL, skip to the timezone code. */
-	cbz	x0, 2f
-
-	/* Compute the time of day. */
-1:	seqcnt_acquire
-	syscall_check fail=4f
-	ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST]

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 148

Quoted 10 lines
arch/arm64/kernel/vdso/gettimeofday.S +6 -4
-	/* w11 = cs_mono_mult, w12 = cs_shift */
-	ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT]
-	ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
-	seqcnt_check fail=1b
+	seqdata_acquire fallback=4f tzonly=2f getdata=__stringify(\
+		ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST];\
+		/* w11 = cs_mono_mult, w12 = cs_shift */;\
+		ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT];\
+		ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC];\
+		ldp	w4, w5, [vdso_data, #VDSO_TZ_MINWEST])

Why do we need the stringify? Is that just so we can pass the code as a macro parameter? If so, it really doesn't look like the way to go...

This is unfortunately painful to read.

Thanks,
Mark.

#7 lore raw
Quoted 15 lines · Mark Rutland

Hi,

Quoted 11 lines · Brent DeGraaf

Introduce explicit control-flow logic immediately prior to virtual counter register read in all cases so that the mrs read will always be accessed after all vdso data elements are read and sequence count is verified. Ensure data elements under the protection provided by the sequence counter are read only within the protection logic loop. Read the virtual counter as soon as possible after the data elements are confirmed correctly read, rather than after several other operations which can affect time. Reduce full barriers required in register read code in favor of the lighter-weight one-way barrier supplied by a load-acquire wherever possible.

Quoted 4 lines

As I commented previously, can you please explain *why* these chagnes should be made?

* What problem does this patch address?

Initially, I set out to fix a control-flow problem, as I originally wrote this against the code prior to the refactoring of commit b33f491f5a9aaf171b7de0f905362eb0314af478, back when there was still a do_get_tspec subroutine. That one used a dmb to order the data accesses prior to the isb/mrs sequence that read the virtual counter. Our Senior Director, who has done extensive work with the ARM cpu and is intimately familiar with the instruction set, indicated that the dmb which was used in that code was not enough to ensure ordering between the loads from the structure and the read of the virtual counter.
Since that code had no control-flow (e.g., some conditional governing the
code's progress) prior to the isb, he suggested that some form of dsb would be required to ensure proper order of access between the loads from the vdso structure and the mrs read of the the virtual counter.

I went to the latest armv8 ARM at that time and found a concrete example of how the code should be structured to ensure an ordered read of the virtual counter. In the most recent copy to which I have access (ARM DDI 0487A.j), that code is given on page D6-1871, under section D6.2.2. I moved the sequence count check immediately prior to the isb to satisfy the particular ordering requirements in that code.

When the refactored code went in recently, however, the seqcnt_read prior to the isb changed to a seqcnt_check, which addressed that ordering
requirement.

In the process of merging the prior code, however, I had noticed several other things that were wrong or not ideal and I describe the details of those problems below.

Quoted 2 lines

* Is this a bug fix? If so, what problem can be seen currently?

The most obvious problem with the existing code is where the timezone data
gets loaded: after sequence counts have been checked and rechecked, completely outside the area of the code protected by the sequence counter.
While I realize that timezone code does not change frequently, this is still a problem as the routine explicitly reads data that could be in the
process of being updated.

To make the demarkation clear with the refactored version, and keep code that reads the vdso_data cleanly contained within the protection of the sequence count, I elected to combine the two macros that check and recheck
the sequence counter. This allowed the recheck to avoid looping on the barrier requirements of a two reads of the sequence count, as branching from one macro to a label created inside another macro (e.g. label 8888 in this case) is ugly at best. This, I figured, would also help prevent future mistakes that involved access vdso_data in an unprotected fashion:
Only the block of code passed to the routine would be allowed to directly
access the vdso data.

The second problem is that the timing between the reading of the vdso data
and the virtual counter is treated as secondary in the code, as a few register manipulations using the structure data are performed prior to reading the virtual counter. While the cpu itself is free to reorder these shifts and loads somewhat, depending on the implementation, the read of the virtual counter should be performed as close as possible to the time the vdso data itself is read to minimize variability. As these manipulations and loads are not dependent on the result of the mrs read, putting them after the virtual counter isb/mrs sequence allows these independent register manipulations to issue while the mrs is still in process.

Finally, the nomenclature ("acquire_seqcnt") used by the existing code pointed toward use of the load-acquire semantic, and using those allowed me to reduce the number of explicit barriers needed in the reader code.

Quoted 3 lines

* Is this an optimisation? If so, how much of an improvement can be seen?

Optimization was not the main goal of this patch, yet performance did improve on my target, with the average time improving marginally (350 nsec after vs 360 nsec before the change), compared to the refactored code. In fact, performance is now slightly better (around a miniscule 2 nsec) than the original code, before the refactor, which hurt performance.

Quoted 10 lines

In the absence of answers to the above, this patch is impossible to review, and will not get applied. It makes invasive changes, ans we need
a rationale for those.

I've made some comments below, but the above is key.

[...]

Quoted 12 lines

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 49

Quoted 8 lines
arch/arm64/kernel/vdso/gettimeofday.S +6 -1
+	.macro	seqdata_acquire fallback, tzonly=NO_TZ, skipvcnt=0, getdata
+9999:	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
+8888:	tbnz	seqcnt, #0, 9999b
 	ldr	w_tmp, [vdso_data, #VDSO_USE_SYSCALL]
-	cbnz	w_tmp, \fail
+	cbnz	w_tmp, \fallback
+	\getdata
+	dmb	ishld	/* No loads from vdso_data after this point */

What ordering guarantee is the DMB attempting to provide? Given we have the acquire, I assume some prior load, but I couldn't figure out what specifically.

That barrier specifically ensures that loads performed by the "getdata" sequence
do not get accessed after the subsequent ldar check of the sequence counter,
since, as you know, ldar may allow loads that come before it in program order
to be accessed after it in much the same way as stlr may allow stores that come
after it to accessed before it.

Quoted 8 lines

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 51

Quoted 3 lines
arch/arm64/kernel/vdso/gettimeofday.S +2 -0
+	mov	w9, seqcnt
+	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]

Usually, acquire operations pair with a release operation elsewhere. What
does this pair with?

It was for that reason that I introduced stlr's into the writer code, but the
barrier provided by stlr was insufficient for my purposes, as Will pointed out.
There is no requirement or even suggestion in the ARM that every use of ldar
needs to be paired with stlr's. If there were, LSE instructions such as ldadda
(which does a load-acquire followed by a regular str) and ldaddl (which pairs an
ordinary load with an stlr) would not exist.

Quoted 14 lines

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 61

Quoted 11 lines
(quoted diff) +2 -0
+	cmp	w9, seqcnt
+	bne	8888b	/* Do not needlessly repeat ldar and its implicit barrier 

*/

arch/arm64/kernel/vdso/gettimeofday.S +7 -0
+	.if (\tzonly) != NO_TZ
+		cbz	x0, \tzonly
+	.endif
+	.if (\skipvcnt) == 0
+		isb
+		mrs	x_tmp, cntvct_el0
+	.endif
 	.endm

All this conitional code makes the callers somehwat painful to read.

Quoted 3 lines

It might be nicer to have this explicit in the calelrs that require it rather than conditional in the macro.

The general use-case of the acquire sequence made this the cleanest safe implementation I could come up. If this isb/mrs sequence is split out into each clock handler, it would serve to obscure the relationship between the control-flow dependency (in this case, the "bne 8888b") and the isb. Keeping this acquire sequence intact helps to ensure that future modifications adhere to the correct sequence. Note that if the caller specifies neither option, the default is to leave these items in place.

Quoted 37 lines
Quoted 23 lines
arch/arm64/kernel/vdso/gettimeofday.S +0 -10
 	.macro get_nsec_per_sec res
@@ -64,9 +70,6 @@ x_tmp		.req	x8
 	 * shift.
 	 */
 	.macro	get_clock_shifted_nsec res, cycle_last, mult
-	/* Read the virtual counter. */
-	isb
-	mrs	x_tmp, cntvct_el0
 	/* Calculate cycle delta and convert to ns. */
 	sub	\res, x_tmp, \cycle_last
 	/* We can only guarantee 56 bits of precision. */
@@ -137,17 +140,12 @@ x_tmp		.req	x8
 ENTRY(__kernel_gettimeofday)
 	.cfi_startproc
 	adr	vdso_data, _vdso_data
-	/* If tv is NULL, skip to the timezone code. */
-	cbz	x0, 2f
-
-	/* Compute the time of day. */
-1:	seqcnt_acquire
-	syscall_check fail=4f
-	ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST]

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 148

Quoted 10 lines
arch/arm64/kernel/vdso/gettimeofday.S +6 -4
-	/* w11 = cs_mono_mult, w12 = cs_shift */
-	ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT]
-	ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
-	seqcnt_check fail=1b
+	seqdata_acquire fallback=4f tzonly=2f getdata=__stringify(\
+		ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST];\
+		/* w11 = cs_mono_mult, w12 = cs_shift */;\
+		ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT];\
+		ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC];\
+		ldp	w4, w5, [vdso_data, #VDSO_TZ_MINWEST])

Why do we need the stringify? Is that just so we can pass the code as a macro parameter? If so, it really doesn't look like the way to go...

Quoted 2 lines

This is unfortunately painful to read.

I implemented it this way to remain as similar as possible with the refactored code that was recently merged, while at the same time ensuring
that, as I explained above, the reads of the vdso_data performed by each clock type are completely contained within a set of proper sequence count
checks. That they were not contained led to problems such as the improper
handling of the timezone data before, and it ensures that the isb follows
the sequence check closely. This use is not entirely dissimilar to other
code which uses stringify currently present in the arm64 kernel code which
passes code as a parameter. See, for example, arch/arm64/lib/copy_*_user.S.
All this said, however, I was never thrilled about going the stringify route, but it was the most readable of any other variants I could come up with (and far better than adding the extra ".if's" in the macro).
Do you happen to have a better suggestion?

Quoted 2 lines

Thanks,
Mark.

Thanks for your comments.
Brent

reviewer
#8 lore raw

Hi Brent,

Thanks for the thorough reply. Comments inline below.

Quoted 18 lines · bdegraaf at codeaurora.org
Quoted 1 line · Mark Rutland

* What problem does this patch address?

Initially, I set out to fix a control-flow problem, as I originally wrote this against the code prior to the refactoring of commit b33f491f5a9aaf171b7de0f905362eb0314af478, back when there was still a do_get_tspec subroutine. That one used a dmb to order the data accesses prior to the isb/mrs sequence that read the virtual counter. Our Senior Director, who has done extensive work with the ARM cpu and is intimately familiar with the instruction set, indicated that the dmb which was used in that code was not enough to ensure ordering between the loads from the structure and the read of the virtual counter.
Since that code had no control-flow (e.g., some conditional governing the code's progress) prior to the isb, he suggested that some form of dsb would be required to ensure proper order of access between the loads from the vdso structure and the mrs read of the the virtual counter.

Ok. So if I've parsed the above correctly, the fear was that an ISB was insufficient to guarantee the ordering of prior loads w.r.t. the subsequent MRS, and a control dependency between the two was necessary, in addition to the ISB.

Quoted 6 lines

I went to the latest armv8 ARM at that time and found a concrete example of how the code should be structured to ensure an ordered read of the virtual counter. In the most recent copy to which I have access (ARM DDI 0487A.j), that code is given on page D6-1871, under section D6.2.2. I moved the sequence count check immediately prior to the isb to satisfy the particular ordering requirements in that code.

My reading of that example is that the control dependency alone was insufficient (given speculation), and the ISB provided the necessary ordering between the signal variable being updated and the MRS. To me, the example does not imply that both are required in all cases, only that a control dependency alone is insufficient.

Per the description of ISB on page B2-87 of ARM DDI 0487A.j, my understanding (which may be flawed), is that the instructions prior to the ISB must be completed before the subsequent instructions are fetched+issued, and hence the MRS should be (locally) ordered w.r.t. the loads.

Quoted 3 lines

When the refactored code went in recently, however, the seqcnt_read prior to the isb changed to a seqcnt_check, which addressed that ordering requirement.

Have you seen an issue in practice prior to this? If so, we may need to go digging further into this, and consider stable kernels.

Quoted 8 lines
Quoted 1 line

* Is this a bug fix? If so, what problem can be seen currently?

The most obvious problem with the existing code is where the timezone data gets loaded: after sequence counts have been checked and rechecked, completely outside the area of the code protected by the sequence counter. While I realize that timezone code does not change frequently, this is still a problem as the routine explicitly reads data that could be in the process of being updated.

I take that you specifically mean the following line in __kernel_gettimeofday, which occurs after the usual seqcnt_acquire + seqcnt_check sequence:

ldp w4, w5, [vdso_data, #VDSO_TZ_MINWEST]

I see that the writer side for the timezone data is also not protected, since commit bdba0051ebcb3c63 ("arm64: vdso: remove broken, redundant sequence counting for timezones"). Following comments in commits I found x86 commit 6c260d586343f7f7 ("x86: vdso: Remove bogus locking in update_vsyscall_tz()").

Per the x86 commit, this is not atomic in the usual syscall path, and thus it is a cross-architecture property that timezone updates are not atomic, and that reordering of accesses may occur around a change of timezone. If we want to tighten up the VDSO, we'll also need to tighten up the syscall.

[...]

Quoted 11 lines

The second problem is that the timing between the reading of the vdso data and the virtual counter is treated as secondary in the code, as a few register manipulations using the structure data are performed prior to reading the virtual counter. While the cpu itself is free to reorder these shifts and loads somewhat, depending on the implementation, the read of the virtual counter should be performed as close as possible to the time the vdso data itself is read to minimize variability. As these manipulations and loads are not dependent on the result of the mrs read, putting them after the virtual counter isb/mrs sequence allows these independent register manipulations to issue while the mrs is still in process.

This is rather dependent on the microarchitecture. Do we have any numbers as to the variability?

Quoted 9 lines
Quoted 2 lines

* Is this an optimisation? If so, how much of an improvement can be seen?

Optimization was not the main goal of this patch, yet performance did improve on my target, with the average time improving marginally (350 nsec after vs 360 nsec before the change), compared to the refactored code. In fact, performance is now slightly better (around a miniscule 2 nsec) than the original code, before the refactor, which hurt performance.

Ok.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/arm64/kernel/vdso/gettimeofday.S +3 -0
+	.macro	seqdata_acquire fallback, tzonly=NO_TZ, skipvcnt=0, getdata
+9999:	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
+8888:	tbnz	seqcnt, #0, 9999b

ldr w_tmp, [vdso_data, #VDSO_USE_SYSCALL]

Quoted 8 lines

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 49

Quoted 4 lines
arch/arm64/kernel/vdso/gettimeofday.S +3 -1
-	cbnz	w_tmp, \fail
+	cbnz	w_tmp, \fallback
+	\getdata
+	dmb	ishld	/* No loads from vdso_data after this point */

What ordering guarantee is the DMB attempting to provide? Given we have the acquire, I assume some prior load, but I couldn't figure out what specifically.

Quoted 6 lines

That barrier specifically ensures that loads performed by the "getdata" sequence do not get accessed after the subsequent ldar check of the sequence counter, since, as you know, ldar may allow loads that come before it in program order to be accessed after it in much the same way as stlr may allow stores that come after it to accessed before it.

Ok. I wonder what the relative performance of a DMB ISHLD; LDAR is relative to a DMB ISH LD, and whether that's actually a win across microarchitectures.

Quoted 10 lines
Quoted 5 lines

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 51

Quoted 2 lines
arch/arm64/kernel/vdso/gettimeofday.S +2 -0
+	mov	w9, seqcnt
+	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]

Usually, acquire operations pair with a release operation elsewhere. What does this pair with?

It was for that reason that I introduced stlr's into the writer code, but the barrier provided by stlr was insufficient for my purposes, as Will pointed out. There is no requirement or even suggestion in the ARM that every use of ldar needs to be paired with stlr's.

Sure. I guess I was asking which updater does this pair with, and I having dug, I see it's just update_vsyscall().

Quoted 17 lines
Quoted 14 lines
Quoted 11 lines
(quoted diff) +2 -0
+	cmp	w9, seqcnt
+	bne	8888b	/* Do not needlessly repeat ldar and its implicit

barrier */

arch/arm64/kernel/vdso/gettimeofday.S +7 -0
+	.if (\tzonly) != NO_TZ
+		cbz	x0, \tzonly
+	.endif
+	.if (\skipvcnt) == 0
+		isb
+		mrs	x_tmp, cntvct_el0
+	.endif

.endm

All this conitional code makes the callers somehwat painful to read.

Quoted 2 lines

It might be nicer to have this explicit in the calelrs that require it rather than conditional in the macro.

Quoted 8 lines

The general use-case of the acquire sequence made this the cleanest safe implementation I could come up. If this isb/mrs sequence is split out into each clock handler, it would serve to obscure the relationship between the control-flow dependency (in this case, the "bne 8888b") and the isb. Keeping this acquire sequence intact helps to ensure that future modifications adhere to the correct sequence. Note that if the caller specifies neither option, the default is to leave these items in place.

As above, I'm not sure that the control dependency is key. Even if so, the logical sequence is:

seqdata_acquire
isb
mrs

I can't fathom why someone would move the ISB (and/or MRS) before the seqcnt_acquire.

Quoted 39 lines
Quoted 36 lines
Quoted 22 lines

.macro get_nsec_per_sec res

arch/arm64/kernel/vdso/gettimeofday.S +0 -10
@@ -64,9 +70,6 @@ x_tmp		.req	x8
 	 * shift.
 	 */
 	.macro	get_clock_shifted_nsec res, cycle_last, mult
-	/* Read the virtual counter. */
-	isb
-	mrs	x_tmp, cntvct_el0
 	/* Calculate cycle delta and convert to ns. */
 	sub	\res, x_tmp, \cycle_last
 	/* We can only guarantee 56 bits of precision. */
@@ -137,17 +140,12 @@ x_tmp		.req	x8
 ENTRY(__kernel_gettimeofday)
 	.cfi_startproc
 	adr	vdso_data, _vdso_data
-	/* If tv is NULL, skip to the timezone code. */
-	cbz	x0, 2f
-
-	/* Compute the time of day. */
-1:	seqcnt_acquire
-	syscall_check fail=4f
-	ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST]

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 148

Quoted 10 lines
arch/arm64/kernel/vdso/gettimeofday.S +6 -4
-	/* w11 = cs_mono_mult, w12 = cs_shift */
-	ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT]
-	ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
-	seqcnt_check fail=1b
+	seqdata_acquire fallback=4f tzonly=2f getdata=__stringify(\
+		ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST];\
+		/* w11 = cs_mono_mult, w12 = cs_shift */;\
+		ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT];\
+		ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC];\
+		ldp	w4, w5, [vdso_data, #VDSO_TZ_MINWEST])

Why do we need the stringify? Is that just so we can pass the code as a macro parameter? If so, it really doesn't look like the way to go...

Quoted 2 lines

This is unfortunately painful to read.

Quoted 15 lines

I implemented it this way to remain as similar as possible with the refactored code that was recently merged, while at the same time ensuring that, as I explained above, the reads of the vdso_data performed by each clock type are completely contained within a set of proper sequence count checks. That they were not contained led to problems such as the improper handling of the timezone data before, and it ensures that the isb follows the sequence check closely. This use is not entirely dissimilar to other code which uses stringify currently present in the arm64 kernel code which passes code as a parameter. See, for example, arch/arm64/lib/copy_*_user.S. All this said, however, I was never thrilled about going the stringify route, but it was the most readable of any other variants I could come up with (and far better than adding the extra ".if's" in the macro).
Do you happen to have a better suggestion?

I think that:

ACQUIRE, blah, blah, blah
< long >
< code >
< sequence >
CONDITION_FAIL, blah, blah, blah

Is clearer than dropping the code sequence into a macro parameter, even if there's come implicit dependency between the ACQUIRE and CONDITION_FAIL macro.

Thanks,
Mark.

#9 lore raw
Quoted 27 lines · Mark Rutland

Hi Brent,

Thanks for the thorough reply. Comments inline below.

Quoted 12 lines · bdegraaf at codeaurora.org
Quoted 1 line · Mark Rutland

* What problem does this patch address?

Initially, I set out to fix a control-flow problem, as I originally wrote this against the code prior to the refactoring of commit b33f491f5a9aaf171b7de0f905362eb0314af478, back when there was still a do_get_tspec subroutine. That one used a dmb to order the data accesses prior to the isb/mrs sequence that read the virtual counter. Our Senior Director, who has done extensive work with the ARM
cpu and is intimately familiar with the instruction set, indicated that

Quoted 8 lines

the dmb which was used in that code was not enough to ensure ordering between the loads from the structure and the read of the virtual counter.
Since that code had no control-flow (e.g., some conditional governing the code's progress) prior to the isb, he suggested that some form of dsb would be required to ensure proper order of access between the loads from the vdso structure and the mrs read of the the virtual counter.

Quoted 5 lines

Ok. So if I've parsed the above correctly, the fear was that an ISB was insufficient to guarantee the ordering of prior loads w.r.t. the subsequent MRS, and a control dependency between the two was necessary, in addition to the ISB.

Exactly.

Quoted 14 lines
Quoted 7 lines

I went to the latest armv8 ARM at that time and found a concrete example
of how the code should be structured to ensure an ordered read of the virtual counter. In the most recent copy to which I have access (ARM DDI 0487A.j), that code is given on page D6-1871, under section D6.2.2. I moved the sequence count check immediately prior to the isb to satisfy the particular ordering requirements in that code.

My reading of that example is that the control dependency alone was insufficient (given speculation), and the ISB provided the necessary ordering between the signal variable being updated and the MRS. To me, the example does not imply that both are required in all cases, only that a control dependency alone is insufficient.

Quoted 7 lines

Per the description of ISB on page B2-87 of ARM DDI 0487A.j, my understanding (which may be flawed), is that the instructions prior to the ISB must be completed before the subsequent instructions are fetched+issued, and hence the MRS should be (locally) ordered w.r.t. the
loads.

Saying the instructions are completed reportedly isn't exactly the same as saying the loads have been accessed (I brought up this same point to him when we discussed it). If a control dependency is not present prior to the ISB, he said we would have to add a DSB to ensure ordering. Not wanting to potentially slow things down across multiple cores with a DSB, the control dependency is the lighter-weight solution. I would not have known that the control dependency were available in this situ- ation had the ARM not mentioned it.

Quoted 7 lines
Quoted 3 lines

When the refactored code went in recently, however, the seqcnt_read prior to the isb changed to a seqcnt_check, which addressed that ordering requirement.

Have you seen an issue in practice prior to this? If so, we may need to go digging further into this, and consider stable kernels.

I had not seen a problem. We were looking at the speed of the code to see if anything could be helped when the Sr. Director noticed the correctness problem. After I applied the fix, results given by gettimeofday got tighter: averages that varied by up to 2 nsec before now vary by only one or two hundredths of a nanosecond.

Quoted 22 lines
Quoted 8 lines
Quoted 1 line

* Is this a bug fix? If so, what problem can be seen currently?

The most obvious problem with the existing code is where the timezone data gets loaded: after sequence counts have been checked and rechecked, completely outside the area of the code protected by the sequence counter. While I realize that timezone code does not change frequently, this is still a problem as the routine explicitly reads data that could be in the process of being updated.

I take that you specifically mean the following line in __kernel_gettimeofday, which occurs after the usual seqcnt_acquire + seqcnt_check sequence:

ldp w4, w5, [vdso_data, #VDSO_TZ_MINWEST]

I see that the writer side for the timezone data is also not protected, since commit bdba0051ebcb3c63 ("arm64: vdso: remove broken, redundant sequence counting for timezones"). Following comments in commits I found
x86 commit 6c260d586343f7f7 ("x86: vdso: Remove bogus locking in update_vsyscall_tz()").

Quoted 8 lines

Per the x86 commit, this is not atomic in the usual syscall path, and thus it is a cross-architecture property that timezone updates are not atomic, and that reordering of accesses may occur around a change of timezone. If we want to tighten up the VDSO, we'll also need to tighten up the syscall.

[...]

Ugh. I had not looked at the writer. That would explain why the comments refer to it as "whacky tz stuff."

Quoted 12 lines
Quoted 11 lines

The second problem is that the timing between the reading of the vdso data and the virtual counter is treated as secondary in the code, as a few register manipulations using the structure data are performed prior to reading the virtual counter. While the cpu itself is free to reorder these shifts and loads somewhat, depending on the implementation, the read of the virtual counter should be performed as close as possible to the time the vdso data itself is read to minimize variability. As these manipulations and loads are not dependent on the result of the mrs read, putting them after the virtual counter isb/mrs sequence allows these independent register manipulations to issue while the mrs is still in process.

Quoted 3 lines

This is rather dependent on the microarchitecture. Do we have any numbers as to the variability?

Other than my strictly anecdotal evidence on my test system above, I do not. But keeping those manipulations on the "mrs" side of the ISB seems like a good idea because, depending on the microarchitecture, that mrs can take a fairly long time. (I've heard of values as high as 90 nsec).

Quoted 32 lines
Quoted 9 lines
Quoted 2 lines

* Is this an optimisation? If so, how much of an improvement can be seen?

Optimization was not the main goal of this patch, yet performance did improve on my target, with the average time improving marginally (350 nsec after vs 360 nsec before the change), compared to the refactored code. In fact, performance is now slightly better (around a miniscule 2 nsec) than the original code, before the refactor, which hurt performance.

Ok.

Quoted 13 lines
Quoted 4 lines
Quoted 4 lines
arch/arm64/kernel/vdso/gettimeofday.S +3 -0
+	.macro	seqdata_acquire fallback, tzonly=NO_TZ, skipvcnt=0, getdata
+9999:	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]
+8888:	tbnz	seqcnt, #0, 9999b

ldr w_tmp, [vdso_data, #VDSO_USE_SYSCALL]

Quoted 8 lines

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 49

Quoted 4 lines
arch/arm64/kernel/vdso/gettimeofday.S +3 -1
-	cbnz	w_tmp, \fail
+	cbnz	w_tmp, \fallback
+	\getdata
+	dmb	ishld	/* No loads from vdso_data after this point */

What ordering guarantee is the DMB attempting to provide? Given we have the acquire, I assume some prior load, but I couldn't figure out what specifically.

Quoted 6 lines

That barrier specifically ensures that loads performed by the "getdata" sequence do not get accessed after the subsequent ldar check of the sequence counter, since, as you know, ldar may allow loads that come before it in program order to be accessed after it in much the same way as stlr may allow stores that come after it to accessed before it.

Quoted 4 lines

Ok. I wonder what the relative performance of a DMB ISHLD; LDAR is relative to a DMB ISH LD, and whether that's actually a win across microarchitectures.

Keep in mind that on the "spin" case (at label 9999), it spins on a single ldar, which due to it's one-way nature, is bound to be lighter weight. In my experience, however, trying to nail down average timing for a single barrier is difficult.

Quoted 45 lines
Quoted 10 lines
Quoted 5 lines

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 51

Quoted 2 lines
arch/arm64/kernel/vdso/gettimeofday.S +2 -0
+	mov	w9, seqcnt
+	ldar	seqcnt, [vdso_data, #VDSO_TB_SEQ_COUNT]

Usually, acquire operations pair with a release operation elsewhere. What does this pair with?

It was for that reason that I introduced stlr's into the writer code, but the barrier provided by stlr was insufficient for my purposes, as Will pointed out. There is no requirement or even suggestion in the ARM that every use of ldar needs to be paired with stlr's.

Sure. I guess I was asking which updater does this pair with, and I having dug, I see it's just update_vsyscall().

Quoted 17 lines
Quoted 14 lines
Quoted 11 lines
(quoted diff) +2 -0
+	cmp	w9, seqcnt
+	bne	8888b	/* Do not needlessly repeat ldar and its implicit

barrier */

arch/arm64/kernel/vdso/gettimeofday.S +7 -0
+	.if (\tzonly) != NO_TZ
+		cbz	x0, \tzonly
+	.endif
+	.if (\skipvcnt) == 0
+		isb
+		mrs	x_tmp, cntvct_el0
+	.endif

.endm

All this conitional code makes the callers somehwat painful to read.

Quoted 2 lines

It might be nicer to have this explicit in the calelrs that require it rather than conditional in the macro.

Quoted 10 lines

The general use-case of the acquire sequence made this the cleanest safe
implementation I could come up. If this isb/mrs sequence is split out into each clock handler, it would serve to obscure the relationship between the control-flow dependency (in this case, the "bne 8888b") and
the isb. Keeping this acquire sequence intact helps to ensure that future modifications adhere to the correct sequence. Note that if the caller specifies neither option, the default is to leave these items in place.

As above, I'm not sure that the control dependency is key. Even if so, the logical sequence is:

Quoted 7 lines

seqdata_acquire
isb
mrs

I can't fathom why someone would move the ISB (and/or MRS) before the seqcnt_acquire.

This can be separated out, but it'll be repeated in a few places. The only place the tz code is used is the gettimeofday logic itself.

Quoted 63 lines
Quoted 39 lines
Quoted 36 lines
Quoted 22 lines

.macro get_nsec_per_sec res

arch/arm64/kernel/vdso/gettimeofday.S +0 -10
@@ -64,9 +70,6 @@ x_tmp		.req	x8
 	 * shift.
 	 */
 	.macro	get_clock_shifted_nsec res, cycle_last, mult
-	/* Read the virtual counter. */
-	isb
-	mrs	x_tmp, cntvct_el0
 	/* Calculate cycle delta and convert to ns. */
 	sub	\res, x_tmp, \cycle_last
 	/* We can only guarantee 56 bits of precision. */
@@ -137,17 +140,12 @@ x_tmp		.req	x8
 ENTRY(__kernel_gettimeofday)
 	.cfi_startproc
 	adr	vdso_data, _vdso_data
-	/* If tv is NULL, skip to the timezone code. */
-	cbz	x0, 2f
-
-	/* Compute the time of day. */
-1:	seqcnt_acquire
-	syscall_check fail=4f
-	ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST]

arch/arm64/kernel/vdso/gettimeofday.S x_tmp .req x8 · line 148

Quoted 10 lines
arch/arm64/kernel/vdso/gettimeofday.S +6 -4
-	/* w11 = cs_mono_mult, w12 = cs_shift */
-	ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT]
-	ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC]
-	seqcnt_check fail=1b
+	seqdata_acquire fallback=4f tzonly=2f getdata=__stringify(\
+		ldr	x10, [vdso_data, #VDSO_CS_CYCLE_LAST];\
+		/* w11 = cs_mono_mult, w12 = cs_shift */;\
+		ldp	w11, w12, [vdso_data, #VDSO_CS_MONO_MULT];\
+		ldp	x13, x14, [vdso_data, #VDSO_XTIME_CLK_SEC];\
+		ldp	w4, w5, [vdso_data, #VDSO_TZ_MINWEST])

Why do we need the stringify? Is that just so we can pass the code as a macro parameter? If so, it really doesn't look like the way to go...

Quoted 2 lines

This is unfortunately painful to read.

Quoted 15 lines

I implemented it this way to remain as similar as possible with the refactored code that was recently merged, while at the same time ensuring that, as I explained above, the reads of the vdso_data performed by each clock type are completely contained within a set of proper sequence count checks. That they were not contained led to problems such as the improper handling of the timezone data before, and it ensures that the isb follows the sequence check closely. This use is not entirely dissimilar to other code which uses stringify currently present in the arm64 kernel code which passes code as a parameter. See, for example, arch/arm64/lib/copy_*_user.S. All this said, however, I was never thrilled about going the stringify route, but it was the most readable of any other variants I could come up with (and far better than adding the extra ".if's" in the macro).
Do you happen to have a better suggestion?

I think that:

ACQUIRE, blah, blah, blah
< long >
< code >
< sequence >
CONDITION_FAIL, blah, blah, blah

Quoted 6 lines

Is clearer than dropping the code sequence into a macro parameter, even if there's come implicit dependency between the ACQUIRE and CONDITION_FAIL macro.

Thanks,
Mark.

I'll see what I can do to split this without spinning on the barrier. Yes, it's only one spin, but if there's any clean way I can do that I will. If the ldar result's bit 0 is set, it needs to reload anyway.