Skip to contentSkip to conversation

x86/irq: switch x2APIC default destination mode

PATCH In review Roger Pau Monne 2 participants 12 messages Jun 30, 2022 – Jul 5, 2022

lore
Share & tools
lore
#0 lore raw

v2 cover x86/irq: switch x2APIC default destination mode

Hello,

The following series aims to change the default x2APIC Destination mode from Logical to Physical. This is done in order to cope with boxes that don't have a huge amount of CPUs, but do have a non trivial amount of PCI devices using MSI(-X).

The default x2APIC destination mode can now be set from Kconfig, and will default to phys in order to reliable boot on all boxes.

Further patches are a bit of cleanup related to the interrupt limits reported at boot, and making those values more realistic.

Thanks, Roger.

Roger Pau Monne (6):
x86/Kconfig: add option for default x2APIC destination mode x86/x2apic: use physical destination mode by default x86/setup: init nr_irqs after having detected x2APIC support x86/irq: fix setting irq limits
x86/irq: print nr_irqs as limit on the number of MSI(-X) interrupts x86/irq: do not set nr_irqs based on nr_irqs_gsi in APIC mode

 docs/misc/xen-command-line.pandoc |  5 ++---
 xen/arch/x86/Kconfig              | 19 +++++++++++++++++++
 xen/arch/x86/genapic/x2apic.c     |  6 ++++--
 xen/arch/x86/include/asm/apic.h   |  2 ++
 xen/arch/x86/io_apic.c            | 10 ----------
 xen/arch/x86/irq.c                | 15 +++++++++++++++
 xen/arch/x86/mpparse.c            |  5 +++++
 7 files changed, 47 insertions(+), 15 deletions(-)
Signature · 1 line
2.36.1
#1 lore raw

PATCH v2 1/6 x86/Kconfig: add option for default x2APIC destination mode

Allow setting the default x2APIC destination mode from Kconfig to Physical.

Note the default destination mode is still Logical (Cluster) mode.

---
Changes since v1:
- Use a boolean rather than a choice.
- Expand to X2APIC_PHYSICAL.
---
TBH I wasn't sure whether to keep X2APIC_PHYSICAL or X2APIC_LOGICAL as the Kconfig option, went with X2APIC_PHYSICAL because that's the define the code was already using.

---
 xen/arch/x86/Kconfig          | 18 ++++++++++++++++++
 xen/arch/x86/genapic/x2apic.c |  6 ++++--
 2 files changed, 22 insertions(+), 2 deletions(-)
xen/arch/x86/Kconfig +18 -0
index 1e31edc99f..6bed72b791 100644
@@ -226,6 +226,24 @@ config XEN_ALIGN_2M
 
 endchoice
 
+config X2APIC_PHYSICAL
+	bool "x2APIC Physical Destination mode"
+	help
+	  Use x2APIC Physical Destination mode by default when available.
+
+	  When using this mode APICs are addressed using the Physical
+	  Destination mode, which allows using all dynamic vectors on each
+	  CPU independently.
+
+	  Physical Destination has the benefit of having more vectors available
+	  for external interrupts, but it also makes the delivery of multi
+	  destination inter processor interrupts (IPIs) slightly slower than
+	  Logical Destination mode.
+
+	  The mode when this option is not selected is Logical Destination.
+
+	  If unsure, say N.
+
 config GUEST
 	bool
 
xen/arch/x86/genapic/x2apic.c +4 -2
index de5032f202..7dfc793514 100644
@@ -228,7 +228,7 @@ static struct notifier_block x2apic_cpu_nfb = {
    .notifier_call = update_clusterinfo
 };
 
-static s8 __initdata x2apic_phys = -1; /* By default we use logical cluster mode. */
+static int8_t __initdata x2apic_phys = -1;
 boolean_param("x2apic_phys", x2apic_phys);
 
 const struct genapic *__init apic_x2apic_probe(void)
@@ -241,7 +241,9 @@ const struct genapic *__init apic_x2apic_probe(void)
          * the usage of the high 16 bits to hold the cluster ID.
          */
         x2apic_phys = !iommu_intremap ||
-                      (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL);
+                      (acpi_gbl_FADT.flags & ACPI_FADT_APIC_PHYSICAL) ||
+                      (IS_ENABLED(CONFIG_X2APIC_PHYSICAL) &&
+                       !(acpi_gbl_FADT.flags & ACPI_FADT_APIC_CLUSTER));
     }
     else if ( !x2apic_phys )
         switch ( iommu_intremap )
