x86/x2apic: introduce a mixed physical/cluster mode
PATCH v3 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 less 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 in a tight loop on a 96 CPU box gives the following average figures:
Physical mode: 26617931ns Mixed mode: 23865337ns
So ~10% improvement versus plain Physical mode. 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 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 | 2 + docs/misc/xen-command-line.pandoc | 12 ++++ xen/arch/x86/Kconfig | 35 +++++++++-- xen/arch/x86/apic.c | 6 +- xen/arch/x86/genapic/x2apic.c | 96 +++++++++++++++++++++++-------- 5 files changed, 116 insertions(+), 35 deletions(-)
CHANGELOG.md +2 -0
@@ -9,6 +9,8 @@ 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
@@ -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
@@ -228,11 +228,18 @@ config XEN_ALIGN_2M endchoice -config X2APIC_PHYSICAL - bool "x2APIC Physical Destination mode" +choice + prompt "x2APIC Destination mode" + 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
@@ -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 +71 -25
122 lines not on this page · raw message
Signature · 1 line
2.42.0
Quoted 1 line · Roger Pau Monne
The current implementation of x2APIC requires to either use Cluster Logical or
I'd suggest starting with "Xen's current ..." to make it clear that this is our logic, not a property of x2APIC itself.
Quoted 4 lines
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.
Quoted 8 lines
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 less accesses to the ICR register
s/less/fewer/
Quoted 6 lines
when sending those, as multiple CPUs can be targeted with a single ICR register write.
A simple test calling
flush_tlb_all()10000 times in a tight loop on a 96 CPU box gives the following average figures:
Quoted 6 lines
Physical mode: 26617931ns Mixed mode: 23865337nsSo ~10% improvement versus plain Physical mode. 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.
96 looks suspiciously like an Intel number. In nothing else, you ought to say which CPU is it, because microarchitecture matters. By any chance can we try this on one of the Bergamos, to give us a datapoint at 512?
As far as the stats go, it would be helpful to give a number for Cluster mode too, because if it's different from Mixed mode (in this test), then it shows an error in our reasoning.
Quoted 10 lines
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.Suggested-by Andrew Cooper <[email protected]>Signed-off-by Roger Pau Monné <[email protected]>
Thankyou for doing this. It's far less invasive than I was fearing.
One real bug (which Gitlab will block on), one other suspected bug that probably nothing will notice. Minor notes otherwise.
CHANGELOG.md The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/) · line 13
Quoted 10 lines
CHANGELOG.md +2
-0@@ -9,6 +9,8 @@ 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.
Could I request that you copy the structure of 4.18 post-reformat, in the hope that I don't have to repeat that patch for 4.19.
Something like
- On x86:
- Introduce a new ...
xen/arch/x86/Kconfig config XEN_ALIGN_2M · line 232
Quoted 12 lines
xen/arch/x86/Kconfig +2
-2@@ -228,11 +228,18 @@ config XEN_ALIGN_2M endchoice -config X2APIC_PHYSICAL - bool "x2APIC Physical Destination mode" +choice + prompt "x2APIC Destination mode"
"x2APIC Driver default" is going to be more meaningful to a non-expert reading this menu entry, IMO.
Quoted 7 lines
xen/arch/x86/genapic/x2apic.c +0
-0@@ -180,6 +180,34 @@ static const struct genapic __initconstrel apic_x2apic_cluster = { .send_IPI_self = send_IPI_self_x2apic };
xen/arch/x86/genapic/x2apic.c static const struct genapic __initconstrel apic_x2apic_cluster = { · line 191
Quoted 10 lines
xen/arch/x86/genapic/x2apic.c +9
-0+/* +* Mixed x2APIC mode: use physical for external (device) interrupts, and +* cluster for inter processor interrupts. Such mode has the benefits of not +* sharing the vector space with all CPUs on the cluster, while still allowing +* IPIs to be more efficiently delivered by not having to perform an ICR write +* for each target CPU. +*/ +static const struct genapic __initconstrel apic_x2apic_mixed = { +APIC_INIT ("x2apic_mixed" ,NULL ),
Newline here please.
xen/arch/x86/genapic/x2apic.c static const struct genapic __initconstrel apic_x2apic_cluster = { · line 199
Quoted 8 lines
xen/arch/x86/genapic/x2apic.c +8
-0+/* +* The following fields are exclusively used by external interrupts and +* hence are set to use Physical destination mode handlers. +*/ + .int_delivery_mode = dest_Fixed, + .int_dest_mode = 0 /* physical delivery */, + .vector_allocation_cpumask = vector_allocation_cpumask_phys, + .cpu_mask_to_apicid = cpu_mask_to_apicid_phys,
And here.
xen/arch/x86/genapic/x2apic.c static const struct genapic __initconstrel apic_x2apic_cluster = { · line 208
Quoted 9 lines
xen/arch/x86/genapic/x2apic.c +9
-0+/* +* The following fields are exclusively used by IPIs and hence are set to +* use Cluster Logical destination mode handlers. Note that init_apic_ldr +* is not used by IPIs, but the per-CPU fields it initializes are only used +* by the IPI hooks. +*/ + .init_apic_ldr = init_apic_ldr_x2apic_cluster, + .send_IPI_mask = send_IPI_mask_x2apic_cluster, + .send_IPI_self = send_IPI_self_x2apic
Trailing comma please.
Quoted 6 lines
xen/arch/x86/genapic/x2apic.c +2
-0+}; + static int cf_check update_clusterinfo(struct notifier_block *nfb,unsigned long action,void *hcpu) {
xen/arch/x86/genapic/x2apic.c static struct notifier_block x2apic_cpu_nfb = { · line 255
Quoted 8 lines
xen/arch/x86/genapic/x2apic.c +5
-0@@ -220,38 +248,56 @@ static struct notifier_block x2apic_cpu_nfb = {static int8_t __initdata x2apic_phys =-1 ;boolean_param ("x2apic_phys" , x2apic_phys); +enum { + unset, physical, cluster, mixed +}static __initdata x2apic_mode = unset; + +static int __init parse_x2apic_mode (const char *s)
cf_check
xen/arch/x86/genapic/x2apic.c static struct notifier_block x2apic_cpu_nfb = { · line 264
Quoted 9 lines
xen/arch/x86/genapic/x2apic.c +9
-0+{ +if ( !cmdline_strcmp (s,"physical" ) ) + x2apic_mode = physical; +else if ( !cmdline_strcmp (s,"cluster" ) ) + x2apic_mode = cluster; +else if ( !cmdline_strcmp (s,"mixed" ) ) + x2apic_mode = mixed; +else +return EINVAL ;
-EINVAL ?
xen/arch/x86/genapic/x2apic.c static struct notifier_block x2apic_cpu_nfb = { · line 274
Quoted 11 lines
xen/arch/x86/genapic/x2apic.c +8
-1+ +return 0 ; +} +custom_param ("x2apic-mode" , parse_x2apic_mode); +const struct genapic *__init apic_x2apic_probe (void ) { -if ( x2apic_phys <0 ) +/* x2apic-mode option has preference over x2apic_phys. */ +if ( x2apic_phys >=0 && x2apic_mode == unset ) + x2apic_mode = x2apic_phys ? physical : cluster;
I know this is just a rearrangement, but IMO it's clearer to follow as:
/* Honour the legacy cmdline setting if it's the only one provided. */ if ( x2apic_mode == unset && x2apic_phys >= 0 )
...
There are too many x2apic's in the first comment to read what's going on at a glance.
~Andrew
Quoted 30 lines · Andrew Cooper
Quoted 1 line · Roger Pau Monne
The current implementation of x2APIC requires to either use Cluster Logical or
I'd suggest starting with "Xen's current ..." to make it clear that this is our logic, not a property of x2APIC itself.
Quoted 4 lines
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.
Quoted 8 lines
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 less accesses to the ICR register
s/less/fewer/
Quoted 9 lines
when sending those, as multiple CPUs can be targeted with a single ICR register write.
A simple test calling
flush_tlb_all()10000 times in a tight loop on a 96 CPU box gives the following average figures:Physical mode: 26617931ns Mixed mode: 23865337ns
Quoted 8 lines
Quoted 3 lines
So ~10% improvement versus plain Physical mode. 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.
96 looks suspiciously like an Intel number. In nothing else, you ought to say which CPU is it, because microarchitecture matters. By any chance can we try this on one of the Bergamos, to give us a datapoint at 512?
Let me see if I can grab the only one that's not broken.
Those figures are from an Intel IceLake IIRC. Cluster mode is the default, so this change shouldn't effect the performance of builds that use the default settings.
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/Kconfig +0
-0@@ -228,11 +228,18 @@ config XEN_ALIGN_2M endchoice
Quoted 7 lines
xen/arch/x86/Kconfig config XEN_ALIGN_2M · line 232
Quoted 4 lines
xen/arch/x86/Kconfig +2
-2-config X2APIC_PHYSICAL - bool "x2APIC Physical Destination mode" +choice + prompt "x2APIC Destination mode""x2APIC Driver default" is going to be more meaningful to a non-expert reading this menu entry, IMO.
I will leave the helps as-is.
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/Kconfig +2
-0+}; + static int cf_check update_clusterinfo( struct notifier_block *nfb, unsigned long action, void *hcpu) {@@ -220,38 +248,56 @@ static struct notifier_block x2apic_cpu_nfb = { static int8_t __initdata x2apic_phys = -1; boolean_param("x2apic_phys", x2apic_phys);
Quoted 7 lines
xen/arch/x86/Kconfig static struct notifier_block x2apic_cpu_nfb = { · line 255
Quoted 5 lines
xen/arch/x86/Kconfig +5
-0+enum { + unset, physical, cluster, mixed +} static __initdata x2apic_mode = unset; + +static int __init parse_x2apic_mode(const char *s)
cf_check
I'm probably confused, but other users of custom_param() do have the cf_check attribute, see parse_spec_ctrl() for example.
Thanks, Roger.
Quoted 19 lines · Roger Pau Monné
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne
xen/arch/x86/genapic/x2apic.c +2
-0+}; + static int cf_check update_clusterinfo(struct notifier_block *nfb,unsigned long action,void *hcpu) {@@ -220,38 +248,56 @@ static struct notifier_block x2apic_cpu_nfb = {static int8_t __initdata x2apic_phys =-1 ;boolean_param ("x2apic_phys" , x2apic_phys);Quoted 7 lines
xen/arch/x86/genapic/x2apic.c static struct notifier_block x2apic_cpu_nfb = { · line 255
Quoted 5 lines
xen/arch/x86/genapic/x2apic.c +5
-0+enum { + unset, physical, cluster, mixed +}static __initdata x2apic_mode = unset; + +static int __init parse_x2apic_mode (const char *s)
cf_check
Quoted 2 lines
I'm probably confused, but other users of
custom_param()do have thecf_checkattribute, seeparse_spec_ctrl()for example.
... other users of custom_param() do _not_ have the ...
Sorry.
Quoted 18 lines · Roger Pau Monné
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne
when sending those, as multiple CPUs can be targeted with a single ICR register write.
A simple test calling
flush_tlb_all()10000 times in a tight loop on a 96 CPU box gives the following average figures:Physical mode: 26617931ns Mixed mode: 23865337nsQuoted 7 lines
Quoted 3 lines
So ~10% improvement versus plain Physical mode. 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.
96 looks suspiciously like an Intel number. In nothing else, you ought to say which CPU is it, because microarchitecture matters. By any chance can we try this on one of the Bergamos, to give us a datapoint at 512?
Quoted 5 lines
Let me see if I can grab the only one that's not broken.
Those figures are from an Intel IceLake IIRC. Cluster mode is the default, so this change shouldn't effect the performance of builds that use the default settings.
"shouldn't" being the operative word.
You're presenting evidence to try and convince the reader that the reasoning is correct.
Therefore, it is important to confirm your assumptions, and that means measuring and reporting all 3.
Part of the analysis should say "we expected mixed and cluster to be the same, and the results show that".
And obviously, if mixed and cluster are wildly different, then we take a step back and think harder.
Quoted 8 lines
Quoted 8 lines
Quoted 8 lines
xen/arch/x86/Kconfig +0
-0@@ -228,11 +228,18 @@ config XEN_ALIGN_2M endchoice
Quoted 7 lines
Quoted 6 lines
xen/arch/x86/Kconfig config XEN_ALIGN_2M · line 232
Quoted 4 lines
xen/arch/x86/Kconfig +2
-2-config X2APIC_PHYSICAL - bool "x2APIC Physical Destination mode" +choice + prompt "x2APIC Destination mode""x2APIC Driver default" is going to be more meaningful to a non-expert reading this menu entry, IMO.
I will leave the helps as-is.
Yeah, the help text is fine. It's just the title itself.
Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
xen/arch/x86/Kconfig +2
-0+}; + static int cf_check update_clusterinfo( struct notifier_block *nfb, unsigned long action, void *hcpu) {@@ -220,38 +248,56 @@ static struct notifier_block x2apic_cpu_nfb = { static int8_t __initdata x2apic_phys = -1; boolean_param("x2apic_phys", x2apic_phys);
Quoted 8 lines
Quoted 6 lines
xen/arch/x86/Kconfig static struct notifier_block x2apic_cpu_nfb = { · line 255
Quoted 5 lines
xen/arch/x86/Kconfig +5
-0+enum { + unset, physical, cluster, mixed +} static __initdata x2apic_mode = unset; + +static int __init parse_x2apic_mode(const char *s)
cf_checkI'm probably confused, but other users of
custom_param()do have thecf_checkattribute, seeparse_spec_ctrl()for example.
Yes, and this function needs one but is missing it.
cf_check equates to "This function needs an ENDBR", which it does because it's invoked by function pointer.
It likely doesn't expode on a CET-active machine because this logic runs prior to turning CET-IBT on, and you'll only get a build error in the buster-ibt pipeline which has a still-unupstreamed GCC patch.
~Andrew
Quoted 30 lines · Andrew Cooper
Quoted 18 lines · Roger Pau Monné
Quoted 10 lines · Andrew Cooper
Quoted 9 lines · Roger Pau Monne
when sending those, as multiple CPUs can be targeted with a single ICR register write.
A simple test calling
flush_tlb_all()10000 times in a tight loop on a 96 CPU box gives the following average figures:Physical mode: 26617931ns Mixed mode: 23865337nsQuoted 7 lines
Quoted 3 lines
So ~10% improvement versus plain Physical mode. 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.
96 looks suspiciously like an Intel number. In nothing else, you ought to say which CPU is it, because microarchitecture matters. By any chance can we try this on one of the Bergamos, to give us a datapoint at 512?
Quoted 5 lines
Let me see if I can grab the only one that's not broken.
Those figures are from an Intel IceLake IIRC. Cluster mode is the default, so this change shouldn't effect the performance of builds that use the default settings.
"shouldn't" being the operative word.
You're presenting evidence to try and convince the reader that the reasoning is correct.
Quoted 8 lines
Therefore, it is important to confirm your assumptions, and that means measuring and reporting all 3.
Part of the analysis should say "we expected mixed and cluster to be the same, and the results show that".
And obviously, if mixed and cluster are wildly different, then we take a step back and think harder.
If they are different I'm definitely not sending the patch :).
Quoted 18 lines
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
xen/arch/x86/genapic/x2apic.c +2
-0+}; + static int cf_check update_clusterinfo(struct notifier_block *nfb,unsigned long action,void *hcpu) {@@ -220,38 +248,56 @@ static struct notifier_block x2apic_cpu_nfb = {static int8_t __initdata x2apic_phys =-1 ;boolean_param ("x2apic_phys" , x2apic_phys);Quoted 8 lines
Quoted 6 lines
xen/arch/x86/genapic/x2apic.c static struct notifier_block x2apic_cpu_nfb = { · line 255
Quoted 5 lines
xen/arch/x86/genapic/x2apic.c +5
-0+enum { + unset, physical, cluster, mixed +}static __initdata x2apic_mode = unset; + +static int __init parse_x2apic_mode (const char *s)
cf_checkI'm probably confused, but other users of
custom_param()do have thecf_checkattribute, seeparse_spec_ctrl()for example.
Quoted 8 lines
Yes, and this function needs one but is missing it.
cf_checkequates to "This function needs an ENDBR", which it does because it's invoked by function pointer.It likely doesn't expode on a CET-active machine because this logic runs prior to turning CET-IBT on, and you'll only get a build error in the buster-ibt pipeline which has a still-unupstreamed GCC patch.
That was my guess, that CET wasn't yet active by the time this is called.
For consistency we should fix all handlers of custom_param() (and other command line parsing helpers) so they uniformly use cf_check, otherwise it's likely I will make the same mistake when copy&paste to introduce a new option.
Thanks, Roger.