Skip to contentSkip to conversation

xen/arm: Correct the coding style of get_cycles

PATCH Superseded Wei Chen 2 participants 5 messages Jan 5, 2021

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Wei Chen

author
#0 lore raw

PATCH 1/2 xen/arm: Correct the coding style of get_cycles

It seems the arm inline function get_cycles has used 8 spaces for line indent since 2012. This patch correct them to 4 spaces and remove extra space between function name and bracket.

---
 xen/include/asm-arm/time.h | 40 +++++++++++++++++++-------------------
 1 file changed, 20 insertions(+), 20 deletions(-)
xen/include/asm-arm/time.h +20 -20
index 1b2c13614b..5c4529ebb5 100644
@@ -11,29 +11,29 @@
 
 typedef uint64_t cycles_t;
 
-static inline cycles_t get_cycles (void)
+static inline cycles_t get_cycles(void)
 {
-        isb();
+    isb();
+    /*
+     * ARM_WORKAROUND_858921: Cortex-A73 (all versions) counter read
+     * can return a wrong value when the counter crosses a 32bit boundary.
+     */
+    if ( !check_workaround_858921() )
+        return READ_SYSREG64(CNTPCT_EL0);
+    else
+    {
         /*
-         * ARM_WORKAROUND_858921: Cortex-A73 (all versions) counter read
-         * can return a wrong value when the counter crosses a 32bit boundary.
+         * A recommended workaround for erratum 858921 is to:
+         *  1- Read twice CNTPCT.
+         *  2- Compare bit[32] of the two read values.
+         *      - If bit[32] is different, keep the old value.
+         *      - If bit[32] is the same, keep the new value.
          */
-        if ( !check_workaround_858921() )
-            return READ_SYSREG64(CNTPCT_EL0);
-        else
-        {
-            /*
-             * A recommended workaround for erratum 858921 is to:
-             *  1- Read twice CNTPCT.
-             *  2- Compare bit[32] of the two read values.
-             *      - If bit[32] is different, keep the old value.
-             *      - If bit[32] is the same, keep the new value.
-             */
-            cycles_t old, new;
-            old = READ_SYSREG64(CNTPCT_EL0);
-            new = READ_SYSREG64(CNTPCT_EL0);
-            return (((old ^ new) >> 32) & 1) ? old : new;
-        }
+        cycles_t old, new;
+        old = READ_SYSREG64(CNTPCT_EL0);
+        new = READ_SYSREG64(CNTPCT_EL0);
+        return (((old ^ new) >> 32) & 1) ? old : new;
+    }
 }
 
 /* List of timer's IRQ */
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 xen/arm: Add defensive barrier in get_cycles for Arm64

As the dicsussion [1] in mailing list. We'd better to have a barrier after reading CNTPCT in get_cycles. If there is not any barrier there. When get_cycles being used in some seqlock critical context in the future, the seqlock can be speculated potentially.

In order to reduce the impact of new barrier, we perfer to use enforce order instead of ISB [2].

Currently, enforce order is not applied to arm32 as this is not done in Linux at the date of this patch. If this is done in Linux it will need to be also done in Xen.

[1] https://lists.xenproject.org/archives/html/xen-devel/2020-12/msg00181.html
[2] https://lkml.org/lkml/2020/3/13/645

---
 xen/include/asm-arm/time.h | 43 ++++++++++++++++++++++++++++++++++++--
 1 file changed, 41 insertions(+), 2 deletions(-)
xen/include/asm-arm/time.h +41 -2
index 5c4529ebb5..3ef4e7cd3d 100644
@@ -11,9 +11,26 @@
 
 typedef uint64_t cycles_t;
 
