x86/MCE: reduce redundant code
cover x86/MCE: reduce redundant code
Following the (so far not really explicable) observation in [1], plus the fact that mca_rdmsr() isn't simply an RDMSR, patch 1 looked like something we may want to have. While putting that together, another inefficiency popped up, which patch 2 covers.
1: reduce number of MSR reads/lookups
2: drop redundant code from mcheck_mca_logout()
Jan
[1] https://lists.xen.org/archives/html/xen-devel/2026-10/msg01825.html
PATCH 1/2 x86/MCE: reduce number of MSR reads/lookups
MSRs mcheck_mca_logout() already reads don't need re-reading by functions it calls, even less so when it calls them exclusively. Seeing reports like [1], this also makes sure that what we use internally also is propagated to the MCE log.
While touching function signatures, also switch bank number parameters to unsigned int.
[1] https://lists.xen.org/archives/html/xen-devel/2026-10/msg01825.html
---
mcheck_cmn_handler()'s read of MSR_IA32_MCG_STATUS could likely also be dropped, by having mcheck_mca_logout() provide the value it read.
xen/arch/x86/cpu/mcheck/mce.c +15 -19
@@ -138,12 +138,8 @@ void mcabanks_free(struct mca_banks *banxfree (banks); } -static void mcabank_clear (int banknum) +static void mcabank_clear (unsigned int banknum,uint64_t status) { -uint64_t status; - - status =mca_rdmsr (MSR_IA32_MCx_STATUS (banknum)); -if ( status & MCi_STATUS_ADDRV )mca_wrmsr (MSR_IA32_MCx_ADDR (banknum),0x0ULL );if ( status & MCi_STATUS_MISCV )
@@ -165,7 +161,8 @@ const struct mca_error_handler *__read_munsigned int __read_mostly mce_dhandler_num;unsigned int __read_mostly mce_uhandler_num; -static void mca_init_bank (enum mca_source who,struct mc_info *mi,int bank) +static void mca_init_bank (enum mca_source who,struct mc_info *mi, +unsigned int bank,uint64_t status) {struct mcinfo_bank *mib;
@@ -179,20 +176,20 @@ static void mca_init_bank(enum mca_sourcreturn ; } - mib->mc_status =mca_rdmsr (MSR_IA32_MCx_STATUS (bank)); + mib->mc_status = status; mib->mc_bank = bank; mib->mc_domid =DOMID_INVALID ; -if ( mib->mc_status & MCi_STATUS_MISCV ) +if ( status & MCi_STATUS_MISCV ) mib->mc_misc =mca_rdmsr (MSR_IA32_MCx_MISC (bank)); -if ( mib->mc_status & MCi_STATUS_ADDRV ) +if ( status & MCi_STATUS_ADDRV ) mib->mc_addr =mca_rdmsr (MSR_IA32_MCx_ADDR (bank)); -if ( (mib->mc_status & MCi_STATUS_MISCV) && - (mib->mc_status & MCi_STATUS_ADDRV) && -alternative_call (mce_callbacks.check_addr, mib->mc_status, +if ( (status & MCi_STATUS_MISCV) && + (status & MCi_STATUS_ADDRV) && +alternative_call (mce_callbacks.check_addr, status, mib->mc_misc,MC_ADDR_PHYSICAL ) && (who ==MCA_POLLER || who ==MCA_CMCI_HANDLER ) && (mfn_valid (_mfn (paddr_to_pfn (mib->mc_addr)))) )
@@ -211,14 +208,13 @@ static void mca_init_bank(enum mca_sourc } } -static int mca_init_global (uint32_t flags,struct mcinfo_global *mig) +static int mca_init_global (uint32_t flags,struct mcinfo_global *mig, +uint64_t status) { -uint64_t status;int cpu_nr;const struct vcpu *curr = current;/* Set global information */ - status =mca_rdmsr (MSR_IA32_MCG_STATUS ); mig->mc_gstatus = status; mig->mc_domid =DOMID_INVALID ; mig->mc_vcpuid =XEN_MC_VCPUID_INVALID ;
@@ -331,7 +327,7 @@ mcheck_mca_logout(enum mca_source who, s mig =x86_mcinfo_reserve (mci,sizeof (*mig),MC_TYPE_GLOBAL );/* mc_info should at least hold up the global information */ ASSERT (mig); -mca_init_global (mc_flags, mig); +mca_init_global (mc_flags, mig, gstatus);/* A hook here to get global extended msrs */ if (IS_ENABLED (CONFIG_INTEL ) && boot_cpu_data.x86_vendor ==X86_VENDOR_INTEL )
@@ -351,7 +347,7 @@ mcheck_mca_logout(enum mca_source who, s/* uc = true, recover = true, we need not panic. */ recover =alternative_call (mce_callbacks.recoverable_scan, status); -mca_init_bank (who, mci, i); +mca_init_bank (who, mci, i, status);if ( mce_callbacks.info_collect )alternative_vcall (mce_callbacks.info_collect, mci, i, status);
@@ -359,7 +355,7 @@ mcheck_mca_logout(enum mca_source who, s/* By default, need_clear = true */ if ( who !=MCA_MCE_SCAN && need_clear )/* Clear bank */ -mcabank_clear (i); +mcabank_clear (i, status);else if ( who ==MCA_MCE_SCAN && need_clear )mcabanks_set (i, clear_bank); }
@@ -548,7 +544,7 @@ void mcheck_mca_clearbanks(struct mca_ba {if ( !mcabanks_test (i, bankmask) )continue ; -mcabank_clear (i); +mcabank_clear (i,mca_rdmsr (MSR_IA32_MCx_STATUS (i))); } }
PATCH 2/2 x86/MCE: drop redundant code from mcheck_mca_logout()
In reply to Jan BeulichPull out the shared part of a two-way conditional, and use plain "else" for the remaining latter half.
---
To my surprise this does change (shrink) generated code (with gcc16; albeit maybe there would be no difference in release [-O2] builds). This looks to be a result of the compiler pulling the calculations out of the loop, for being loop-invariant.
xen/arch/x86/cpu/mcheck/mce.c +7 -5
@@ -353,11 +353,13 @@ mcheck_mca_logout(enum mca_source who, salternative_vcall (mce_callbacks.info_collect, mci, i, status);/* By default, need_clear = true */ -if ( who !=MCA_MCE_SCAN && need_clear ) -/* Clear bank */ -mcabank_clear (i, status); -else if ( who ==MCA_MCE_SCAN && need_clear ) -mcabanks_set (i, clear_bank); +if ( need_clear ) + { +if ( who !=MCA_MCE_SCAN ) +mcabank_clear (i, status); +else +mcabanks_set (i, clear_bank); + } }if ( mig && errcnt >0 )