Skip to contentSkip to conversation

x86/time: Adjust init-time handling of pit0_ticks

PATCH In review Andrew Cooper 2 participants 3 messages Feb 7, 2017 – Feb 8, 2017

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/time: Adjust init-time handling of pit0_ticks

There is no need for the volatile cast in the timer interrupt; the compiler may not elide the update. This reduces the generated assembly from a read, local modify, write to a single add instruction.

Drop the memory barriers from timer_irq_works(), as they are not needed. pit0_ticks is only modified by timer_interrupt() running on the same CPU, so that is required is a volatile reference to prevent the compiler from eliding the second read.

---
CC: Jan Beulich <[email protected]>

v2:
* Update commit message

---
 xen/arch/x86/io_apic.c | 6 ++----
 xen/arch/x86/time.c    | 2 +-
 xen/include/xen/lib.h  | 2 ++
 3 files changed, 5 insertions(+), 5 deletions(-)
xen/arch/x86/io_apic.c +2 -4
index 33e5927..f989978 100644
@@ -1485,8 +1485,7 @@ static int __init timer_irq_works(void)
 {
     unsigned long t1, flags;
 
-    t1 = pit0_ticks;
-    mb();
+    t1 = ACCESS_ONCE(pit0_ticks);
 
     local_save_flags(flags);
     local_irq_enable();
@@ -1501,8 +1500,7 @@ static int __init timer_irq_works(void)
      * might have cached one ExtINT interrupt.  Finally, at
      * least one tick may be lost due to delays.
      */
-    mb();
-    if (pit0_ticks - t1 > 4)
+    if ( (ACCESS_ONCE(pit0_ticks) - t1) > 4 )
         return 1;
 
     return 0;
xen/arch/x86/time.c +1 -1
index d3b0c69..699dfb6 100644
@@ -197,7 +197,7 @@ static void timer_interrupt(int irq, void *dev_id, struct cpu_user_regs *regs)
         return;
 
     /* Only for start-of-day interruopt tests in io_apic.c. */
-    (*(volatile unsigned long *)&pit0_ticks)++;
+    pit0_ticks++;
 
     /* Rough hack to allow accurate timers to sort-of-work with no APIC. */
     if ( !cpu_has_apic )
xen/include/xen/lib.h +2 -0
index d1171b7..1976e4b 100644
@@ -56,6 +56,8 @@
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]) + __must_be_array(x))
 
+#define ACCESS_ONCE(x) (*(volatile typeof(x) *)&(x))
+
 #define MASK_EXTR(v, m) (((v) & (m)) / ((m) & -(m)))
 #define MASK_INSR(v, m) (((v) * ((m) & -(m))) & (m))
 
Signature · 7 lines
2.1.4


_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel
Reviewed-by on v2 1/1
#1 lore raw
Quoted 11 lines
Quoted 1 line
Quoted 1 line · On 07.02.17 at 19:51, <[email protected]> wrote:

There is no need for the volatile cast in the timer interrupt; the compiler may not elide the update. This reduces the generated assembly from a read, local modify, write to a single add instruction.

Drop the memory barriers from timer_irq_works(), as they are not needed. pit0_ticks is only modified by timer_interrupt() running on the same CPU, so that is required is a volatile reference to prevent the compiler from eliding the second read.

Is there an "all" missing at the beginning of the last line of the description?

Jan

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel

#2 lore raw
Quoted 10 lines · Jan Beulich
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 07.02.17 at 19:51, <[email protected]> wrote:

There is no need for the volatile cast in the timer interrupt; the compiler may not elide the update. This reduces the generated assembly from a read, local modify, write to a single add instruction.

Drop the memory barriers from timer_irq_works(), as they are not needed. pit0_ticks is only modified by timer_interrupt() running on the same CPU, so that is required is a volatile reference to prevent the compiler from eliding the second read.

Quoted 5 lines
Quoted 1 line

Is there an "all" missing at the beginning of the last line of the description?

Yes. Fixed.

Thanks.

~Andrew

_______________________________________________
Xen-devel mailing list
[email protected]
https://lists.xen.org/xen-devel