Signature · 1 line
2.36.1
#3 lore raw

PATCH v2 2/6 x86/x2apic: use physical destination mode by default

In reply to Roger Pau Monne

Using cluster mode by default greatly limits the amount of vectors available, as then vector space is shared amongst all the CPUs in the logical cluster.

This can lead to vector shortage issues on boxes with not a huge amount of CPUs but with a non-trivial amount of devices. There are reports of boxes with 32 CPUs (2 logical clusters, and thus only 414 dynamic vectors) that run out of vectors and fail to setup interrupts for dom0.

This could be considered a regression when switching from xAPIC, as when using xAPIC physical mode is the default when the system has more than 8 CPUs.

Note that using Physical Destination mode is less efficient than Logical mode when sending IPIs to multiple CPUs, as in Logical mode IPIs to CPUs on the same Cluster can be batched together.

Switch default Kconfig selection to use x2APIC physical destination mode.

---
Changes since v1:
- Expand commit message.

---
 docs/misc/xen-command-line.pandoc | 5 ++---
 xen/arch/x86/Kconfig              | 3 ++-
 2 files changed, 4 insertions(+), 4 deletions(-)
docs/misc/xen-command-line.pandoc +2 -3
index a92b7d228c..952874c4f4 100644
@@ -2646,11 +2646,10 @@ Permit use of x2apic setup for SMP environments.
 ### x2apic_phys (x86)
 > `= <boolean>`
 
-> Default: `true` if **FADT** mandates physical mode or if interrupt remapping
->          is not available, `false` otherwise.
+> Default: `false` if **FADT** mandates cluster mode, `true` otherwise.
 
 In the case that x2apic is in use, this option switches between physical and
-clustered mode.  The default, given no hint from the **FADT**, is cluster
+clustered mode.  The default, given no hint from the **FADT**, is physical
 mode.
 
 ### xenheap_megabytes (arm32)
xen/arch/x86/Kconfig +2 -1
index 6bed72b791..09441761d1 100644
@@ -228,6 +228,7 @@ endchoice
 
 config X2APIC_PHYSICAL
 	bool "x2APIC Physical Destination mode"
+	default y
 	help
 	  Use x2APIC Physical Destination mode by default when available.
 
@@ -242,7 +243,7 @@ config X2APIC_PHYSICAL
 
 	  The mode when this option is not selected is Logical Destination.
 
-	  If unsure, say N.
+	  If unsure, say Y.
 
 config GUEST
 	bool
Signature · 1 line
2.36.1
#4 lore raw

PATCH v2 3/6 x86/setup: init nr_irqs after having detected x2APIC support

In reply to Roger Pau Monne

Logic in ioapic_init() that sets the number of available vectors for external interrupts requires knowing the x2APIC Destination Mode. As such move the call after x2APIC BSP setup.

Do it as part of init_irq_data(), which is called just after x2APIC BSP init and also makes use of nr_irqs itself.

No functional change intended.

---
 xen/arch/x86/io_apic.c | 10 ----------
 xen/arch/x86/irq.c     | 10 ++++++++++
 2 files changed, 10 insertions(+), 10 deletions(-)
xen/arch/x86/io_apic.c +0 -10
index c086f40f63..8d4923ba9a 100644
@@ -2653,16 +2653,6 @@ void __init ioapic_init(void)
                max_gsi_irqs, nr_irqs_gsi);
         nr_irqs_gsi = max_gsi_irqs;
     }
-
-    if ( nr_irqs == 0 )
-        nr_irqs = cpu_has_apic ?
-                  max(0U + num_present_cpus() * NR_DYNAMIC_VECTORS,
-                      8 * nr_irqs_gsi) :
-                  nr_irqs_gsi;
-    else if ( nr_irqs < 16 )
-        nr_irqs = 16;
-    printk(XENLOG_INFO "IRQ limits: %u GSI, %u MSI/MSI-X\n",
-           nr_irqs_gsi, nr_irqs - nr_irqs_gsi);
 }
 
 unsigned int arch_hwdom_irqs(domid_t domid)
xen/arch/x86/irq.c +10 -0
index de30ee7779..b51e25f696 100644
@@ -420,6 +420,16 @@ int __init init_irq_data(void)
     struct irq_desc *desc;
     int irq, vector;
 
