Skip to contentSkip to conversation

xen/x86: Optimize timer_irq_works()

PATCH In review Julien Grall 3 participants 10 messages Dec 11, 2023 – Jan 15, 2024

lore
Share & tools
lore
#0 lore raw

v2 cover xen/x86: Optimize timer_irq_works()

From: Julien Grall <[email protected]>

Hi all,

At the moment timer_irq_works() will always wait 100ms even with enough ticks elapsed. This is a bit wasteful when most of the HW should not be buggy.

The admin may also know that their HW is not buggy so they could decide to skip the full 100ms. Also, looking at Linux changes, it was pointed out that the timer_irq_works() may always return false when using para-virtualized timer. It is not a problem for Xen, but still it makes sense to provide an option to disable the timer check.

Patch #1 of the series will add the command line option while patch #2 will optimize timer_irq_works() for the good case.

Cheers,

Julien Grall (2):
xen/x86: io_apic: Introduce a command line option to skip timer check xen/x86: ioapic: Bail out from timer_irq_works() as soon as possible

 docs/misc/xen-command-line.pandoc | 11 +++++++
 xen/arch/x86/io_apic.c            | 48 ++++++++++++++++++++++---------
 2 files changed, 45 insertions(+), 14 deletions(-)
Signature · 1 line
2.40.1
#1 lore raw

PATCH v2 1/2 xen/x86: io_apic: Introduce a command line option to skip timer check

From: Julien Grall <[email protected]>

Currently, Xen will spend ~100ms to check if the timer works. If the Admin knows their platform have a working timer, then it would be handy to be able to bypass the check.

Introduce a command line option 'pit-irq-works' for this purpose.

---

Changelog since v1:
- Rename the command line option. I went with pit-irq-works rather than timer-irq-works because Roger thought it would be better suited
- Rework the command line description

---
 docs/misc/xen-command-line.pandoc | 11 +++++++++++
 xen/arch/x86/io_apic.c            | 11 +++++++++++
 2 files changed, 22 insertions(+)
docs/misc/xen-command-line.pandoc +11 -0
index 8e65f8bd18bf..c382b061b302 100644
@@ -2535,6 +2535,17 @@ pages) must also be specified via the tbuf_size parameter.
 ### tickle_one_idle_cpu
 > `= <boolean>`
 
+### pit-irq-works (x86)
+> `=<boolean>`
+
+> Default: `false`
+
+Disables the code which tests for broken timer IRQ sources. Enabling
+this option will reduce boot time on HW where the timer works properly.
+
+If the system is unstable when enabling the option, then it means you
+may have a broken HW and therefore the testing cannot be be skipped.
+
 ### timer_slop
 > `= <integer>`
 
xen/arch/x86/io_apic.c +11 -0
index d11c880544e6..238b6c1c2837 100644
@@ -57,6 +57,14 @@ bool __initdata ioapic_ack_forced;
 int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
 int __read_mostly nr_ioapics;
 