-static inline cycles_t get_cycles(void)
+/*
+ * Ensure that reads of the counter are treated the same as memory reads
+ * for the purposes of ordering by subsequent memory barriers.
+ */
+#if defined(CONFIG_ARM_64)
+#define read_cntpct_enforce_ordering(val) do { \
+    u64 tmp, _val = (val);                     \
+                                               \
+    asm volatile(                              \
+    "eor %0, %1, %1\n"                         \
+    "add %0, sp, %0\n"                         \
+    "ldr xzr, [%0]"                            \
+    : "=r" (tmp) : "r" (_val));                \
+} while (0)
+#else
+#define read_cntpct_enforce_ordering(val) do {} while (0)
+#endif
+
+static inline cycles_t read_cntpct_stable(void)
 {
-    isb();
     /*
      * ARM_WORKAROUND_858921: Cortex-A73 (all versions) counter read
      * can return a wrong value when the counter crosses a 32bit boundary.
@@ -36,6 +53,28 @@ static inline cycles_t get_cycles(void)
     }
 }
 
+static inline cycles_t get_cycles(void)
+{
+    cycles_t cnt;
+
+    isb();
+    cnt = read_cntpct_stable();
+
+    /*
+     * If there is not any barrier here. When get_cycles being used in
+     * some seqlock critical context in the future, the seqlock can be
+     * speculated potentially.
+     *
+     * To prevent seqlock from being speculated silently, we add a barrier
+     * here defensively. Normally, we just need an ISB here is enough, but
+     * considering the minimum performance cost. We prefer to use enforce
+     * order here.
+     */
+    read_cntpct_enforce_ordering(cnt);
+
+    return cnt;
+}
+
 /* List of timer's IRQ */
 enum timer_ppi
 {
Signature · 1 line
2.25.1
#2 lore raw

Re: [PATCH 2/2] xen/arm: Add defensive barrier in get_cycles for Arm64

Hi Wei,

Quoted 1 line · Wei Chen

As the dicsussion [1] in mailing list. We'd better to have

I would say "Per the discussion [1] on the ...". I would also suggest to replace the "." with ",".

Quoted 4 lines

a barrier after reading CNTPCT in get_cycles. If there is not any barrier there. When get_cycles being used in some seqlock critical context in the future, the seqlock can be speculated potentially.

This comment seems a little off because we don't have seqlock on Xen. I think it would be best if you re-use the Linux commit as it would be clear that this is a backport.

Something like:

"Import commit .... from Linux:

<entire commit message indented by one>

While we are not aware of such use in Xen, it would be best to add the barrier to avoid any suprise." "

Quoted 21 lines

In order to reduce the impact of new barrier, we perfer to use enforce order instead of ISB [2].

Currently, enforce order is not applied to arm32 as this is not done in Linux at the date of this patch. If this is done in Linux it will need to be also done in Xen.

[1] https://lists.xenproject.org/archives/html/xen-devel/2020-12/msg00181.html
[2] https://lkml.org/lkml/2020/3/13/645

---
  xen/include/asm-arm/time.h | 43 ++++++++++++++++++++++++++++++++++++--
  1 file changed, 41 insertions(+), 2 deletions(-)
xen/include/asm-arm/time.h +0 -0
index 5c4529ebb5..3ef4e7cd3d 100644

xen/include/asm-arm/time.h line 17

Quoted 8 lines
xen/include/asm-arm/time.h +4 -1
@@ -11,9 +11,26 @@
 
  typedef uint64_t cycles_t;
 
-static inline cycles_t get_cycles(void)
+/*
+ * Ensure that reads of the counter are treated the same as memory reads
+ * for the purposes of ordering by subsequent memory barriers.
+ */

The comment is before the #ifdef which suggests the ordering is required for Arm as well. I would suggest to either mention that oddity or move the comment after the #ifdef.

xen/include/asm-arm/time.h line 20

Quoted 3 lines
xen/include/asm-arm/time.h +3 -0
+#if defined(CONFIG_ARM_64)
+#define read_cntpct_enforce_ordering(val) do { \
+    u64 tmp, _val = (val);                     \

Please use uint64_t here.

Quoted 28 lines
xen/include/asm-arm/time.h +19 -1
+                                               \
+    asm volatile(                              \
+    "eor %0, %1, %1\n"                         \
+    "add %0, sp, %0\n"                         \
+    "ldr xzr, [%0]"                            \
+    : "=r" (tmp) : "r" (_val));                \
+} while (0)
+#else
+#define read_cntpct_enforce_ordering(val) do {} while (0)
+#endif
+
+static inline cycles_t read_cntpct_stable(void)
  {
-    isb();
      /*
       * ARM_WORKAROUND_858921: Cortex-A73 (all versions) counter read
       * can return a wrong value when the counter crosses a 32bit boundary.
@@ -36,6 +53,28 @@ static inline cycles_t get_cycles(void)
      }
  }
 
+static inline cycles_t get_cycles(void)
+{
+    cycles_t cnt;
+
+    isb();
+    cnt = read_cntpct_stable();
+

xen/include/asm-arm/time.h static inline cycles_t get_cycles(void) · line 72

Quoted 10 lines
xen/include/asm-arm/time.h +10 -0
+    /*
+     * If there is not any barrier here. When get_cycles being used in
+     * some seqlock critical context in the future, the seqlock can be
+     * speculated potentially.
+     *
+     * To prevent seqlock from being speculated silently, we add a barrier
+     * here defensively. Normally, we just need an ISB here is enough, but
+     * considering the minimum performance cost. We prefer to use enforce
+     * order here.
+     */

We don't use seqlock in Xen, so this comment looks quite confusing.. I think the comment on top of reach_cntpct_enforce_ordering() is sufficient, so I would drop this one. The alternative is to find a way to make the comment more Xen focused.

Although, I don't have a good suggestion so far.

xen/include/asm-arm/time.h static inline cycles_t get_cycles(void) · line 80

Quoted 9 lines
xen/include/asm-arm/time.h +5 -0
+    read_cntpct_enforce_ordering(cnt);
+
+    return cnt;
+}
+
  /* List of timer's IRQ */
  enum timer_ppi
  {

Cheers,

Signature · 1 line
Julien Grall
author
#3 lore raw

RE: [PATCH 2/2] xen/arm: Add defensive barrier in get_cycles for Arm64

Hi Julien,

Quoted 29 lines

-----Original Message-----
From: Julien Grall <[email protected]>
Sent: 2021年1月5日 19:17
To: Wei Chen <[email protected]>; [email protected]; [email protected]
Cc: Bertrand Marquis <[email protected]>; Penny Zheng <[email protected]>; Jiamei Xie <[email protected]>; nd <[email protected]>
Subject: Re: [PATCH 2/2] xen/arm: Add defensive barrier in get_cycles for Arm64

Hi Wei,

Quoted 1 line · Wei Chen

As the dicsussion [1] in mailing list. We'd better to have

I would say "Per the discussion [1] on the ...". I would also suggest to replace the "." with ",".

Quoted 4 lines

a barrier after reading CNTPCT in get_cycles. If there is not any barrier there. When get_cycles being used in some seqlock critical context in the future, the seqlock can be speculated potentially.

This comment seems a little off because we don't have seqlock on Xen. I think it would be best if you re-use the Linux commit as it would be clear that this is a backport.

Something like:

Quoted 8 lines

"Import commit .... from Linux:

<entire commit message indented by one>

While we are not aware of such use in Xen, it would be best to add the barrier to avoid any suprise."
"

Yes, that would be better. I will add it in next version. Thanks.

Quoted 36 lines
Quoted 9 lines

In order to reduce the impact of new barrier, we perfer to use enforce order instead of ISB [2].

Currently, enforce order is not applied to arm32 as this is not done in Linux at the date of this patch. If this is done in Linux it will need to be also done in Xen.

[1] https://lists.xenproject.org/archives/html/xen-devel/2020-

12/msg00181.html

Quoted 5 lines

[2] https://lkml.org/lkml/2020/3/13/645

---
  xen/include/asm-arm/time.h | 43

++++++++++++++++++++++++++++++++++++--

Quoted 7 lines
  1 file changed, 41 insertions(+), 2 deletions(-)
xen/include/asm-arm/time.h +0 -0
index 5c4529ebb5..3ef4e7cd3d 100644

xen/include/asm-arm/time.h line 17

Quoted 8 lines
xen/include/asm-arm/time.h +4 -1
@@ -11,9 +11,26 @@
 
  typedef uint64_t cycles_t;
 
-static inline cycles_t get_cycles(void)
+/*
+ * Ensure that reads of the counter are treated the same as memory reads
+ * for the purposes of ordering by subsequent memory barriers.
+ */

The comment is before the #ifdef which suggests the ordering is required for Arm as well. I would suggest to either mention that oddity or move the comment after the #ifdef.

Quoted 5 lines

xen/include/asm-arm/time.h line 20

Quoted 3 lines
xen/include/asm-arm/time.h +3 -0
+#if defined(CONFIG_ARM_64)
+#define read_cntpct_enforce_ordering(val) do { \
+    u64 tmp, _val = (val);                     \

Please use uint64_t here.

Got it.

Quoted 45 lines
Quoted 29 lines
xen/include/asm-arm/time.h +19 -1
+                                               \
+    asm volatile(                              \
+    "eor %0, %1, %1\n"                         \
+    "add %0, sp, %0\n"                         \
+    "ldr xzr, [%0]"                            \
+    : "=r" (tmp) : "r" (_val));                \
+} while (0)
+#else
+#define read_cntpct_enforce_ordering(val) do {} while (0)
+#endif
+
+static inline cycles_t read_cntpct_stable(void)
  {
-    isb();
      /*
       * ARM_WORKAROUND_858921: Cortex-A73 (all versions) counter read
       * can return a wrong value when the counter crosses a 32bit boundary.
@@ -36,6 +53,28 @@ static inline cycles_t get_cycles(void)
      }
  }
 
+static inline cycles_t get_cycles(void)
+{
+    cycles_t cnt;
+
+    isb();
+    cnt = read_cntpct_stable();
+

xen/include/asm-arm/time.h static inline cycles_t get_cycles(void) · line 72

Quoted 10 lines
xen/include/asm-arm/time.h +10 -0
+    /*
+     * If there is not any barrier here. When get_cycles being used in
+     * some seqlock critical context in the future, the seqlock can be
+     * speculated potentially.
+     *
+     * To prevent seqlock from being speculated silently, we add a barrier
+     * here defensively. Normally, we just need an ISB here is enough, but
+     * considering the minimum performance cost. We prefer to use enforce
+     * order here.
+     */

We don't use seqlock in Xen, so this comment looks quite confusing.. I think the comment on top of reach_cntpct_enforce_ordering() is sufficient, so I would drop this one. The alternative is to find a way to make the comment more Xen focused.

Quoted 2 lines

Although, I don't have a good suggestion so far.

Ok, I will drop it.

Quoted 14 lines

xen/include/asm-arm/time.h static inline cycles_t get_cycles(void) · line 80

Quoted 9 lines
xen/include/asm-arm/time.h +5 -0
+    read_cntpct_enforce_ordering(cnt);
+
+    return cnt;
+}
+
  /* List of timer's IRQ */
  enum timer_ppi
  {

Cheers,

--
Julien Grall

Acked-by on v1 1/2
#4 lore raw
In reply to Wei Chen

Hi Wei,

Quoted 5 lines · Wei Chen

It seems the arm inline function get_cycles has used 8 spaces for line indent since 2012. This patch correct them to 4 spaces and remove extra space between function name and bracket.

Cheers,

Quoted 59 lines
---
  xen/include/asm-arm/time.h | 40 +++++++++++++++++++-------------------
  1 file changed, 20 insertions(+), 20 deletions(-)
xen/include/asm-arm/time.h +20 -20
index 1b2c13614b..5c4529ebb5 100644
@@ -11,29 +11,29 @@
 
  typedef uint64_t cycles_t;
 
-static inline cycles_t get_cycles (void)
+static inline cycles_t get_cycles(void)
  {
-        isb();
+    isb();
+    /*
+     * ARM_WORKAROUND_858921: Cortex-A73 (all versions) counter read
+     * can return a wrong value when the counter crosses a 32bit boundary.
+     */
+    if ( !check_workaround_858921() )
+        return READ_SYSREG64(CNTPCT_EL0);
+    else
+    {
          /*
-         * ARM_WORKAROUND_858921: Cortex-A73 (all versions) counter read
-         * can return a wrong value when the counter crosses a 32bit boundary.
+         * A recommended workaround for erratum 858921 is to:
+         *  1- Read twice CNTPCT.
+         *  2- Compare bit[32] of the two read values.
+         *      - If bit[32] is different, keep the old value.
+         *      - If bit[32] is the same, keep the new value.
           */
-        if ( !check_workaround_858921() )
-            return READ_SYSREG64(CNTPCT_EL0);
-        else
-        {
-            /*
-             * A recommended workaround for erratum 858921 is to:
-             *  1- Read twice CNTPCT.
-             *  2- Compare bit[32] of the two read values.
-             *      - If bit[32] is different, keep the old value.
-             *      - If bit[32] is the same, keep the new value.
-             */
-            cycles_t old, new;
-            old = READ_SYSREG64(CNTPCT_EL0);
-            new = READ_SYSREG64(CNTPCT_EL0);
-            return (((old ^ new) >> 32) & 1) ? old : new;
-        }
+        cycles_t old, new;
+        old = READ_SYSREG64(CNTPCT_EL0);
+        new = READ_SYSREG64(CNTPCT_EL0);
+        return (((old ^ new) >> 32) & 1) ? old : new;
+    }
  }
 
  /* List of timer's IRQ */
Signature · 1 line
Julien Grall