+    if ( nr_irqs == 0 )
+        nr_irqs = cpu_has_apic ? max(0U + num_present_cpus() *
+                                     NR_DYNAMIC_VECTORS, 8 * nr_irqs_gsi)
+                               : nr_irqs_gsi;
+    else if ( nr_irqs < 16 )
+        nr_irqs = 16;
+
+    printk(XENLOG_INFO "IRQ limits: %u GSI, %u MSI/MSI-X\n",
+           nr_irqs_gsi, nr_irqs - nr_irqs_gsi);
+
     for ( vector = 0; vector < X86_NR_VECTORS; ++vector )
         this_cpu(vector_irq)[vector] = INT_MIN;
 
Signature · 1 line
2.36.1
#5 lore raw

Re: [PATCH v2 3/6] x86/setup: init nr_irqs after having detected x2APIC support

Quoted 3 lines · Roger Pau Monne

Logic in ioapic_init() that sets the number of available vectors for external interrupts requires knowing the x2APIC Destination Mode. As such move the call after x2APIC BSP setup.

"requires" reads as if this was the case already, which I don't think is true. The dependency likely appears with the next patch (didn't look there, yet).

Quoted 22 lines
xen/arch/x86/io_apic.c +0 -10
@@ -2653,16 +2653,6 @@ void __init ioapic_init(void)
                max_gsi_irqs, nr_irqs_gsi);
         nr_irqs_gsi = max_gsi_irqs;
     }
-
-    if ( nr_irqs == 0 )
-        nr_irqs = cpu_has_apic ?
-                  max(0U + num_present_cpus() * NR_DYNAMIC_VECTORS,
-                      8 * nr_irqs_gsi) :
-                  nr_irqs_gsi;
-    else if ( nr_irqs < 16 )
-        nr_irqs = 16;
-    printk(XENLOG_INFO "IRQ limits: %u GSI, %u MSI/MSI-X\n",
-           nr_irqs_gsi, nr_irqs - nr_irqs_gsi);
 }
 
 unsigned int arch_hwdom_irqs(domid_t domid)

xen/arch/x86/irq.c int __init init_irq_data(void) · line 426

Quoted 7 lines
xen/arch/x86/irq.c +4 -0
@@ -420,6 +420,16 @@ int __init init_irq_data(void)
     struct irq_desc *desc;
     int irq, vector;
 
+    if ( nr_irqs == 0 )
+        nr_irqs = cpu_has_apic ? max(0U + num_present_cpus() *
+                                     NR_DYNAMIC_VECTORS, 8 * nr_irqs_gsi)
+                               : nr_irqs_gsi;

Splitting a function argument across lines and then putting the next argument on the same line is, well, confusing. May I suggest to either stick to the original line splitting or to go to

    if ( nr_irqs == 0 )
        nr_irqs = cpu_has_apic
                  ? max(0U + num_present_cpus() * NR_DYNAMIC_VECTORS,
                        8 * nr_irqs_gsi)
                  : nr_irqs_gsi;

?

Jan

#6 lore raw

PATCH v2 4/6 x86/irq: fix setting irq limits

In reply to Roger Pau Monne

Current code to calculate nr_irqs assumes the APIC destination mode to be physical, so all vectors on each possible CPU is available for use by a different interrupt source. This is not true when using Logical (Cluster) destination mode, where CPUs in the same cluster share the vector space.

Fix by calculating the maximum Cluster ID and use it to derive the number of clusters in the system. Note the code assumes Cluster IDs to be contiguous, or else we will set nr_irqs to a number higher than the real amount of vectors (still not fatal).

The number of clusters is then used instead of the number of present CPUs when calculating the value of nr_irqs.

---
 xen/arch/x86/genapic/x2apic.c   |  2 +-
 xen/arch/x86/include/asm/apic.h |  2 ++
 xen/arch/x86/irq.c              | 10 ++++++++--
 xen/arch/x86/mpparse.c          |  5 +++++
 4 files changed, 16 insertions(+), 3 deletions(-)
xen/arch/x86/genapic/x2apic.c +1 -1
index 7dfc793514..ad95564e90 100644
@@ -228,7 +228,7 @@ static struct notifier_block x2apic_cpu_nfb = {
    .notifier_call = update_clusterinfo
 };
 
-static int8_t __initdata x2apic_phys = -1;
+int8_t __initdata x2apic_phys = -1;
 boolean_param("x2apic_phys", x2apic_phys);
 
 const struct genapic *__init apic_x2apic_probe(void)