+/*
+ * The logic to check if the timer is working is expensive. So allow
+ * the admin to bypass it if they know their platform doesn't have
+ * a buggy timer.
+ */
+static bool __initdata pit_irq_works;
+boolean_param("pit-irq-works", pit_irq_works);
+
 /*
  * Rough estimation of how many shared IRQs there are, can
  * be changed anytime.
@@ -1502,6 +1510,9 @@ static int __init timer_irq_works(void)
 {
     unsigned long t1, flags;
 
+    if ( pit_irq_works )
+        return 1;
+
     t1 = ACCESS_ONCE(pit0_ticks);
 
     local_save_flags(flags);
Signature · 1 line
2.40.1
#2 lore raw

Re: [PATCH v2 1/2] xen/x86: io_apic: Introduce a command line option to skip timer check

Quoted 9 lines · Julien Grall
docs/misc/xen-command-line.pandoc +3 -0
@@ -2535,6 +2535,17 @@ pages) must also be specified via the tbuf_size parameter.
 ### tickle_one_idle_cpu
 > `= <boolean>`
 
+### pit-irq-works (x86)
+> `=<boolean>`
+

docs/misc/xen-command-line.pandoc pages) must also be specified via the tbuf_size parameter. · line 2550

Quoted 10 lines
docs/misc/xen-command-line.pandoc +8 -0
+> Default: `false`
+
+Disables the code which tests for broken timer IRQ sources. Enabling
+this option will reduce boot time on HW where the timer works properly.
+
+If the system is unstable when enabling the option, then it means you
+may have a broken HW and therefore the testing cannot be be skipped.
+
 ### timer_slop
 > `= <integer>`

With the rename this now needs to move up to retain sorting.

Quoted 18 lines
xen/arch/x86/io_apic.c +8 -0
@@ -57,6 +57,14 @@ bool __initdata ioapic_ack_forced;
 int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
 int __read_mostly nr_ioapics;
 
+/*
+ * The logic to check if the timer is working is expensive. So allow
+ * the admin to bypass it if they know their platform doesn't have
+ * a buggy timer.
+ */
+static bool __initdata pit_irq_works;
+boolean_param("pit-irq-works", pit_irq_works);
+
 /*
  * Rough estimation of how many shared IRQs there are, can
  * be changed anytime.

xen/arch/x86/io_apic.c static int __init timer_irq_works(void) · line 1514

Quoted 5 lines
xen/arch/x86/io_apic.c +2 -0
@@ -1502,6 +1510,9 @@ static int __init timer_irq_works(void)
 {
     unsigned long t1, flags;
 
+    if ( pit_irq_works )
+        return 1;

When the check is placed here, what exactly use of the option means is system dependent. I consider this somewhat risky, so I'd prefer if the check was put on the "normal" path in check_timer(). That way it'll affect only the one case which we can generally consider "known good", but not the cases where the virtual wire setups are being probed. I.e.

    if (pin1 != -1) {
        /*
         * Ok, does IRQ0 through the IOAPIC work?
         */
        unmask_IO_APIC_irq(irq_to_desc(0));
        if (pit_irq_works || timer_irq_works()) {
            local_irq_restore(flags);
            return;
        }

Plus this way changes to the various fallback paths can also be done without needing to consider users who might be making use of the new option.

Jan

#3 lore raw

Re: [PATCH v2 1/2] xen/x86: io_apic: Introduce a command line option to skip timer check

Hi,

Quoted 7 lines · Jan Beulich
Quoted 6 lines · Julien Grall
docs/misc/xen-command-line.pandoc +0 -0
@@ -2535,6 +2535,17 @@ pages) must also be specified via the tbuf_size parameter.
  ### tickle_one_idle_cpu
  > `= <boolean>`
 
Quoted 15 lines

docs/misc/xen-command-line.pandoc pages) must also be specified via the tbuf_size parameter. · line 2550

Quoted 13 lines
docs/misc/xen-command-line.pandoc +11 -0
+### pit-irq-works (x86)
+> `=<boolean>`
+
+> Default: `false`
+
+Disables the code which tests for broken timer IRQ sources. Enabling
+this option will reduce boot time on HW where the timer works properly.
+
+If the system is unstable when enabling the option, then it means you
+may have a broken HW and therefore the testing cannot be be skipped.
+
  ### timer_slop
  > `= <integer>`

With the rename this now needs to move up to retain sorting.

Ok.

Quoted 22 lines
Quoted 22 lines
xen/arch/x86/io_apic.c +8 -0
@@ -57,6 +57,14 @@ bool __initdata ioapic_ack_forced;
  int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
  int __read_mostly nr_ioapics;
 
