Skip to contentSkip to conversation

x86/x2apic: introduce a mixed physical/cluster mode

PATCH In review Roger Pau Monne 4 participants 4 messages Nov 6, 2023 – Nov 7, 2023

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 x86/x2apic: introduce a mixed physical/cluster mode

The current implementation of x2APIC requires to either use Cluster Logical or Physical mode for all interrupts. However the selection of Physical vs Logical is not done at APIC setup, an APIC can be addressed both in Physical or Logical destination modes concurrently.

Introduce a new x2APIC mode called Mixed, which uses Logical Cluster mode for IPIs, and Physical mode for external interrupts, thus attempting to use the best method for each interrupt type.

Using Physical mode for external interrupts allows more vectors to be used, and interrupt balancing to be more accurate.

Using Logical Cluster mode for IPIs allows fewer accesses to the ICR register when sending those, as multiple CPUs can be targeted with a single ICR register write.

A simple test calling flush_tlb_all() 10000 times on a tight loop on AMD EPYC 9754 with 512 CPUs gives the following figures in nano seconds:

x mixed
+ phys
* cluster
N Min Max Median Avg Stddev

x  25 3.5131328e+08 3.5716441e+08 3.5410987e+08 3.5432659e+08     1566737.4
+  12  1.231082e+09  1.238824e+09 1.2370528e+09 1.2357981e+09     2853892.9
Difference at 95.0% confidence
	8.81472e+08 +/- 1.46849e+06
	248.774% +/- 0.96566%
	(Student's t, pooled s = 2.05985e+06)

* 11 3.5099276e+08 3.5561459e+08 3.5461234e+08 3.5415668e+08 1415071.9
No difference proven at 95.0% confidence

So Mixed has no difference when compared to Cluster mode, and Physical mode is 248% slower when compared to either Mixed or Cluster modes with a 95% confidence.

Note that Xen uses Cluster mode by default, and hence is already using the fastest way for IPI delivery at the cost of reducing the amount of vectors available system-wide.

Make the newly introduced mode the default one.

Note the printing of the APIC addressing mode done in connect_bsp_APIC() has been removed, as with the newly introduced mixed mode this would require more fine grained printing, or else would be incorrect. The addressing mode can already be derived from the APIC driver in use, which is printed by different helpers.

---
Changes since v3:
- Rename Kconfig option.
- Cosmetic code fixes.
- Return negative error from parse_x2apic_mode().
- Add cf_check attribute to cmdline parsing hook.
- Add more figures re the performance difference.

Changes since v2:
- Split comment regarding usage of genapic fields.
- Remove the printing of the APIC addressing mode in connect_bsp_APIC().

Changes since v1:
- Add change log entry.
- Fix indentation and usage of tristate in Kconfig.
- Adjust comment regarding hooks used by external interrupts in apic_x2apic_mixed.

---
 CHANGELOG.md                      |  3 +
 docs/misc/xen-command-line.pandoc | 12 ++++
 xen/arch/x86/Kconfig              | 35 +++++++++--
 xen/arch/x86/apic.c               |  6 +-
 xen/arch/x86/genapic/x2apic.c     | 98 +++++++++++++++++++++++--------
 5 files changed, 119 insertions(+), 35 deletions(-)
CHANGELOG.md +3 -0
index b184dde8b15f..c341c9d0bf5d 100644
@@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
 ### Changed
 
 ### Added
+ - On x86:
+   - Introduce a new x2APIC driver that uses Cluster Logical addressing mode
+     for IPIs and Physical addressing mode for external interrupts.
 
 ### Removed
 
docs/misc/xen-command-line.pandoc +12 -0
index 9a19a04157cb..8e65f8bd18bf 100644
@@ -2804,6 +2804,15 @@ the watchdog.
 
 Permit use of x2apic setup for SMP environments.
 
+### x2apic-mode (x86)
+> `= physical | cluster | mixed`
+
+> Default: `physical` if **FADT** mandates physical mode, otherwise set at
+>          build time by CONFIG_X2APIC_{PHYSICAL,LOGICAL,MIXED}.
+
+In the case that x2apic is in use, this option switches between modes to
+address APICs in the system as interrupt destinations.
+
 ### x2apic_phys (x86)
 > `= <boolean>`
 
@@ -2814,6 +2823,9 @@ 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
 mode.
 
+**WARNING: `x2apic_phys` is deprecated and superseded by `x2apic-mode`.
+The latter takes precedence if both are set.**
+
 ### xenheap_megabytes (arm32)
 > `= <size>`
 
xen/arch/x86/Kconfig +30 -5
index eac77573bd75..1acdffc51c22 100644
@@ -228,11 +228,18 @@ config XEN_ALIGN_2M
 
 endchoice
 
-config X2APIC_PHYSICAL
-	bool "x2APIC Physical Destination mode"
+choice
+	prompt "x2APIC Driver default"
+	default X2APIC_MIXED
 	help
-	  Use x2APIC Physical Destination mode by default when available.
+	  Select APIC addressing when x2APIC is enabled.
+
+	  The default mode is mixed which should provide the best aspects
+	  of both physical and cluster modes.
 
+config X2APIC_PHYSICAL
+	bool "Physical Destination mode"
+	help
 	  When using this mode APICs are addressed using the Physical
 	  Destination mode, which allows using all dynamic vectors on each
 	  CPU independently.
@@ -242,9 +249,27 @@ config X2APIC_PHYSICAL
 	  destination inter processor interrupts (IPIs) slightly slower than
 	  Logical Destination mode.
 
-	  The mode when this option is not selected is Logical Destination.
+config X2APIC_CLUSTER
+	bool "Cluster Destination mode"
+	help
+	  When using this mode APICs are addressed using the Cluster Logical
+	  Destination mode.
+
+	  Cluster Destination has the benefit of sending IPIs faster since
+	  multiple APICs can be targeted as destinations of a single IPI.
+	  However the vector space is shared between all CPUs on the cluster,
+	  and hence using this mode reduces the number of available vectors
+	  when compared to Physical mode.
 
-	  If unsure, say N.
+config X2APIC_MIXED
+	bool "Mixed Destination mode"
+	help
+	  When using this mode APICs are addressed using the Cluster Logical
+	  Destination mode for IPIs and Physical mode for external interrupts.
+
+	  Should provide the best of both modes.
+
+endchoice
 
 config GUEST
 	bool
xen/arch/x86/apic.c +1 -5
index f1264ce7ed1e..6acdd0ec1468 100644
@@ -229,11 +229,7 @@ void __init connect_bsp_APIC(void)
         outb(0x01, 0x23);
     }
 