xen/arch/x86/include/asm/apic.h +2 -0
index 7625c0ecd6..6060628836 100644
@@ -27,6 +27,8 @@ enum apic_mode {
 extern bool iommu_x2apic_enabled;
 extern u8 apic_verbosity;
 extern bool directed_eoi_enabled;
+extern uint16_t x2apic_max_cluster_id;
+extern int8_t x2apic_phys;
 
 void check_x2apic_preenabled(void);
 void x2apic_bsp_setup(void);
xen/arch/x86/irq.c +8 -2
index b51e25f696..b64d18c450 100644
@@ -421,9 +421,15 @@ int __init init_irq_data(void)
     int irq, vector;
 
     if ( nr_irqs == 0 )
-        nr_irqs = cpu_has_apic ? max(0U + num_present_cpus() *
-                                     NR_DYNAMIC_VECTORS, 8 * nr_irqs_gsi)
+    {
+        unsigned int vec_spaces =
+            (x2apic_enabled && !x2apic_phys) ? x2apic_max_cluster_id + 1
+                                             : num_present_cpus();
+
+        nr_irqs = cpu_has_apic ? max(vec_spaces * NR_DYNAMIC_VECTORS,
+                                     8 * nr_irqs_gsi)
                                : nr_irqs_gsi;
+    }
     else if ( nr_irqs < 16 )
         nr_irqs = 16;
 
xen/arch/x86/mpparse.c +5 -0
index d8ccab2449..dc112bffc7 100644
@@ -131,6 +131,8 @@ static int __init mpf_checksum(unsigned char *mp, int len)
 	return sum & 0xFF;
 }
 
+uint16_t __initdata x2apic_max_cluster_id;
+
 /* Return xen's logical cpu_id of the new added cpu or <0 if error */
 static int MP_processor_info_x(struct mpc_config_processor *m,
 			       u32 apicid, bool hotplug)
@@ -199,6 +201,9 @@ static int MP_processor_info_x(struct mpc_config_processor *m,
 		def_to_bigsmp = true;
 	}
 
+	x2apic_max_cluster_id = max(x2apic_max_cluster_id,
+				    (uint16_t)(apicid >> 4));
+
 	return cpu;
 }
 
Signature · 1 line
2.36.1
#7 lore raw

Re: [PATCH v2 4/6] x86/irq: fix setting irq limits

Quoted 10 lines · Roger Pau Monne

Current code to calculate nr_irqs assumes the APIC destination mode to be physical, so all vectors on each possible CPU is available for use by a different interrupt source. This is not true when using Logical (Cluster) destination mode, where CPUs in the same cluster share the vector space.

Fix by calculating the maximum Cluster ID and use it to derive the number of clusters in the system. Note the code assumes Cluster IDs to be contiguous, or else we will set nr_irqs to a number higher than the real amount of vectors (still not fatal).

While not fatal, it could be (pretty) wasteful. Iirc discontiguous cluster numbers aren't unusual. It shouldn't be that difficult to actually count clusters.

Quoted 2 lines

The number of clusters is then used instead of the number of present CPUs when calculating the value of nr_irqs.

This takes care of x2APIC clustered mode, but leaves xAPIC flat mode still as broken as before. vec_spaces should be 1 in that case, shouldn't it?