+/*
+ * The logic to check if the timer is working is expensive. So allow
+ * the admin to bypass it if they know their platform doesn't have
+ * a buggy timer.
+ */
+static bool __initdata pit_irq_works;
+boolean_param("pit-irq-works", pit_irq_works);
+
  /*
   * Rough estimation of how many shared IRQs there are, can
   * be changed anytime.
@@ -1502,6 +1510,9 @@ static int __init timer_irq_works(void)
  {
      unsigned long t1, flags;
 
Quoted 8 lines

xen/arch/x86/io_apic.c static int __init timer_irq_works(void) · line 1514

Quoted 2 lines
xen/arch/x86/io_apic.c +2 -0
+    if ( pit_irq_works )
+        return 1;

When the check is placed here, what exactly use of the option means is system dependent. I consider this somewhat risky, so I'd prefer if the check was put on the "normal" path in check_timer(). That way it'll affect only the one case which we can generally consider "known good", but not the cases where the virtual wire setups are being probed. I.e.

I am not against restricting when we allow skipping the timer check. But in that case, I wonder why Linux is doing it differently?

After all, this code is heavily borrowed from Linux. So shouldn't we follow what they are doing?

Cheers,

Signature · 1 line
Julien Grall
#4 lore raw

Re: [PATCH v2 1/2] xen/x86: io_apic: Introduce a command line option to skip timer check

Quoted 32 lines · Julien Grall
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Julien Grall
xen/arch/x86/io_apic.c +8 -0
@@ -57,6 +57,14 @@ bool __initdata ioapic_ack_forced;
  int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
  int __read_mostly nr_ioapics;
 
+/*
+ * The logic to check if the timer is working is expensive. So allow
+ * the admin to bypass it if they know their platform doesn't have
+ * a buggy timer.
+ */
+static bool __initdata pit_irq_works;
+boolean_param("pit-irq-works", pit_irq_works);
+
  /*
   * Rough estimation of how many shared IRQs there are, can
   * be changed anytime.
@@ -1502,6 +1510,9 @@ static int __init timer_irq_works(void)
  {
      unsigned long t1, flags;
 
Quoted 8 lines

xen/arch/x86/io_apic.c static int __init timer_irq_works(void) · line 1514

Quoted 2 lines
xen/arch/x86/io_apic.c +2 -0
+    if ( pit_irq_works )
+        return 1;

When the check is placed here, what exactly use of the option means is system dependent. I consider this somewhat risky, so I'd prefer if the check was put on the "normal" path in check_timer(). That way it'll affect only the one case which we can generally consider "known good", but not the cases where the virtual wire setups are being probed. I.e.

Quoted 2 lines

I am not against restricting when we allow skipping the timer check. But in that case, I wonder why Linux is doing it differently?

Sadly Linux'es git history doesn't go back far enough (begins only at past 2.6.11), so I can't (easily) find the patch (and description) for the x86-64 change. The later i386 change is justified mainly by paravirt needs, so isn't applicable here. I wouldn't therefore exclude that my point above wasn't even taken into consideration. Furthermore their command line option is "no_timer_check", which to me firmly says "don't check" without regard to whether the source (PIT) is actually okay. That's different with the option name you (imo validly) chose.

Jan

#5 lore raw

Re: [PATCH v2 1/2] xen/x86: io_apic: Introduce a command line option to skip timer check

Hi Jan,

Quoted 24 lines · Jan Beulich
Quoted 23 lines · Julien Grall
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Julien Grall
xen/arch/x86/io_apic.c +8 -0
@@ -57,6 +57,14 @@ bool __initdata ioapic_ack_forced;
   int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
   int __read_mostly nr_ioapics;
 
+/*
+ * The logic to check if the timer is working is expensive. So allow
+ * the admin to bypass it if they know their platform doesn't have
+ * a buggy timer.
+ */
+static bool __initdata pit_irq_works;
+boolean_param("pit-irq-works", pit_irq_works);
+
   /*
    * Rough estimation of how many shared IRQs there are, can
    * be changed anytime.
@@ -1502,6 +1510,9 @@ static int __init timer_irq_works(void)
   {
       unsigned long t1, flags;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/io_apic.c static int __init timer_irq_works(void) · line 1514

Quoted 2 lines
xen/arch/x86/io_apic.c +2 -0
+    if ( pit_irq_works )
+        return 1;

When the check is placed here, what exactly use of the option means is system dependent. I consider this somewhat risky, so I'd prefer if the check was put on the "normal" path in check_timer(). That way it'll affect only the one case which we can generally consider "known good", but not the cases where the virtual wire setups are being probed. I.e.

By "known good", do you mean the following:

xen/arch/x86/io_apic.c +2 -1
index c89fbed8d675..c39d39ee951a 100644
@@ -1960,7 +1959,8 @@ static void __init check_timer(void)
          * Ok, does IRQ0 through the IOAPIC work?
          */
         unmask_IO_APIC_irq(irq_to_desc(0));