-    printk("Enabling APIC mode:  %s.  Using %d I/O APICs\n",
-           !INT_DEST_MODE ? "Physical"
-                          : init_apic_ldr == init_apic_ldr_flat ? "Flat"
-                                                                : "Clustered",
-           nr_ioapics);
+    printk("Enabling APIC mode.  Using %d I/O APICs\n", nr_ioapics);
     enable_apic_mode();
 }
 
xen/arch/x86/genapic/x2apic.c +73 -25

124 lines not on this page · raw message

Signature · 1 line
2.42.0
Reviewed-by on v4 1/1
#1 lore raw
Quoted 42 lines · Roger Pau Monne

The current implementation of x2APIC requires to either use Cluster Logical or Physical mode for all interrupts. However the selection of Physical vs Logical is not done at APIC setup, an APIC can be addressed both in Physical or Logical destination modes concurrently.

Introduce a new x2APIC mode called Mixed, which uses Logical Cluster mode for IPIs, and Physical mode for external interrupts, thus attempting to use the best method for each interrupt type.

Using Physical mode for external interrupts allows more vectors to be used, and interrupt balancing to be more accurate.

Using Logical Cluster mode for IPIs allows fewer accesses to the ICR register when sending those, as multiple CPUs can be targeted with a single ICR register write.

A simple test calling flush_tlb_all() 10000 times on a tight loop on AMD EPYC 9754 with 512 CPUs gives the following figures in nano seconds:

x mixed
+ phys
* cluster
N Min Max Median Avg Stddev