xen/arch/x86/include/asm/apic.h enum apic_mode { · line 30

Quoted 7 lines
xen/arch/x86/include/asm/apic.h +1 -0
@@ -27,6 +27,8 @@ enum apic_mode {
 extern bool iommu_x2apic_enabled;
 extern u8 apic_verbosity;
 extern bool directed_eoi_enabled;
+extern uint16_t x2apic_max_cluster_id;

I don't see a need to use a fixed width type here; unsigned int will be quite fine afaict (and be in line with ./CODING_STYLE). Or if you want to save space, then it still would better be "unsigned short".

xen/arch/x86/include/asm/apic.h static int MP_processor_info_x(struct mpc_config_processor *m, · line 208

Quoted 9 lines
xen/arch/x86/include/asm/apic.h +3 -0
@@ -199,6 +201,9 @@ static int MP_processor_info_x(struct mpc_config_processor *m,
 		def_to_bigsmp = true;
 	}
 
+	x2apic_max_cluster_id = max(x2apic_max_cluster_id,
+				    (uint16_t)(apicid >> 4));
+
 	return cpu;
 }

I assume you don't mean to also account for hotplug CPUs? If so, please add a respective statement to the description.

Jan

#8 lore raw

PATCH v2 5/6 x86/irq: print nr_irqs as limit on the number of MSI(-X) interrupts

In reply to Roger Pau Monne

Using nr_irqs minus nr_irqs_gsi is misleading, as GSI interrupts are not allocated unless requested by the hardware domain, so a hardware domain could not use any GSI (or just one for the ACPI SCI), and hence (almost) all nr_irqs will be available for MSI(-X) usage.

No functional difference, just affects the printed message.

---
 xen/arch/x86/irq.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/irq.c +1 -1
index b64d18c450..7f75ec8bcc 100644
@@ -434,7 +434,7 @@ int __init init_irq_data(void)
         nr_irqs = 16;
 
     printk(XENLOG_INFO "IRQ limits: %u GSI, %u MSI/MSI-X\n",
-           nr_irqs_gsi, nr_irqs - nr_irqs_gsi);
+           nr_irqs_gsi, nr_irqs);
 
     for ( vector = 0; vector < X86_NR_VECTORS; ++vector )
         this_cpu(vector_irq)[vector] = INT_MIN;
Signature · 1 line
2.36.1
#9 lore raw

Re: [PATCH v2 5/6] x86/irq: print nr_irqs as limit on the number of MSI(-X) interrupts

Quoted 4 lines · Roger Pau Monne

Using nr_irqs minus nr_irqs_gsi is misleading, as GSI interrupts are not allocated unless requested by the hardware domain, so a hardware domain could not use any GSI (or just one for the ACPI SCI), and hence (almost) all nr_irqs will be available for MSI(-X) usage.

Looking at create_irq() I find a loop going from nr_irqs_gsi to nr_irqs. Similar in get_free_pirq(). All lower numbers aren't eligible for use by MSI/MSI-X. We can't allow the lower ones to be used for other than GSI purposes because we can't know whether any device will become enabled (perhaps long after boot) which wants to use a specific GSI.

Jan

#10 lore raw

PATCH v2 6/6 x86/irq: do not set nr_irqs based on nr_irqs_gsi in APIC mode

In reply to Roger Pau Monne

When using an APIC do not set nr_irqs based on a factor of nr_irqs_gsi (currently x8), and instead do so exclusively based on the amount of available vectors on the system.

There's no point in setting nr_irqs to a value higher than the available set of vectors, as vector allocation will fail anyway.

Fixes e99d45da8a ('x86/x2apic: properly implement cluster mode')
---
 xen/arch/x86/irq.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
xen/arch/x86/irq.c +1 -2
index 7f75ec8bcc..e3b0bee527 100644
@@ -426,8 +426,7 @@ int __init init_irq_data(void)
             (x2apic_enabled && !x2apic_phys) ? x2apic_max_cluster_id + 1
                                              : num_present_cpus();
 
-        nr_irqs = cpu_has_apic ? max(vec_spaces * NR_DYNAMIC_VECTORS,
-                                     8 * nr_irqs_gsi)
+        nr_irqs = cpu_has_apic ? vec_spaces * NR_DYNAMIC_VECTORS
                                : nr_irqs_gsi;
     }
     else if ( nr_irqs < 16 )
Signature · 1 line
2.36.1
#11 lore raw

Re: [PATCH v2 6/6] x86/irq: do not set nr_irqs based on nr_irqs_gsi in APIC mode

Quoted 6 lines · Roger Pau Monne

When using an APIC do not set nr_irqs based on a factor of nr_irqs_gsi (currently x8), and instead do so exclusively based on the amount of available vectors on the system.

There's no point in setting nr_irqs to a value higher than the available set of vectors, as vector allocation will fail anyway.

Only if in the earlier calculation you include hotplug CPUs.

xen/arch/x86/irq.c int __init init_irq_data(void) · line 430

Quoted 10 lines
xen/arch/x86/irq.c +1 -2
@@ -426,8 +426,7 @@ int __init init_irq_data(void)
             (x2apic_enabled && !x2apic_phys) ? x2apic_max_cluster_id + 1
                                              : num_present_cpus();
 
-        nr_irqs = cpu_has_apic ? max(vec_spaces * NR_DYNAMIC_VECTORS,
-                                     8 * nr_irqs_gsi)
+        nr_irqs = cpu_has_apic ? vec_spaces * NR_DYNAMIC_VECTORS
                                : nr_irqs_gsi;

I wonder how useful it is to retain the !cpu_has_apic case here. I don't have much hope for Xen to work properly if APIC use was suppressed, and I'm unaware of x86-64 CPUs without APIC.

Jan