-        if (timer_irq_works()) {
+        if (pit_irq_works || timer_irq_works()) {
+            printk("====== pirq_irq_works %d =====\n", pit_irq_works);
             local_irq_restore(flags);
             return;
         }
Quoted 4 lines
Quoted 3 lines

I am not against restricting when we allow skipping the timer check. But in that case, I wonder why Linux is doing it differently?

Quoted 8 lines

Sadly Linux'es git history doesn't go back far enough (begins only at past 2.6.11), so I can't (easily) find the patch (and description) for the x86-64 change. The later i386 change is justified mainly by paravirt needs, so isn't applicable here. I wouldn't therefore exclude that my point above wasn't even taken into consideration. Furthermore their command line option is "no_timer_check", which to me firmly says "don't check" without regard to whether the source (PIT) is actually okay. That's different with the option name you (imo validly) chose.

Just to note that the name was suggested by Roger. I have to admit that I didn't check if this made sense for the existing placement.

Anyway, I tested the change on the HW where I wanted to skip the timer check. And I can confirm this is still skipping the timer check.

So I will send a new version with the diff above and some updated comments.

Cheers,

Signature · 1 line
Julien Grall
#6 lore raw

Re: [PATCH v2 1/2] xen/x86: io_apic: Introduce a command line option to skip timer check

Quoted 43 lines · Julien Grall

Hi Jan,

Quoted 24 lines · Jan Beulich
Quoted 23 lines · Julien Grall
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Julien Grall
xen/arch/x86/io_apic.c +8 -0
@@ -57,6 +57,14 @@ bool __initdata ioapic_ack_forced;
   int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
   int __read_mostly nr_ioapics;
 
+/*
+ * The logic to check if the timer is working is expensive. So allow
+ * the admin to bypass it if they know their platform doesn't have
+ * a buggy timer.
+ */
+static bool __initdata pit_irq_works;
+boolean_param("pit-irq-works", pit_irq_works);
+
   /*
    * Rough estimation of how many shared IRQs there are, can
    * be changed anytime.
@@ -1502,6 +1510,9 @@ static int __init timer_irq_works(void)
   {
       unsigned long t1, flags;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/io_apic.c static int __init timer_irq_works(void) · line 1514

Quoted 2 lines
xen/arch/x86/io_apic.c +2 -0
+    if ( pit_irq_works )
+        return 1;

When the check is placed here, what exactly use of the option means is system dependent. I consider this somewhat risky, so I'd prefer if the check was put on the "normal" path in check_timer(). That way it'll affect only the one case which we can generally consider "known good", but not the cases where the virtual wire setups are being probed. I.e.

By "known good", do you mean the following:

xen/arch/x86/io_apic.c +0 -0
index c89fbed8d675..c39d39ee951a 100644

xen/arch/x86/io_apic.c static void __init check_timer(void) · line 1966

Quoted 9 lines
xen/arch/x86/io_apic.c +2 -1
@@ -1960,7 +1959,8 @@ static void __init check_timer(void)
           * Ok, does IRQ0 through the IOAPIC work?
           */
          unmask_IO_APIC_irq(irq_to_desc(0));
-        if (timer_irq_works()) {
+        if (pit_irq_works || timer_irq_works()) {
+            printk("====== pirq_irq_works %d =====\n", pit_irq_works);
              local_irq_restore(flags);
              return;
          }

Yes.

Quoted 12 lines
Quoted 11 lines
Quoted 2 lines

I am not against restricting when we allow skipping the timer check. But in that case, I wonder why Linux is doing it differently?

Sadly Linux'es git history doesn't go back far enough (begins only at past 2.6.11), so I can't (easily) find the patch (and description) for the x86-64 change. The later i386 change is justified mainly by paravirt needs, so isn't applicable here. I wouldn't therefore exclude that my point above wasn't even taken into consideration. Furthermore their command line option is "no_timer_check", which to me firmly says "don't check" without regard to whether the source (PIT) is actually okay. That's different with the option name you (imo validly) chose.

Quoted 2 lines

Just to note that the name was suggested by Roger. I have to admit that I didn't check if this made sense for the existing placement.

Roger, thoughts?

Jan

Quoted 7 lines

Anyway, I tested the change on the HW where I wanted to skip the timer check. And I can confirm this is still skipping the timer check.

So I will send a new version with the diff above and some updated comments.

Cheers,

#7 lore raw

Re: [PATCH v2 1/2] xen/x86: io_apic: Introduce a command line option to skip timer check

Quoted 68 lines · Jan Beulich
Quoted 43 lines · Julien Grall

Hi Jan,

Quoted 24 lines · Jan Beulich
Quoted 23 lines · Julien Grall
Quoted 22 lines · Jan Beulich
Quoted 21 lines · Julien Grall
xen/arch/x86/io_apic.c +8 -0
@@ -57,6 +57,14 @@ bool __initdata ioapic_ack_forced;
   int __read_mostly nr_ioapic_entries[MAX_IO_APICS];
   int __read_mostly nr_ioapics;
 
+/*
+ * The logic to check if the timer is working is expensive. So allow
+ * the admin to bypass it if they know their platform doesn't have
+ * a buggy timer.
+ */
+static bool __initdata pit_irq_works;
+boolean_param("pit-irq-works", pit_irq_works);
+
   /*
    * Rough estimation of how many shared IRQs there are, can
    * be changed anytime.
@@ -1502,6 +1510,9 @@ static int __init timer_irq_works(void)
   {
       unsigned long t1, flags;
 
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines

xen/arch/x86/io_apic.c static int __init timer_irq_works(void) · line 1514

Quoted 2 lines
xen/arch/x86/io_apic.c +2 -0
+    if ( pit_irq_works )
+        return 1;

When the check is placed here, what exactly use of the option means is system dependent. I consider this somewhat risky, so I'd prefer if the check was put on the "normal" path in check_timer(). That way it'll affect only the one case which we can generally consider "known good", but not the cases where the virtual wire setups are being probed. I.e.

By "known good", do you mean the following:

xen/arch/x86/io_apic.c +0 -0
index c89fbed8d675..c39d39ee951a 100644

xen/arch/x86/io_apic.c static void __init check_timer(void) · line 1966

Quoted 9 lines
xen/arch/x86/io_apic.c +2 -1
@@ -1960,7 +1959,8 @@ static void __init check_timer(void)
           * Ok, does IRQ0 through the IOAPIC work?
           */
          unmask_IO_APIC_irq(irq_to_desc(0));
-        if (timer_irq_works()) {
+        if (pit_irq_works || timer_irq_works()) {
+            printk("====== pirq_irq_works %d =====\n", pit_irq_works);
              local_irq_restore(flags);
              return;
          }

Yes.

Quoted 12 lines
Quoted 11 lines
Quoted 2 lines

I am not against restricting when we allow skipping the timer check. But in that case, I wonder why Linux is doing it differently?

Sadly Linux'es git history doesn't go back far enough (begins only at past 2.6.11), so I can't (easily) find the patch (and description) for the x86-64 change. The later i386 change is justified mainly by paravirt needs, so isn't applicable here. I wouldn't therefore exclude that my point above wasn't even taken into consideration. Furthermore their command line option is "no_timer_check", which to me firmly says "don't check" without regard to whether the source (PIT) is actually okay. That's different with the option name you (imo validly) chose.

Quoted 4 lines
Quoted 2 lines

Just to note that the name was suggested by Roger. I have to admit that I didn't check if this made sense for the existing placement.

Roger, thoughts?

Right, with the usage of HPET in legacy replacement mode we are no longer exclusively testing the PIT, so might make sense to use a more generic name, timer-irq-works or some such.

Thanks, Roger.

#8 lore raw

PATCH v2 2/2 xen/x86: ioapic: Bail out from timer_irq_works() as soon as possible

In reply to Julien Grall

From: Julien Grall <[email protected]>

Currently timer_irq_works() will wait the full 100ms before checking that pit0_ticks has been incremented at least 4 times.

However, the bulk of the BIOS/platform should not have a buggy timer. So waiting for the full 100ms is a bit harsh.

Rework the logic to only wait until 100ms passed or we saw more than 4 ticks. So now, in the good case, this will reduce the wait time to ~50ms.

Take the opportunity to change the prototype of timer_irq_works() to return a bool rather than int (which was already acting as a bool because only 0/1 could be returned).

---

Note that local_irq_restore() cannot be replaced with local_irq_disable() because the function is not consistently called with IRQs off.

Changes in v2:
- Return bool rather than int
- Have a single return path
- Use 'unsigned int' rather than 'unsigned long' for msec

---
 xen/arch/x86/io_apic.c | 39 ++++++++++++++++++++++++---------------
 1 file changed, 24 insertions(+), 15 deletions(-)
xen/arch/x86/io_apic.c +24 -15
index 238b6c1c2837..c89fbed8d675 100644
@@ -1506,32 +1506,41 @@ static void __init setup_ioapic_ids_from_mpc(void)
  *	- if this function detects that timer IRQs are defunct, then we fall
  *	  back to ISA timer IRQs
  */
-static int __init timer_irq_works(void)
+static bool __init timer_irq_works(void)
 {
     unsigned long t1, flags;
+    /* Wait for maximum 10 ticks */
+    unsigned int msec = (10 * 1000) / HZ;
+    bool works = false;
 
     if ( pit_irq_works )
-        return 1;
+        return true;
 
     t1 = ACCESS_ONCE(pit0_ticks);
 
     local_save_flags(flags);
     local_irq_enable();
-    /* Let ten ticks pass... */
-    mdelay((10 * 1000) / HZ);
-    local_irq_restore(flags);
 
-    /*
-     * Expect a few ticks at least, to be sure some possible
-     * glue logic does not lock up after one or two first
-     * ticks in a non-ExtINT mode.  Also the local APIC
-     * might have cached one ExtINT interrupt.  Finally, at
-     * least one tick may be lost due to delays.
-     */
-    if ( (ACCESS_ONCE(pit0_ticks) - t1) > 4 )
-        return 1;
+    while ( msec-- )
+    {
+        mdelay(1);
+        /*
+         * Expect a few ticks at least, to be sure some possible
+         * glue logic does not lock up after one or two first
+         * ticks in a non-ExtINT mode.  Also the local APIC
+         * might have cached one ExtINT interrupt.  Finally, at
+         * least one tick may be lost due to delays.
+         */
+        if ( (ACCESS_ONCE(pit0_ticks) - t1) <= 4 )
+            continue;
 
-    return 0;
+        works = true;
+        break;
+    }
+
+    local_irq_restore(flags);
+
+    return works;
 }
 
 /*
Signature · 1 line
2.40.1
Reviewed-by on v2 2/2
#9 lore raw

Re: [PATCH v2 2/2] xen/x86: ioapic: Bail out from timer_irq_works() as soon as possible

Quoted 11 lines · Julien Grall

From: Julien Grall <[email protected]>

Currently timer_irq_works() will wait the full 100ms before checking that pit0_ticks has been incremented at least 4 times.

However, the bulk of the BIOS/platform should not have a buggy timer. So waiting for the full 100ms is a bit harsh.

Rework the logic to only wait until 100ms passed or we saw more than 4 ticks. So now, in the good case, this will reduce the wait time to ~50ms.

Isn't this more like 40ms (4 ticks 10ms apart)? And really somewhere between 30 and 40, because the first tick has already partly elapsed?

Quoted 5 lines

Take the opportunity to change the prototype of timer_irq_works() to return a bool rather than int (which was already acting as a bool because only 0/1 could be returned).

Jan