x  25 3.5131328e+08 3.5716441e+08 3.5410987e+08 3.5432659e+08     1566737.4
+  12  1.231082e+09  1.238824e+09 1.2370528e+09 1.2357981e+09     2853892.9
Difference at 95.0% confidence
	8.81472e+08 +/- 1.46849e+06
	248.774% +/- 0.96566%
	(Student's t, pooled s = 2.05985e+06)

* 11 3.5099276e+08 3.5561459e+08 3.5461234e+08 3.5415668e+08 1415071.9
No difference proven at 95.0% confidence

So Mixed has no difference when compared to Cluster mode, and Physical mode is 248% slower when compared to either Mixed or Cluster modes with a 95% confidence.

Note that Xen uses Cluster mode by default, and hence is already using the fastest way for IPI delivery at the cost of reducing the amount of vectors available system-wide.

Make the newly introduced mode the default one.

Quoted 8 lines

Note the printing of the APIC addressing mode done in connect_bsp_APIC() has been removed, as with the newly introduced mixed mode this would require more fine grained printing, or else would be incorrect. The addressing mode can already be derived from the APIC driver in use, which is printed by different helpers.

Acked-by
#2 lore raw

Hi Roger,

Quoted 51 lines · Andrew Cooper
Quoted 48 lines · Roger Pau Monne

The current implementation of x2APIC requires to either use Cluster Logical or Physical mode for all interrupts. However the selection of Physical vs Logical is not done at APIC setup, an APIC can be addressed both in Physical or Logical destination modes concurrently.

Introduce a new x2APIC mode called Mixed, which uses Logical Cluster mode for IPIs, and Physical mode for external interrupts, thus attempting to use the best method for each interrupt type.

Using Physical mode for external interrupts allows more vectors to be used, and interrupt balancing to be more accurate.

Using Logical Cluster mode for IPIs allows fewer accesses to the ICR register when sending those, as multiple CPUs can be targeted with a single ICR register write.

A simple test calling flush_tlb_all() 10000 times on a tight loop on AMD EPYC 9754 with 512 CPUs gives the following figures in nano seconds:

x mixed
+ phys
* cluster
N Min Max Median Avg Stddev

x  25 3.5131328e+08 3.5716441e+08 3.5410987e+08 3.5432659e+08     1566737.4
+  12  1.231082e+09  1.238824e+09 1.2370528e+09 1.2357981e+09     2853892.9

Difference at 95.0% confidence
8.81472e+08 +/- 1.46849e+06
248.774% +/- 0.96566%
(Student's t, pooled s = 2.05985e+06)
* 11 3.5099276e+08 3.5561459e+08 3.5461234e+08 3.5415668e+08 1415071.9
No difference proven at 95.0% confidence

So Mixed has no difference when compared to Cluster mode, and Physical mode is 248% slower when compared to either Mixed or Cluster modes with a 95% confidence.

Note that Xen uses Cluster mode by default, and hence is already using the fastest way for IPI delivery at the cost of reducing the amount of vectors available system-wide.

Make the newly introduced mode the default one.

Note the printing of the APIC addressing mode done in connect_bsp_APIC() has been removed, as with the newly introduced mixed mode this would require more fine grained printing, or else would be incorrect. The addressing mode can already be derived from the APIC driver in use, which is printed by different helpers.

Quoted 4 lines
Quoted 2 lines

Kind regards,
Henry

Reviewed-by on v4 1/1
#3 lore raw
In reply to Roger Pau Monne
Quoted 42 lines · Roger Pau Monne

The current implementation of x2APIC requires to either use Cluster Logical or Physical mode for all interrupts. However the selection of Physical vs Logical is not done at APIC setup, an APIC can be addressed both in Physical or Logical destination modes concurrently.

Introduce a new x2APIC mode called Mixed, which uses Logical Cluster mode for IPIs, and Physical mode for external interrupts, thus attempting to use the best method for each interrupt type.

Using Physical mode for external interrupts allows more vectors to be used, and interrupt balancing to be more accurate.

Using Logical Cluster mode for IPIs allows fewer accesses to the ICR register when sending those, as multiple CPUs can be targeted with a single ICR register write.

A simple test calling flush_tlb_all() 10000 times on a tight loop on AMD EPYC 9754 with 512 CPUs gives the following figures in nano seconds:

x mixed
+ phys
* cluster
N Min Max Median Avg Stddev

x  25 3.5131328e+08 3.5716441e+08 3.5410987e+08 3.5432659e+08     1566737.4
+  12  1.231082e+09  1.238824e+09 1.2370528e+09 1.2357981e+09     2853892.9
Difference at 95.0% confidence
	8.81472e+08 +/- 1.46849e+06
	248.774% +/- 0.96566%
	(Student's t, pooled s = 2.05985e+06)

* 11 3.5099276e+08 3.5561459e+08 3.5461234e+08 3.5415668e+08 1415071.9
No difference proven at 95.0% confidence

So Mixed has no difference when compared to Cluster mode, and Physical mode is 248% slower when compared to either Mixed or Cluster modes with a 95% confidence.

Note that Xen uses Cluster mode by default, and hence is already using the fastest way for IPI delivery at the cost of reducing the amount of vectors available system-wide.

Make the newly introduced mode the default one.

Quoted 8 lines

Note the printing of the APIC addressing mode done in connect_bsp_APIC() has been removed, as with the newly introduced mixed mode this would require more fine grained printing, or else would be incorrect. The addressing mode can already be derived from the APIC driver in use, which is printed by different helpers.